moviewyrm/bookwyrm/models/group.py

156 lines
4.9 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
# from .user import User
2021-09-23 21:49:54 +00:00
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)
2021-10-04 10:31:28 +00:00
user = fields.ForeignKey("User", on_delete=models.PROTECT)
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}"
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()
# accepts and requests are handled by the GroupInvitation 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
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,
)
def accept(self):
"""turn this request into the real deal"""
with transaction.atomic():
GroupMember.from_request(self)
model = apps.get_model("bookwyrm.Notification", require_ready=True)
2021-10-04 10:31:28 +00:00
# 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():
2021-10-04 10:31:28 +00:00
member = membership.user
if member != self.user and member != 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()