2022-11-17 18:21:00 -08:00
|
|
|
from functools import partial
|
2022-11-16 16:23:46 -08:00
|
|
|
from typing import ClassVar
|
|
|
|
|
|
|
|
import pydantic
|
2022-11-17 18:21:00 -08:00
|
|
|
from django.core.files import File
|
2022-11-16 16:23:46 -08:00
|
|
|
from django.db import models
|
2022-11-22 18:52:40 -08:00
|
|
|
from django.utils.functional import lazy
|
2022-11-16 16:23:46 -08:00
|
|
|
|
2022-11-17 18:21:00 -08:00
|
|
|
from core.uploads import upload_namer
|
2022-12-14 23:35:04 -08:00
|
|
|
from core.uris import StaticAbsoluteUrl
|
2022-11-17 18:21:00 -08:00
|
|
|
from takahe import __version__
|
|
|
|
|
|
|
|
|
|
|
|
class UploadedImage(str):
|
|
|
|
"""
|
|
|
|
Type used to indicate a setting is an image
|
|
|
|
"""
|
|
|
|
|
2022-11-16 16:23:46 -08:00
|
|
|
|
|
|
|
class Config(models.Model):
|
|
|
|
"""
|
|
|
|
A configuration setting for either the server or a specific user or identity.
|
|
|
|
|
|
|
|
The possible options and their defaults are defined at the bottom of the file.
|
|
|
|
"""
|
|
|
|
|
|
|
|
key = models.CharField(max_length=500)
|
|
|
|
|
|
|
|
user = models.ForeignKey(
|
|
|
|
"users.user",
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
related_name="configs",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
|
|
|
|
|
|
|
identity = models.ForeignKey(
|
|
|
|
"users.identity",
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
related_name="configs",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
|
|
|
|
2023-05-03 21:42:37 -07:00
|
|
|
domain = models.ForeignKey(
|
|
|
|
"users.domain",
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
related_name="configs",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
|
|
|
|
2022-11-16 16:23:46 -08:00
|
|
|
json = models.JSONField(blank=True, null=True)
|
2022-11-17 18:21:00 -08:00
|
|
|
image = models.ImageField(
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
upload_to=partial(upload_namer, "config"),
|
|
|
|
)
|
2022-11-16 16:23:46 -08:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = [
|
2023-05-03 21:42:37 -07:00
|
|
|
("key", "user", "identity", "domain"),
|
2022-11-16 16:23:46 -08:00
|
|
|
]
|
|
|
|
|
|
|
|
system: ClassVar["Config.ConfigOptions"] # type: ignore
|
|
|
|
|
2022-11-22 18:52:40 -08:00
|
|
|
@classmethod
|
|
|
|
def lazy_system_value(cls, key: str):
|
|
|
|
"""
|
|
|
|
Lazily load a System.Config value
|
|
|
|
"""
|
|
|
|
if key not in cls.SystemOptions.__fields__:
|
|
|
|
raise KeyError(f"Undefined SystemOption for {key}")
|
|
|
|
return lazy(lambda: getattr(Config.system, key))
|
|
|
|
|
2022-11-16 16:23:46 -08:00
|
|
|
@classmethod
|
2022-11-17 18:21:00 -08:00
|
|
|
def load_values(cls, options_class, filters):
|
2022-11-16 16:23:46 -08:00
|
|
|
"""
|
2022-11-17 18:21:00 -08:00
|
|
|
Loads config options and returns an object with them
|
2022-11-16 16:23:46 -08:00
|
|
|
"""
|
|
|
|
values = {}
|
2022-11-17 18:21:00 -08:00
|
|
|
for config in cls.objects.filter(**filters):
|
|
|
|
values[config.key] = config.image.url if config.image else config.json
|
|
|
|
if values[config.key] is None:
|
|
|
|
del values[config.key]
|
|
|
|
values["version"] = __version__
|
|
|
|
return options_class(**values)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def load_system(cls):
|
|
|
|
"""
|
|
|
|
Loads the system config options object
|
|
|
|
"""
|
|
|
|
return cls.load_values(
|
|
|
|
cls.SystemOptions,
|
2023-05-03 21:42:37 -07:00
|
|
|
{"identity__isnull": True, "user__isnull": True, "domain__isnull": True},
|
2022-11-17 18:21:00 -08:00
|
|
|
)
|
2022-11-16 16:23:46 -08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def load_user(cls, user):
|
|
|
|
"""
|
2022-11-17 18:21:00 -08:00
|
|
|
Loads a user config options object
|
2022-11-16 16:23:46 -08:00
|
|
|
"""
|
2022-11-17 18:21:00 -08:00
|
|
|
return cls.load_values(
|
2022-11-22 20:06:21 -08:00
|
|
|
cls.UserOptions,
|
2023-05-03 21:42:37 -07:00
|
|
|
{"identity__isnull": True, "user": user, "domain__isnull": True},
|
2022-11-22 20:06:21 -08:00
|
|
|
)
|
|
|
|
|
2022-11-16 16:23:46 -08:00
|
|
|
@classmethod
|
|
|
|
def load_identity(cls, identity):
|
|
|
|
"""
|
2022-11-22 20:06:21 -08:00
|
|
|
Loads an identity config options object
|
2022-11-16 16:23:46 -08:00
|
|
|
"""
|
2022-11-17 18:21:00 -08:00
|
|
|
return cls.load_values(
|
|
|
|
cls.IdentityOptions,
|
2023-05-03 21:42:37 -07:00
|
|
|
{"identity": identity, "user__isnull": True, "domain__isnull": True},
|
2022-11-17 18:21:00 -08:00
|
|
|
)
|
|
|
|
|
2023-05-03 21:42:37 -07:00
|
|
|
@classmethod
|
|
|
|
def load_domain(cls, domain):
|
|
|
|
"""
|
|
|
|
Loads an domain config options object
|
|
|
|
"""
|
|
|
|
return cls.load_values(
|
|
|
|
cls.DomainOptions,
|
|
|
|
{"domain": domain, "user__isnull": True, "identity__isnull": True},
|
|
|
|
)
|
|
|
|
|
2022-11-17 18:21:00 -08:00
|
|
|
@classmethod
|
|
|
|
def set_value(cls, key, value, options_class, filters):
|
|
|
|
config_field = options_class.__fields__[key]
|
|
|
|
if isinstance(value, File):
|
|
|
|
if config_field.type_ is not UploadedImage:
|
|
|
|
raise ValueError(f"Cannot save file to {key} of type: {type(value)}")
|
|
|
|
cls.objects.update_or_create(
|
|
|
|
key=key,
|
|
|
|
defaults={"json": None, "image": value},
|
|
|
|
**filters,
|
|
|
|
)
|
|
|
|
elif value is None:
|
|
|
|
cls.objects.filter(key=key, **filters).delete()
|
|
|
|
else:
|
|
|
|
if not isinstance(value, config_field.type_):
|
|
|
|
raise ValueError(f"Invalid type for {key}: {type(value)}")
|
|
|
|
if value == config_field.default:
|
|
|
|
cls.objects.filter(key=key, **filters).delete()
|
|
|
|
else:
|
|
|
|
cls.objects.update_or_create(
|
|
|
|
key=key,
|
|
|
|
defaults={"json": value},
|
|
|
|
**filters,
|
|
|
|
)
|
2022-11-16 16:23:46 -08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def set_system(cls, key, value):
|
2022-11-17 18:21:00 -08:00
|
|
|
cls.set_value(
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
cls.SystemOptions,
|
2023-05-03 21:42:37 -07:00
|
|
|
{"identity__isnull": True, "user__isnull": True, "domain__isnull": True},
|
2022-11-17 18:21:00 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def set_user(cls, user, key, value):
|
|
|
|
cls.set_value(
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
cls.UserOptions,
|
2023-05-03 21:42:37 -07:00
|
|
|
{"identity__isnull": True, "user": user, "domain__isnull": True},
|
2022-11-16 16:23:46 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def set_identity(cls, identity, key, value):
|
2022-11-17 18:21:00 -08:00
|
|
|
cls.set_value(
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
cls.IdentityOptions,
|
2023-05-03 21:42:37 -07:00
|
|
|
{"identity": identity, "user__isnull": True, "domain__isnull": True},
|
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def set_domain(cls, domain, key, value):
|
|
|
|
cls.set_value(
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
cls.DomainOptions,
|
|
|
|
{"domain": domain, "user__isnull": True, "identity__isnull": True},
|
2022-11-16 16:23:46 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
class SystemOptions(pydantic.BaseModel):
|
2022-11-17 18:21:00 -08:00
|
|
|
version: str = __version__
|
|
|
|
|
2022-11-20 17:29:19 -08:00
|
|
|
system_actor_public_key: str = ""
|
|
|
|
system_actor_private_key: str = ""
|
|
|
|
|
2022-11-17 18:16:34 -08:00
|
|
|
site_name: str = "Takahē"
|
2022-11-16 16:23:46 -08:00
|
|
|
highlight_color: str = "#449c8c"
|
2022-11-17 18:21:00 -08:00
|
|
|
site_about: str = "<h2>Welcome!</h2>\n\nThis is a community running Takahē."
|
2022-12-22 13:16:08 -08:00
|
|
|
site_frontpage_posts: bool = True
|
2022-12-14 23:35:04 -08:00
|
|
|
site_icon: UploadedImage = StaticAbsoluteUrl("img/icon-128.png").relative # type: ignore
|
|
|
|
site_banner: UploadedImage = StaticAbsoluteUrl(
|
|
|
|
"img/fjords-banner-600.jpg"
|
|
|
|
).relative # type: ignore
|
2022-11-17 18:21:00 -08:00
|
|
|
|
2022-12-05 18:21:00 -08:00
|
|
|
policy_terms: str = ""
|
|
|
|
policy_privacy: str = ""
|
|
|
|
policy_rules: str = ""
|
2023-05-03 21:42:37 -07:00
|
|
|
policy_issues: str = ""
|
2022-12-05 18:21:00 -08:00
|
|
|
|
2022-11-18 07:28:15 -08:00
|
|
|
signup_allowed: bool = True
|
2022-11-17 23:09:04 -08:00
|
|
|
signup_text: str = ""
|
2023-01-03 00:32:00 -08:00
|
|
|
signup_max_users: int = 0
|
2023-01-15 13:48:17 -08:00
|
|
|
signup_email_admins: bool = True
|
2022-11-22 18:52:40 -08:00
|
|
|
content_warning_text: str = "Content Warning"
|
2022-11-17 18:16:34 -08:00
|
|
|
|
2022-11-17 16:43:00 -08:00
|
|
|
post_length: int = 500
|
2023-11-28 08:52:04 -08:00
|
|
|
max_media_attachments: int = 4
|
2022-12-15 14:55:33 -08:00
|
|
|
post_minimum_interval: int = 3 # seconds
|
2022-11-20 10:13:44 -08:00
|
|
|
identity_min_length: int = 2
|
2022-11-17 18:16:34 -08:00
|
|
|
identity_max_per_user: int = 5
|
2022-11-16 16:23:46 -08:00
|
|
|
identity_max_age: int = 24 * 60 * 60
|
2022-12-29 09:31:32 -08:00
|
|
|
public_timeline: bool = True
|
2022-11-16 16:23:46 -08:00
|
|
|
|
2022-11-28 20:41:36 -08:00
|
|
|
hashtag_unreviewed_are_public: bool = True
|
|
|
|
hashtag_stats_max_age: int = 60 * 60
|
|
|
|
|
2022-12-16 19:04:28 -08:00
|
|
|
emoji_unreviewed_are_public: bool = True
|
2022-12-14 23:50:54 -08:00
|
|
|
|
2022-12-05 09:55:30 -08:00
|
|
|
cache_timeout_page_default: int = 60
|
|
|
|
cache_timeout_page_timeline: int = 60 * 3
|
|
|
|
cache_timeout_page_post: int = 60 * 2
|
|
|
|
cache_timeout_identity_feed: int = 60 * 5
|
|
|
|
|
2022-11-18 16:24:43 -08:00
|
|
|
restricted_usernames: str = "admin\nadmins\nadministrator\nadministrators\nsystem\nroot\nannounce\nannouncement\nannouncements"
|
|
|
|
|
2023-01-04 15:42:03 -08:00
|
|
|
custom_head: str | None
|
|
|
|
|
2022-11-16 16:23:46 -08:00
|
|
|
class UserOptions(pydantic.BaseModel):
|
2023-05-03 21:42:37 -07:00
|
|
|
light_theme: bool = False
|
2022-11-16 16:23:46 -08:00
|
|
|
|
|
|
|
class IdentityOptions(pydantic.BaseModel):
|
|
|
|
toot_mode: bool = False
|
2022-11-27 14:40:01 -08:00
|
|
|
default_post_visibility: int = 0 # Post.Visibilities.public
|
2022-12-14 09:15:46 -08:00
|
|
|
visible_follows: bool = True
|
2023-05-03 21:42:37 -07:00
|
|
|
search_enabled: bool = True
|
2022-12-23 21:16:26 -08:00
|
|
|
visible_reaction_counts: bool = True
|
2023-05-13 10:07:57 -07:00
|
|
|
expand_content_warnings: bool = False
|
|
|
|
boosts_on_profile: bool = True
|
2022-12-24 06:28:39 -08:00
|
|
|
|
2023-05-03 21:42:37 -07:00
|
|
|
class DomainOptions(pydantic.BaseModel):
|
|
|
|
site_name: str = ""
|
|
|
|
site_icon: UploadedImage | None = None
|
|
|
|
hide_login: bool = False
|
|
|
|
custom_css: str = ""
|
|
|
|
single_user: str = ""
|