2020-02-11 23:17:21 +00:00
|
|
|
''' database schema for user data '''
|
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
|
2020-02-11 23:17:21 +00:00
|
|
|
from django.contrib.auth.models import AbstractUser
|
|
|
|
from django.db import models
|
|
|
|
from django.dispatch import receiver
|
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm import activitypub
|
2020-11-29 18:08:19 +00:00
|
|
|
from bookwyrm.connectors import get_data
|
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
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm.settings import DOMAIN
|
|
|
|
from bookwyrm.signatures import create_key_pair
|
2020-11-28 19:48:17 +00:00
|
|
|
from bookwyrm.tasks import app
|
2020-11-30 18:32:13 +00:00
|
|
|
from .base_model import OrderedCollectionPageMixin
|
|
|
|
from .base_model import ActivitypubMixin, BookWyrmModel
|
2020-11-28 19:48:17 +00:00
|
|
|
from .federated_server import FederatedServer
|
2020-11-30 18:32:13 +00:00
|
|
|
from . import fields
|
2020-02-11 23:17:21 +00:00
|
|
|
|
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
class User(OrderedCollectionPageMixin, AbstractUser):
|
2020-02-11 23:17:21 +00:00
|
|
|
''' a user who wants to read books '''
|
2020-11-30 18:32:13 +00:00
|
|
|
username = fields.UsernameField()
|
|
|
|
|
|
|
|
key_pair = fields.OneToOneField(
|
|
|
|
'KeyPair',
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
blank=True, null=True,
|
2020-11-30 18:32:54 +00:00
|
|
|
activitypub_field='publicKey',
|
2020-11-30 18:32:13 +00:00
|
|
|
related_name='owner'
|
|
|
|
)
|
|
|
|
inbox = fields.RemoteIdField(unique=True)
|
|
|
|
shared_inbox = fields.RemoteIdField(
|
2020-11-30 18:32:54 +00:00
|
|
|
activitypub_field='sharedInbox',
|
2020-12-12 21:39:55 +00:00
|
|
|
activitypub_wrapper='endpoints',
|
|
|
|
deduplication_field=False,
|
|
|
|
null=True)
|
2020-02-11 23:17:21 +00:00
|
|
|
federated_server = models.ForeignKey(
|
|
|
|
'FederatedServer',
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
null=True,
|
2020-10-30 20:11:13 +00:00
|
|
|
blank=True,
|
2020-02-11 23:17:21 +00:00
|
|
|
)
|
2020-11-30 18:32:13 +00:00
|
|
|
outbox = fields.RemoteIdField(unique=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)
|
2020-02-11 23:17:21 +00:00
|
|
|
localname = models.CharField(
|
|
|
|
max_length=255,
|
|
|
|
null=True,
|
|
|
|
unique=True
|
|
|
|
)
|
|
|
|
# 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(
|
2020-12-17 20:46:05 +00:00
|
|
|
upload_to='avatars/', blank=True, null=True,
|
|
|
|
activitypub_field='icon', alt_field='alt_text')
|
2020-11-30 18:32:13 +00:00
|
|
|
followers = fields.ManyToManyField(
|
2020-02-19 06:44:13 +00:00
|
|
|
'self',
|
2020-11-30 18:32:13 +00:00
|
|
|
link_only=True,
|
2020-02-19 06:44:13 +00:00
|
|
|
symmetrical=False,
|
2020-03-11 12:20:19 +00:00
|
|
|
through='UserFollows',
|
2020-11-30 18:32:13 +00:00
|
|
|
through_fields=('user_object', 'user_subject'),
|
|
|
|
related_name='following'
|
2020-03-11 12:20:19 +00:00
|
|
|
)
|
|
|
|
follow_requests = models.ManyToManyField(
|
|
|
|
'self',
|
|
|
|
symmetrical=False,
|
|
|
|
through='UserFollowRequest',
|
|
|
|
through_fields=('user_subject', 'user_object'),
|
|
|
|
related_name='follower_requests'
|
|
|
|
)
|
|
|
|
blocks = models.ManyToManyField(
|
|
|
|
'self',
|
|
|
|
symmetrical=False,
|
|
|
|
through='UserBlocks',
|
|
|
|
through_fields=('user_subject', 'user_object'),
|
|
|
|
related_name='blocked_by'
|
2020-02-19 06:44:13 +00:00
|
|
|
)
|
2020-02-19 07:26:42 +00:00
|
|
|
favorites = models.ManyToManyField(
|
|
|
|
'Status',
|
|
|
|
symmetrical=False,
|
|
|
|
through='Favorite',
|
|
|
|
through_fields=('user', 'status'),
|
|
|
|
related_name='favorite_statuses'
|
|
|
|
)
|
2020-11-30 18:32:13 +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)
|
2020-11-01 17:16:49 +00:00
|
|
|
last_active_date = models.DateTimeField(auto_now=True)
|
2020-11-30 18:32:13 +00:00
|
|
|
manually_approves_followers = fields.BooleanField(default=False)
|
|
|
|
|
2020-12-18 19:00:30 +00:00
|
|
|
name_field = 'username'
|
2020-12-17 20:46:05 +00:00
|
|
|
@property
|
|
|
|
def alt_text(self):
|
|
|
|
''' alt text with username '''
|
|
|
|
return 'avatar for %s' % (self.localname or self.username)
|
|
|
|
|
2020-11-30 22:24:31 +00:00
|
|
|
@property
|
|
|
|
def display_name(self):
|
|
|
|
''' show the cleanest version of the user's name possible '''
|
2020-12-20 20:50:35 +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
|
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
activity_serializer = activitypub.Person
|
|
|
|
|
2020-12-30 21:14:16 +00:00
|
|
|
def to_outbox(self, filter_type=None, **kwargs):
|
2020-09-17 20:02:52 +00:00
|
|
|
''' an ordered collection of statuses '''
|
2020-12-30 21:14:16 +00:00
|
|
|
if filter_type:
|
|
|
|
filter_class = apps.get_model(
|
|
|
|
'bookwyrm.%s' % filter_type, require_ready=True)
|
|
|
|
if not issubclass(filter_class, Status):
|
|
|
|
raise TypeError(
|
|
|
|
'filter_status_class must be a subclass of models.Status')
|
|
|
|
queryset = filter_class.objects
|
|
|
|
else:
|
|
|
|
queryset = Status.objects
|
|
|
|
|
|
|
|
queryset = queryset.filter(
|
2020-09-17 20:02:52 +00:00
|
|
|
user=self,
|
2020-11-06 22:53:27 +00:00
|
|
|
deleted=False,
|
2020-12-30 20:41:19 +00:00
|
|
|
privacy__in=['public', 'unlisted'],
|
2020-12-04 16:07:47 +00:00
|
|
|
).select_subclasses().order_by('-published_date')
|
2020-09-17 20:02:52 +00:00
|
|
|
return self.to_ordered_collection(queryset, \
|
|
|
|
remote_id=self.outbox, **kwargs)
|
|
|
|
|
|
|
|
def to_following_activity(self, **kwargs):
|
|
|
|
''' activitypub following list '''
|
|
|
|
remote_id = '%s/following' % self.remote_id
|
2020-12-31 21:47:26 +00:00
|
|
|
return self.to_ordered_collection(
|
|
|
|
self.following.order_by('-updated_date').all(),
|
|
|
|
remote_id=remote_id,
|
|
|
|
id_only=True,
|
|
|
|
**kwargs
|
|
|
|
)
|
2020-09-17 20:02:52 +00:00
|
|
|
|
|
|
|
def to_followers_activity(self, **kwargs):
|
|
|
|
''' activitypub followers list '''
|
|
|
|
remote_id = '%s/followers' % self.remote_id
|
2020-12-31 21:47:26 +00:00
|
|
|
return self.to_ordered_collection(
|
|
|
|
self.followers.order_by('-updated_date').all(),
|
|
|
|
remote_id=remote_id,
|
|
|
|
id_only=True,
|
|
|
|
**kwargs
|
|
|
|
)
|
2020-09-17 20:02:52 +00:00
|
|
|
|
2020-11-30 22:24:31 +00:00
|
|
|
def to_activity(self):
|
2020-09-17 20:02:52 +00:00
|
|
|
''' override default AP serializer to add context object
|
|
|
|
idk if this is the best way to go about this '''
|
|
|
|
activity_object = super().to_activity()
|
|
|
|
activity_object['@context'] = [
|
|
|
|
'https://www.w3.org/ns/activitystreams',
|
|
|
|
'https://w3id.org/security/v1',
|
|
|
|
{
|
|
|
|
'manuallyApprovesFollowers': 'as:manuallyApprovesFollowers',
|
|
|
|
'schema': 'http://schema.org#',
|
|
|
|
'PropertyValue': 'schema:PropertyValue',
|
|
|
|
'value': 'schema:value',
|
|
|
|
}
|
|
|
|
]
|
|
|
|
return activity_object
|
2020-05-10 04:55:00 +00:00
|
|
|
|
2020-02-17 03:17:11 +00:00
|
|
|
|
2020-11-01 16:54:10 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
''' populate fields for new local users '''
|
|
|
|
# this user already exists, no need to populate fields
|
|
|
|
if self.id:
|
2020-11-01 20:07:51 +00:00
|
|
|
return super().save(*args, **kwargs)
|
2020-11-01 17:16:49 +00:00
|
|
|
|
2020-11-01 16:54:10 +00:00
|
|
|
if not self.local:
|
|
|
|
# generate a username that uses the domain (webfinger format)
|
|
|
|
actor_parts = urlparse(self.remote_id)
|
|
|
|
self.username = '%s@%s' % (self.username, actor_parts.netloc)
|
2020-11-01 20:07:51 +00:00
|
|
|
return super().save(*args, **kwargs)
|
2020-11-01 16:54:10 +00:00
|
|
|
|
|
|
|
# populate fields for local users
|
|
|
|
self.remote_id = 'https://%s/user/%s' % (DOMAIN, self.username)
|
|
|
|
self.localname = self.username
|
|
|
|
self.username = '%s@%s' % (self.username, DOMAIN)
|
|
|
|
self.actor = self.remote_id
|
|
|
|
self.inbox = '%s/inbox' % self.remote_id
|
|
|
|
self.shared_inbox = 'https://%s/inbox' % DOMAIN
|
|
|
|
self.outbox = '%s/outbox' % self.remote_id
|
2020-11-30 18:32:13 +00:00
|
|
|
|
|
|
|
return super().save(*args, **kwargs)
|
2020-11-01 16:54:10 +00:00
|
|
|
|
2020-12-31 01:36:35 +00:00
|
|
|
@property
|
|
|
|
def local_path(self):
|
|
|
|
''' this model doesn't inherit bookwyrm model, so here we are '''
|
|
|
|
return '/user/%s' % (self.localname or self.username)
|
|
|
|
|
2020-11-30 18:32:13 +00:00
|
|
|
|
|
|
|
class KeyPair(ActivitypubMixin, BookWyrmModel):
|
|
|
|
''' public and private keys for a user '''
|
|
|
|
private_key = models.TextField(blank=True, null=True)
|
|
|
|
public_key = fields.TextField(
|
|
|
|
blank=True, null=True, activitypub_field='publicKeyPem')
|
|
|
|
|
|
|
|
activity_serializer = activitypub.PublicKey
|
2020-12-08 02:28:42 +00:00
|
|
|
serialize_reverse_fields = [('owner', 'owner')]
|
2020-11-30 18:32:13 +00:00
|
|
|
|
|
|
|
def get_remote_id(self):
|
|
|
|
# self.owner is set by the OneToOneField on User
|
|
|
|
return '%s/#main-key' % self.owner.remote_id
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
''' create a key pair '''
|
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
|
|
|
|
2020-11-30 18:32:54 +00:00
|
|
|
def to_activity(self):
|
|
|
|
''' override default AP serializer to add context object
|
|
|
|
idk if this is the best way to go about this '''
|
|
|
|
activity_object = super().to_activity()
|
|
|
|
del activity_object['@context']
|
|
|
|
del activity_object['type']
|
|
|
|
return activity_object
|
|
|
|
|
2020-02-11 23:17:21 +00:00
|
|
|
|
|
|
|
@receiver(models.signals.post_save, sender=User)
|
2020-12-12 23:00:20 +00:00
|
|
|
#pylint: disable=unused-argument
|
2020-02-11 23:17:21 +00:00
|
|
|
def execute_after_save(sender, instance, created, *args, **kwargs):
|
|
|
|
''' create shelves for new users '''
|
2020-11-28 19:48:17 +00:00
|
|
|
if not created:
|
|
|
|
return
|
|
|
|
|
|
|
|
if not instance.local:
|
2020-11-29 18:08:19 +00:00
|
|
|
set_remote_server.delay(instance.id)
|
2020-12-03 20:35:57 +00:00
|
|
|
return
|
2020-02-11 23:17:21 +00:00
|
|
|
|
2020-12-01 03:33:50 +00:00
|
|
|
instance.key_pair = KeyPair.objects.create(
|
|
|
|
remote_id='%s/#main-key' % instance.remote_id)
|
2020-12-03 20:45:01 +00:00
|
|
|
instance.save()
|
2020-12-01 03:33:50 +00:00
|
|
|
|
2020-02-11 23:17:21 +00:00
|
|
|
shelves = [{
|
|
|
|
'name': 'To Read',
|
2020-02-15 22:38:46 +00:00
|
|
|
'identifier': 'to-read',
|
2020-02-11 23:17:21 +00:00
|
|
|
}, {
|
|
|
|
'name': 'Currently Reading',
|
2020-02-15 22:38:46 +00:00
|
|
|
'identifier': 'reading',
|
2020-02-11 23:17:21 +00:00
|
|
|
}, {
|
|
|
|
'name': 'Read',
|
2020-02-15 22:38:46 +00:00
|
|
|
'identifier': 'read',
|
2020-02-11 23:17:21 +00:00
|
|
|
}]
|
|
|
|
|
|
|
|
for shelf in shelves:
|
|
|
|
Shelf(
|
|
|
|
name=shelf['name'],
|
2020-02-15 22:38:46 +00:00
|
|
|
identifier=shelf['identifier'],
|
2020-02-11 23:17:21 +00:00
|
|
|
user=instance,
|
|
|
|
editable=False
|
|
|
|
).save()
|
2020-11-28 19:48:17 +00:00
|
|
|
|
2020-11-30 18:32:13 +00:00
|
|
|
|
2020-11-29 18:08:19 +00:00
|
|
|
@app.task
|
|
|
|
def set_remote_server(user_id):
|
|
|
|
''' figure out the user's remote server in the background '''
|
|
|
|
user = User.objects.get(id=user_id)
|
|
|
|
actor_parts = urlparse(user.remote_id)
|
|
|
|
user.federated_server = \
|
|
|
|
get_or_create_remote_server(actor_parts.netloc)
|
|
|
|
user.save()
|
|
|
|
if user.bookwyrm_user:
|
|
|
|
get_remote_reviews.delay(user.outbox)
|
|
|
|
|
2020-11-28 19:48:17 +00:00
|
|
|
|
|
|
|
def get_or_create_remote_server(domain):
|
|
|
|
''' get info on a remote server '''
|
|
|
|
try:
|
|
|
|
return FederatedServer.objects.get(
|
|
|
|
server_name=domain
|
|
|
|
)
|
|
|
|
except FederatedServer.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
2020-11-29 18:08:19 +00:00
|
|
|
data = get_data('https://%s/.well-known/nodeinfo' % domain)
|
2020-11-28 19:48:17 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
nodeinfo_url = data.get('links')[0].get('href')
|
|
|
|
except (TypeError, KeyError):
|
|
|
|
return None
|
|
|
|
|
2020-11-29 18:08:19 +00:00
|
|
|
data = get_data(nodeinfo_url)
|
2020-11-28 19:48:17 +00:00
|
|
|
|
|
|
|
server = FederatedServer.objects.create(
|
|
|
|
server_name=domain,
|
|
|
|
application_type=data['software']['name'],
|
|
|
|
application_version=data['software']['version'],
|
|
|
|
)
|
|
|
|
return server
|
|
|
|
|
|
|
|
|
|
|
|
@app.task
|
|
|
|
def get_remote_reviews(outbox):
|
|
|
|
''' ingest reviews by a new remote bookwyrm user '''
|
2020-12-30 21:16:09 +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?
|
|
|
|
for activity in data['orderedItems']:
|
|
|
|
if not activity['type'] == 'Review':
|
|
|
|
continue
|
|
|
|
activitypub.Review(**activity).to_model(Review)
|