From cf113e50fecaa733f866bf4dc30e525b27ae6558 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 28 Mar 2020 19:12:17 -0700 Subject: [PATCH] Broadcast book update events --- fedireads/activitypub/__init__.py | 2 +- fedireads/activitypub/create.py | 13 +++++++++++++ fedireads/broadcast.py | 9 +++++++++ fedireads/outgoing.py | 8 ++++++++ fedireads/view_actions.py | 7 ++++--- 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/fedireads/activitypub/__init__.py b/fedireads/activitypub/__init__.py index 1da3cf614..c14ff51ba 100644 --- a/fedireads/activitypub/__init__.py +++ b/fedireads/activitypub/__init__.py @@ -1,7 +1,7 @@ ''' bring activitypub functions into the namespace ''' from .actor import get_actor 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_follow_request, get_unfollow, get_accept, get_reject from .outbox import get_outbox, get_outbox_page diff --git a/fedireads/activitypub/create.py b/fedireads/activitypub/create.py index a3d4d7a16..f78f7d794 100644 --- a/fedireads/activitypub/create.py +++ b/fedireads/activitypub/create.py @@ -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, + } diff --git a/fedireads/broadcast.py b/fedireads/broadcast.py index bc2a0da81..d4b3cadf6 100644 --- a/fedireads/broadcast.py +++ b/fedireads/broadcast.py @@ -8,10 +8,19 @@ import json import requests from urllib.parse import urlparse +from fedireads import models + def get_recipients(user, post_privacy, direct_recipients=None, limit=False): ''' deduplicated list of recipient inboxes ''' 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': # all we care about is direct_recipients, not followers return [u.inbox for u in recipients] diff --git a/fedireads/outgoing.py b/fedireads/outgoing.py index e2d99563e..22f105909 100644 --- a/fedireads/outgoing.py +++ b/fedireads/outgoing.py @@ -290,3 +290,11 @@ def handle_outgoing_unfavorite(user, status): recipients = get_recipients(user, 'direct', [status.user]) 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) + diff --git a/fedireads/view_actions.py b/fedireads/view_actions.py index 99d0bacea..0a7e47abc 100644 --- a/fedireads/view_actions.py +++ b/fedireads/view_actions.py @@ -1,17 +1,16 @@ ''' 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.decorators import login_required from django.http import HttpResponseBadRequest, HttpResponseNotFound from django.shortcuts import redirect from django.template.response import TemplateResponse +from io import TextIOWrapper import re from fedireads import forms, models, books_manager, outgoing +from fedireads.goodreads_import import GoodreadsCsv from fedireads.settings import DOMAIN from fedireads.views import get_user_from_username -from fedireads.goodreads_import import GoodreadsCsv def user_login(request): @@ -103,6 +102,7 @@ def edit_book(request, book_id): return redirect(request.headers.get('Referer', '/')) form.save() + outgoing.handle_update_book(request.user, book) return redirect('/book/%s' % book.fedireads_key) @@ -126,6 +126,7 @@ def upload_cover(request, book_id): book.sync_cover = False book.save() + outgoing.handle_update_book(request.user, book) return redirect('/book/%s' % book.fedireads_key)