bookwyrm/fedireads/outgoing.py

362 lines
12 KiB
Python
Raw Normal View History

2020-01-28 19:45:27 +00:00
''' handles all the activity coming out of the server '''
2020-04-01 14:18:45 +00:00
from urllib.parse import urlencode
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
2020-02-18 05:39:08 +00:00
from fedireads import activitypub
2020-01-28 19:45:27 +00:00
from fedireads import models
2020-02-11 23:17:21 +00:00
from fedireads.broadcast import get_recipients, broadcast
2020-04-08 16:40:47 +00:00
from fedireads.status import create_review, create_status
from fedireads.status import create_quotation, create_comment
2020-04-03 19:43:49 +00:00
from fedireads.status import create_tag, create_notification, create_rating
2020-03-29 07:05:09 +00:00
from fedireads.remote_user import get_or_create_remote_user
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
# paginated list of messages
if request.GET.get('page'):
limit = 20
min_id = request.GET.get('min_id')
max_id = request.GET.get('max_id')
# filters for use in the django queryset min/max
filters = {}
# params for the outbox page id
params = {'page': 'true'}
if min_id != None:
params['min_id'] = min_id
filters['id__gt'] = min_id
if max_id != None:
params['max_id'] = max_id
filters['id__lte'] = max_id
page_id = user.outbox + '?' + urlencode(params)
statuses = models.Status.objects.filter(
user=user,
**filters
2020-02-19 20:13:08 +00:00
).select_subclasses().all()[:limit]
return JsonResponse(
activitypub.get_outbox_page(user, page_id, statuses, max_id, min_id)
)
2020-02-11 03:42:21 +00:00
# collection overview
size = models.Status.objects.filter(user=user).count()
return JsonResponse(activitypub.get_outbox(user, size))
2020-01-28 19:13:13 +00:00
def handle_account_search(query):
''' webfingerin' other servers '''
2020-01-28 19:13:13 +00:00
user = None
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':
try:
user = get_or_create_remote_user(link['href'])
except KeyError:
return HttpResponseNotFound()
return user
def handle_follow(user, to_follow):
''' someone local wants to follow someone '''
activity = activitypub.get_follow_request(user, to_follow)
2020-04-01 17:16:20 +00:00
broadcast(user, activity, [to_follow.inbox])
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 = activitypub.get_unfollow(relationship)
2020-04-01 17:16:20 +00:00
broadcast(user, activity, [to_unfollow.inbox])
2020-02-19 06:44:13 +00:00
to_unfollow.followers.remove(user)
def handle_accept(user, to_follow, follow_request):
2020-02-15 06:44:07 +00:00
''' send an acceptance message to a follow request '''
with transaction.atomic():
relationship = models.UserFollows.from_request(follow_request)
follow_request.delete()
relationship.save()
activity = activitypub.get_accept(to_follow, follow_request)
recipient = get_recipients(to_follow, 'direct', direct_recipients=[user])
2020-02-15 06:44:07 +00:00
broadcast(to_follow, activity, recipient)
2020-03-29 07:05:09 +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 '''
relationship.delete()
activity = activitypub.get_reject(to_follow, relationship)
recipient = get_recipients(to_follow, 'direct', direct_recipients=[user])
broadcast(to_follow, activity, recipient)
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
models.ShelfBook(book=book, shelf=shelf, added_by=user).save()
activity = activitypub.get_add(user, book, shelf)
2020-01-28 19:13:13 +00:00
recipients = get_recipients(user, 'public')
broadcast(user, activity, recipients)
2020-02-17 02:45:25 +00:00
# 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)
2020-02-17 02:45:25 +00:00
status = create_status(user, message, mention_books=[book])
2020-03-15 21:15:36 +00:00
status.status_type = 'Update'
status.save()
2020-02-17 02:45:25 +00:00
activity = activitypub.get_status(status)
create_activity = activitypub.get_create(user, activity)
2020-02-17 02:45:25 +00:00
broadcast(user, create_activity, recipients)
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)
row.delete()
activity = activitypub.get_remove(user, book, shelf)
2020-01-29 08:22:48 +00:00
recipients = get_recipients(user, 'public')
broadcast(user, activity, recipients)
def handle_import_books(user, items):
2020-03-27 16:33:31 +00:00
''' 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
2020-03-27 16:33:31 +00:00
_, 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)
recipients = get_recipients(user, 'public')
broadcast(user, activity, recipients)
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()
2020-03-27 16:33:31 +00:00
create_activity = activitypub.get_create(
user, activitypub.get_status(status))
2020-04-01 01:03:58 +00:00
recipients = get_recipients(user, 'public')
2020-04-01 17:16:20 +00:00
broadcast(user, create_activity, recipients)
2020-04-03 19:43:49 +00:00
def handle_rate(user, book, rating):
''' a review that's just a rating '''
2020-04-06 00:29:25 +00:00
rating = create_rating(user, book, rating)
rating_activity = activitypub.get_rating(rating)
rating_create_activity = activitypub.get_create(user, rating_activity)
fr_recipients = get_recipients(user, 'public', limit='fedireads')
broadcast(user, rating_create_activity, fr_recipients)
# re-format the activity for non-fedireads servers
note_activity = activitypub.get_rating_note(rating)
note_create_activity = activitypub.get_create(user, note_activity)
other_recipients = get_recipients(user, 'public', limit='other')
broadcast(user, note_create_activity, other_recipients)
2020-04-03 19:43:49 +00:00
def handle_review(user, book, name, content, rating):
''' post a review '''
# validated and saves the review in the database so it has an id
2020-03-10 19:04:38 +00:00
review = create_review(user, book, name, content, rating)
review_activity = activitypub.get_review(review)
review_create_activity = activitypub.get_create(user, review_activity)
fr_recipients = get_recipients(user, 'public', limit='fedireads')
broadcast(user, review_create_activity, fr_recipients)
# re-format the activity for non-fedireads servers
article_activity = activitypub.get_review_article(review)
article_create_activity = activitypub.get_create(user, article_activity)
2020-04-08 16:40:47 +00:00
other_recipients = get_recipients(user, 'public', limit='other')
broadcast(user, article_create_activity, other_recipients)
def handle_quotation(user, book, content, quote):
''' post a review '''
# validated and saves the review in the database so it has an id
quotation = create_quotation(user, book, content, quote)
quotation_activity = activitypub.get_quotation(quotation)
quotation_create_activity = activitypub.get_create(user, quotation_activity)
fr_recipients = get_recipients(user, 'public', limit='fedireads')
broadcast(user, quotation_create_activity, fr_recipients)
# re-format the activity for non-fedireads servers
article_activity = activitypub.get_quotation_article(quotation)
article_create_activity = activitypub.get_create(user, article_activity)
other_recipients = get_recipients(user, 'public', limit='other')
broadcast(user, article_create_activity, other_recipients)
2020-02-21 06:19:19 +00:00
def handle_comment(user, book, content):
2020-03-21 23:50:49 +00:00
''' post a review '''
# validated and saves the review in the database so it has an id
comment = create_comment(user, book, content)
2020-03-21 23:50:49 +00:00
comment_activity = activitypub.get_comment(comment)
comment_create_activity = activitypub.get_create(user, comment_activity)
fr_recipients = get_recipients(user, 'public', limit='fedireads')
broadcast(user, comment_create_activity, fr_recipients)
# re-format the activity for non-fedireads servers
article_activity = activitypub.get_comment_article(comment)
article_create_activity = activitypub.get_create(user, article_activity)
other_recipients = get_recipients(user, 'public', limit='other')
broadcast(user, article_create_activity, other_recipients)
def handle_tag(user, book, name):
2020-02-21 06:19:19 +00:00
''' tag a book '''
tag = create_tag(user, book, name)
2020-02-21 06:19:19 +00:00
tag_activity = activitypub.get_add_tag(tag)
recipients = get_recipients(user, 'public')
broadcast(user, tag_activity, recipients)
def handle_untag(user, book, name):
''' tag a book '''
book = models.Book.objects.get(fedireads_key=book)
2020-02-21 06:19:19 +00:00
tag = models.Tag.objects.get(name=name, book=book, user=user)
tag_activity = activitypub.get_remove_tag(tag)
tag.delete()
recipients = get_recipients(user, 'public')
broadcast(user, tag_activity, recipients)
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-03-21 23:50:49 +00:00
reply_activity = activitypub.get_status(reply)
create_activity = activitypub.get_create(user, reply_activity)
2020-02-18 05:39:08 +00:00
recipients = get_recipients(user, 'public')
broadcast(user, create_activity, recipients)
2020-02-19 08:13:06 +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
2020-02-19 08:13:06 +00:00
fav_activity = activitypub.get_favorite(favorite)
recipients = get_recipients(user, 'direct', [status.user])
broadcast(user, fav_activity, recipients)
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
fav_activity = activitypub.get_unfavorite(favorite)
recipients = get_recipients(user, 'direct', [status.user])
broadcast(user, fav_activity, recipients)
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 = activitypub.get_boost(boost)
recipients = get_recipients(user, 'public')
broadcast(user, boost_activity, recipients)
2020-03-29 02:12:17 +00:00
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)
def handle_update_user(user):
''' broadcast editing a user's profile '''
actor = activitypub.get_actor(user)
update_activity = activitypub.get_update(user, actor)
recipients = get_recipients(user, 'public')
broadcast(user, update_activity, recipients)