2021-03-08 16:49:10 +00:00
|
|
|
""" base model with default fields """
|
2021-08-06 21:42:18 +00:00
|
|
|
import base64
|
|
|
|
from Crypto import Random
|
2020-02-18 00:50:44 +00:00
|
|
|
from django.db import models
|
2020-05-13 01:56:28 +00:00
|
|
|
from django.dispatch import receiver
|
2020-02-18 00:50:44 +00:00
|
|
|
|
2021-02-04 18:47:03 +00:00
|
|
|
from bookwyrm.settings import DOMAIN
|
|
|
|
from .fields import RemoteIdField
|
2020-02-18 00:50:44 +00:00
|
|
|
|
2020-10-30 18:21:02 +00:00
|
|
|
|
2021-05-11 18:31:02 +00:00
|
|
|
DeactivationReason = models.TextChoices(
|
|
|
|
"DeactivationReason",
|
|
|
|
[
|
2021-08-06 21:42:18 +00:00
|
|
|
"pending",
|
2021-05-11 18:31:02 +00:00
|
|
|
"self_deletion",
|
|
|
|
"moderator_deletion",
|
|
|
|
"domain_block",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-06 21:42:18 +00:00
|
|
|
def new_access_code():
|
|
|
|
"""the identifier for a user invite"""
|
|
|
|
return base64.b32encode(Random.get_random_bytes(5)).decode("ascii")
|
|
|
|
|
|
|
|
|
2020-09-21 15:16:34 +00:00
|
|
|
class BookWyrmModel(models.Model):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""shared fields"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2020-02-18 00:50:44 +00:00
|
|
|
created_date = models.DateTimeField(auto_now_add=True)
|
2020-02-21 01:33:50 +00:00
|
|
|
updated_date = models.DateTimeField(auto_now=True)
|
2021-03-08 16:49:10 +00:00
|
|
|
remote_id = RemoteIdField(null=True, activitypub_field="id")
|
2020-02-18 00:50:44 +00:00
|
|
|
|
2020-05-13 01:56:28 +00:00
|
|
|
def get_remote_id(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""generate a url that resolves to the local object"""
|
2021-03-08 16:49:10 +00:00
|
|
|
base_path = "https://%s" % DOMAIN
|
|
|
|
if hasattr(self, "user"):
|
|
|
|
base_path = "%s%s" % (base_path, self.user.local_path)
|
2020-02-18 01:53:40 +00:00
|
|
|
model_name = type(self).__name__.lower()
|
2021-03-08 16:49:10 +00:00
|
|
|
return "%s/%s/%d" % (base_path, model_name, self.id)
|
2020-02-18 00:50:44 +00:00
|
|
|
|
|
|
|
class Meta:
|
2021-04-26 16:15:42 +00:00
|
|
|
"""this is just here to provide default fields for other models"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2020-02-18 00:50:44 +00:00
|
|
|
abstract = True
|
2020-05-13 01:56:28 +00:00
|
|
|
|
2020-12-31 01:36:35 +00:00
|
|
|
@property
|
|
|
|
def local_path(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""how to link to this object in the local app"""
|
2021-03-08 16:49:10 +00:00
|
|
|
return self.get_remote_id().replace("https://%s" % DOMAIN, "")
|
2020-12-31 01:36:35 +00:00
|
|
|
|
2021-04-11 17:45:08 +00:00
|
|
|
def visible_to_user(self, viewer):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""is a user authorized to view an object?"""
|
2021-04-11 17:45:08 +00:00
|
|
|
# make sure this is an object with privacy owned by a user
|
|
|
|
if not hasattr(self, "user") or not hasattr(self, "privacy"):
|
|
|
|
return None
|
|
|
|
|
|
|
|
# viewer can't see it if the object's owner blocked them
|
|
|
|
if viewer in self.user.blocks.all():
|
|
|
|
return False
|
|
|
|
|
|
|
|
# you can see your own posts and any public or unlisted posts
|
|
|
|
if viewer == self.user or self.privacy in ["public", "unlisted"]:
|
|
|
|
return True
|
|
|
|
|
|
|
|
# you can see the followers only posts of people you follow
|
|
|
|
if (
|
|
|
|
self.privacy == "followers"
|
|
|
|
and self.user.followers.filter(id=viewer.id).first()
|
|
|
|
):
|
|
|
|
return True
|
|
|
|
|
|
|
|
# you can see dms you are tagged in
|
|
|
|
if hasattr(self, "mention_users"):
|
|
|
|
if (
|
|
|
|
self.privacy == "direct"
|
|
|
|
and self.mention_users.filter(id=viewer.id).first()
|
|
|
|
):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2020-05-13 01:56:28 +00:00
|
|
|
|
2020-05-14 01:23:54 +00:00
|
|
|
@receiver(models.signals.post_save)
|
2021-03-08 16:49:10 +00:00
|
|
|
# pylint: disable=unused-argument
|
2021-03-22 21:11:23 +00:00
|
|
|
def set_remote_id(sender, instance, created, *args, **kwargs):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""set the remote_id after save (when the id is available)"""
|
2021-03-08 16:49:10 +00:00
|
|
|
if not created or not hasattr(instance, "get_remote_id"):
|
2020-05-13 01:56:28 +00:00
|
|
|
return
|
2020-05-14 18:28:45 +00:00
|
|
|
if not instance.remote_id:
|
|
|
|
instance.remote_id = instance.get_remote_id()
|
2021-02-07 00:13:59 +00:00
|
|
|
try:
|
|
|
|
instance.save(broadcast=False)
|
|
|
|
except TypeError:
|
|
|
|
instance.save()
|