From 81b762fbc2b0a25de4388a35f5cb8e5c0b61d714 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Mon, 18 Jul 2022 02:46:46 +0200 Subject: [PATCH] add a discord server for api users to help eachother --- files/classes/clients.py | 2 +- files/classes/user.py | 1 + files/routes/discord.py | 10 ++++++++++ files/routes/oauth.py | 33 +++++++++++++++++---------------- files/templates/admin/app.html | 2 +- files/templates/admin/apps.html | 2 +- 6 files changed, 31 insertions(+), 19 deletions(-) diff --git a/files/classes/clients.py b/files/classes/clients.py index 59a5ba0c2..2a767d3fd 100644 --- a/files/classes/clients.py +++ b/files/classes/clients.py @@ -19,7 +19,7 @@ class OauthApp(Base): description = Column(String) author_id = Column(Integer, ForeignKey("users.id")) - author = relationship("User") + author = relationship("User", back_populates="apps") def __repr__(self): return f"" diff --git a/files/classes/user.py b/files/classes/user.py index c0c22d9f0..738255561 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -138,6 +138,7 @@ class User(Base): blocking = relationship("UserBlock", lazy="dynamic", primaryjoin="User.id==UserBlock.user_id", back_populates="user") blocked = relationship("UserBlock", lazy="dynamic", primaryjoin="User.id==UserBlock.target_id", back_populates="target") authorizations = relationship("ClientAuth", back_populates="user") + apps = relationship("OauthApp", back_populates="author") awards = relationship("AwardRelationship", primaryjoin="User.id==AwardRelationship.user_id", back_populates="user") referrals = relationship("User") diff --git a/files/routes/discord.py b/files/routes/discord.py index 379345a30..e8f8c67dd 100644 --- a/files/routes/discord.py +++ b/files/routes/discord.py @@ -88,6 +88,16 @@ def discord_redirect(v): v.discord_id=x["id"] g.db.add(v) + if v.apps: + url=f"https://discord.com/api/guilds/998387482984980541/members/{x['id']}" + name=v.username + data={ + "access_token":token, + "nick":name, + } + requests.put(url, headers=headers, json=data, timeout=5) + time.sleep(0.1) + url=f"https://discord.com/api/guilds/{DISCORD_SERVER_ID}/members/{x['id']}" name=v.username diff --git a/files/routes/oauth.py b/files/routes/oauth.py index e48a6ea42..fd517063e 100644 --- a/files/routes/oauth.py +++ b/files/routes/oauth.py @@ -141,26 +141,27 @@ def admin_app_approve(v, aid): app = g.db.get(OauthApp, aid) user = app.author - app.client_id = secrets.token_urlsafe(64)[:64] - g.db.add(app) + if not app.client_id: + app.client_id = secrets.token_urlsafe(64)[:64] + g.db.add(app) - access_token = secrets.token_urlsafe(128)[:128] - new_auth = ClientAuth( - oauth_client = app.id, - user_id = user.id, - access_token=access_token - ) + access_token = secrets.token_urlsafe(128)[:128] + new_auth = ClientAuth( + oauth_client = app.id, + user_id = user.id, + access_token=access_token + ) - g.db.add(new_auth) + g.db.add(new_auth) - send_repeatable_notification(user.id, f"@{v.username} has approved your application `{app.app_name}`. Here's your access token: `{access_token}`\nPlease check the guide [here](/api) if you don't know what to do next.") + send_repeatable_notification(user.id, f"@{v.username} has approved your application `{app.app_name}`. Here's your access token: `{access_token}`\nPlease check the guide [here](/api) if you don't know what to do next, and join this [discord server](/discord) if you need help!") - ma = ModAction( - kind="approve_app", - user_id=v.id, - target_user_id=user.id, - ) - g.db.add(ma) + ma = ModAction( + kind="approve_app", + user_id=v.id, + target_user_id=user.id, + ) + g.db.add(ma) return {"message": "Application approved"} diff --git a/files/templates/admin/app.html b/files/templates/admin/app.html index 46fd1d75f..c7586130a 100644 --- a/files/templates/admin/app.html +++ b/files/templates/admin/app.html @@ -34,7 +34,7 @@
{% if not app.client_id%} - Approve + Approve Reject {% else %} diff --git a/files/templates/admin/apps.html b/files/templates/admin/apps.html index 188a18f3a..a61e99855 100644 --- a/files/templates/admin/apps.html +++ b/files/templates/admin/apps.html @@ -39,7 +39,7 @@
{% if not app.client_id %} - Approve + Approve Reject {% else %}