2020-01-28 19:45:27 +00:00
|
|
|
''' handles all the activity coming out of the server '''
|
2020-04-15 12:27:19 +00:00
|
|
|
from datetime import datetime
|
2020-04-01 14:18:45 +00:00
|
|
|
|
2020-03-11 12:32:50 +00:00
|
|
|
from django.db import IntegrityError, transaction
|
2020-02-11 03:42:21 +00:00
|
|
|
from django.http import HttpResponseNotFound, JsonResponse
|
2020-01-28 09:01:33 +00:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
import requests
|
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm import activitypub
|
|
|
|
from bookwyrm import models
|
|
|
|
from bookwyrm.broadcast import broadcast
|
|
|
|
from bookwyrm.status import create_review, create_status
|
|
|
|
from bookwyrm.status import create_quotation, create_comment
|
|
|
|
from bookwyrm.status import create_tag, create_notification, create_rating
|
2020-09-29 00:26:15 +00:00
|
|
|
from bookwyrm.status import create_generated_note
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm.remote_user import get_or_create_remote_user
|
2020-01-28 19:45:27 +00:00
|
|
|
|
2020-01-28 09:01:33 +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
|
2020-09-17 20:02:52 +00:00
|
|
|
return JsonResponse(
|
|
|
|
user.to_outbox(**request.GET),
|
|
|
|
encoder=activitypub.ActivityEncoder
|
|
|
|
)
|
2020-01-28 19:13:13 +00:00
|
|
|
|
|
|
|
|
2020-01-28 09:01:33 +00:00
|
|
|
def handle_account_search(query):
|
|
|
|
''' webfingerin' other servers '''
|
2020-01-28 19:13:13 +00:00
|
|
|
user = None
|
2020-01-28 09:01:33 +00:00
|
|
|
domain = query.split('@')[1]
|
|
|
|
try:
|
|
|
|
user = models.User.objects.get(username=query)
|
|
|
|
except models.User.DoesNotExist:
|
|
|
|
url = 'https://%s/.well-known/webfinger?resource=acct:%s' % \
|
|
|
|
(domain, query)
|
|
|
|
response = requests.get(url)
|
|
|
|
if not response.ok:
|
|
|
|
response.raise_for_status()
|
|
|
|
data = response.json()
|
|
|
|
for link in data['links']:
|
|
|
|
if link['rel'] == 'self':
|
2020-02-19 19:33:00 +00:00
|
|
|
try:
|
|
|
|
user = get_or_create_remote_user(link['href'])
|
|
|
|
except KeyError:
|
|
|
|
return HttpResponseNotFound()
|
2020-01-28 09:01:33 +00:00
|
|
|
return user
|
|
|
|
|
|
|
|
|
2020-03-29 02:23:19 +00:00
|
|
|
def handle_follow(user, to_follow):
|
2020-01-28 09:01:33 +00:00
|
|
|
''' someone local wants to follow someone '''
|
2020-09-17 20:02:52 +00:00
|
|
|
try:
|
|
|
|
relationship, _ = models.UserFollowRequest.objects.get_or_create(
|
|
|
|
user_subject=user,
|
|
|
|
user_object=to_follow,
|
|
|
|
)
|
|
|
|
except IntegrityError as err:
|
|
|
|
if err.__cause__.diag.constraint_name != 'userfollowrequest_unique':
|
|
|
|
raise
|
|
|
|
activity = relationship.to_activity()
|
2020-05-04 21:06:53 +00:00
|
|
|
broadcast(user, activity, direct_recipients=[to_follow])
|
2020-01-28 09:01:33 +00:00
|
|
|
|
|
|
|
|
2020-03-29 02:23:19 +00:00
|
|
|
def handle_unfollow(user, to_unfollow):
|
2020-02-19 06:44:13 +00:00
|
|
|
''' someone local wants to follow someone '''
|
2020-03-11 12:32:50 +00:00
|
|
|
relationship = models.UserFollows.objects.get(
|
2020-02-24 16:10:09 +00:00
|
|
|
user_subject=user,
|
|
|
|
user_object=to_unfollow
|
2020-02-19 06:44:13 +00:00
|
|
|
)
|
2020-09-17 20:02:52 +00:00
|
|
|
activity = relationship.to_undo_activity(user)
|
2020-05-04 21:06:53 +00:00
|
|
|
broadcast(user, activity, direct_recipients=[to_unfollow])
|
2020-02-19 06:44:13 +00:00
|
|
|
to_unfollow.followers.remove(user)
|
|
|
|
|
|
|
|
|
2020-03-29 02:23:19 +00:00
|
|
|
def handle_accept(user, to_follow, follow_request):
|
2020-02-15 06:44:07 +00:00
|
|
|
''' send an acceptance message to a follow request '''
|
2020-03-11 12:32:50 +00:00
|
|
|
with transaction.atomic():
|
|
|
|
relationship = models.UserFollows.from_request(follow_request)
|
|
|
|
follow_request.delete()
|
|
|
|
relationship.save()
|
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
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-03-29 02:23:19 +00:00
|
|
|
def handle_reject(user, to_follow, relationship):
|
2020-03-15 21:15:36 +00:00
|
|
|
''' a local user who managed follows rejects a follow request '''
|
2020-09-17 20:02:52 +00:00
|
|
|
activity = relationship.to_reject_activity(user)
|
2020-03-13 17:04:39 +00:00
|
|
|
relationship.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
|
|
|
|
2020-01-28 09:01:33 +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 '''
|
2020-01-28 09:01:33 +00:00
|
|
|
# update the database
|
2020-09-28 21:52:25 +00:00
|
|
|
shelve = models.ShelfBook(book=book, shelf=shelf, added_by=user)
|
|
|
|
shelve.save()
|
2020-01-28 09:01:33 +00:00
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
broadcast(user, shelve.to_add_activity(user))
|
2020-01-28 09:01:33 +00:00
|
|
|
|
2020-02-17 02:45:25 +00:00
|
|
|
# tell the world about this cool thing that happened
|
2020-09-29 00:26:15 +00:00
|
|
|
message = {
|
2020-02-17 02:45:25 +00:00
|
|
|
'to-read': 'wants to read',
|
|
|
|
'reading': 'started reading',
|
|
|
|
'read': 'finished reading'
|
|
|
|
}[shelf.identifier]
|
2020-09-29 00:26:15 +00:00
|
|
|
status = create_generated_note(user, message, mention_books=[book])
|
2020-03-15 21:15:36 +00:00
|
|
|
status.save()
|
2020-02-17 02:45:25 +00:00
|
|
|
|
2020-04-15 12:27:19 +00:00
|
|
|
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()
|
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
broadcast(user, status.to_create_activity(user))
|
2020-02-17 02:45:25 +00:00
|
|
|
|
2020-01-28 09:01:33 +00:00
|
|
|
|
2020-01-29 08:22:48 +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)
|
2020-09-17 20:02:52 +00:00
|
|
|
activity = row.to_remove_activity(user)
|
2020-01-29 08:22:48 +00:00
|
|
|
row.delete()
|
|
|
|
|
2020-04-21 00:43:42 +00:00
|
|
|
broadcast(user, activity)
|
2020-01-29 08:22:48 +00:00
|
|
|
|
|
|
|
|
2020-03-25 14:14:38 +00:00
|
|
|
def handle_import_books(user, items):
|
2020-03-27 16:33:31 +00:00
|
|
|
''' process a goodreads csv and then post about it '''
|
2020-03-25 14:14:38 +00:00
|
|
|
new_books = []
|
|
|
|
for item in items:
|
|
|
|
if item.shelf:
|
|
|
|
desired_shelf = models.Shelf.objects.get(
|
|
|
|
identifier=item.shelf,
|
|
|
|
user=user
|
|
|
|
)
|
2020-04-01 01:58:13 +00:00
|
|
|
if isinstance(item.book, models.Work):
|
|
|
|
item.book = item.book.default_edition
|
|
|
|
if not item.book:
|
|
|
|
continue
|
2020-09-17 20:02:52 +00:00
|
|
|
shelf_book, created = models.ShelfBook.objects.get_or_create(
|
2020-03-27 16:33:31 +00:00
|
|
|
book=item.book, shelf=desired_shelf, added_by=user)
|
2020-03-25 14:14:38 +00:00
|
|
|
if created:
|
|
|
|
new_books.append(item.book)
|
2020-09-17 20:02:52 +00:00
|
|
|
activity = shelf_book.to_add_activity(user)
|
2020-04-21 00:43:42 +00:00
|
|
|
broadcast(user, activity)
|
2020-03-25 14:14:38 +00:00
|
|
|
|
2020-04-29 15:13:09 +00:00
|
|
|
if item.rating or item.review:
|
|
|
|
review_title = "Review of {!r} on Goodreads".format(
|
|
|
|
item.book.title,
|
|
|
|
) if item.review else ""
|
|
|
|
handle_review(
|
|
|
|
user,
|
|
|
|
item.book,
|
|
|
|
review_title,
|
|
|
|
item.review,
|
|
|
|
item.rating,
|
|
|
|
)
|
2020-04-15 11:13:38 +00:00
|
|
|
for read in item.reads:
|
|
|
|
read.book = item.book
|
|
|
|
read.user = user
|
|
|
|
read.save()
|
|
|
|
|
2020-03-25 14:14:38 +00:00
|
|
|
if new_books:
|
|
|
|
message = 'imported {} books'.format(len(new_books))
|
2020-09-29 00:26:15 +00:00
|
|
|
status = create_generated_note(user, message, mention_books=new_books)
|
2020-03-25 14:14:38 +00:00
|
|
|
status.save()
|
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
broadcast(user, status.to_create_activity(user))
|
2020-04-22 13:16:46 +00:00
|
|
|
return status
|
2020-09-17 20:02:52 +00:00
|
|
|
return None
|
2020-03-25 14:14:38 +00:00
|
|
|
|
|
|
|
|
2020-04-03 19:43:49 +00:00
|
|
|
def handle_rate(user, book, rating):
|
|
|
|
''' a review that's just a rating '''
|
2020-04-21 00:06:11 +00:00
|
|
|
builder = create_rating
|
2020-09-17 20:02:52 +00:00
|
|
|
handle_status(user, book, builder, rating)
|
2020-04-03 19:43:49 +00:00
|
|
|
|
|
|
|
|
2020-01-28 09:01:33 +00:00
|
|
|
def handle_review(user, book, name, content, rating):
|
|
|
|
''' post a review '''
|
2020-02-15 19:13:49 +00:00
|
|
|
# validated and saves the review in the database so it has an id
|
2020-04-21 00:06:11 +00:00
|
|
|
builder = create_review
|
2020-09-17 20:02:52 +00:00
|
|
|
handle_status(user, book, builder, name, content, rating)
|
2020-04-08 16:40:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def handle_quotation(user, book, content, quote):
|
|
|
|
''' post a review '''
|
|
|
|
# validated and saves the review in the database so it has an id
|
2020-04-21 00:06:11 +00:00
|
|
|
builder = create_quotation
|
2020-09-17 20:02:52 +00:00
|
|
|
handle_status(user, book, builder, content, quote)
|
2020-01-28 09:01:33 +00:00
|
|
|
|
2020-02-21 06:19:19 +00:00
|
|
|
|
2020-04-01 18:22:22 +00:00
|
|
|
def handle_comment(user, book, content):
|
2020-04-21 00:43:42 +00:00
|
|
|
''' post a comment '''
|
2020-03-21 23:50:49 +00:00
|
|
|
# validated and saves the review in the database so it has an id
|
2020-04-21 00:06:11 +00:00
|
|
|
builder = create_comment
|
2020-09-17 20:02:52 +00:00
|
|
|
handle_status(user, book, builder, content)
|
2020-03-21 23:50:49 +00:00
|
|
|
|
2020-04-21 00:06:11 +00:00
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
def handle_status(user, book_id, builder, *args):
|
2020-04-21 00:06:11 +00:00
|
|
|
''' generic handler for statuses '''
|
2020-05-11 01:08:18 +00:00
|
|
|
book = models.Edition.objects.get(id=book_id)
|
2020-04-21 00:06:11 +00:00
|
|
|
status = builder(user, book, *args)
|
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
broadcast(user, status.to_create_activity(user), software='bookwyrm')
|
2020-03-21 23:50:49 +00:00
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
# re-format the activity for non-bookwyrm servers
|
2020-09-17 20:02:52 +00:00
|
|
|
remote_activity = status.to_create_activity(user, pure=True)
|
2020-03-21 23:50:49 +00:00
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
broadcast(user, remote_activity, software='other')
|
2020-03-21 23:50:49 +00:00
|
|
|
|
|
|
|
|
2020-02-21 02:01:50 +00:00
|
|
|
def handle_tag(user, book, name):
|
2020-02-21 06:19:19 +00:00
|
|
|
''' tag a book '''
|
2020-02-21 02:01:50 +00:00
|
|
|
tag = create_tag(user, book, name)
|
2020-09-17 20:02:52 +00:00
|
|
|
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)
|
2020-09-17 20:02:52 +00:00
|
|
|
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-21 02:01:50 +00:00
|
|
|
|
2020-02-18 05:39:08 +00:00
|
|
|
|
2020-03-21 23:50:49 +00:00
|
|
|
def handle_reply(user, review, content):
|
2020-02-19 08:13:06 +00:00
|
|
|
''' respond to a review or status '''
|
2020-02-18 05:39:08 +00:00
|
|
|
# validated and saves the comment in the database so it has an id
|
2020-03-21 23:50:49 +00:00
|
|
|
reply = create_status(user, content, reply_parent=review)
|
|
|
|
if reply.reply_parent:
|
2020-03-07 22:50:29 +00:00
|
|
|
create_notification(
|
2020-03-21 23:50:49 +00:00
|
|
|
reply.reply_parent.user,
|
2020-03-07 22:50:29 +00:00
|
|
|
'REPLY',
|
|
|
|
related_user=user,
|
2020-03-21 23:50:49 +00:00
|
|
|
related_status=reply,
|
2020-03-07 22:50:29 +00:00
|
|
|
)
|
2020-02-18 05:39:08 +00:00
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
broadcast(user, reply.to_create_activity(user))
|
2020-02-18 05:39:08 +00:00
|
|
|
|
2020-02-19 08:13:06 +00:00
|
|
|
|
2020-03-29 02:23:19 +00:00
|
|
|
def handle_favorite(user, status):
|
2020-02-19 08:13:06 +00:00
|
|
|
''' a user likes a status '''
|
2020-02-19 08:22:55 +00:00
|
|
|
try:
|
|
|
|
favorite = models.Favorite.objects.create(
|
|
|
|
status=status,
|
|
|
|
user=user
|
|
|
|
)
|
|
|
|
except IntegrityError:
|
|
|
|
# you already fav'ed that
|
|
|
|
return
|
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
fav_activity = favorite.to_activity()
|
2020-04-22 13:53:22 +00:00
|
|
|
broadcast(
|
|
|
|
user, fav_activity, privacy='direct', direct_recipients=[status.user])
|
2020-02-19 08:13:06 +00:00
|
|
|
|
2020-03-21 22:21:27 +00:00
|
|
|
|
2020-03-29 02:23:19 +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-09-17 20:02:52 +00:00
|
|
|
fav_activity = activitypub.Undo(actor=user, object=favorite)
|
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
|
|
|
|
2020-03-30 14:13:32 +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.
|
2020-03-30 14:13:32 +00:00
|
|
|
return
|
2020-03-30 15:39:53 +00:00
|
|
|
boost = models.Boost.objects.create(
|
|
|
|
boosted_status=status,
|
|
|
|
user=user,
|
|
|
|
)
|
|
|
|
boost.save()
|
2020-03-30 14:13:32 +00:00
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
boost_activity = boost.to_activity()
|
2020-04-21 00:43:42 +00:00
|
|
|
broadcast(user, boost_activity)
|
|
|
|
|
2020-03-29 02:12:17 +00:00
|
|
|
|
|
|
|
def handle_update_book(user, book):
|
|
|
|
''' broadcast the news about our book '''
|
2020-09-17 20:02:52 +00:00
|
|
|
broadcast(user, book.to_update_activity(user))
|
2020-03-29 02:12:17 +00:00
|
|
|
|
2020-03-29 02:19:13 +00:00
|
|
|
|
|
|
|
def handle_update_user(user):
|
|
|
|
''' broadcast editing a user's profile '''
|
2020-09-17 20:02:52 +00:00
|
|
|
broadcast(user, user.to_update_activity())
|