2020-02-18 04:12:19 +00:00
|
|
|
''' Handle user activity '''
|
2020-10-17 00:00:10 +00:00
|
|
|
from datetime import datetime
|
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-10-08 19:32:45 +00:00
|
|
|
def delete_status(status):
|
|
|
|
''' replace the status with a tombstone '''
|
|
|
|
status.deleted = True
|
2020-10-17 00:00:10 +00:00
|
|
|
status.deleted_date = datetime.now()
|
2020-10-08 19:32:45 +00:00
|
|
|
status.save()
|
|
|
|
|
2020-10-26 22:00:15 +00:00
|
|
|
|
2020-11-06 16:51:50 +00:00
|
|
|
def create_generated_note(user, content, mention_books=None, privacy='public'):
|
2020-09-29 00:26:15 +00:00
|
|
|
''' a note created by the app about user activity '''
|
|
|
|
# sanitize input html
|
|
|
|
parser = InputHtmlParser()
|
|
|
|
parser.feed(content)
|
|
|
|
content = parser.get_output()
|
|
|
|
|
2020-10-30 22:22:20 +00:00
|
|
|
status = models.GeneratedNote.objects.create(
|
2020-09-29 00:26:15 +00:00
|
|
|
user=user,
|
|
|
|
content=content,
|
2020-11-06 16:51:50 +00:00
|
|
|
privacy=privacy
|
2020-09-29 00:26:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if mention_books:
|
|
|
|
for book in mention_books:
|
|
|
|
status.mention_books.add(book)
|
|
|
|
|
|
|
|
return status
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
)
|