Broadcast book update events

This commit is contained in:
Mouse Reeve 2020-03-28 19:12:17 -07:00
parent db6af61f03
commit cf113e50fe
5 changed files with 35 additions and 4 deletions

View file

@ -1,7 +1,7 @@
''' bring activitypub functions into the namespace ''' ''' bring activitypub functions into the namespace '''
from .actor import get_actor from .actor import get_actor
from .book import get_book from .book import get_book
from .create import get_create from .create import get_create, get_update
from .follow import get_following, get_followers from .follow import get_following, get_followers
from .follow import get_follow_request, get_unfollow, get_accept, get_reject from .follow import get_follow_request, get_unfollow, get_accept, get_reject
from .outbox import get_outbox, get_outbox_page from .outbox import get_outbox, get_outbox_page

View file

@ -30,4 +30,17 @@ def get_create(user, status_json):
} }
def get_update(user, activity_json):
''' a user profile or book or whatever got updated '''
# TODO: should this have a signature??
return {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': 'https://friend.camp/users/tripofmice#updates/1585446332',
'type': 'Update',
'actor': user.actor,
'to': [
'https://www.w3.org/ns/activitystreams#Public'
],
'object': activity_json,
}

View file

@ -8,10 +8,19 @@ import json
import requests import requests
from urllib.parse import urlparse from urllib.parse import urlparse
from fedireads import models
def get_recipients(user, post_privacy, direct_recipients=None, limit=False): def get_recipients(user, post_privacy, direct_recipients=None, limit=False):
''' deduplicated list of recipient inboxes ''' ''' deduplicated list of recipient inboxes '''
recipients = direct_recipients or [] recipients = direct_recipients or []
if not user:
users = models.User.objects.filter(local=False).all()
recipients += list(set(
u.shared_inbox if u.shared_inbox else u.inbox for u in users
))
return recipients
if post_privacy == 'direct': if post_privacy == 'direct':
# all we care about is direct_recipients, not followers # all we care about is direct_recipients, not followers
return [u.inbox for u in recipients] return [u.inbox for u in recipients]

View file

@ -290,3 +290,11 @@ def handle_outgoing_unfavorite(user, status):
recipients = get_recipients(user, 'direct', [status.user]) recipients = get_recipients(user, 'direct', [status.user])
broadcast(user, fav_activity, recipients) broadcast(user, fav_activity, recipients)
def handle_update_book(user, book):
''' broadcast the news about our book '''
book_activity = activitypub.get_book(book)
update_activity = activitypub.get_update(user, book_activity)
recipients = get_recipients(None, 'public')
broadcast(user, update_activity, recipients)

View file

@ -1,17 +1,16 @@
''' views for actions you can take in the application ''' ''' views for actions you can take in the application '''
from io import TextIOWrapper
from django.contrib.auth import authenticate, login, logout from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.http import HttpResponseBadRequest, HttpResponseNotFound from django.http import HttpResponseBadRequest, HttpResponseNotFound
from django.shortcuts import redirect from django.shortcuts import redirect
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
from io import TextIOWrapper
import re import re
from fedireads import forms, models, books_manager, outgoing from fedireads import forms, models, books_manager, outgoing
from fedireads.goodreads_import import GoodreadsCsv
from fedireads.settings import DOMAIN from fedireads.settings import DOMAIN
from fedireads.views import get_user_from_username from fedireads.views import get_user_from_username
from fedireads.goodreads_import import GoodreadsCsv
def user_login(request): def user_login(request):
@ -103,6 +102,7 @@ def edit_book(request, book_id):
return redirect(request.headers.get('Referer', '/')) return redirect(request.headers.get('Referer', '/'))
form.save() form.save()
outgoing.handle_update_book(request.user, book)
return redirect('/book/%s' % book.fedireads_key) return redirect('/book/%s' % book.fedireads_key)
@ -126,6 +126,7 @@ def upload_cover(request, book_id):
book.sync_cover = False book.sync_cover = False
book.save() book.save()
outgoing.handle_update_book(request.user, book)
return redirect('/book/%s' % book.fedireads_key) return redirect('/book/%s' % book.fedireads_key)