2021-09-23 21:49:54 +00:00
|
|
|
""" do book related things with other users """
|
|
|
|
from django.apps import apps
|
2021-10-04 22:05:20 +00:00
|
|
|
from django.db import models, IntegrityError, transaction
|
2021-10-02 00:10:37 +00:00
|
|
|
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
|
2021-10-02 00:10:37 +00:00
|
|
|
from .relationship import UserBlocks
|
2021-10-04 10:31:28 +00:00
|
|
|
|
2021-10-09 11:15:24 +00:00
|
|
|
|
2021-10-02 06:52:34 +00:00
|
|
|
class Group(BookWyrmModel):
|
2021-09-23 21:49:54 +00:00
|
|
|
"""A group of users"""
|
|
|
|
|
|
|
|
name = fields.CharField(max_length=100)
|
2021-10-08 10:21:19 +00:00
|
|
|
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()
|
2021-10-02 00:10:37 +00:00
|
|
|
|
2021-10-02 06:54:44 +00:00
|
|
|
def get_remote_id(self):
|
|
|
|
"""don't want the user to be in there in this case"""
|
|
|
|
return f"https://{DOMAIN}/group/{self.id}"
|
|
|
|
|
2021-10-09 05:11:11 +00:00
|
|
|
@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
|
2021-10-22 07:42:18 +00:00
|
|
|
group members to see the existence of group-curated lists"""
|
2021-10-09 05:11:11 +00:00
|
|
|
|
|
|
|
return queryset.exclude(
|
2021-10-10 02:18:16 +00:00
|
|
|
~Q( # user is not a group member
|
2021-10-09 05:11:11 +00:00
|
|
|
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"""
|
2021-10-09 05:11:11 +00:00
|
|
|
|
|
|
|
return queryset.exclude(~Q(memberships__user=viewer), privacy="direct")
|
2021-10-04 10:31:28 +00:00
|
|
|
|
2021-10-09 11:15:24 +00:00
|
|
|
|
2021-10-02 06:52:34 +00:00
|
|
|
class GroupMember(models.Model):
|
2021-10-02 00:10:37 +00:00
|
|
|
"""Users who are members of a group"""
|
2021-10-04 10:31:28 +00:00
|
|
|
|
2021-10-02 00:10:37 +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
|
|
|
)
|
2021-10-02 00:10:37 +00:00
|
|
|
user = models.ForeignKey(
|
2021-10-04 10:31:28 +00:00
|
|
|
"User", on_delete=models.CASCADE, related_name="memberships"
|
|
|
|
)
|
2021-10-02 00:10:37 +00:00
|
|
|
|
|
|
|
class Meta:
|
2021-10-04 22:05:20 +00:00
|
|
|
"""Users can only have one membership per group"""
|
2021-10-05 10:32:48 +00:00
|
|
|
|
2021-10-02 00:10:37 +00:00
|
|
|
constraints = [
|
2021-10-04 10:31:28 +00:00
|
|
|
models.UniqueConstraint(fields=["group", "user"], name="unique_membership")
|
2021-10-02 00:10:37 +00:00
|
|
|
]
|
2021-09-23 21:49:54 +00:00
|
|
|
|
2021-10-02 00:10:37 +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-10-02 06:52:34 +00:00
|
|
|
# accepts and requests are handled by the GroupInvitation model
|
2021-10-02 00:10:37 +00:00
|
|
|
super().save(*args, **kwargs)
|
2021-09-27 09:27:39 +00:00
|
|
|
|
2021-10-02 00:10:37 +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
|
|
|
|
2021-10-09 11:11:46 +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()
|
2021-10-09 11:11:46 +00:00
|
|
|
|
2021-10-02 00:10:37 +00:00
|
|
|
|
|
|
|
class GroupMemberInvitation(models.Model):
|
|
|
|
"""adding a user to a group requires manual confirmation"""
|
2021-10-04 10:31:28 +00:00
|
|
|
|
2021-10-02 00:10:37 +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"
|
2021-10-02 00:10:37 +00:00
|
|
|
)
|
|
|
|
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:
|
2021-10-04 22:05:20 +00:00
|
|
|
"""Users can only have one outstanding invitation per group"""
|
2021-10-05 10:32:48 +00:00
|
|
|
|
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-02 00:10:37 +00:00
|
|
|
]
|
2021-10-04 10:31:28 +00:00
|
|
|
|
2021-10-04 10:34:54 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2021-10-02 00:10:37 +00:00
|
|
|
"""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():
|
2021-10-02 00:10:37 +00:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
|
|
|
def accept(self):
|
|
|
|
"""turn this request into the real deal"""
|
|
|
|
|
|
|
|
with transaction.atomic():
|
2021-10-02 06:52:34 +00:00
|
|
|
GroupMember.from_request(self)
|
2021-10-02 00:10:37 +00:00
|
|
|
|
|
|
|
model = apps.get_model("bookwyrm.Notification", require_ready=True)
|
2021-10-04 10:31:28 +00:00
|
|
|
# tell the group owner
|
2021-10-02 05:48:55 +00:00
|
|
|
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
|
2021-10-02 02:29:01 +00:00
|
|
|
for membership in self.group.memberships.all():
|
2021-10-04 10:31:28 +00:00
|
|
|
member = membership.user
|
2021-10-05 10:29:33 +00:00
|
|
|
if member not in (self.user, self.group.user):
|
2021-10-02 00:10:37 +00:00
|
|
|
model.objects.create(
|
|
|
|
user=member,
|
|
|
|
related_user=self.user,
|
|
|
|
related_group=self.group,
|
2021-10-02 05:48:55 +00:00
|
|
|
notification_type="JOIN",
|
2021-10-02 00:10:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def reject(self):
|
|
|
|
"""generate a Reject for this membership request"""
|
|
|
|
|
|
|
|
self.delete()
|