Skip to content

Commit

Permalink
Render reddit links as bookmark-of
Browse files Browse the repository at this point in the history
As noted in #213, it'd be much useful to have the post's URL, if one is
available, as a bookmark.

Closes #213.
  • Loading branch information
jamietanna committed Oct 13, 2020
1 parent 602480e commit 03210d9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions granary/reddit.py
Expand Up @@ -139,6 +139,7 @@ def praw_to_object(self, thing, type):
if type == 'submission':
obj['title'] = getattr(thing, 'title', None)
obj['content'] = getattr(thing, 'selftext', None)
obj['bookmark-of'] = getattr(thing, 'url', None)
obj['objectType'] = 'note'
obj['tags'] = [
{'objectType': 'article',
Expand Down
62 changes: 62 additions & 0 deletions granary/tests/test_reddit.py
Expand Up @@ -134,6 +134,61 @@ def parent(self):
'updatedSince': False
}

ACTIVITY_WITH_LINK = {
'filtered': False,
'items': [
{'actor':
{'description': 'https://bonkerfield.org https://viewfoil.bonkerfield.org',
'displayName': 'bonkerfield',
'id': 'tag:reddit.com:bonkerfield',
'image': {'url': 'https://styles.redditmedia.com/t5_2az095/styles/profileIcon_ek6onop1xbf41.png'},
'numeric_id': '59ucsixw',
'objectType': 'person',
'published': '2019-12-21T17:40:11Z',
'url': 'https://reddit.com/user/bonkerfield/',
'urls': [
{'value': 'https://reddit.com/user/bonkerfield/'},
{'value': 'https://bonkerfield.org'},
{'value': 'https://viewfoil.bonkerfield.org'}
],
'username': 'bonkerfield'
},
'id': 'ezv3f2',
'object':
{'author':
{'description': 'https://bonkerfield.org https://viewfoil.bonkerfield.org',
'displayName': 'bonkerfield',
'id': 'tag:reddit.com:bonkerfield',
'image': {'url': 'https://styles.redditmedia.com/t5_2az095/styles/profileIcon_ek6onop1xbf41.png'},
'numeric_id': '59ucsixw',
'objectType': 'person',
'published': '2019-12-21T17:40:11Z',
'url': 'https://reddit.com/user/bonkerfield/',
'urls': [
{'value': 'https://reddit.com/user/bonkerfield/'},
{'value': 'https://bonkerfield.org'},
{'value': 'https://viewfoil.bonkerfield.org'}
],
'username': 'bonkerfield'
},
'title': '[P] GPT-2 + BERT reddit replier. I built a system that generates replies by taking output from GPT-2 and using BERT models to select the most realistic replies. People on r/artificial replied to it as if it were a person.',
'bookmark-of': 'https://reddit.com/ezv3f2',
'id': 'tag:reddit.com:ezv3f2',
'objectType': 'note',
'published': '2020-02-06T16:51:59Z',
'to': [{'alias': '@public', 'objectType': 'group'}],
'url': 'https://reddit.com/r/MachineLearning/comments/ezv3f2/p_gpt2_bert_reddit_replier_i_built_a_system_that/'},
'url': 'https://reddit.com/r/MachineLearning/comments/ezv3f2/p_gpt2_bert_reddit_replier_i_built_a_system_that/',
'verb': 'post'
}
],
'itemsPerPage': 1,
'sorted': False,
'startIndex': 0,
'totalResults': 1,
'updatedSince': False
}

COMMENT_OBJECT = {
'author': {
'description': 'https://bonkerfield.org https://viewfoil.bonkerfield.org',
Expand Down Expand Up @@ -211,6 +266,13 @@ def test_praw_to_comment(self):
def test_broken_praw_to_comment(self):
self.assert_equals(MISSING_OBJECT, self.reddit.praw_to_object(util.Struct(),'comment'))

def test_submission_to_activity_with_link(self):
submission = FakeSubmission(FakeRedditor())
submission.selftext = ''
submission.url = 'https://reddit.com/ezv3f2'
fake_activities = [self.reddit.praw_to_activity(submission,type='submission')]
self.assert_equals(ACTIVITY_WITH_LINK, self.reddit.make_activities_base_response(fake_activities))

def test_submission_to_activity_with_selftext(self):
fake_activities = [self.reddit.praw_to_activity(FakeSubmission(FakeRedditor()),type='submission')]
self.assert_equals(ACTIVITY_WITH_SELFTEXT, self.reddit.make_activities_base_response(fake_activities))
Expand Down

0 comments on commit 03210d9

Please sign in to comment.