Moves blocking side effects to model

This commit is contained in:
Mouse Reeve 2021-01-24 17:07:19 -08:00
parent 4e0ec12052
commit d994d8d3c8
5 changed files with 28 additions and 10 deletions

View file

@ -186,16 +186,7 @@ def handle_block(activity):
''' blocking a user ''' ''' blocking a user '''
# create "block" databse entry # create "block" databse entry
block = activitypub.Block(**activity).to_model(models.UserBlocks) block = activitypub.Block(**activity).to_model(models.UserBlocks)
# the removing relationships is handled in post-save hook in model
# remove follow relationships
models.UserFollows.objects.filter(
Q(user_subject=block.user_subject, user_object=block.user_object) | \
Q(user_subject=block.user_object, user_object=block.user_subject)
).delete()
models.UserFollowRequest.objects.filter(
Q(user_subject=block.user_subject, user_object=block.user_object) | \
Q(user_subject=block.user_object, user_object=block.user_subject)
).delete()
@app.task @app.task

View file

@ -1,5 +1,7 @@
''' defines relationships between users ''' ''' defines relationships between users '''
from django.db import models from django.db import models
from django.db.models import Q
from django.dispatch import receiver
from bookwyrm import activitypub from bookwyrm import activitypub
from .base_model import ActivitypubMixin, BookWyrmModel from .base_model import ActivitypubMixin, BookWyrmModel
@ -96,3 +98,21 @@ class UserBlocks(UserRelationship):
''' prevent another user from following you and seeing your posts ''' ''' prevent another user from following you and seeing your posts '''
status = 'blocks' status = 'blocks'
activity_serializer = activitypub.Block activity_serializer = activitypub.Block
@receiver(models.signals.post_save, sender=UserBlocks)
#pylint: disable=unused-argument
def execute_after_save(sender, instance, created, *args, **kwargs):
''' remove follow or follow request rels after a block is created '''
UserFollows.objects.filter(
Q(user_subject=instance.user_subject,
user_object=instance.user_object) | \
Q(user_subject=instance.user_object,
user_object=instance.user_subject)
).delete()
UserFollowRequest.objects.filter(
Q(user_subject=instance.user_subject,
user_object=instance.user_object) | \
Q(user_subject=instance.user_object,
user_object=instance.user_subject)
).delete()

View file

@ -36,6 +36,10 @@
</div> </div>
{% if not is_self %} {% if not is_self %}
{% include 'snippets/follow_button.html' with user=user %} {% include 'snippets/follow_button.html' with user=user %}
<form name="blocks" method="post" action="/block/{{ user.id }}">
{% csrf_token %}
<button type="submit">Block</button>
</form>
{% endif %} {% endif %}
{% if is_self and user.follower_requests.all %} {% if is_self and user.follower_requests.all %}

View file

@ -136,4 +136,6 @@ urlpatterns = [
re_path(r'^unfollow/?$', views.unfollow), re_path(r'^unfollow/?$', views.unfollow),
re_path(r'^accept-follow-request/?$', views.accept_follow_request), re_path(r'^accept-follow-request/?$', views.accept_follow_request),
re_path(r'^delete-follow-request/?$', views.delete_follow_request), re_path(r'^delete-follow-request/?$', views.delete_follow_request),
re_path(r'^block/(?P<user_id>\d+)/?$', views.Block.as_view()),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

View file

@ -1,6 +1,7 @@
''' make sure all our nice views are available ''' ''' make sure all our nice views are available '''
from .authentication import Login, Register, Logout from .authentication import Login, Register, Logout
from .author import Author, EditAuthor from .author import Author, EditAuthor
from .block import Block
from .books import Book, EditBook, Editions from .books import Book, EditBook, Editions
from .books import upload_cover, add_description, switch_edition, resolve_book from .books import upload_cover, add_description, switch_edition, resolve_book
from .direct_message import DirectMessage from .direct_message import DirectMessage