Exemple #1
0
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
Exemple #2
0
    def test_create_status(self):
        content = 'statuses are usually <i>replies</i>'
        status = status_builder.create_status(
            self.user, content)
        self.assertEqual(status.content, content)

        reply = status_builder.create_status(
            self.user, content, reply_parent=status)
        self.assertEqual(reply.content, content)
        self.assertEqual(reply.reply_parent, status)
Exemple #3
0
def handle_import_books(user, items):
    ''' process a goodreads csv and then post about it '''
    new_books = []
    for item in items:
        if item.shelf:
            desired_shelf = models.Shelf.objects.get(identifier=item.shelf,
                                                     user=user)
            if isinstance(item.book, models.Work):
                item.book = item.book.default_edition
            if not item.book:
                continue
            _, created = models.ShelfBook.objects.get_or_create(
                book=item.book, shelf=desired_shelf, added_by=user)
            if created:
                new_books.append(item.book)
                activity = activitypub.get_add(user, item.book, desired_shelf)
                broadcast(user, activity)

                for read in item.reads:
                    read.book = item.book
                    read.user = user
                    read.save()

    if new_books:
        message = 'imported {} books'.format(len(new_books))
        status = create_status(user, message, mention_books=new_books)
        status.status_type = 'Update'
        status.save()

        create_activity = activitypub.get_create(
            user, activitypub.get_status(status))
        broadcast(user, create_activity)
Exemple #4
0
def handle_comment(user, review, content):
    ''' respond to a review or status '''
    # validated and saves the comment in the database so it has an id
    comment = create_status(user, content, reply_parent=review)
    comment_activity = activitypub.get_status(comment)
    create_activity = activitypub.get_create(user, comment_activity)

    recipients = get_recipients(user, 'public')
    broadcast(user, create_activity, recipients)
Exemple #5
0
def handle_reply(user, review, content):
    ''' respond to a review or status '''
    # validated and saves the comment in the database so it has an id
    reply = create_status(user, content, reply_parent=review)
    if reply.reply_parent:
        create_notification(
            reply.reply_parent.user,
            'REPLY',
            related_user=user,
            related_status=reply,
        )
    reply_activity = activitypub.get_status(reply)
    create_activity = activitypub.get_create(user, reply_activity)

    broadcast(user, create_activity)
Exemple #6
0
def handle_shelve(user, book, shelf):
    ''' a local user is getting a book put on their shelf '''
    # update the database
    models.ShelfBook(book=book, shelf=shelf, added_by=user).save()

    activity = activitypub.get_add(user, book, shelf)
    recipients = get_recipients(user, 'public')
    broadcast(user, activity, recipients)

    # tell the world about this cool thing that happened
    verb = {
        'to-read': 'wants to read',
        'reading': 'started reading',
        'read': 'finished reading'
    }[shelf.identifier]
    message = '%s "%s"' % (verb, book.title)
    status = create_status(user, message, mention_books=[book])
    status.status_type = 'Update'
    status.save()

    if shelf.identifier == 'reading':
        read = models.ReadThrough(user=user,
                                  book=book,
                                  start_date=datetime.now())
        read.save()
    elif shelf.identifier == 'read':
        read = models.ReadThrough.objects.filter(
            user=user, book=book,
            finish_date=None).order_by('-created_date').first()
        if not read:
            read = models.ReadThrough(user=user,
                                      book=book,
                                      start_date=datetime.now())
        read.finish_date = datetime.now()
        read.save()

    activity = activitypub.get_status(status)
    create_activity = activitypub.get_create(user, activity)

    broadcast(user, create_activity, recipients)
Exemple #7
0
def handle_shelve(user, book, shelf):
    ''' a local user is getting a book put on their shelf '''
    # update the database
    models.ShelfBook(book=book, shelf=shelf, added_by=user).save()

    activity = activitypub.get_add(user, book, shelf)
    recipients = get_recipients(user, 'public')
    broadcast(user, activity, recipients)

    # tell the world about this cool thing that happened
    verb = {
        'to-read': 'wants to read',
        'reading': 'started reading',
        'read': 'finished reading'
    }[shelf.identifier]
    name = user.name if user.name else user.localname
    message = '%s %s %s' % (name, verb, book.data['title'])
    status = create_status(user, message, mention_books=[book])

    activity = activitypub.get_status(status)
    create_activity = activitypub.get_create(user, activity)

    broadcast(user, create_activity, recipients)