moviewyrm/bookwyrm/outgoing.py

356 lines
10 KiB
Python
Raw Normal View History

2020-01-28 19:45:27 +00:00
''' handles all the activity coming out of the server '''
import re
2020-04-01 14:18:45 +00:00
from django.db import IntegrityError, transaction
2020-02-11 03:42:21 +00:00
from django.http import HttpResponseNotFound, JsonResponse
from django.views.decorators.csrf import csrf_exempt
import requests
from bookwyrm import activitypub
from bookwyrm import models
from bookwyrm.broadcast import broadcast
2020-11-06 22:25:48 +00:00
from bookwyrm.status import create_notification
from bookwyrm.status import create_generated_note
2020-10-08 19:32:45 +00:00
from bookwyrm.status import delete_status
from bookwyrm.settings import DOMAIN
2020-11-01 18:42:48 +00:00
from bookwyrm.utils import regex
2020-01-28 19:45:27 +00:00
2020-01-28 19:13:13 +00:00
@csrf_exempt
def outbox(request, username):
''' outbox for the requested user '''
2020-02-11 03:42:21 +00:00
if request.method != 'GET':
return HttpResponseNotFound()
2020-02-22 22:02:03 +00:00
try:
user = models.User.objects.get(localname=username)
except models.User.DoesNotExist:
return HttpResponseNotFound()
2020-02-11 03:42:21 +00:00
# collection overview
return JsonResponse(
user.to_outbox(**request.GET),
encoder=activitypub.ActivityEncoder
)
2020-01-28 19:13:13 +00:00
def handle_remote_webfinger(query):
''' webfingerin' other servers '''
2020-01-28 19:13:13 +00:00
user = None
# usernames could be @user@domain or user@domain
if query[0] == '@':
query = query[1:]
2020-11-01 18:42:48 +00:00
try:
domain = query.split('@')[1]
2020-11-01 18:42:48 +00:00
except IndexError:
return None
try:
user = models.User.objects.get(username=query)
except models.User.DoesNotExist:
2020-11-01 20:29:31 +00:00
url = 'https://%s/.well-known/webfinger?resource=acct:%s' % \
(domain, query)
2020-09-30 18:23:23 +00:00
try:
response = requests.get(url)
except requests.exceptions.ConnectionError:
return None
if not response.ok:
2020-09-30 18:33:15 +00:00
return None
data = response.json()
for link in data['links']:
if link.get('rel') == 'self':
try:
user = activitypub.resolve_remote_id(
models.User, link['href']
)
except KeyError:
2020-09-30 18:23:23 +00:00
return None
2020-11-01 18:42:48 +00:00
return user
def handle_follow(user, to_follow):
''' someone local wants to follow someone '''
relationship, _ = models.UserFollowRequest.objects.get_or_create(
user_subject=user,
user_object=to_follow,
)
activity = relationship.to_activity()
2020-10-16 19:24:29 +00:00
broadcast(user, activity, privacy='direct', direct_recipients=[to_follow])
def handle_unfollow(user, to_unfollow):
2020-02-19 06:44:13 +00:00
''' someone local wants to follow someone '''
relationship = models.UserFollows.objects.get(
user_subject=user,
user_object=to_unfollow
2020-02-19 06:44:13 +00:00
)
activity = relationship.to_undo_activity(user)
2020-10-16 19:24:29 +00:00
broadcast(user, activity, privacy='direct', direct_recipients=[to_unfollow])
2020-02-19 06:44:13 +00:00
to_unfollow.followers.remove(user)
2020-10-16 21:14:07 +00:00
def handle_accept(follow_request):
2020-02-15 06:44:07 +00:00
''' send an acceptance message to a follow request '''
2020-10-16 21:14:07 +00:00
user = follow_request.user_subject
to_follow = follow_request.user_object
with transaction.atomic():
relationship = models.UserFollows.from_request(follow_request)
follow_request.delete()
relationship.save()
activity = relationship.to_accept_activity()
2020-04-21 00:43:42 +00:00
broadcast(to_follow, activity, privacy='direct', direct_recipients=[user])
2020-02-15 06:44:07 +00:00
2020-03-29 07:05:09 +00:00
2020-10-16 21:28:25 +00:00
def handle_reject(follow_request):
2020-03-15 21:15:36 +00:00
''' a local user who managed follows rejects a follow request '''
2020-10-16 21:28:25 +00:00
user = follow_request.user_subject
to_follow = follow_request.user_object
activity = follow_request.to_reject_activity()
follow_request.delete()
2020-04-21 00:43:42 +00:00
broadcast(to_follow, activity, privacy='direct', direct_recipients=[user])
2020-02-15 06:44:07 +00:00
2020-03-15 21:15:36 +00:00
def handle_shelve(user, book, shelf):
2020-01-28 19:13:13 +00:00
''' a local user is getting a book put on their shelf '''
# update the database
shelve = models.ShelfBook(book=book, shelf=shelf, added_by=user)
shelve.save()
broadcast(user, shelve.to_add_activity(user))
2020-11-06 16:51:50 +00:00
def handle_unshelve(user, book, shelf):
''' a local user is getting a book put on their shelf '''
# update the database
row = models.ShelfBook.objects.get(book=book, shelf=shelf)
activity = row.to_remove_activity(user)
row.delete()
broadcast(user, activity)
def handle_reading_status(user, shelf, book, privacy):
''' post about a user reading a book '''
2020-02-17 02:45:25 +00:00
# tell the world about this cool thing that happened
2020-10-16 22:07:41 +00:00
try:
message = {
'to-read': 'wants to read',
'reading': 'started reading',
'read': 'finished reading'
}[shelf.identifier]
except KeyError:
# it's a non-standard shelf, don't worry about it
return
2020-11-06 16:51:50 +00:00
status = create_generated_note(
user,
message,
mention_books=[book],
privacy=privacy
)
2020-03-15 21:15:36 +00:00
status.save()
2020-02-17 02:45:25 +00:00
broadcast(user, status.to_create_activity(user))
2020-02-17 02:45:25 +00:00
def handle_imported_book(user, item, include_reviews, privacy):
2020-03-27 16:33:31 +00:00
''' process a goodreads csv and then post about it '''
if isinstance(item.book, models.Work):
item.book = item.book.default_edition
if not item.book:
return
if item.shelf:
desired_shelf = models.Shelf.objects.get(
identifier=item.shelf,
user=user
)
# shelve the book if it hasn't been shelved already
shelf_book, created = models.ShelfBook.objects.get_or_create(
book=item.book, shelf=desired_shelf, added_by=user)
if created:
broadcast(user, shelf_book.to_add_activity(user), privacy=privacy)
# only add new read-throughs if the item isn't already shelved
for read in item.reads:
read.book = item.book
read.user = user
read.save()
if include_reviews and (item.rating or item.review):
review_title = 'Review of {!r} on Goodreads'.format(
item.book.title,
) if item.review else ''
# we don't know the publication date of the review,
# but "now" is a bad guess
published_date_guess = item.date_read or item.date_added
review = models.Review.objects.create(
user=user,
book=item.book,
name=review_title,
content=item.review,
rating=item.rating,
published_date=published_date_guess,
privacy=privacy,
)
# we don't need to send out pure activities because non-bookwyrm
# instances don't need this data
broadcast(user, review.to_create_activity(user), privacy=privacy)
2020-10-08 19:32:45 +00:00
def handle_delete_status(user, status):
''' delete a status and broadcast deletion to other servers '''
delete_status(status)
broadcast(user, status.to_delete_activity(user))
2020-10-08 19:32:45 +00:00
2020-10-26 22:00:15 +00:00
def handle_status(user, form):
2020-04-21 00:06:11 +00:00
''' generic handler for statuses '''
2020-10-26 22:00:15 +00:00
status = form.save()
2020-04-21 00:06:11 +00:00
# inspect the text for user tags
text = status.content
matches = re.finditer(
2020-11-01 18:42:48 +00:00
regex.username,
text
)
for match in matches:
username = match.group().strip().split('@')[1:]
if len(username) == 1:
# this looks like a local user (@user), fill in the domain
username.append(DOMAIN)
username = '@'.join(username)
2020-11-01 18:42:48 +00:00
mention_user = handle_remote_webfinger(username)
if not mention_user:
# we can ignore users we don't know about
continue
# add them to status mentions fk
status.mention_users.add(mention_user)
# create notification if the mentioned user is local
if mention_user.local:
create_notification(
mention_user,
'MENTION',
related_user=user,
related_status=status
)
status.save()
# notify reply parent or tagged users
2020-10-27 18:32:15 +00:00
if status.reply_parent and status.reply_parent.user.local:
create_notification(
status.reply_parent.user,
'REPLY',
related_user=user,
related_status=status
)
broadcast(user, status.to_create_activity(user), software='bookwyrm')
2020-03-21 23:50:49 +00:00
# re-format the activity for non-bookwyrm servers
2020-10-27 18:32:15 +00:00
if hasattr(status, 'pure_activity_serializer'):
remote_activity = status.to_create_activity(user, pure=True)
broadcast(user, remote_activity, software='other')
2020-03-21 23:50:49 +00:00
2020-11-06 22:25:48 +00:00
def handle_tag(user, tag):
2020-02-21 06:19:19 +00:00
''' tag a book '''
broadcast(user, tag.to_add_activity(user))
2020-02-21 06:19:19 +00:00
def handle_untag(user, book, name):
''' tag a book '''
2020-05-04 00:53:14 +00:00
book = models.Book.objects.get(id=book)
2020-02-21 06:19:19 +00:00
tag = models.Tag.objects.get(name=name, book=book, user=user)
tag_activity = tag.to_remove_activity(user)
2020-02-21 06:19:19 +00:00
tag.delete()
2020-04-21 00:43:42 +00:00
broadcast(user, tag_activity)
2020-02-18 05:39:08 +00:00
def handle_favorite(user, status):
2020-02-19 08:13:06 +00:00
''' a user likes a status '''
try:
favorite = models.Favorite.objects.create(
status=status,
user=user
)
except IntegrityError:
# you already fav'ed that
return
fav_activity = favorite.to_activity()
2020-04-22 13:53:22 +00:00
broadcast(
user, fav_activity, privacy='direct', direct_recipients=[status.user])
create_notification(
status.user,
'FAVORITE',
related_user=user,
related_status=status
)
2020-02-19 08:13:06 +00:00
2020-03-21 22:21:27 +00:00
def handle_unfavorite(user, status):
2020-03-21 22:21:27 +00:00
''' a user likes a status '''
try:
favorite = models.Favorite.objects.get(
status=status,
user=user
)
except models.Favorite.DoesNotExist:
# can't find that status, idk
return
2020-11-08 02:18:44 +00:00
fav_activity = favorite.to_undo_activity(user)
favorite.delete()
2020-05-04 21:06:53 +00:00
broadcast(user, fav_activity, direct_recipients=[status.user])
2020-04-21 00:43:42 +00:00
2020-03-21 22:21:27 +00:00
def handle_boost(user, status):
''' a user wishes to boost a status '''
2020-03-30 15:39:53 +00:00
if models.Boost.objects.filter(
boosted_status=status, user=user).exists():
# you already boosted that.
return
2020-03-30 15:39:53 +00:00
boost = models.Boost.objects.create(
boosted_status=status,
user=user,
)
boost.save()
boost_activity = boost.to_activity()
2020-04-21 00:43:42 +00:00
broadcast(user, boost_activity)
create_notification(
status.user,
'BOOST',
related_user=user,
related_status=status
)
2020-03-29 02:12:17 +00:00
2020-11-08 02:31:01 +00:00
def handle_unboost(user, status):
''' a user regrets boosting a status '''
boost = models.Boost.objects.filter(
2020-11-08 02:59:38 +00:00
boosted_status=status, user=user
).first()
2020-11-08 02:31:01 +00:00
activity = boost.to_undo_activity(user)
2020-11-08 02:59:38 +00:00
boost.delete()
2020-11-08 02:31:01 +00:00
broadcast(user, activity)
2020-03-29 02:12:17 +00:00
def handle_update_book(user, book):
''' broadcast the news about our book '''
broadcast(user, book.to_update_activity(user))
2020-03-29 02:12:17 +00:00
def handle_update_user(user):
''' broadcast editing a user's profile '''
2020-09-29 20:23:49 +00:00
broadcast(user, user.to_update_activity(user))