-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
274 lines (195 loc) · 6.37 KB
/
main.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import json
import time
from datetime import datetime
import jwt
import uvicorn
from fastapi import Depends, FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from motor.motor_asyncio import AsyncIOMotorClient
from oauthlib.oauth2.rfc6749.errors import MissingTokenError
from redis import asyncio as aioredis
from starlette_discord import DiscordOAuthClient
from config import (
ALGORITHM,
CLIENT_ID,
CLIENT_SECRET,
DATABASE_NAME,
DATABASE_URI,
DBL_TOKEN,
KOFI_TOKEN,
REDIRECT_URL,
REDIS_URL,
SECRET,
)
app = FastAPI()
discord = DiscordOAuthClient(
CLIENT_ID, CLIENT_SECRET, REDIRECT_URL, ("identify", "email")
)
# Middleware
app.add_middleware(
CORSMiddleware,
allow_origins=[REDIRECT_URL],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Databases
client = AsyncIOMotorClient(DATABASE_URI)
db = client[DATABASE_NAME]
redis = None
def verify_dbl_auth(req: Request):
token = req.headers["Authorization"]
if token != DBL_TOKEN:
raise HTTPException(401)
return req
async def verify_kofi_auth(req: Request):
form_data = await req.form()
data = get_data_from_form(form_data)
if data is None:
raise HTTPException(401)
if data.get("verification_token", None) != KOFI_TOKEN:
raise HTTPException(401)
return req
def get_data_from_form(form_data):
data = str(form_data.get("data", None))
return json.loads(data)
def decode_token(token: str) -> dict:
try:
payload = jwt.decode(token, SECRET, algorithms=[ALGORITHM])
except jwt.InvalidTokenError:
raise HTTPException(401)
return payload
@app.on_event("startup")
async def startup_event():
global redis # thomas doesn't like globals :(
redis = await aioredis.from_url(REDIS_URL, socket_timeout=10, max_connections=2)
@app.on_event("shutdown")
async def shutdown_event():
await redis.close()
@app.get("/")
def main():
return "Thomas is ready!"
@app.post("/premium")
async def premium(request: Request = Depends(verify_kofi_auth)):
data = get_data_from_form(await request.form())
if data["type"] != "Subscription":
raise HTTPException(401)
expire_time = int(time.time() + 60 * 60 * 24 * 32) # giving an extra day
await db.subscriptions.insert_one(
{
"_id": data["message_id"],
"email": data["email"],
"name": data["from_name"],
"tier": data["tier_name"],
"amount": float(data["amount"]),
"first_time": data["is_first_subscription_payment"],
"activated_by": None,
"expired": False,
"expire_time": expire_time,
}
)
return "Thomas is very happy!"
@app.post("/vote")
async def vote(request: Request = Depends(verify_dbl_auth)):
data = await request.json()
# Getting the user id from the request
if "id" in data:
user_id = data["id"]
site = "dbls"
elif "user" in data:
user_id = data["user"]
if "guild" in data:
site = "topgg-server"
else:
site = "topgg"
else:
raise HTTPException(400)
user_id = int(user_id)
user = await db.users.find_one({"_id": user_id})
if user is None:
raise HTTPException(400)
votes = user["votes"] + 1
now = datetime.utcnow()
await db.users.update_one(
{"_id": user["_id"]},
{"$set": {"votes": votes, f"last_voted.{site}": now}, "$inc": {"xp": 750}},
)
await redis.hdel("user", user["_id"])
return "Thomas is happy!"
@app.get("/login")
async def start_login():
return discord.redirect()
@app.get("/token")
async def callback(code: str):
try:
user = await discord.login(code)
except MissingTokenError:
raise HTTPException(500)
payload = {"id": user.id, "email": user.email}
jwt_token = jwt.encode(payload, SECRET, algorithm=ALGORITHM)
return JSONResponse({"token": jwt_token})
@app.get("/user")
async def get_user(token: str):
payload = decode_token(token)
user = await db.users.find_one({"_id": payload["id"]})
if user is None:
raise HTTPException(404)
user_data = {
"id": str(user["_id"]),
"name": user["name"],
"discriminator": user["discriminator"],
"avatar": user["avatar"],
}
return JSONResponse(user_data)
@app.get("/subs")
async def get_subs(token: str):
payload = decode_token(token)
subs = db.subscriptions.find({"email": payload["email"]})
unordered_subs = [sub async for sub in subs]
# Ordering subscriptions by expiry date and whether or not they are claimed
active_subs = []
inactive_subs = []
# Diving the subscriptions into active and unclaimed
for sub in unordered_subs:
sub["expired"] = sub["expired"] or sub["expire_time"] <= time.time()
if sub["activated_by"] is None and not sub["expired"]:
active_subs.append(sub)
else:
inactive_subs.append(sub)
# Sorting the subscriptions by expiry date
active_subs.sort(key=lambda sub: sub["expire_time"], reverse=True)
inactive_subs.sort(key=lambda sub: sub["expire_time"], reverse=True)
ordered_subs = active_subs + inactive_subs
return JSONResponse(ordered_subs)
@app.post("/activate/{sub_id}")
async def activate_sub(sub_id: str, token: str):
payload = decode_token(token)
sub = await db.subscriptions.find_one({"_id": sub_id, "email": payload["email"]})
if sub is None:
raise HTTPException(404)
if sub["activated_by"] is not None:
raise HTTPException(400)
if sub["expire_time"] < time.time():
raise HTTPException(400)
# Updating the user's subscription
await db.users.update_one(
{"_id": payload["id"]},
{
"$set": {
"premium": {
"expire_date": datetime.utcfromtimestamp(sub["expire_time"]),
"name": sub["tier"],
}
}
},
)
# Removing the user from the cache
await redis.hdel("user", payload["id"])
# Updating the subscription
await db.subscriptions.update_one(
{"_id": sub["_id"]}, {"$set": {"activated_by": payload["id"]}}
)
return "Thomas is very happy!"
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8080)