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-05-18 13:41:41 -05:00
|
|
|
import requests
|
2018-06-16 15:02:10 -05:00
|
|
|
import yaml
|
2018-05-30 16:47:01 -05:00
|
|
|
from itsdangerous import JSONWebSignatureSerializer
|
2018-06-16 15:02:10 -05:00
|
|
|
from pymongo import MongoClient
|
2018-05-18 13:41:41 -05:00
|
|
|
|
2018-06-16 15:02:10 -05:00
|
|
|
from little_boxes import strtobool
|
|
|
|
from utils.key import KEY_DIR
|
|
|
|
from utils.key import get_key
|
|
|
|
from utils.key import get_secret_key
|
2018-06-16 14:24:53 -05:00
|
|
|
|
2018-05-18 13:41:41 -05:00
|
|
|
|
2018-05-21 10:04:53 -05:00
|
|
|
def noop():
|
|
|
|
pass
|
2018-05-18 13:41:41 -05:00
|
|
|
|
2018-05-21 10:04:53 -05:00
|
|
|
|
|
|
|
CUSTOM_CACHE_HOOKS = False
|
|
|
|
try:
|
2018-06-16 14:24:53 -05:00
|
|
|
from cache_hooks import purge as custom_cache_purge_hook
|
2018-05-21 10:04:53 -05:00
|
|
|
except ModuleNotFoundError:
|
|
|
|
custom_cache_purge_hook = noop
|
|
|
|
|
2018-06-17 12:21:59 -05:00
|
|
|
VERSION = (
|
|
|
|
subprocess.check_output(["git", "describe", "--always"]).split()[0].decode("utf-8")
|
|
|
|
)
|
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-27 04:01:34 -05:00
|
|
|
|
|
|
|
|
2018-06-17 12:21:59 -05:00
|
|
|
CTX_AS = "https://www.w3.org/ns/activitystreams"
|
|
|
|
CTX_SECURITY = "https://w3id.org/security/v1"
|
|
|
|
AS_PUBLIC = "https://www.w3.org/ns/activitystreams#Public"
|
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"]
|
|
|
|
PUBLIC_INSTANCES = conf.get("public_instances", [])
|
2018-05-25 16:57:29 -05:00
|
|
|
# TODO(tsileo): choose dark/light style
|
2018-06-17 12:21:59 -05:00
|
|
|
THEME_COLOR = conf.get("theme_color")
|
2018-05-18 13:41:41 -05:00
|
|
|
|
|
|
|
USER_AGENT = (
|
2018-06-22 17:29:06 -05:00
|
|
|
f"{requests.utils.default_user_agent()} (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(".", "_"))
|
2018-05-27 04:50:09 -05:00
|
|
|
DB = mongo_client[DB_NAME]
|
|
|
|
|
2018-06-16 14:24:53 -05:00
|
|
|
|
2018-05-27 04:50:09 -05:00
|
|
|
def _drop_db():
|
|
|
|
if not DEBUG_MODE:
|
|
|
|
return
|
|
|
|
|
|
|
|
mongo_client.drop_database(DB_NAME)
|
|
|
|
|
2018-06-16 14:24:53 -05:00
|
|
|
|
|
|
|
KEY = get_key(ID, 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
|
|
|
|
2018-05-18 13:41:41 -05:00
|
|
|
ME = {
|
2018-06-17 12:21:59 -05:00
|
|
|
"@context": [CTX_AS, CTX_SECURITY],
|
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",
|
|
|
|
"liked": ID + "/liked",
|
|
|
|
"inbox": ID + "/inbox",
|
|
|
|
"outbox": ID + "/outbox",
|
2018-05-18 13:41:41 -05:00
|
|
|
"preferredUsername": USERNAME,
|
|
|
|
"name": NAME,
|
|
|
|
"summary": SUMMARY,
|
|
|
|
"endpoints": {},
|
|
|
|
"url": ID,
|
2018-06-17 12:21:59 -05:00
|
|
|
"icon": {"mediaType": "image/png", "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
|
|
|
}
|