From 4ec64c02f442d8836a798e05af98b0eb966d9289 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 12 Jan 2021 13:47:00 -0800 Subject: [PATCH] Adds status views --- bookwyrm/outgoing.py | 105 ---------- .../snippets/create_status_form.html | 2 +- bookwyrm/tests/test_view_actions.py | 12 -- bookwyrm/tests/test_views.py | 51 ----- bookwyrm/urls.py | 42 ++-- bookwyrm/view_actions.py | 64 ------ bookwyrm/views/__init__.py | 1 + bookwyrm/views/helpers.py | 58 ++++++ bookwyrm/views/status.py | 193 ++++++++++++++++++ bookwyrm/vviews.py | 55 ----- 10 files changed, 269 insertions(+), 314 deletions(-) create mode 100644 bookwyrm/views/status.py diff --git a/bookwyrm/outgoing.py b/bookwyrm/outgoing.py index 2a62fd047..1e80852b7 100644 --- a/bookwyrm/outgoing.py +++ b/bookwyrm/outgoing.py @@ -213,111 +213,6 @@ def handle_imported_book(user, item, include_reviews, privacy): broadcast(user, review.to_create_activity(user), privacy=privacy) -def handle_delete_status(user, status): - ''' delete a status and broadcast deletion to other servers ''' - delete_status(status) - broadcast(user, status.to_delete_activity(user)) - - -def handle_status(user, form): - ''' generic handler for statuses ''' - status = form.save(commit=False) - if not status.sensitive and status.content_warning: - # the cw text field remains populated when you click "remove" - status.content_warning = None - status.save() - - # inspect the text for user tags - content = status.content - for (mention_text, mention_user) in find_mentions(content): - # add them to status mentions fk - status.mention_users.add(mention_user) - - # turn the mention into a link - content = re.sub( - r'%s([^@]|$)' % mention_text, - r'%s\g<1>' % \ - (mention_user.remote_id, mention_text), - content) - - # add reply parent to mentions and notify - if status.reply_parent: - status.mention_users.add(status.reply_parent.user) - for mention_user in status.reply_parent.mention_users.all(): - status.mention_users.add(mention_user) - - if status.reply_parent.user.local: - create_notification( - status.reply_parent.user, - 'REPLY', - related_user=user, - related_status=status - ) - - # deduplicate mentions - status.mention_users.set(set(status.mention_users.all())) - # create mention notifications - for mention_user in status.mention_users.all(): - if status.reply_parent and mention_user == status.reply_parent.user: - continue - if mention_user.local: - create_notification( - mention_user, - 'MENTION', - related_user=user, - related_status=status - ) - - # don't apply formatting to generated notes - if not isinstance(status, models.GeneratedNote): - status.content = to_markdown(content) - # do apply formatting to quotes - if hasattr(status, 'quote'): - status.quote = to_markdown(status.quote) - - status.save() - - broadcast(user, status.to_create_activity(user), software='bookwyrm') - - # re-format the activity for non-bookwyrm servers - remote_activity = status.to_create_activity(user, pure=True) - broadcast(user, remote_activity, software='other') - - -def find_mentions(content): - ''' detect @mentions in raw status content ''' - for match in re.finditer(regex.strict_username, content): - username = match.group().strip().split('@')[1:] - if len(username) == 1: - # this looks like a local user (@user), fill in the domain - username.append(DOMAIN) - username = '@'.join(username) - - mention_user = handle_remote_webfinger(username) - if not mention_user: - # we can ignore users we don't know about - continue - yield (match.group(), mention_user) - - -def format_links(content): - ''' detect and format links ''' - return re.sub( - r'([^(href=")]|^|\()(https?:\/\/(%s([\w\.\-_\/+&\?=:;,])*))' % \ - regex.domain, - r'\g<1>\g<3>', - content) - -def to_markdown(content): - ''' catch links and convert to markdown ''' - content = format_links(content) - content = markdown(content) - # sanitize resulting html - sanitizer = InputHtmlParser() - sanitizer.feed(content) - return sanitizer.get_output() - - def handle_favorite(user, status): ''' a user likes a status ''' try: diff --git a/bookwyrm/templates/snippets/create_status_form.html b/bookwyrm/templates/snippets/create_status_form.html index a0c6b817d..c34aa3483 100644 --- a/bookwyrm/templates/snippets/create_status_form.html +++ b/bookwyrm/templates/snippets/create_status_form.html @@ -1,4 +1,4 @@ -