From 7e0077188788ec0ffdc129fde97c61c5d3166334 Mon Sep 17 00:00:00 2001 From: Adam Saudagar Date: Fri, 30 Oct 2020 03:34:43 +0530 Subject: [PATCH] discord login panel reworked for backend changes --- fishy/gui/config_top.py | 2 +- fishy/gui/discord_login.py | 26 ++++++++++++++------------ fishy/gui/main_gui.py | 7 ++++++- fishy/web/urls.py | 1 + fishy/web/web.py | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 14 deletions(-) diff --git a/fishy/gui/config_top.py b/fishy/gui/config_top.py index 27c81e4..baf8a93 100644 --- a/fishy/gui/config_top.py +++ b/fishy/gui/config_top.py @@ -61,7 +61,7 @@ def start_semifisher_config(gui: 'GUI'): gui._notify_check = Checkbutton(controls_frame, command=toggle_sub, variable=gui._notify) gui._notify_check.grid(row=0, column=1) gui._notify_check['state'] = DISABLED - is_subbed = web.is_subbed(config.get('uid'), lazy=False) + is_subbed = web.is_subbed(config.get('uid')) if is_subbed[1]: gui._notify_check['state'] = NORMAL gui._notify.set(is_subbed[0]) diff --git a/fishy/gui/discord_login.py b/fishy/gui/discord_login.py index 5be5525..6c59db3 100644 --- a/fishy/gui/discord_login.py +++ b/fishy/gui/discord_login.py @@ -3,9 +3,10 @@ from tkinter import * from tkinter import messagebox from tkinter.ttk import * -from fishy import web import typing +from fishy.web import web + from fishy.libs.tkhtmlview import HTMLLabel from ..helper.config import config @@ -15,25 +16,26 @@ if typing.TYPE_CHECKING: # noinspection PyProtectedMember def discord_login(gui: 'GUI'): - if web.is_subbed(config.get("uid"))[0]: - web.unsub(config.get("uid")) + if web.is_logged_in(config.get("uid")): + if web.logout(config.get("uid")): + gui.login.set(0) return # set notification checkbutton - gui._notify.set(0) + gui.login.set(0) def quit_top(): top.destroy() top_running[0] = False def check(): - if web.sub(config.get("uid"), discord_name.get()): - if web.is_subbed(config.get("uid"), False)[0]: - gui._notify.set(1) - messagebox.showinfo("Note!", "Notification configured successfully!") - quit_top() + code = int(login_code.get()) if login_code.get().isdigit() else 0 + if web.login(config.get("uid"), code): + gui.login.set(1) + messagebox.showinfo("Note!", "Logged in successfuly!") + quit_top() else: - messagebox.showerror("Error", "Subscription wasn't successful") + messagebox.showerror("Error", "Logged wasn't successful") top_running = [True] @@ -54,8 +56,8 @@ def discord_login(gui: 'GUI'): html_label.pack(pady=(20, 5)) html_label.fit_height() - discord_name = Entry(top, justify=CENTER, font="Calibri 15") - discord_name.pack(padx=(15, 15), expand=True, fill=BOTH) + login_code = Entry(top, justify=CENTER, font="Calibri 15") + login_code.pack(padx=(15, 15), expand=True, fill=BOTH) html_label = HTMLLabel(top, html=f'
' diff --git a/fishy/gui/main_gui.py b/fishy/gui/main_gui.py index 3f9adf5..1853915 100644 --- a/fishy/gui/main_gui.py +++ b/fishy/gui/main_gui.py @@ -3,6 +3,7 @@ import time from tkinter import * from tkinter.ttk import * +from fishy.web import web from ttkthemes import ThemedTk from fishy import helper @@ -39,7 +40,11 @@ def _create(gui: 'GUI'): filemenu = Menu(menubar, tearoff=0) - filemenu.add_checkbutton(label="Login", command=lambda: discord_login(gui), variable=gui.login) + login = web.is_logged_in(config.get('uid')) + gui.login = IntVar() + gui.login.set(1 if login > 0 else 0) + state = DISABLED if login == -1 else ACTIVE + filemenu.add_checkbutton(label="Login", command=lambda: discord_login(gui), variable=gui.login, state=state) filemenu.add_command(label="Create Shortcut", command=lambda: helper.create_shortcut(False)) # filemenu.add_command(label="Create Anti-Ghost Shortcut", command=lambda: helper.create_shortcut(True)) diff --git a/fishy/web/urls.py b/fishy/web/urls.py index 52f0f84..7779ccc 100644 --- a/fishy/web/urls.py +++ b/fishy/web/urls.py @@ -13,6 +13,7 @@ subscription = domain + "/api/notify_semifish" hole_depleted = domain + "/api/hole_depleted" session = domain + "/api/session" terms = domain + "/terms.html" +discord = domain + "/api/discord" def get_notification_page(uid): diff --git a/fishy/web/web.py b/fishy/web/web.py index 3d49a49..4f25b5d 100644 --- a/fishy/web/web.py +++ b/fishy/web/web.py @@ -10,6 +10,38 @@ from ..helper.config import config _session_id = None +@fallback(-1) +def is_logged_in(uid): + if uid is None: + return -1 + + body = {"uid": uid} + response = requests.get(urls.discord, params=body) + logged_in = response.json()["discord_login"] + return 1 if logged_in else 0 + + +@fallback(False) +def login(uid, login_code): + body = { + "uid": uid, + "login_code": login_code + } + reponse = requests.post(urls.discord, json=body) + result = reponse.json() + return result["success"] + + +@fallback(False) +def logout(uid): + body = { + "uid": uid, + } + reponse = requests.delete(urls.discord, json=body) + result = reponse.json() + return result["success"] + + @fallback(False) def register_user(uid): ip = get_ip(GoogleDnsProvider) @@ -62,6 +94,10 @@ def is_subbed(uid): body = {"uid": uid} response = requests.get(urls.subscription, params=body) + + if response.status_code != 200: + return False, False + is_subbed = response.json()["subbed"] return is_subbed, True