microblog.pub/config.py

184 lines
4.8 KiB
Python
Raw Normal View History

2018-07-04 14:08:45 -05:00
import mimetypes
2018-05-18 13:41:41 -05:00
import os
2018-06-16 15:02:10 -05:00
import subprocess
from datetime import datetime
2018-07-01 04:40:44 -05:00
from enum import Enum
2019-08-20 15:16:47 -05:00
from pathlib import Path
2018-06-16 15:02:10 -05:00
2018-07-01 05:49:40 -05:00
import yaml
2019-09-08 05:09:34 -05:00
from bleach import linkify
2018-05-30 16:47:01 -05:00
from itsdangerous import JSONWebSignatureSerializer
2018-07-11 16:22:47 -05:00
from little_boxes import strtobool
from little_boxes.activitypub import CTX_AS as AP_DEFAULT_CTX
2018-06-16 15:02:10 -05:00
from pymongo import MongoClient
2018-05-18 13:41:41 -05:00
2019-04-22 04:34:55 -05:00
import sass
2019-08-20 15:16:47 -05:00
from utils.emojis import _load_emojis
2018-06-16 15:02:10 -05:00
from utils.key import KEY_DIR
from utils.key import get_key
from utils.key import get_secret_key
from utils.media import MediaCache
2018-06-16 14:24:53 -05:00
2019-08-20 15:16:47 -05:00
ROOT_DIR = Path(__file__).parent.absolute()
2018-05-18 13:41:41 -05:00
2018-07-01 04:40:44 -05:00
class ThemeStyle(Enum):
LIGHT = "light"
DARK = "dark"
DEFAULT_THEME_STYLE = ThemeStyle.LIGHT.value
DEFAULT_THEME_PRIMARY_COLOR = {
ThemeStyle.LIGHT: "#1d781d", # Green
2018-07-01 05:49:16 -05:00
ThemeStyle.DARK: "#33ff00", # Purple
2018-07-01 04:40:44 -05:00
}
2018-06-17 12:21:59 -05:00
VERSION = (
subprocess.check_output(["git", "describe", "--always"]).split()[0].decode("utf-8")
)
2019-07-07 14:09:04 -05:00
VERSION_DATE = (
subprocess.check_output(["git", "show", VERSION])
2019-07-07 07:28:43 -05:00
.decode()
.splitlines()[2]
.split("Date:")[-1]
.strip()
)
2018-05-18 13:41:41 -05:00
2018-06-17 12:21:59 -05:00
DEBUG_MODE = strtobool(os.getenv("MICROBLOGPUB_DEBUG", "false"))
2018-05-18 13:41:41 -05:00
HEADERS = [
2018-06-17 12:21:59 -05:00
"application/activity+json",
"application/ld+json;profile=https://www.w3.org/ns/activitystreams",
2018-05-18 13:41:41 -05:00
'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
2018-06-17 12:21:59 -05:00
"application/ld+json",
2018-05-18 13:41:41 -05:00
]
2018-06-17 12:21:59 -05:00
with open(os.path.join(KEY_DIR, "me.yml")) as f:
2018-05-18 13:41:41 -05:00
conf = yaml.load(f)
2018-06-17 12:21:59 -05:00
USERNAME = conf["username"]
NAME = conf["name"]
DOMAIN = conf["domain"]
SCHEME = "https" if conf.get("https", True) else "http"
BASE_URL = SCHEME + "://" + DOMAIN
2018-05-18 13:41:41 -05:00
ID = BASE_URL
2018-06-17 12:21:59 -05:00
SUMMARY = conf["summary"]
ICON_URL = conf["icon_url"]
PASS = conf["pass"]
2018-07-01 04:40:44 -05:00
2019-09-08 09:55:24 -05:00
PROFILE_METADATA = conf.get("profile_metadata", {})
2018-07-17 17:29:45 -05:00
HIDE_FOLLOWING = conf.get("hide_following", True)
2018-07-17 16:42:21 -05:00
2018-07-01 04:40:44 -05:00
# Theme-related config
theme_conf = conf.get("theme", {})
THEME_STYLE = ThemeStyle(theme_conf.get("style", DEFAULT_THEME_STYLE))
THEME_COLOR = theme_conf.get("color", DEFAULT_THEME_PRIMARY_COLOR[THEME_STYLE])
2019-08-17 14:02:30 -05:00
DEFAULT_CTX = [
AP_DEFAULT_CTX,
2019-08-20 15:16:47 -05:00
f"{BASE_URL}/microblogpub-0.1.jsonld",
2019-08-17 14:02:30 -05:00
{"@language": "und"},
]
2018-07-01 04:40:44 -05:00
SASS_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "sass")
theme_css = f"$primary-color: {THEME_COLOR};\n"
with open(os.path.join(SASS_DIR, f"{THEME_STYLE.value}.scss")) as f:
theme_css += f.read()
2018-07-01 05:49:40 -05:00
theme_css += "\n"
2018-07-01 04:40:44 -05:00
with open(os.path.join(SASS_DIR, "base_theme.scss")) as f:
raw_css = theme_css + f.read()
2018-07-04 14:08:45 -05:00
CSS = sass.compile(string=raw_css, output_style="compressed")
2018-07-01 04:40:44 -05:00
2018-05-18 13:41:41 -05:00
2019-07-22 15:00:56 -05:00
USER_AGENT = f"microblog.pub/{VERSION}; +{BASE_URL}"
2018-05-18 13:41:41 -05:00
mongo_client = MongoClient(
2018-06-17 12:21:59 -05:00
host=[os.getenv("MICROBLOGPUB_MONGODB_HOST", "localhost:27017")]
2018-05-18 13:41:41 -05:00
)
2018-06-17 12:21:59 -05:00
DB_NAME = "{}_{}".format(USERNAME, DOMAIN.replace(".", "_"))
DB = mongo_client[DB_NAME]
2018-07-04 18:02:51 -05:00
GRIDFS = mongo_client[f"{DB_NAME}_gridfs"]
MEDIA_CACHE = MediaCache(GRIDFS, USER_AGENT)
2018-06-16 14:24:53 -05:00
def _drop_db():
if not DEBUG_MODE:
return
mongo_client.drop_database(DB_NAME)
2018-06-16 14:24:53 -05:00
2019-04-17 16:36:28 -05:00
KEY = get_key(ID, ID + "#main-key", USERNAME, DOMAIN)
2018-05-18 13:41:41 -05:00
2018-05-30 16:47:01 -05:00
2018-06-17 12:21:59 -05:00
JWT_SECRET = get_secret_key("jwt")
2018-05-30 16:47:01 -05:00
JWT = JSONWebSignatureSerializer(JWT_SECRET)
2018-06-16 14:24:53 -05:00
2018-05-30 16:47:01 -05:00
def _admin_jwt_token() -> str:
2018-06-18 15:04:24 -05:00
return JWT.dumps( # type: ignore
2018-06-17 15:05:38 -05:00
{"me": "ADMIN", "ts": datetime.now().timestamp()}
).decode( # type: ignore
2018-06-17 12:21:59 -05:00
"utf-8"
2018-06-17 13:51:23 -05:00
)
2018-05-30 16:47:01 -05:00
2018-06-17 12:21:59 -05:00
ADMIN_API_KEY = get_secret_key("admin_api_key", _admin_jwt_token)
2018-05-30 16:47:01 -05:00
2019-09-06 13:43:49 -05:00
attachments = []
2019-09-08 09:55:24 -05:00
if PROFILE_METADATA:
for key, value in PROFILE_METADATA.items():
2019-09-06 13:43:49 -05:00
attachments.append(
{"type": "PropertyValue", "name": key, "value": linkify(value)}
)
2018-05-18 13:41:41 -05:00
ME = {
2018-07-22 14:34:42 -05:00
"@context": DEFAULT_CTX,
2018-05-18 13:41:41 -05:00
"type": "Person",
"id": ID,
2018-06-17 12:21:59 -05:00
"following": ID + "/following",
"followers": ID + "/followers",
"featured": ID + "/featured",
2018-06-17 12:21:59 -05:00
"inbox": ID + "/inbox",
"outbox": ID + "/outbox",
2018-05-18 13:41:41 -05:00
"preferredUsername": USERNAME,
"name": NAME,
"summary": SUMMARY,
"endpoints": {},
"url": ID,
2018-07-24 17:02:46 -05:00
"manuallyApprovesFollowers": False,
2019-09-06 13:43:49 -05:00
"attachment": attachments,
2018-07-04 14:08:45 -05:00
"icon": {
"mediaType": mimetypes.guess_type(ICON_URL)[0],
"type": "Image",
"url": ICON_URL,
},
2018-06-16 14:24:53 -05:00
"publicKey": KEY.to_dict(),
2018-05-18 13:41:41 -05:00
}
2019-04-10 15:50:36 -05:00
2019-07-21 14:47:52 -05:00
# Default emojis, space-separated, update `me.yml` to customize emojis
2019-04-10 16:08:38 -05:00
EMOJIS = "😺 😸 😹 😻 😼 😽 🙀 😿 😾"
2019-05-12 06:05:27 -05:00
if conf.get("emojis"):
EMOJIS = conf["emojis"]
# Emoji template for the FE
2019-08-17 14:33:15 -05:00
EMOJI_TPL = '<img src="/static/twemoji/{filename}.svg" alt="{raw}" class="emoji">'
2019-05-12 06:05:27 -05:00
if conf.get("emoji_tpl"):
EMOJI_TPL = conf["emoji_tpl"]
2019-07-08 16:18:33 -05:00
2019-07-21 14:47:52 -05:00
# Hosts blacklist
2019-07-08 16:18:33 -05:00
BLACKLIST = conf.get("blacklist", [])
2019-07-21 14:47:52 -05:00
DISABLE_WEBMENTIONS = conf.get("disable_webmentions", False)
2019-07-21 14:47:52 -05:00
# By default, we keep 14 of inbox data ; outbox is kept forever (along with bookmarked stuff, outbox replies, liked...)
2019-09-08 09:55:24 -05:00
DAYS_TO_KEEP = int(conf.get("days_to_keep", 14))
2019-08-20 15:16:47 -05:00
# Load custom emojis (stored in static/emojis)
_load_emojis(ROOT_DIR, BASE_URL)