def handle_incoming_create(activity): ''' someone did something, good on them ''' user = get_or_create_remote_user(activity['actor']) if not 'object' in activity: return HttpResponseBadRequest() # TODO: should only create notes if they are relevent to a book, # so, not every single thing someone posts on mastodon response = HttpResponse() content = activity['object'].get('content') if activity['object'].get('fedireadsType') == 'Review' and \ 'inReplyToBook' in activity['object']: book = activity['object']['inReplyToBook'] book = book.split('/')[-1] name = activity['object'].get('name') rating = activity['object'].get('rating') if user.local: review_id = activity['object']['id'].split('/')[-1] models.Review.objects.get(id=review_id) else: try: create_review(user, book, name, content, rating) except ValueError: return HttpResponseBadRequest() elif not user.local: try: create_status(user, content) except ValueError: return HttpResponseBadRequest() return response
def get_remote_reviews(user): ''' ingest reviews by a new remote fedireads user ''' outbox_page = user.outbox + '?page=true' response = requests.get(outbox_page, headers={'Accept': 'application/activity+json'}) data = response.json() for status in data['orderedItems']: if status.get('fedireadsType') == 'Review': book_id = status['inReplyToBook'].split('/')[-1] create_review(user, book_id, status['name'], status['content'], status['rating'])
def test_review_rating(self): review = status_builder.create_review( self.user, self.book, 'review name', 'content', -1) self.assertEqual(review.name, 'review name') self.assertEqual(review.content, 'content') self.assertEqual(review.rating, None) review = status_builder.create_review( self.user, self.book, 'review name', 'content', 6) self.assertEqual(review.name, 'review name') self.assertEqual(review.content, 'content') self.assertEqual(review.rating, None)
def test_create_review(self): review = status_builder.create_review( self.user, self.book, 'review name', 'content', 5) self.assertEqual(review.name, 'review name') self.assertEqual(review.content, 'content') self.assertEqual(review.rating, 5) review = status_builder.create_review( self.user, self.book, '<div>review</div> name', '<b>content', 5) self.assertEqual(review.name, 'review name') self.assertEqual(review.content, 'content') self.assertEqual(review.rating, 5)
def handle_review(user, book, name, content, rating): ''' post a review ''' # validated and saves the review in the database so it has an id review = create_review(user, book, name, content, rating) review_activity = activitypub.get_review(review) review_create_activity = activitypub.get_create(user, review_activity) fr_recipients = get_recipients(user, 'public', limit='fedireads') broadcast(user, review_create_activity, fr_recipients) # re-format the activity for non-fedireads servers article_activity = activitypub.get_review_article(review) article_create_activity = activitypub.get_create(user, article_activity) other_recipients = get_recipients(user, 'public', limit='other') broadcast(user, article_create_activity, other_recipients)
def test_create_status_from_activity(self): book = models.Edition.objects.create(title='Example Edition') review = status_builder.create_review( self.user, book, 'review name', 'content', 5) activity = { 'id': 'https://example.com/user/mouse/status/12', 'url': 'https://example.com/user/mouse/status/12', 'inReplyTo': review.remote_id, 'published': '2020-05-10T02:15:59.635557+00:00', 'attributedTo': 'https://example.com/user/mouse', 'to': [ 'https://www.w3.org/ns/activitystreams#Public' ], 'cc': [ 'https://example.com/user/mouse/followers' ], 'sensitive': False, 'content': 'reply to status', 'type': 'Note', 'attachment': [], 'replies': { 'id': 'https://example.com/user/mouse/status/12/replies', 'type': 'Collection', 'first': { 'type': 'CollectionPage', 'next': 'https://example.com/user/mouse/status/12/replies?only_other_accounts=true&page=true', 'partOf': 'https://example.com/user/mouse/status/12/replies', 'items': [] } } } status = status_builder.create_status_from_activity( self.user, activity) self.assertEqual(status.reply_parent, review) self.assertEqual(status.content, 'reply to status') self.assertEqual( status.published_date, '2020-05-10T02:15:59.635557+00:00' )