moviewyrm/bookwyrm/models/group.py

182 lines
5.8 KiB
Python
Raw Normal View History

2021-09-23 21:49:54 +00:00
""" do book related things with other users """
from django.apps import apps
from django.db import models, IntegrityError, transaction
from django.db.models import Q
2021-09-23 21:49:54 +00:00
from bookwyrm.settings import DOMAIN
from .base_model import BookWyrmModel
from . import fields
from .relationship import UserBlocks
2021-10-04 10:31:28 +00:00
class Group(BookWyrmModel):
2021-09-23 21:49:54 +00:00
"""A group of users"""
name = fields.CharField(max_length=100)
user = fields.ForeignKey("User", on_delete=models.CASCADE)
2021-09-23 21:49:54 +00:00
description = fields.TextField(blank=True, null=True)
privacy = fields.PrivacyField()
def get_remote_id(self):
"""don't want the user to be in there in this case"""
return f"https://{DOMAIN}/group/{self.id}"
@classmethod
def followers_filter(cls, queryset, viewer):
2021-10-10 02:18:16 +00:00
"""Override filter for "followers" privacy level to allow non-following
group members to see the existence of group-curated lists"""
return queryset.exclude(
2021-10-10 02:18:16 +00:00
~Q( # user is not a group member
Q(user__followers=viewer) | Q(user=viewer) | Q(memberships__user=viewer)
),
privacy="followers", # and the status of the group is followers only
)
@classmethod
def direct_filter(cls, queryset, viewer):
2021-10-10 02:18:16 +00:00
"""Override filter for "direct" privacy level to allow group members
to see the existence of groups and group lists"""
return queryset.exclude(~Q(memberships__user=viewer), privacy="direct")
2021-10-04 10:31:28 +00:00
class GroupMember(models.Model):
"""Users who are members of a group"""
2021-10-04 10:31:28 +00:00
created_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
group = models.ForeignKey(
2021-10-04 10:31:28 +00:00
"Group", on_delete=models.CASCADE, related_name="memberships"
2021-09-23 21:49:54 +00:00
)
user = models.ForeignKey(
2021-10-04 10:31:28 +00:00
"User", on_delete=models.CASCADE, related_name="memberships"
)
class Meta:
"""Users can only have one membership per group"""
constraints = [
2021-10-04 10:31:28 +00:00
models.UniqueConstraint(fields=["group", "user"], name="unique_membership")
]
2021-09-23 21:49:54 +00:00
def save(self, *args, **kwargs):
"""don't let a user invite someone who blocked them"""
# blocking in either direction is a no-go
if UserBlocks.objects.filter(
Q(
user_subject=self.group.user,
user_object=self.user,
)
| Q(
user_subject=self.user,
user_object=self.group.user,
)
).exists():
raise IntegrityError()
2021-12-10 20:46:40 +00:00
# accepts and requests are handled by the GroupMemberInvitation model
super().save(*args, **kwargs)
2021-09-27 09:27:39 +00:00
@classmethod
def from_request(cls, join_request):
"""converts a join request into a member relationship"""
# remove the invite
join_request.delete()
# make a group member
return cls.objects.create(
user=join_request.user,
group=join_request.group,
)
2021-09-23 21:49:54 +00:00
@classmethod
def remove(cls, owner, user):
"""remove a user from a group"""
memberships = cls.objects.filter(group__user=owner, user=user).all()
2021-10-10 02:18:16 +00:00
for member in memberships:
member.delete()
class GroupMemberInvitation(models.Model):
"""adding a user to a group requires manual confirmation"""
2021-10-04 10:31:28 +00:00
created_date = models.DateTimeField(auto_now_add=True)
group = models.ForeignKey(
2021-10-04 10:31:28 +00:00
"Group", on_delete=models.CASCADE, related_name="user_invitations"
)
user = models.ForeignKey(
2021-10-04 10:31:28 +00:00
"User", on_delete=models.CASCADE, related_name="group_invitations"
)
2021-09-25 07:34:44 +00:00
class Meta:
"""Users can only have one outstanding invitation per group"""
2021-09-25 07:34:44 +00:00
constraints = [
2021-10-04 10:31:28 +00:00
models.UniqueConstraint(fields=["group", "user"], name="unique_invitation")
]
2021-10-04 10:31:28 +00:00
def save(self, *args, **kwargs):
"""make sure the membership doesn't already exist"""
# if there's an invitation for a membership that already exists, accept it
# without changing the local database state
2021-10-04 10:31:28 +00:00
if GroupMember.objects.filter(user=self.user, group=self.group).exists():
self.accept()
return
# blocking in either direction is a no-go
if UserBlocks.objects.filter(
Q(
user_subject=self.group.user,
user_object=self.user,
)
| Q(
user_subject=self.user,
user_object=self.group.user,
)
).exists():
raise IntegrityError()
# make an invitation
super().save(*args, **kwargs)
# now send the invite
model = apps.get_model("bookwyrm.Notification", require_ready=True)
notification_type = "INVITE"
model.objects.create(
user=self.user,
related_user=self.group.user,
related_group=self.group,
notification_type=notification_type,
)
@transaction.atomic
def accept(self):
"""turn this request into the real deal"""
GroupMember.from_request(self)
model = apps.get_model("bookwyrm.Notification", require_ready=True)
# tell the group owner
model.objects.create(
user=self.group.user,
related_user=self.user,
related_group=self.group,
notification_type="ACCEPT",
)
# let the other members know about it
for membership in self.group.memberships.all():
member = membership.user
if member not in (self.user, self.group.user):
model.objects.create(
user=member,
related_user=self.user,
related_group=self.group,
notification_type="JOIN",
)
def reject(self):
"""generate a Reject for this membership request"""
self.delete()