2018-03-22 21:56:13 -04:00
|
|
|
from computed_property import ComputedCharField
|
2017-10-22 18:04:59 -04:00
|
|
|
from django.db import models
|
2018-01-29 00:16:21 -05:00
|
|
|
from django.contrib.auth.models import AbstractUser, UserManager as DjangoUserManager
|
2017-10-30 23:33:16 -04:00
|
|
|
from django.contrib.sites.models import Site as DjangoSite
|
2017-10-26 17:52:26 -04:00
|
|
|
from django.utils.functional import cached_property
|
2018-03-22 21:56:13 -04:00
|
|
|
from hashlib import md5, sha256
|
2017-10-24 06:57:07 -04:00
|
|
|
from meta.models import ModelMeta
|
2017-10-30 23:33:16 -04:00
|
|
|
from urllib.parse import urljoin
|
2017-12-10 21:30:46 -05:00
|
|
|
from lemoncurry import utils
|
2017-10-22 18:04:59 -04:00
|
|
|
|
|
|
|
|
2017-10-22 20:53:51 -04:00
|
|
|
def avatar_path(instance, name):
|
2023-08-10 02:52:37 -04:00
|
|
|
return "avatars/{id}/{name}".format(id=instance.id, name=name)
|
2017-10-22 20:53:51 -04:00
|
|
|
|
|
|
|
|
2017-10-22 21:59:10 -04:00
|
|
|
class Site(models.Model):
|
|
|
|
name = models.CharField(max_length=100, unique=True)
|
|
|
|
icon = models.CharField(max_length=100)
|
2017-10-30 22:46:52 -04:00
|
|
|
domain = models.CharField(max_length=100, blank=True)
|
|
|
|
url_template = models.CharField(max_length=100)
|
|
|
|
|
2023-08-10 02:52:37 -04:00
|
|
|
def format(self, username=""):
|
2017-10-30 22:46:52 -04:00
|
|
|
return self.url_template.format(domain=self.domain, username=username)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def url(self):
|
|
|
|
return self.format()
|
2017-10-22 21:59:10 -04:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Meta:
|
2023-08-10 02:52:37 -04:00
|
|
|
ordering = ("name",)
|
2017-10-22 21:59:10 -04:00
|
|
|
|
|
|
|
|
2018-01-29 00:16:21 -05:00
|
|
|
class UserManager(DjangoUserManager):
|
2018-01-23 21:18:22 -05:00
|
|
|
def get_queryset(self):
|
2023-08-10 02:52:37 -04:00
|
|
|
return (
|
|
|
|
super(UserManager, self).get_queryset().prefetch_related("keys", "profiles")
|
|
|
|
)
|
2018-01-23 21:18:22 -05:00
|
|
|
|
|
|
|
|
2017-10-24 06:57:07 -04:00
|
|
|
class User(ModelMeta, AbstractUser):
|
2018-05-03 23:20:14 -04:00
|
|
|
"""
|
|
|
|
A user in the system - each user will have a representative h-card
|
|
|
|
generated based on all their associated information and may author as many
|
|
|
|
h-entries (:model:`entries.Entry`) as they wish.
|
|
|
|
"""
|
2023-08-10 02:52:37 -04:00
|
|
|
|
2018-01-23 21:18:22 -05:00
|
|
|
objects = UserManager()
|
|
|
|
|
2018-05-03 23:20:14 -04:00
|
|
|
avatar = models.ImageField(
|
2023-08-10 02:52:37 -04:00
|
|
|
upload_to=avatar_path, help_text="an avatar or photo that represents this user"
|
2018-05-03 23:20:14 -04:00
|
|
|
)
|
|
|
|
note = models.TextField(
|
2023-08-10 02:52:37 -04:00
|
|
|
blank=True, help_text="a bio or short description provided by the user"
|
2018-05-03 23:20:14 -04:00
|
|
|
)
|
|
|
|
xmpp = models.EmailField(
|
2023-08-10 02:52:37 -04:00
|
|
|
blank=True, help_text="an XMPP address through which the user may be reached"
|
2018-05-03 23:20:14 -04:00
|
|
|
)
|
2017-11-06 06:02:12 -05:00
|
|
|
|
2017-10-24 21:07:57 -04:00
|
|
|
# This is gonna need to change if I ever decide to add multiple-user support ;)
|
2023-08-10 02:52:37 -04:00
|
|
|
url = "/"
|
2017-10-24 21:07:57 -04:00
|
|
|
|
2018-03-22 21:56:13 -04:00
|
|
|
email_md5 = ComputedCharField(
|
2023-08-10 02:52:37 -04:00
|
|
|
compute_from="calc_email_md5",
|
|
|
|
max_length=32,
|
|
|
|
unique=True,
|
|
|
|
help_text="MD5 hash of the user's email, used for Libravatar",
|
2018-03-22 21:56:13 -04:00
|
|
|
)
|
|
|
|
email_sha256 = ComputedCharField(
|
2023-08-10 02:52:37 -04:00
|
|
|
compute_from="calc_email_sha256",
|
|
|
|
max_length=64,
|
|
|
|
unique=True,
|
|
|
|
help_text="SHA-256 hash of the user's email, used for Libravatar",
|
2018-03-22 21:56:13 -04:00
|
|
|
)
|
2018-07-10 23:13:12 -04:00
|
|
|
openid_sha256 = ComputedCharField(
|
2023-08-10 02:52:37 -04:00
|
|
|
compute_from="calc_openid_sha256",
|
|
|
|
max_length=64,
|
|
|
|
unique=True,
|
|
|
|
help_text="SHA-256 hash of the user's OpenID URL, used for Libravatar",
|
2018-07-10 23:13:12 -04:00
|
|
|
)
|
2018-03-22 21:56:13 -04:00
|
|
|
|
2023-08-10 04:05:46 -04:00
|
|
|
nostr_key = models.CharField(
|
|
|
|
max_length=32,
|
|
|
|
unique=True,
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
help_text="A Nostr public key in 32-byte hex format",
|
|
|
|
)
|
|
|
|
nostr_relays = models.JSONField(
|
|
|
|
default=list,
|
|
|
|
blank=True,
|
|
|
|
help_text="An array of Nostr relay URLs that this public key posts to",
|
|
|
|
)
|
|
|
|
|
2018-03-22 21:56:13 -04:00
|
|
|
@property
|
|
|
|
def calc_email_md5(self):
|
2023-08-10 02:52:37 -04:00
|
|
|
return md5(self.email.lower().encode("utf-8")).hexdigest()
|
2018-03-22 21:56:13 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def calc_email_sha256(self):
|
2023-08-10 02:52:37 -04:00
|
|
|
return sha256(self.email.lower().encode("utf-8")).hexdigest()
|
2018-03-22 21:56:13 -04:00
|
|
|
|
2018-07-10 23:13:12 -04:00
|
|
|
@property
|
|
|
|
def calc_openid_sha256(self):
|
2023-08-10 02:52:37 -04:00
|
|
|
return sha256(self.full_url.encode("utf-8")).hexdigest()
|
2018-07-10 23:13:12 -04:00
|
|
|
|
2017-10-30 23:51:50 -04:00
|
|
|
@property
|
|
|
|
def name(self):
|
2023-08-10 02:52:37 -04:00
|
|
|
return "{0} {1}".format(self.first_name, self.last_name)
|
2017-10-30 23:51:50 -04:00
|
|
|
|
2017-10-27 01:51:46 -04:00
|
|
|
def get_absolute_url(self):
|
2018-05-10 23:23:47 -04:00
|
|
|
return self.absolute_url
|
|
|
|
|
|
|
|
@property
|
|
|
|
def absolute_url(self):
|
|
|
|
return self.full_url
|
2017-10-22 21:33:24 -04:00
|
|
|
|
2017-12-17 17:51:06 -05:00
|
|
|
@property
|
|
|
|
def full_url(self):
|
2023-08-10 02:52:37 -04:00
|
|
|
base = "https://" + DjangoSite.objects.get_current().domain
|
2017-12-17 17:51:06 -05:00
|
|
|
return urljoin(base, self.url)
|
|
|
|
|
2017-12-10 21:30:46 -05:00
|
|
|
@property
|
|
|
|
def description(self):
|
|
|
|
return utils.to_plain(self.note)
|
|
|
|
|
2017-10-24 06:57:07 -04:00
|
|
|
@property
|
|
|
|
def avatar_url(self):
|
|
|
|
return self.avatar.url
|
|
|
|
|
2017-10-26 17:52:26 -04:00
|
|
|
@cached_property
|
2017-10-24 18:34:42 -04:00
|
|
|
def facebook_id(self):
|
2018-01-23 21:18:22 -05:00
|
|
|
for p in self.profiles.all():
|
2023-08-10 02:52:37 -04:00
|
|
|
if p.site.name == "Facebook":
|
2018-01-23 21:18:22 -05:00
|
|
|
return p.username
|
|
|
|
return None
|
2017-10-24 18:34:42 -04:00
|
|
|
|
2017-10-26 17:52:26 -04:00
|
|
|
@cached_property
|
2017-10-24 18:34:42 -04:00
|
|
|
def twitter_username(self):
|
2018-01-23 21:18:22 -05:00
|
|
|
for p in self.profiles.all():
|
2023-08-10 02:52:37 -04:00
|
|
|
if p.site.name == "Twitter":
|
|
|
|
return "@" + p.username
|
2018-01-23 21:18:22 -05:00
|
|
|
return None
|
2017-10-24 18:34:42 -04:00
|
|
|
|
2017-10-30 23:33:16 -04:00
|
|
|
@property
|
|
|
|
def json_ld(self):
|
2023-08-10 02:52:37 -04:00
|
|
|
base = "https://" + DjangoSite.objects.get_current().domain
|
2017-10-30 23:33:16 -04:00
|
|
|
return {
|
2023-08-10 02:52:37 -04:00
|
|
|
"@context": "http://schema.org",
|
|
|
|
"@type": "Person",
|
|
|
|
"@id": self.full_url,
|
|
|
|
"url": self.full_url,
|
|
|
|
"name": self.name,
|
|
|
|
"email": self.email,
|
|
|
|
"image": urljoin(base, self.avatar.url),
|
|
|
|
"givenName": self.first_name,
|
|
|
|
"familyName": self.last_name,
|
|
|
|
"sameAs": [profile.url for profile in self.profiles.all()],
|
2017-10-30 23:33:16 -04:00
|
|
|
}
|
|
|
|
|
2017-10-24 06:57:07 -04:00
|
|
|
_metadata = {
|
2023-08-10 02:52:37 -04:00
|
|
|
"image": "avatar_url",
|
|
|
|
"description": "description",
|
|
|
|
"og_type": "profile",
|
|
|
|
"og_profile_id": "facebook_id",
|
|
|
|
"twitter_creator": "twitter_username",
|
2017-10-24 06:57:07 -04:00
|
|
|
}
|
|
|
|
|
2017-10-22 21:33:24 -04:00
|
|
|
|
2017-10-26 17:52:26 -04:00
|
|
|
class ProfileManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
2023-08-10 02:52:37 -04:00
|
|
|
return super(ProfileManager, self).get_queryset().select_related("site")
|
2017-10-26 17:52:26 -04:00
|
|
|
|
|
|
|
|
2017-10-22 21:59:10 -04:00
|
|
|
class Profile(models.Model):
|
2018-05-03 23:20:14 -04:00
|
|
|
"""
|
|
|
|
Represents a particular :model:`users.User`'s identity on a particular
|
|
|
|
:model:`users.Site` - each user may have as many profiles on as many sites
|
|
|
|
as they wish, and all profiles will become `rel="me"` links on their
|
|
|
|
representative h-card. Additionally, :model:`entries.Syndication` is
|
|
|
|
tracked by linking each syndication to a particular profile.
|
|
|
|
"""
|
2023-08-10 02:52:37 -04:00
|
|
|
|
2017-10-26 17:52:26 -04:00
|
|
|
objects = ProfileManager()
|
2023-08-10 02:52:37 -04:00
|
|
|
user = models.ForeignKey(User, related_name="profiles", on_delete=models.CASCADE)
|
2017-10-22 21:59:10 -04:00
|
|
|
site = models.ForeignKey(Site, on_delete=models.CASCADE)
|
2018-05-03 23:20:14 -04:00
|
|
|
username = models.CharField(
|
2023-08-10 02:52:37 -04:00
|
|
|
max_length=100, help_text="the user's actual handle or ID on the remote site"
|
2018-05-03 23:20:14 -04:00
|
|
|
)
|
|
|
|
display_name = models.CharField(
|
|
|
|
max_length=100,
|
|
|
|
blank=True,
|
2023-08-10 02:52:37 -04:00
|
|
|
help_text="overrides the username for display - useful for sites that use ugly IDs",
|
2018-05-03 23:20:14 -04:00
|
|
|
)
|
2017-10-22 21:59:10 -04:00
|
|
|
|
|
|
|
def __str__(self):
|
2017-10-30 22:46:52 -04:00
|
|
|
if self.site.domain:
|
2023-08-10 02:52:37 -04:00
|
|
|
return self.name + "@" + self.site.domain
|
2017-10-30 22:46:52 -04:00
|
|
|
return self.name
|
2017-10-22 21:59:10 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return self.display_name or self.username
|
|
|
|
|
|
|
|
@property
|
|
|
|
def url(self):
|
2017-10-30 22:46:52 -04:00
|
|
|
return self.site.format(username=self.username)
|
2017-10-22 21:59:10 -04:00
|
|
|
|
|
|
|
class Meta:
|
2023-08-10 02:52:37 -04:00
|
|
|
ordering = ("site", "username")
|
2017-10-22 21:59:10 -04:00
|
|
|
|
|
|
|
|
2023-08-10 02:50:35 -04:00
|
|
|
class PgpKey(models.Model):
|
2018-05-03 23:20:14 -04:00
|
|
|
"""
|
|
|
|
Represents a PGP key that belongs to a particular :model:`users.User`. Each
|
|
|
|
key will be added to the user's h-card with rel="pgpkey", a format
|
|
|
|
compatible with IndieAuth.com.
|
|
|
|
"""
|
2023-08-10 02:52:37 -04:00
|
|
|
|
|
|
|
user = models.ForeignKey(User, related_name="keys", on_delete=models.CASCADE)
|
2017-10-22 21:33:24 -04:00
|
|
|
fingerprint = models.CharField(max_length=40)
|
2023-08-10 02:52:37 -04:00
|
|
|
file = models.FileField(upload_to="keys")
|
2017-10-22 21:33:24 -04:00
|
|
|
|
2017-10-22 21:59:10 -04:00
|
|
|
@property
|
2018-05-03 23:20:14 -04:00
|
|
|
def key_id(self):
|
|
|
|
"""
|
|
|
|
Returns the key ID, defined as the last eight characters of the key's
|
|
|
|
fingerprint. Key IDs are not cryptographically secure (it's easy to
|
|
|
|
forge a key with any key ID of your choosing), but when you have
|
|
|
|
already imported a key using its full fingerprint, the key ID is a
|
|
|
|
convenient way to refer to it.
|
|
|
|
"""
|
|
|
|
return self.fingerprint[32:]
|
2017-10-22 21:59:10 -04:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.key_id
|
|
|
|
|
2017-10-22 21:33:24 -04:00
|
|
|
def pretty_print(self):
|
2018-05-03 23:20:14 -04:00
|
|
|
"""
|
|
|
|
Groups the PGP fingerprint into four-character chunks for display, the
|
|
|
|
same way GnuPG does. This can make reading the fingerprint a little
|
|
|
|
friendlier.
|
|
|
|
"""
|
2023-08-10 02:52:37 -04:00
|
|
|
return " ".join(self.fingerprint[i : i + 4] for i in range(0, 40, 4))
|