aboutsummaryrefslogtreecommitdiffstats
path: root/server.py
blob: 957d23bd685baf0133311f3582819f2a7f3690dd (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from src.exceptions import BBJException, BBJParameterError
from src import db, schema, endpoints
from functools import wraps
import cherrypy
import sqlite3
import json

dbname = "data.sqlite"

with sqlite3.connect(dbname) as _c:
    if not db.user_resolve(_c, "anonymous"):
        db.user_register(_c, *db.anonymous)


# creates a database connection for each thread
def connect(_):
    cherrypy.thread_data.db = sqlite3.connect(dbname)
cherrypy.engine.subscribe('start_thread', connect)


def bbjapi(function):
    """
    A wrapper that handles encoding of objects and errors to a
    standard format for the API, resolves and authorizes users
    from header data, and prepares thread data to handle the
    request.

    In addition, all BBJException's will return their attached
    schema, and unhandled exceptions return a code 1 error schema.
    """
    @wraps(function)
    def wrapper(*args, **kwargs):
        headers = cherrypy.request.headers
        username = headers.get("User")
        auth = headers.get("Auth")
        anon = False

        if not username and not auth:
            user = db.user_resolve(cherrypy.thread_data.db, "anonymous")
            anon = True
        elif not username or not auth:
            return json.dumps(schema.error(5,
                "User or Auth was given without the other."))

        if not anon:
            user = db.user_resolve(cherrypy.thread_data.db, username)
            if not user:
                return json.dumps(schema.error(4,
                    "Username is not registered."))

            elif auth != user["auth_hash"]:
                return json.dumps(schema.error(5,
                    "Invalid authorization key for user."))

        cherrypy.thread_data.user = user
        cherrypy.thread_data.anon = anon

        try:
            value = function(*args, **kwargs)

        except BBJException as e:
            value = e.schema

        except Exception as e:
            value = schema.error(1, str(e))

        return json.dumps(value)
    return wrapper


def create_usermap(connection, obj):
    """
    Creates a mapping of all the user_ids that occur in OBJ to
    their full user objects (names, profile info, etc). Can
    be a thread_index or a messages object from one.
    """

    if isinstance(obj, dict):
        # this is a message object for a thread, unravel it
        obj = [value for key, value in obj.items()]

    return {
        user_id: db.user_resolve(
            connection,
            user_id,
            externalize=True,
            return_false=False)
        for user_id in {
                item["author"] for item in obj
        }
    }



def validate(json, args):
    """
    Ensure the json object contains all the keys needed to satisfy
    its endpoint.
    """
    for arg in args:
        if arg not in json.keys():
            raise BBJParameterError(
                "Required parameter %s is "
                "absent from the request." % arg)


APICONFIG = {
    "/": {
        "tools.response_headers.on": True,
        "tools.response_headers.headers": [
            ("Content-Type", "application/json")
        ],
    }
}

class API(object):
    @bbjapi
    @cherrypy.expose
    def thread_index(self):
        threads = db.thread_index(cherrypy.thread_data.db)
        usermap = create_usermap(cherrypy.thread_data.db, threads)
        return schema.response({
            "data": threads,
            "usermap": usermap
        })


    @bbjapi
    @cherrypy.expose
    @cherrypy.tools.json_in()
    def thread_create(self):
        args = cherrypy.request.json
        validate(args, ["body", "title"])

        thread = db.thread_create(
            cherrypy.thread_data.db,
            cherrypy.thread_data.user["user_id"],
            args["body"], args["title"])

        usermap = {
            cherrypy.thread_data.user["user_id"]:
              cherrypy.thread_data.user
        }

        return schema.response({
            "data": thread,
            "usermap": usermap
        })


    @bbjapi
    @cherrypy.expose
    @cherrypy.tools.json_in()
    def thread_reply(self):
        args = cherrypy.request.json
        validate(args, ["thread_id", "body"])
        return schema.response({
            "data": db.thread_reply(
                cherrypy.thread_data.db,
                cherrypy.thread_data.user["user_id"],
                args["thread_id"], args["body"])
        })


    @bbjapi
    @cherrypy.expose
    @cherrypy.tools.json_in()
    def thread_load(self):
        args = cherrypy.request.json
        validate(args, ["thread_id"])

        thread = db.thread_get(
            cherrypy.thread_data.db,
            args["thread_id"])

        usermap = create_usermap(
            cherrypy.thread_data.db,
            thread["messages"])

        return schema.response({
            "data": thread,
            "usermap": usermap
        })


    @bbjapi
    @cherrypy.expose
    @cherrypy.tools.json_in()
    def user_register(self):
        args = cherrypy.request.json
        validate(args, ["user_name", "auth_hash"])
        return schema.response({
            "data": db.user_register(
                cherrypy.thread_data.db,
                args["user_name"], args["auth_hash"])
        })


    @bbjapi
    @cherrypy.expose
    @cherrypy.tools.json_in()
    def edit_query(self):
        args = cherrypy.request.json
        validate(args, ["thread_id", "post_id"])
        return schema.response({
            "data": message_edit_query(
                cherrypy.thread_data.db,
                cherrypy.thread_data.user["user_id"],
                args["thread_id"],
                args["post_id"])
        })



def run():
    cherrypy.quickstart(API(), "/api")


if __name__ == "__main__":
    print("wew")
Un proyecto texto-plano.xyz