bookwyrm/fedireads/status.py

139 lines
3.9 KiB
Python
Raw Normal View History

''' Handle user activity '''
from fedireads import models
2020-03-07 20:22:28 +00:00
from fedireads.books_manager import get_or_create_book
from fedireads.sanitize_html import InputHtmlParser
2020-02-21 06:19:19 +00:00
from django.db import IntegrityError
2020-03-10 19:04:38 +00:00
def create_review_from_activity(author, activity):
''' parse an activity json blob into a status '''
book = activity['inReplyToBook']
book = book.split('/')[-1]
name = activity.get('name')
rating = activity.get('rating')
content = activity.get('content')
published = activity.get('published')
remote_id = activity['id']
review = create_review(author, book, name, content, rating)
review.published_date = published
review.remote_id = remote_id
review.save()
return review
def create_review(user, possible_book, name, content, rating):
''' a book review has been added '''
# throws a value error if the book is not found
book = get_or_create_book(possible_book)
2020-02-18 05:39:08 +00:00
content = sanitize(content)
# no ratings outside of 0-5
rating = rating if 0 <= rating <= 5 else 0
2020-03-10 19:04:38 +00:00
return models.Review.objects.create(
user=user,
book=book,
name=name,
rating=rating,
content=content,
)
2020-03-10 19:04:38 +00:00
def create_status_from_activity(author, activity):
''' parse a status object out of an activity json blob '''
content = activity.get('content')
reply_parent_id = activity.get('inReplyTo')
reply_parent = get_status(reply_parent_id)
remote_id = activity['id']
2020-03-13 00:16:55 +00:00
if models.Status.objects.filter(remote_id=remote_id).count():
return None
status = create_status(author, content, reply_parent=reply_parent,
remote_id=remote_id)
2020-03-10 19:04:38 +00:00
status.published_date = activity.get('published')
status.save()
return status
def get_status(absolute_id):
''' find a status in the database '''
# check if it's a remote status
try:
return models.Status.objects.get(remote_id=absolute_id)
except models.Status.DoesNotExist:
pass
# try finding a local status with that id
local_id = absolute_id.split('/')[-1]
try:
2020-03-10 19:16:21 +00:00
possible_match = models.Status.objects.select_subclasses() \
.get(id=local_id)
2020-03-10 19:04:38 +00:00
except models.Status.DoesNotExist:
return None
# make sure it's not actually a remote status with an id that
# clashes with a local id
if possible_match.absolute_id == absolute_id:
return possible_match
return None
2020-03-13 00:16:55 +00:00
def create_status(user, content, reply_parent=None, mention_books=None,
remote_id=None):
''' a status update '''
# TODO: handle @'ing users
# sanitize input html
parser = InputHtmlParser()
parser.feed(content)
content = parser.get_output()
status = models.Status.objects.create(
user=user,
content=content,
reply_parent=reply_parent,
2020-03-13 00:16:55 +00:00
remote_id=remote_id,
)
2020-02-18 05:39:08 +00:00
if mention_books:
for book in mention_books:
status.mention_books.add(book)
return status
2020-02-18 05:39:08 +00:00
def create_tag(user, possible_book, name):
''' add a tag to a book '''
book = get_or_create_book(possible_book)
try:
2020-02-21 06:19:19 +00:00
tag = models.Tag.objects.create(name=name, book=book, user=user)
except IntegrityError:
return models.Tag.objects.get(name=name, book=book, user=user)
return tag
2020-03-07 22:50:29 +00:00
def create_notification(user, notification_type, related_user=None, \
related_book=None, related_status=None):
''' let a user know when someone interacts with their content '''
if user == related_user:
# don't create notification when you interact with your own stuff
return
2020-03-07 22:50:29 +00:00
models.Notification.objects.create(
user=user,
related_book=related_book,
related_user=related_user,
related_status=related_status,
notification_type=notification_type,
)
2020-02-18 05:39:08 +00:00
def sanitize(content):
''' remove invalid html from free text '''
parser = InputHtmlParser()
parser.feed(content)
return parser.get_output()