aboutsummaryrefslogtreecommitdiffstats
path: root/src/endpoints.py
blob: 30426275a146cd9b278d8cce8df09b1fcf802d53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from src import formatting
from src import schema
from src import db
from json import dumps

endpoints = {
    "check_auth": ["user", "auth_hash"],
    "is_registered": ["target_user"],
    "thread_load": ["thread_id"],
    "thread_index": [],
    "thread_create": ["title", "body", "tags"],
    "thread_reply": ["thread_id", "body"],
    "user_register": ["user", "auth_hash", "quip", "bio"],
    "user_get": ["target_user"],
}


authless = [
    "is_registered",
    "user_register"
]


def create_usermap(thread, index=False):
    if index:
        return {user: db.user_get(user) for user in
                  {i["author"] for i in thread}}

    result = {reply["author"] for reply in thread["replies"]}
    result.add(thread["author"])
    return {x: db.user_get(x) for x in result}


def is_registered(json):
    return bool(db.USERDB["namemap"].get(json["target_user"]))


def check_auth(json):
    return bool(db.user_auth(json["user"], json["auth_hash"]))


def user_register(json):
    return schema.response(
        db.user_register(
            json["auth_hash"],
            json["user"],
            json["quip"],
            json["bio"]))


def thread_index(json):
    index = db.thread_index(markup=not json.get("nomarkup"))
    return schema.response({"threads": index}, create_usermap(index, True))


def thread_load(json):
    thread = db.thread_load(json["thread_id"], not json.get("nomarkup"))
    if not thread:
        return schema.error(7, "Requested thread does not exist")
    return schema.response(thread, create_usermap(thread))


def thread_create(json):
    thread = db.thread_create(
        json["user"],
        json["body"],
        json["title"],
        json["tags"])
    if json.get("nomarkup"):
        thread["body"] = formatting.cleanse(thread["body"])
    return schema.response(thread)


def thread_reply(json):
    reply = db.thread_reply(
        json["thread_id"],
        json["user"],
        json["body"])
    if json.get("nomarkup"):
        reply["body"] = formatting.cleanse(reply["body"])
    return schema.response(reply)
Un proyecto texto-plano.xyz