2021-03-08 16:49:10 +00:00
|
|
|
""" database schema for user data """
|
2021-01-04 18:42:31 +00:00
|
|
|
import re
|
2020-09-17 20:02:52 +00:00
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
2020-12-30 21:14:16 +00:00
|
|
|
from django.apps import apps
|
2021-02-28 21:45:49 +00:00
|
|
|
from django.contrib.auth.models import AbstractUser, Group
|
2021-11-21 23:25:47 +00:00
|
|
|
from django.contrib.postgres.fields import ArrayField, CICharField
|
2021-01-19 15:30:35 +00:00
|
|
|
from django.core.validators import MinValueValidator
|
2021-05-26 10:54:57 +00:00
|
|
|
from django.dispatch import receiver
|
2021-08-03 14:00:31 +00:00
|
|
|
from django.db import models, transaction
|
2021-01-16 16:18:54 +00:00
|
|
|
from django.utils import timezone
|
2021-11-24 10:59:45 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2021-05-27 19:09:57 +00:00
|
|
|
from model_utils import FieldTracker
|
2021-03-28 22:51:40 +00:00
|
|
|
import pytz
|
2020-02-11 23:17:21 +00:00
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm import activitypub
|
2021-02-22 18:01:19 +00:00
|
|
|
from bookwyrm.connectors import get_data, ConnectorException
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm.models.shelf import Shelf
|
2020-11-28 19:48:17 +00:00
|
|
|
from bookwyrm.models.status import Status, Review
|
2021-05-26 10:54:57 +00:00
|
|
|
from bookwyrm.preview_images import generate_user_preview_image_task
|
2021-10-06 19:19:52 +00:00
|
|
|
from bookwyrm.settings import DOMAIN, ENABLE_PREVIEW_IMAGES, USE_HTTPS, LANGUAGES
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm.signatures import create_key_pair
|
2020-11-28 19:48:17 +00:00
|
|
|
from bookwyrm.tasks import app
|
2021-01-04 18:42:31 +00:00
|
|
|
from bookwyrm.utils import regex
|
2021-02-04 18:47:03 +00:00
|
|
|
from .activitypub_mixin import OrderedCollectionPageMixin, ActivitypubMixin
|
2021-08-06 21:42:18 +00:00
|
|
|
from .base_model import BookWyrmModel, DeactivationReason, new_access_code
|
2020-11-28 19:48:17 +00:00
|
|
|
from .federated_server import FederatedServer
|
2021-01-16 19:34:19 +00:00
|
|
|
from . import fields, Review
|
2020-02-11 23:17:21 +00:00
|
|
|
|
2021-08-06 23:24:57 +00:00
|
|
|
|
2021-11-24 10:59:45 +00:00
|
|
|
FeedFilterChoices = [
|
|
|
|
("review", _("Reviews")),
|
|
|
|
("comment", _("Comments")),
|
|
|
|
("quotation", _("Quotations")),
|
|
|
|
("everything", _("Everything else")),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def get_feed_filter_choices():
|
2021-11-24 18:04:14 +00:00
|
|
|
"""return a list of filter choice keys"""
|
2021-11-24 10:59:45 +00:00
|
|
|
return [f[0] for f in FeedFilterChoices]
|
|
|
|
|
|
|
|
|
2021-08-06 22:38:37 +00:00
|
|
|
def site_link():
|
|
|
|
"""helper for generating links to the site"""
|
|
|
|
protocol = "https" if USE_HTTPS else "http"
|
|
|
|
return f"{protocol}://{DOMAIN}"
|
2021-08-04 17:58:23 +00:00
|
|
|
|
2021-08-06 23:24:57 +00:00
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
class User(OrderedCollectionPageMixin, AbstractUser):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""a user who wants to read books"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2020-11-30 18:32:13 +00:00
|
|
|
username = fields.UsernameField()
|
2021-01-18 19:51:38 +00:00
|
|
|
email = models.EmailField(unique=True, null=True)
|
2020-11-30 18:32:13 +00:00
|
|
|
|
|
|
|
key_pair = fields.OneToOneField(
|
2021-03-08 16:49:10 +00:00
|
|
|
"KeyPair",
|
2020-11-30 18:32:13 +00:00
|
|
|
on_delete=models.CASCADE,
|
2021-03-08 16:49:10 +00:00
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
activitypub_field="publicKey",
|
|
|
|
related_name="owner",
|
2020-11-30 18:32:13 +00:00
|
|
|
)
|
|
|
|
inbox = fields.RemoteIdField(unique=True)
|
|
|
|
shared_inbox = fields.RemoteIdField(
|
2021-03-08 16:49:10 +00:00
|
|
|
activitypub_field="sharedInbox",
|
|
|
|
activitypub_wrapper="endpoints",
|
2020-12-12 21:39:55 +00:00
|
|
|
deduplication_field=False,
|
2021-03-08 16:49:10 +00:00
|
|
|
null=True,
|
|
|
|
)
|
2020-02-11 23:17:21 +00:00
|
|
|
federated_server = models.ForeignKey(
|
2021-03-08 16:49:10 +00:00
|
|
|
"FederatedServer",
|
2020-02-11 23:17:21 +00:00
|
|
|
on_delete=models.PROTECT,
|
|
|
|
null=True,
|
2020-10-30 20:11:13 +00:00
|
|
|
blank=True,
|
2020-02-11 23:17:21 +00:00
|
|
|
)
|
2021-04-02 14:35:56 +00:00
|
|
|
outbox = fields.RemoteIdField(unique=True, null=True)
|
2020-12-20 20:09:19 +00:00
|
|
|
summary = fields.HtmlField(null=True, blank=True)
|
2020-11-30 18:32:13 +00:00
|
|
|
local = models.BooleanField(default=False)
|
|
|
|
bookwyrm_user = fields.BooleanField(default=True)
|
2021-04-08 15:59:07 +00:00
|
|
|
localname = CICharField(
|
2020-02-11 23:17:21 +00:00
|
|
|
max_length=255,
|
|
|
|
null=True,
|
2020-12-24 19:39:27 +00:00
|
|
|
unique=True,
|
|
|
|
validators=[fields.validate_localname],
|
2020-02-11 23:17:21 +00:00
|
|
|
)
|
|
|
|
# name is your display name, which you can change at will
|
2020-12-20 20:09:19 +00:00
|
|
|
name = fields.CharField(max_length=100, null=True, blank=True)
|
2020-11-30 18:32:13 +00:00
|
|
|
avatar = fields.ImageField(
|
2021-03-08 16:49:10 +00:00
|
|
|
upload_to="avatars/",
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
activitypub_field="icon",
|
|
|
|
alt_field="alt_text",
|
|
|
|
)
|
2021-05-26 10:54:57 +00:00
|
|
|
preview_image = models.ImageField(
|
|
|
|
upload_to="previews/avatars/", blank=True, null=True
|
|
|
|
)
|
2021-08-28 17:33:57 +00:00
|
|
|
followers_url = fields.CharField(max_length=255, activitypub_field="followers")
|
|
|
|
followers = models.ManyToManyField(
|
2021-03-08 16:49:10 +00:00
|
|
|
"self",
|
2020-02-19 06:44:13 +00:00
|
|
|
symmetrical=False,
|
2021-03-08 16:49:10 +00:00
|
|
|
through="UserFollows",
|
|
|
|
through_fields=("user_object", "user_subject"),
|
2021-08-28 17:41:16 +00:00
|
|
|
related_name="following",
|
2020-03-11 12:20:19 +00:00
|
|
|
)
|
|
|
|
follow_requests = models.ManyToManyField(
|
2021-03-08 16:49:10 +00:00
|
|
|
"self",
|
2020-03-11 12:20:19 +00:00
|
|
|
symmetrical=False,
|
2021-03-08 16:49:10 +00:00
|
|
|
through="UserFollowRequest",
|
|
|
|
through_fields=("user_subject", "user_object"),
|
|
|
|
related_name="follower_requests",
|
2020-03-11 12:20:19 +00:00
|
|
|
)
|
|
|
|
blocks = models.ManyToManyField(
|
2021-03-08 16:49:10 +00:00
|
|
|
"self",
|
2020-03-11 12:20:19 +00:00
|
|
|
symmetrical=False,
|
2021-03-08 16:49:10 +00:00
|
|
|
through="UserBlocks",
|
|
|
|
through_fields=("user_subject", "user_object"),
|
|
|
|
related_name="blocked_by",
|
2020-02-19 06:44:13 +00:00
|
|
|
)
|
2021-08-23 18:19:15 +00:00
|
|
|
saved_lists = models.ManyToManyField(
|
2021-09-11 15:57:38 +00:00
|
|
|
"List", symmetrical=False, related_name="saved_lists", blank=True
|
2021-08-23 18:19:15 +00:00
|
|
|
)
|
2020-02-19 07:26:42 +00:00
|
|
|
favorites = models.ManyToManyField(
|
2021-03-08 16:49:10 +00:00
|
|
|
"Status",
|
2020-02-19 07:26:42 +00:00
|
|
|
symmetrical=False,
|
2021-03-08 16:49:10 +00:00
|
|
|
through="Favorite",
|
|
|
|
through_fields=("user", "status"),
|
|
|
|
related_name="favorite_statuses",
|
2020-02-19 07:26:42 +00:00
|
|
|
)
|
2021-02-14 01:42:25 +00:00
|
|
|
default_post_privacy = models.CharField(
|
2021-08-04 17:58:23 +00:00
|
|
|
max_length=255, default="public", choices=fields.PrivacyLevels.choices
|
2021-02-14 01:42:25 +00:00
|
|
|
)
|
2021-03-08 16:49:10 +00:00
|
|
|
remote_id = fields.RemoteIdField(null=True, unique=True, activitypub_field="id")
|
2020-02-21 01:33:50 +00:00
|
|
|
created_date = models.DateTimeField(auto_now_add=True)
|
|
|
|
updated_date = models.DateTimeField(auto_now=True)
|
2021-08-06 02:51:31 +00:00
|
|
|
last_active_date = models.DateTimeField(default=timezone.now)
|
2020-11-30 18:32:13 +00:00
|
|
|
manually_approves_followers = fields.BooleanField(default=False)
|
2021-09-08 16:28:32 +00:00
|
|
|
|
|
|
|
# options to turn features on and off
|
2021-03-18 16:00:25 +00:00
|
|
|
show_goal = models.BooleanField(default=True)
|
2021-09-08 16:28:32 +00:00
|
|
|
show_suggested_users = models.BooleanField(default=True)
|
2021-03-21 21:50:36 +00:00
|
|
|
discoverable = fields.BooleanField(default=False)
|
2021-09-08 16:28:32 +00:00
|
|
|
|
2021-11-21 23:25:47 +00:00
|
|
|
# feed options
|
|
|
|
feed_status_types = ArrayField(
|
2021-11-24 10:59:45 +00:00
|
|
|
models.CharField(max_length=10, blank=False, choices=FeedFilterChoices),
|
2021-11-21 23:25:47 +00:00
|
|
|
size=8,
|
2021-11-24 10:59:45 +00:00
|
|
|
default=get_feed_filter_choices,
|
2021-11-21 23:25:47 +00:00
|
|
|
)
|
2021-12-22 13:57:26 +00:00
|
|
|
# annual summary keys
|
|
|
|
summary_keys = models.JSONField(null=True)
|
2021-11-21 23:25:47 +00:00
|
|
|
|
2021-03-28 22:51:40 +00:00
|
|
|
preferred_timezone = models.CharField(
|
|
|
|
choices=[(str(tz), str(tz)) for tz in pytz.all_timezones],
|
|
|
|
default=str(pytz.utc),
|
|
|
|
max_length=255,
|
|
|
|
)
|
2021-10-06 19:19:52 +00:00
|
|
|
preferred_language = models.CharField(
|
|
|
|
choices=LANGUAGES,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
max_length=255,
|
|
|
|
)
|
2021-04-12 16:51:10 +00:00
|
|
|
deactivation_reason = models.CharField(
|
2021-09-11 21:16:52 +00:00
|
|
|
max_length=255, choices=DeactivationReason, null=True, blank=True
|
2021-04-12 16:51:10 +00:00
|
|
|
)
|
2021-09-11 15:57:38 +00:00
|
|
|
deactivation_date = models.DateTimeField(null=True, blank=True)
|
2021-08-06 21:42:18 +00:00
|
|
|
confirmation_code = models.CharField(max_length=32, default=new_access_code)
|
2020-11-30 18:32:13 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
name_field = "username"
|
2021-04-07 17:32:16 +00:00
|
|
|
property_fields = [("following_link", "following")]
|
2021-05-27 19:40:23 +00:00
|
|
|
field_tracker = FieldTracker(fields=["name", "avatar"])
|
2021-04-07 17:32:16 +00:00
|
|
|
|
2021-08-06 22:38:37 +00:00
|
|
|
@property
|
|
|
|
def confirmation_link(self):
|
|
|
|
"""helper for generating confirmation links"""
|
|
|
|
link = site_link()
|
|
|
|
return f"{link}/confirm-email/{self.confirmation_code}"
|
|
|
|
|
2021-04-07 17:32:16 +00:00
|
|
|
@property
|
|
|
|
def following_link(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""just how to find out the following info"""
|
2021-09-18 04:39:18 +00:00
|
|
|
return f"{self.remote_id}/following"
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2020-12-17 20:46:05 +00:00
|
|
|
@property
|
|
|
|
def alt_text(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""alt text with username"""
|
2021-09-18 04:39:18 +00:00
|
|
|
# pylint: disable=consider-using-f-string
|
|
|
|
return "avatar for {:s}".format(self.localname or self.username)
|
2020-12-17 20:46:05 +00:00
|
|
|
|
2020-11-30 22:24:31 +00:00
|
|
|
@property
|
|
|
|
def display_name(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""show the cleanest version of the user's name possible"""
|
2021-03-08 16:49:10 +00:00
|
|
|
if self.name and self.name != "":
|
2020-11-30 22:24:31 +00:00
|
|
|
return self.name
|
|
|
|
return self.localname or self.username
|
|
|
|
|
2021-04-18 00:55:22 +00:00
|
|
|
@property
|
|
|
|
def deleted(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""for consistent naming"""
|
2021-04-18 00:55:22 +00:00
|
|
|
return not self.is_active
|
|
|
|
|
2021-04-30 14:49:34 +00:00
|
|
|
@property
|
|
|
|
def unread_notification_count(self):
|
2021-04-30 14:57:38 +00:00
|
|
|
"""count of notifications, for the templates"""
|
2021-04-30 14:49:34 +00:00
|
|
|
return self.notification_set.filter(read=False).count()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def has_unread_mentions(self):
|
2021-04-30 14:57:38 +00:00
|
|
|
"""whether any of the unread notifications are conversations"""
|
2021-04-30 14:49:34 +00:00
|
|
|
return self.notification_set.filter(
|
|
|
|
read=False,
|
2021-04-30 20:38:03 +00:00
|
|
|
notification_type__in=["REPLY", "MENTION", "TAG", "REPORT"],
|
2021-04-30 14:49:34 +00:00
|
|
|
).exists()
|
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
activity_serializer = activitypub.Person
|
|
|
|
|
2021-02-23 20:41:37 +00:00
|
|
|
@classmethod
|
|
|
|
def viewer_aware_objects(cls, viewer):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""the user queryset filtered for the context of the logged in user"""
|
2021-02-23 20:41:37 +00:00
|
|
|
queryset = cls.objects.filter(is_active=True)
|
2021-04-11 16:42:55 +00:00
|
|
|
if viewer and viewer.is_authenticated:
|
2021-03-08 16:49:10 +00:00
|
|
|
queryset = queryset.exclude(blocks=viewer)
|
2021-02-23 20:41:37 +00:00
|
|
|
return queryset
|
|
|
|
|
2021-09-22 15:35:20 +00:00
|
|
|
def update_active_date(self):
|
|
|
|
"""this user is here! they are doing things!"""
|
|
|
|
self.last_active_date = timezone.now()
|
|
|
|
self.save(broadcast=False, update_fields=["last_active_date"])
|
|
|
|
|
2020-12-30 21:14:16 +00:00
|
|
|
def to_outbox(self, filter_type=None, **kwargs):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""an ordered collection of statuses"""
|
2020-12-30 21:14:16 +00:00
|
|
|
if filter_type:
|
2021-09-18 18:33:43 +00:00
|
|
|
filter_class = apps.get_model(f"bookwyrm.{filter_type}", require_ready=True)
|
2020-12-30 21:14:16 +00:00
|
|
|
if not issubclass(filter_class, Status):
|
|
|
|
raise TypeError(
|
2021-03-08 16:49:10 +00:00
|
|
|
"filter_status_class must be a subclass of models.Status"
|
|
|
|
)
|
2020-12-30 21:14:16 +00:00
|
|
|
queryset = filter_class.objects
|
|
|
|
else:
|
|
|
|
queryset = Status.objects
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
queryset = (
|
|
|
|
queryset.filter(
|
|
|
|
user=self,
|
|
|
|
deleted=False,
|
|
|
|
privacy__in=["public", "unlisted"],
|
|
|
|
)
|
|
|
|
.select_subclasses()
|
|
|
|
.order_by("-published_date")
|
|
|
|
)
|
|
|
|
return self.to_ordered_collection(
|
|
|
|
queryset, collection_only=True, remote_id=self.outbox, **kwargs
|
|
|
|
).serialize()
|
2020-09-17 20:02:52 +00:00
|
|
|
|
|
|
|
def to_following_activity(self, **kwargs):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""activitypub following list"""
|
2021-09-18 04:39:18 +00:00
|
|
|
remote_id = f"{self.remote_id}/following"
|
2020-12-31 21:47:26 +00:00
|
|
|
return self.to_ordered_collection(
|
2021-03-08 16:49:10 +00:00
|
|
|
self.following.order_by("-updated_date").all(),
|
2020-12-31 21:47:26 +00:00
|
|
|
remote_id=remote_id,
|
|
|
|
id_only=True,
|
2021-08-06 23:24:57 +00:00
|
|
|
**kwargs,
|
2020-12-31 21:47:26 +00:00
|
|
|
)
|
2020-09-17 20:02:52 +00:00
|
|
|
|
|
|
|
def to_followers_activity(self, **kwargs):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""activitypub followers list"""
|
2021-08-28 17:33:57 +00:00
|
|
|
remote_id = self.followers_url
|
2020-12-31 21:47:26 +00:00
|
|
|
return self.to_ordered_collection(
|
2021-03-08 16:49:10 +00:00
|
|
|
self.followers.order_by("-updated_date").all(),
|
2020-12-31 21:47:26 +00:00
|
|
|
remote_id=remote_id,
|
|
|
|
id_only=True,
|
2021-08-06 23:24:57 +00:00
|
|
|
**kwargs,
|
2020-12-31 21:47:26 +00:00
|
|
|
)
|
2020-09-17 20:02:52 +00:00
|
|
|
|
2021-04-02 14:33:36 +00:00
|
|
|
def to_activity(self, **kwargs):
|
2021-03-08 16:49:10 +00:00
|
|
|
"""override default AP serializer to add context object
|
|
|
|
idk if this is the best way to go about this"""
|
2021-04-17 20:31:37 +00:00
|
|
|
if not self.is_active:
|
|
|
|
return self.remote_id
|
|
|
|
|
2021-04-02 14:33:36 +00:00
|
|
|
activity_object = super().to_activity(**kwargs)
|
2021-03-08 16:49:10 +00:00
|
|
|
activity_object["@context"] = [
|
|
|
|
"https://www.w3.org/ns/activitystreams",
|
|
|
|
"https://w3id.org/security/v1",
|
2020-09-17 20:02:52 +00:00
|
|
|
{
|
2021-03-08 16:49:10 +00:00
|
|
|
"manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
|
|
|
|
"schema": "http://schema.org#",
|
|
|
|
"PropertyValue": "schema:PropertyValue",
|
|
|
|
"value": "schema:value",
|
|
|
|
},
|
2020-09-17 20:02:52 +00:00
|
|
|
]
|
|
|
|
return activity_object
|
2020-05-10 04:55:00 +00:00
|
|
|
|
2020-11-01 16:54:10 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""populate fields for new local users"""
|
2021-02-22 16:53:01 +00:00
|
|
|
created = not bool(self.id)
|
2021-06-18 21:12:56 +00:00
|
|
|
if not self.local and not re.match(regex.FULL_USERNAME, self.username):
|
2020-11-01 16:54:10 +00:00
|
|
|
# generate a username that uses the domain (webfinger format)
|
|
|
|
actor_parts = urlparse(self.remote_id)
|
2021-09-18 04:39:18 +00:00
|
|
|
self.username = f"{self.username}@{actor_parts.netloc}"
|
2021-02-10 20:43:53 +00:00
|
|
|
|
|
|
|
# this user already exists, no need to populate fields
|
2021-02-22 17:41:52 +00:00
|
|
|
if not created:
|
2021-09-11 16:00:52 +00:00
|
|
|
if self.is_active:
|
|
|
|
self.deactivation_date = None
|
|
|
|
elif not self.deactivation_date:
|
|
|
|
self.deactivation_date = timezone.now()
|
|
|
|
|
2021-02-10 20:43:53 +00:00
|
|
|
super().save(*args, **kwargs)
|
|
|
|
return
|
2020-11-01 16:54:10 +00:00
|
|
|
|
2021-02-10 20:43:53 +00:00
|
|
|
# this is a new remote user, we need to set their remote server field
|
|
|
|
if not self.local:
|
|
|
|
super().save(*args, **kwargs)
|
2021-08-03 14:00:31 +00:00
|
|
|
transaction.on_commit(lambda: set_remote_server.delay(self.id))
|
2021-02-10 20:43:53 +00:00
|
|
|
return
|
2021-01-04 18:42:31 +00:00
|
|
|
|
2021-09-07 17:09:28 +00:00
|
|
|
with transaction.atomic():
|
|
|
|
# populate fields for local users
|
|
|
|
link = site_link()
|
|
|
|
self.remote_id = f"{link}/user/{self.localname}"
|
|
|
|
self.followers_url = f"{self.remote_id}/followers"
|
|
|
|
self.inbox = f"{self.remote_id}/inbox"
|
|
|
|
self.shared_inbox = f"{link}/inbox"
|
|
|
|
self.outbox = f"{self.remote_id}/outbox"
|
|
|
|
|
|
|
|
# an id needs to be set before we can proceed with related models
|
|
|
|
super().save(*args, **kwargs)
|
2020-11-30 18:32:13 +00:00
|
|
|
|
2021-09-07 17:09:28 +00:00
|
|
|
# make users editors by default
|
|
|
|
try:
|
|
|
|
self.groups.add(Group.objects.get(name="editor"))
|
|
|
|
except Group.DoesNotExist:
|
|
|
|
# this should only happen in tests
|
|
|
|
pass
|
2021-02-10 20:43:53 +00:00
|
|
|
|
2021-09-07 17:09:28 +00:00
|
|
|
# create keys and shelves for new local users
|
|
|
|
self.key_pair = KeyPair.objects.create(
|
|
|
|
remote_id=f"{self.remote_id}/#main-key"
|
|
|
|
)
|
|
|
|
self.save(broadcast=False, update_fields=["key_pair"])
|
2021-02-28 21:45:49 +00:00
|
|
|
|
2021-09-07 17:09:28 +00:00
|
|
|
self.create_shelves()
|
|
|
|
|
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
"""deactivate rather than delete a user"""
|
|
|
|
self.is_active = False
|
|
|
|
# skip the logic in this class's save()
|
|
|
|
super().save(*args, **kwargs)
|
2021-02-10 20:43:53 +00:00
|
|
|
|
2021-09-07 17:09:28 +00:00
|
|
|
@property
|
|
|
|
def local_path(self):
|
|
|
|
"""this model doesn't inherit bookwyrm model, so here we are"""
|
2021-09-18 04:39:18 +00:00
|
|
|
# pylint: disable=consider-using-f-string
|
|
|
|
return "/user/{:s}".format(self.localname or self.username)
|
2021-09-07 17:09:28 +00:00
|
|
|
|
|
|
|
def create_shelves(self):
|
|
|
|
"""default shelves for a new user"""
|
2021-03-08 16:49:10 +00:00
|
|
|
shelves = [
|
|
|
|
{
|
|
|
|
"name": "To Read",
|
|
|
|
"identifier": "to-read",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "Currently Reading",
|
|
|
|
"identifier": "reading",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "Read",
|
|
|
|
"identifier": "read",
|
|
|
|
},
|
|
|
|
]
|
2021-02-10 20:43:53 +00:00
|
|
|
|
|
|
|
for shelf in shelves:
|
|
|
|
Shelf(
|
2021-03-08 16:49:10 +00:00
|
|
|
name=shelf["name"],
|
|
|
|
identifier=shelf["identifier"],
|
2021-02-10 20:43:53 +00:00
|
|
|
user=self,
|
2021-03-08 16:49:10 +00:00
|
|
|
editable=False,
|
2021-02-10 20:43:53 +00:00
|
|
|
).save(broadcast=False)
|
2020-11-01 16:54:10 +00:00
|
|
|
|
2020-11-30 18:32:13 +00:00
|
|
|
|
|
|
|
class KeyPair(ActivitypubMixin, BookWyrmModel):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""public and private keys for a user"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2020-11-30 18:32:13 +00:00
|
|
|
private_key = models.TextField(blank=True, null=True)
|
|
|
|
public_key = fields.TextField(
|
2021-03-08 16:49:10 +00:00
|
|
|
blank=True, null=True, activitypub_field="publicKeyPem"
|
|
|
|
)
|
2020-11-30 18:32:13 +00:00
|
|
|
|
|
|
|
activity_serializer = activitypub.PublicKey
|
2021-03-08 16:49:10 +00:00
|
|
|
serialize_reverse_fields = [("owner", "owner", "id")]
|
2020-11-30 18:32:13 +00:00
|
|
|
|
|
|
|
def get_remote_id(self):
|
|
|
|
# self.owner is set by the OneToOneField on User
|
2021-09-18 04:39:18 +00:00
|
|
|
return f"{self.owner.remote_id}/#main-key"
|
2020-11-30 18:32:13 +00:00
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""create a key pair"""
|
2021-02-08 00:23:20 +00:00
|
|
|
# no broadcasting happening here
|
2021-03-08 16:49:10 +00:00
|
|
|
if "broadcast" in kwargs:
|
|
|
|
del kwargs["broadcast"]
|
2020-12-03 20:35:57 +00:00
|
|
|
if not self.public_key:
|
|
|
|
self.private_key, self.public_key = create_key_pair()
|
2020-11-01 20:07:51 +00:00
|
|
|
return super().save(*args, **kwargs)
|
2020-02-11 23:17:21 +00:00
|
|
|
|
2021-04-02 14:33:36 +00:00
|
|
|
def to_activity(self, **kwargs):
|
2021-03-08 16:49:10 +00:00
|
|
|
"""override default AP serializer to add context object
|
|
|
|
idk if this is the best way to go about this"""
|
2021-04-02 14:33:36 +00:00
|
|
|
activity_object = super().to_activity(**kwargs)
|
2021-03-08 16:49:10 +00:00
|
|
|
del activity_object["@context"]
|
|
|
|
del activity_object["type"]
|
2020-11-30 18:32:54 +00:00
|
|
|
return activity_object
|
|
|
|
|
2020-02-11 23:17:21 +00:00
|
|
|
|
2021-01-16 16:18:54 +00:00
|
|
|
class AnnualGoal(BookWyrmModel):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""set a goal for how many books you read in a year"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
|
|
|
user = models.ForeignKey("User", on_delete=models.PROTECT)
|
|
|
|
goal = models.IntegerField(validators=[MinValueValidator(1)])
|
2021-01-16 16:18:54 +00:00
|
|
|
year = models.IntegerField(default=timezone.now().year)
|
2021-01-16 19:34:19 +00:00
|
|
|
privacy = models.CharField(
|
2021-03-08 16:49:10 +00:00
|
|
|
max_length=255, default="public", choices=fields.PrivacyLevels.choices
|
2021-01-16 19:34:19 +00:00
|
|
|
)
|
2021-01-16 16:18:54 +00:00
|
|
|
|
|
|
|
class Meta:
|
2021-04-26 16:15:42 +00:00
|
|
|
"""unqiueness constraint"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
|
|
|
unique_together = ("user", "year")
|
2021-01-16 16:18:54 +00:00
|
|
|
|
|
|
|
def get_remote_id(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""put the year in the path"""
|
2021-09-18 04:39:18 +00:00
|
|
|
return f"{self.user.remote_id}/goal/{self.year}"
|
2021-01-16 16:18:54 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def books(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""the books you've read this year"""
|
2021-03-08 16:49:10 +00:00
|
|
|
return (
|
2021-05-10 21:38:57 +00:00
|
|
|
self.user.readthrough_set.filter(
|
|
|
|
finish_date__year__gte=self.year,
|
|
|
|
finish_date__year__lt=self.year + 1,
|
|
|
|
)
|
2021-03-08 16:49:10 +00:00
|
|
|
.order_by("-finish_date")
|
|
|
|
.all()
|
|
|
|
)
|
2021-01-16 19:34:19 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def ratings(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""ratings for books read this year"""
|
2021-01-16 19:34:19 +00:00
|
|
|
book_ids = [r.book.id for r in self.books]
|
|
|
|
reviews = Review.objects.filter(
|
|
|
|
user=self.user,
|
|
|
|
book__in=book_ids,
|
|
|
|
)
|
|
|
|
return {r.book.id: r.rating for r in reviews}
|
|
|
|
|
|
|
|
@property
|
2021-05-23 00:33:10 +00:00
|
|
|
def progress(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""how many books you've read this year"""
|
2021-05-23 00:33:10 +00:00
|
|
|
count = self.user.readthrough_set.filter(
|
2021-05-10 21:38:57 +00:00
|
|
|
finish_date__year__gte=self.year,
|
|
|
|
finish_date__year__lt=self.year + 1,
|
2021-03-08 16:49:10 +00:00
|
|
|
).count()
|
2021-05-23 00:33:10 +00:00
|
|
|
return {
|
|
|
|
"count": count,
|
|
|
|
"percent": int(float(count / self.goal) * 100),
|
|
|
|
}
|
2021-01-16 16:18:54 +00:00
|
|
|
|
|
|
|
|
2021-09-08 00:09:44 +00:00
|
|
|
@app.task(queue="low_priority")
|
2020-11-29 18:08:19 +00:00
|
|
|
def set_remote_server(user_id):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""figure out the user's remote server in the background"""
|
2020-11-29 18:08:19 +00:00
|
|
|
user = User.objects.get(id=user_id)
|
|
|
|
actor_parts = urlparse(user.remote_id)
|
2021-03-08 16:49:10 +00:00
|
|
|
user.federated_server = get_or_create_remote_server(actor_parts.netloc)
|
2021-08-04 15:50:50 +00:00
|
|
|
user.save(broadcast=False, update_fields=["federated_server"])
|
2021-04-02 14:35:56 +00:00
|
|
|
if user.bookwyrm_user and user.outbox:
|
2020-11-29 18:08:19 +00:00
|
|
|
get_remote_reviews.delay(user.outbox)
|
|
|
|
|
2020-11-28 19:48:17 +00:00
|
|
|
|
|
|
|
def get_or_create_remote_server(domain):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get info on a remote server"""
|
2020-11-28 19:48:17 +00:00
|
|
|
try:
|
2021-03-08 16:49:10 +00:00
|
|
|
return FederatedServer.objects.get(server_name=domain)
|
2020-11-28 19:48:17 +00:00
|
|
|
except FederatedServer.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
2021-09-18 04:39:18 +00:00
|
|
|
data = get_data(f"https://{domain}/.well-known/nodeinfo")
|
2021-02-22 18:01:19 +00:00
|
|
|
try:
|
2021-03-08 16:49:10 +00:00
|
|
|
nodeinfo_url = data.get("links")[0].get("href")
|
2021-02-22 18:01:19 +00:00
|
|
|
except (TypeError, KeyError):
|
2021-02-22 19:38:11 +00:00
|
|
|
raise ConnectorException()
|
2021-02-22 18:01:19 +00:00
|
|
|
|
|
|
|
data = get_data(nodeinfo_url)
|
2021-03-08 16:49:10 +00:00
|
|
|
application_type = data.get("software", {}).get("name")
|
|
|
|
application_version = data.get("software", {}).get("version")
|
2021-02-22 18:01:19 +00:00
|
|
|
except ConnectorException:
|
|
|
|
application_type = application_version = None
|
2020-11-28 19:48:17 +00:00
|
|
|
|
|
|
|
server = FederatedServer.objects.create(
|
|
|
|
server_name=domain,
|
2021-02-22 18:01:19 +00:00
|
|
|
application_type=application_type,
|
|
|
|
application_version=application_version,
|
2020-11-28 19:48:17 +00:00
|
|
|
)
|
|
|
|
return server
|
|
|
|
|
|
|
|
|
2021-09-08 00:09:44 +00:00
|
|
|
@app.task(queue="low_priority")
|
2020-11-28 19:48:17 +00:00
|
|
|
def get_remote_reviews(outbox):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""ingest reviews by a new remote bookwyrm user"""
|
2021-03-08 16:49:10 +00:00
|
|
|
outbox_page = outbox + "?page=true&type=Review"
|
2020-11-29 18:08:19 +00:00
|
|
|
data = get_data(outbox_page)
|
|
|
|
|
2020-11-28 19:48:17 +00:00
|
|
|
# TODO: pagination?
|
2021-03-08 16:49:10 +00:00
|
|
|
for activity in data["orderedItems"]:
|
|
|
|
if not activity["type"] == "Review":
|
2020-11-28 19:48:17 +00:00
|
|
|
continue
|
2021-02-16 05:41:08 +00:00
|
|
|
activitypub.Review(**activity).to_model()
|
2021-05-26 10:54:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
2021-06-18 22:24:10 +00:00
|
|
|
@receiver(models.signals.post_save, sender=User)
|
2021-05-26 10:54:57 +00:00
|
|
|
def preview_image(instance, *args, **kwargs):
|
2021-06-18 22:24:10 +00:00
|
|
|
"""create preview images when user is updated"""
|
|
|
|
if not ENABLE_PREVIEW_IMAGES:
|
|
|
|
return
|
2021-05-27 19:09:57 +00:00
|
|
|
changed_fields = instance.field_tracker.changed()
|
2021-05-26 10:54:57 +00:00
|
|
|
|
2021-05-27 19:09:57 +00:00
|
|
|
if len(changed_fields) > 0:
|
2021-05-26 10:54:57 +00:00
|
|
|
generate_user_preview_image_task.delay(instance.id)
|