-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.py
238 lines (185 loc) · 7.8 KB
/
index.py
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import json
import time
import re
import markdown
import asyncio
import markupsafe
from datetime import datetime, UTC
from quart import Quart, render_template, request, jsonify, redirect
from utils import sqlite, tickets, jinja_filters
from utils.md_extensions import URLifyExtension, DiscordEmojiExtension, SmallLineExtension
# RegEx patterns
re_mentions = re.compile(r"(<(@|#|@&)(\d+)>)")
# Quart itself
app = Quart(__name__)
app.config["JSON_SORT_KEYS"] = False
# Create a loop (Python 3.10)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# General configs
db = sqlite.Database()
db.create_tables() # Attempt to create table(s) if not exists already.
with open("config.json", "r") as f:
config = json.load(f)
md = markdown.Markdown(
extensions=[
DiscordEmojiExtension(),
"meta", "extra", "toc", "sane_lists",
URLifyExtension(),
SmallLineExtension()
]
)
# Jinja2 template filters
app.jinja_env.filters["markdown"] = lambda text: markupsafe.Markup(md.convert(text))
app.jinja_env.filters["detect_file"] = lambda file: jinja_filters.detect_file(file)
# Database cleaning task
async def background_task():
""" Delete old ticket entries for privacy reasons """
while True:
db.execute("DELETE FROM tickets WHERE ? > expire", (int(time.time()),))
await asyncio.sleep(5)
@app.before_serving
async def startup():
app.background_task = asyncio.ensure_future(background_task())
@app.after_serving
async def shutdown():
app.background_task.cancel()
def jsonify_standard(name: str, description: str, code: int = 200):
""" The standard JSON output to my API """
return jsonify({
"code": code, "name": name, "description": description
}), code
@app.route("/")
async def index():
return await render_template("index.html", config=config)
@app.route("/<ticket_id>")
async def show_ticket(ticket_id):
ticket_db = tickets.Ticket(db=db)
data = ticket_db.fetch_ticket(ticket_id)
if not data:
return {"status": 404, "code": ticket_id}
if str(data["submitted_by"]) == str(config["bot_id"]):
valid_source = tickets.TicketSource.valid
else:
valid_source = tickets.TicketSource.unknown
get_logs = json.loads(data["logs"])
# To prevent XSS, fucking hell it's shit, but I'll find a better solution later...
converted_logs = []
for msg in get_logs["messages"]:
temp_holder = []
for content in msg["content"]:
converted_msg = None
if content["msg"]:
converted_msg = content["msg"]
_mentions = {
"users": content.get("mentions", {}).get("users", {}),
"roles": content.get("mentions", {}).get("roles", {}),
"channels": content.get("mentions", {}).get("channels", {})
}
if converted_msg:
for g in re_mentions.findall(converted_msg):
if g[1] == "@":
converted_msg = converted_msg.replace(g[0], f"@{_mentions['users'][g[2]]}")
elif g[1] == "#":
converted_msg = converted_msg.replace(g[0], f"#{_mentions['channels'][g[2]]}")
elif g[1] == "@&":
converted_msg = converted_msg.replace(g[0], f"@{_mentions['roles'][g[2]]}")
temp_holder.append({
"id": content["id"],
"msg": converted_msg,
"attachments": content.get("attachments", []),
"reply": content.get("reply", None),
"stickers": content.get("stickers", []),
"edited": content.get("edited", False),
"deleted": content.get("deleted", False)
})
converted_logs.append({
"author": msg["author"],
"timestamp": datetime.fromtimestamp(msg["timestamp"], UTC).strftime("%Y-%m-%d %H:%M:%S (UTC)"),
"content": temp_holder
})
get_logs["messages"] = converted_logs
messages_map = {}
for i, entry in enumerate(get_logs["messages"], start=1):
for ii, msg_entry in enumerate(entry["content"], start=1):
messages_map[msg_entry["id"]] = msg_entry
messages_map[msg_entry["id"]]["href_id"] = f"message-{i}-{ii}"
messages_map[msg_entry["id"]]["author"] = entry["author"]
if msg_entry["msg"] is not None:
msg_entry["msg"] = msg_entry["msg"].replace("\n", " ")
messages_map[msg_entry["id"]]["msg_shoten"] = (
msg_entry["msg"]
if len(msg_entry["msg"] or "...") < 32
else msg_entry["msg"][:32].strip() + "..."
)
def reference_message(msg_id):
return messages_map.get(msg_id, None)
def get_author(user_id: int):
if str(user_id) not in get_logs["users"]:
return {
"avatar": "/static/images/default.png",
"username": "Unknown#0000",
}
return get_logs["users"][str(user_id)]
return await render_template(
"ticket.html",
status=200, title=f"#{get_logs['channel_name']} | xelA Tickets", submitted_by=data["submitted_by"],
ticket_id=data["ticket_id"], guild_id=data["guild_id"], author_id=str(data["author_id"]),
created_at=data["created_at"], confirmed_by=str(data["confirmed_by"]),
expires=data["expire"], context=data["context"], official_bot=config["bot_id"],
channel_name=get_logs["channel_name"], valid_source=valid_source, logs=get_logs,
reference_message=reference_message, str=str, get_author=get_author
)
@app.route("/<ticket_id>/download")
async def download_ticket(ticket_id):
ticket_db = tickets.Ticket(db=db)
data = ticket_db.fetch_ticket(ticket_id)
if not data:
return jsonify_standard("Not found", f"Ticket {ticket_id} not found", 404)
return jsonify(json.loads(data["logs"]))
@app.route("/submit/example")
async def submit_example():
with open("examples/submit.json", "r") as f:
data = json.load(f)
return jsonify(data)
@app.route("/submit", methods=["POST"])
async def submit():
token = request.headers.get("Authorization") or None
uploaded_file = False
files = await request.files
if files:
uploaded_file = True
file = files.get("ticket_file") or None
if not file:
return jsonify_standard("Missing file", "Missing uploaded file...", 400)
if file.content_type != "application/json":
return jsonify_standard("Invalid file", "Invalid file type, only JSON is allowed...", 400)
file_body = file.stream.read().decode("utf-8")
try:
post_data = json.loads(file_body)
except Exception as e:
return jsonify_standard("Broken", str(e), 400)
else:
post_data = await request.json
if not post_data:
return jsonify_standard("Missing data", "Missing JSON/data...", 400)
if "submitted_by" not in post_data:
return jsonify_standard("Missing data", "Missing 'submitted_by' in JSON", 400)
if post_data["submitted_by"] == config["bot_id"]:
if not token:
post_data["submitted_by"] = "86477779717066752" # If a user is uploading the JSON file without changing submitted_by
if token and token != config["token"]:
return jsonify_standard("Invalid token", "Invalid Authorization token...", 403)
make_ticket = tickets.Ticket(payload=post_data, db=db)
code, data = make_ticket.attempt_post()
if code != 200:
return jsonify_standard(f"Error: {data.message}", data.validator, code)
if uploaded_file:
return redirect(f"/{data}")
else:
return jsonify_standard("Success", data, code)
if __name__ == "__main__":
app.run(
port=config.get("port", 8080),
debug=config.get("debug", False)
)