bbj/src/endpoints.py

82 lines
2.0 KiB
Python
Raw Normal View History

2017-03-03 01:10:16 +00:00
from src import formatting
2017-03-01 16:59:33 +00:00
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"],
2017-03-01 17:01:07 +00:00
"user_register": ["user", "auth_hash", "quip", "bio"],
2017-03-01 16:59:33 +00:00
"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):
2017-03-03 06:15:13 +00:00
return bool(db.USERDB["namemap"].get(json["target_user"]))
2017-03-01 16:59:33 +00:00
def check_auth(json):
2017-03-01 21:54:56 +00:00
return bool(db.user_auth(json["user"], json["auth_hash"]))
2017-03-01 16:59:33 +00:00
def user_register(json):
return schema.response(
db.user_register(
json["auth_hash"],
json["user"],
2017-03-01 17:01:07 +00:00
json["quip"],
2017-03-01 16:59:33 +00:00
json["bio"]))
def thread_index(json):
2017-03-03 01:10:16 +00:00
index = db.thread_index(markup=not json.get("nomarkup"))
return schema.response({"threads": index}, create_usermap(index, True))
2017-03-01 16:59:33 +00:00
def thread_load(json):
2017-03-03 01:10:16 +00:00
thread = db.thread_load(json["thread_id"], not json.get("nomarkup"))
2017-03-01 16:59:33 +00:00
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"])
2017-03-03 01:10:16 +00:00
if json.get("nomarkup"):
thread["body"] = formatting.cleanse(thread["body"])
return schema.response(thread)
2017-03-01 16:59:33 +00:00
def thread_reply(json):
2017-03-03 01:10:16 +00:00
reply = db.thread_reply(
2017-03-01 16:59:33 +00:00
json["thread_id"],
json["user"],
json["body"])
2017-03-03 01:10:16 +00:00
if json.get("nomarkup"):
reply["body"] = formatting.cleanse(reply["body"])
return schema.response(reply)