2020-02-18 04:12:19 +00:00
|
|
|
''' Handle user activity '''
|
2020-03-29 07:05:09 +00:00
|
|
|
from django.db import IntegrityError
|
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm import models
|
|
|
|
from bookwyrm.books_manager import get_or_create_book
|
|
|
|
from bookwyrm.sanitize_html import InputHtmlParser
|
2020-02-18 04:12:19 +00:00
|
|
|
|
|
|
|
|
2020-04-03 19:43:49 +00:00
|
|
|
def create_rating(user, book, rating):
|
|
|
|
''' a review that's just a rating '''
|
|
|
|
if not rating or rating < 1 or rating > 5:
|
|
|
|
raise ValueError('Invalid rating')
|
|
|
|
return models.Review.objects.create(
|
|
|
|
user=user,
|
|
|
|
book=book,
|
|
|
|
rating=rating,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-04-01 14:10:59 +00:00
|
|
|
def create_review(user, book, name, content, rating):
|
2020-02-18 04:12:19 +00:00
|
|
|
''' a book review has been added '''
|
2020-04-02 16:05:22 +00:00
|
|
|
name = sanitize(name)
|
2020-02-18 05:39:08 +00:00
|
|
|
content = sanitize(content)
|
2020-02-18 04:12:19 +00:00
|
|
|
|
|
|
|
# no ratings outside of 0-5
|
2020-04-02 09:25:47 +00:00
|
|
|
if rating:
|
2020-04-01 18:22:22 +00:00
|
|
|
rating = rating if 1 <= rating <= 5 else None
|
2020-04-02 09:25:47 +00:00
|
|
|
else:
|
2020-04-01 18:22:22 +00:00
|
|
|
rating = None
|
2020-02-18 04:12:19 +00:00
|
|
|
|
2020-03-10 19:04:38 +00:00
|
|
|
return models.Review.objects.create(
|
2020-02-18 04:12:19 +00:00
|
|
|
user=user,
|
|
|
|
book=book,
|
|
|
|
name=name,
|
|
|
|
rating=rating,
|
|
|
|
content=content,
|
|
|
|
)
|
2020-03-10 19:04:38 +00:00
|
|
|
|
|
|
|
|
2020-04-08 16:40:47 +00:00
|
|
|
def create_quotation_from_activity(author, activity):
|
|
|
|
''' parse an activity json blob into a status '''
|
2020-05-04 19:36:55 +00:00
|
|
|
book_id = activity['inReplyToBook']
|
2020-05-11 00:40:22 +00:00
|
|
|
book = get_or_create_book(book_id)
|
2020-04-08 16:40:47 +00:00
|
|
|
quote = activity.get('quote')
|
|
|
|
content = activity.get('content')
|
|
|
|
published = activity.get('published')
|
|
|
|
remote_id = activity['id']
|
|
|
|
|
|
|
|
quotation = create_quotation(author, book, content, quote)
|
|
|
|
quotation.published_date = published
|
|
|
|
quotation.remote_id = remote_id
|
|
|
|
quotation.save()
|
|
|
|
return quotation
|
|
|
|
|
|
|
|
|
2020-05-04 19:36:55 +00:00
|
|
|
def create_quotation(user, book, content, quote):
|
2020-04-08 16:40:47 +00:00
|
|
|
''' a quotation has been added '''
|
|
|
|
# throws a value error if the book is not found
|
|
|
|
content = sanitize(content)
|
|
|
|
quote = sanitize(quote)
|
|
|
|
|
|
|
|
return models.Quotation.objects.create(
|
|
|
|
user=user,
|
|
|
|
book=book,
|
|
|
|
content=content,
|
|
|
|
quote=quote,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-03-21 23:50:49 +00:00
|
|
|
def create_comment_from_activity(author, activity):
|
|
|
|
''' parse an activity json blob into a status '''
|
2020-05-04 19:36:55 +00:00
|
|
|
book_id = activity['inReplyToBook']
|
2020-05-11 00:40:22 +00:00
|
|
|
book = get_or_create_book(book_id)
|
2020-03-21 23:50:49 +00:00
|
|
|
content = activity.get('content')
|
|
|
|
published = activity.get('published')
|
|
|
|
remote_id = activity['id']
|
|
|
|
|
2020-04-01 18:22:22 +00:00
|
|
|
comment = create_comment(author, book, content)
|
2020-03-21 23:50:49 +00:00
|
|
|
comment.published_date = published
|
|
|
|
comment.remote_id = remote_id
|
|
|
|
comment.save()
|
|
|
|
return comment
|
|
|
|
|
|
|
|
|
2020-05-04 19:36:55 +00:00
|
|
|
def create_comment(user, book, content):
|
2020-03-21 23:50:49 +00:00
|
|
|
''' a book comment has been added '''
|
|
|
|
# throws a value error if the book is not found
|
|
|
|
content = sanitize(content)
|
|
|
|
|
|
|
|
return models.Comment.objects.create(
|
|
|
|
user=user,
|
|
|
|
book=book,
|
|
|
|
content=content,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-05-13 01:56:28 +00:00
|
|
|
def get_status(remote_id):
|
2020-03-10 19:04:38 +00:00
|
|
|
''' find a status in the database '''
|
2020-05-13 01:56:28 +00:00
|
|
|
return models.Status.objects.select_subclasses().filter(
|
|
|
|
remote_id=remote_id
|
|
|
|
).first()
|
2020-03-21 22:21:27 +00:00
|
|
|
|
|
|
|
|
2020-09-29 00:26:15 +00:00
|
|
|
def create_generated_note(user, content, mention_books=None):
|
|
|
|
''' a note created by the app about user activity '''
|
|
|
|
# sanitize input html
|
|
|
|
parser = InputHtmlParser()
|
|
|
|
parser.feed(content)
|
|
|
|
content = parser.get_output()
|
|
|
|
|
|
|
|
status = models.GeneratedStatus.objects.create(
|
|
|
|
user=user,
|
|
|
|
content=content,
|
|
|
|
)
|
|
|
|
|
|
|
|
if mention_books:
|
|
|
|
for book in mention_books:
|
|
|
|
status.mention_books.add(book)
|
|
|
|
|
|
|
|
return status
|
|
|
|
|
|
|
|
|
|
|
|
def create_status(user, content, reply_parent=None, mention_books=None):
|
2020-02-18 04:12:19 +00:00
|
|
|
''' 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-02-18 05:39:08 +00:00
|
|
|
if mention_books:
|
|
|
|
for book in mention_books:
|
|
|
|
status.mention_books.add(book)
|
2020-02-18 04:12:19 +00:00
|
|
|
|
|
|
|
return status
|
|
|
|
|
2020-02-18 05:39:08 +00:00
|
|
|
|
2020-02-21 02:01:50 +00:00
|
|
|
def create_tag(user, possible_book, name):
|
|
|
|
''' add a tag to a book '''
|
2020-05-11 00:40:22 +00:00
|
|
|
book = get_or_create_book(possible_book)
|
2020-02-21 02:01:50 +00:00
|
|
|
|
|
|
|
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)
|
2020-02-21 02:01:50 +00:00
|
|
|
return tag
|
|
|
|
|
|
|
|
|
2020-03-07 22:50:29 +00:00
|
|
|
def create_notification(user, notification_type, related_user=None, \
|
2020-04-22 11:43:10 +00:00
|
|
|
related_book=None, related_status=None, related_import=None):
|
2020-03-07 22:50:29 +00:00
|
|
|
''' let a user know when someone interacts with their content '''
|
2020-03-10 00:05:57 +00:00
|
|
|
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,
|
2020-04-22 11:43:10 +00:00
|
|
|
related_import=related_import,
|
2020-03-07 22:50:29 +00:00
|
|
|
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()
|