From e368d7201299b710ebccd53e83db4b5152a7c76e Mon Sep 17 00:00:00 2001 From: FusionSid Date: Wed, 27 Jul 2022 15:50:19 +1200 Subject: [PATCH] Fixed issues with building --- package.json | 4 ++- src/html/create_room.html | 59 ++++++++++++++++++++++++++++++++ src/html/login.html | 2 +- src/html/signed_in.html | 15 +++++++++ src/index.js | 71 +++++++++++++++++++++++++-------------- src/scripts/requests.js | 34 +++++++++++++++---- src/scripts/windows.js | 27 +++++++++++++-- 7 files changed, 176 insertions(+), 36 deletions(-) create mode 100644 src/html/create_room.html diff --git a/package.json b/package.json index 7ea9c66..eb83b1d 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,9 @@ "publish": "electron-forge publish", "lint": "echo \"No linting configured\"", "build-css": "tailwindcss build -i src/assets/tailwindcss/input.css -o src/assets/tailwindcss/styles.css --watch", - "build-osx": "electron-packager ./ --platform darwin --out ./dist --overwrite" + "build-osx": "electron-packager ./ untitled-chat-app --platform darwin --out ./dist/mac --overwrite", + "build-windows": "electron-packager ./ untitled-chat-app --overwrite --asar=true --platform=win32 --arch=ia32 --prune=true --out ./dist/windows --version-string.CompanyName=CE --version-string.FileDescription=CE --version-string.ProductName='Unititled Chat'", + "build-linux": "electron-packager ./ untitled-chat-app --overwrite --asar=true --platform=linux --arch=x64 --prune=true --out ./dist/linux" }, "keywords": [], "author": { diff --git a/src/html/create_room.html b/src/html/create_room.html new file mode 100644 index 0000000..3bfea3a --- /dev/null +++ b/src/html/create_room.html @@ -0,0 +1,59 @@ + + + + + + Login + + + + +
+
+

+

Create new room

+
+
+
+ +
+ +
+

+ + +
+
+ + + + + diff --git a/src/html/login.html b/src/html/login.html index 844712d..19776a7 100644 --- a/src/html/login.html +++ b/src/html/login.html @@ -64,4 +64,4 @@

Login to the app

- \ No newline at end of file + diff --git a/src/html/signed_in.html b/src/html/signed_in.html index aa1e2a6..3c2b28b 100644 --- a/src/html/signed_in.html +++ b/src/html/signed_in.html @@ -29,6 +29,12 @@

What would you like to do?

Sign Out + +
+ +
diff --git a/src/index.js b/src/index.js index 05fb812..9005637 100644 --- a/src/index.js +++ b/src/index.js @@ -17,7 +17,8 @@ const { signupUser, getUserWithToken, updateUserDetails, - getRoomById + getRoomById, + createNewRoom, } = require("./scripts/requests"); const { firstTimeUserPage, @@ -26,6 +27,7 @@ const { createSignupWindow, createRoomWindow, createEnterRoomIDWindow, + createNewRoomWindow, } = require("./scripts/windows"); const mainWindowMenuTemplate = require("./scripts/menu"); @@ -38,7 +40,7 @@ if (require("electron-squirrel-startup")) { require("dotenv").config(); /* Constants */ -const API_URL = process.env.API_URL; +const API_URL = "https://chatapi.fusionsid.xyz"; const mainMenu = Menu.buildFromTemplate(mainWindowMenuTemplate); Menu.setApplicationMenu(mainMenu); @@ -48,7 +50,13 @@ const store = new Store(); global.app = app; // Define for later -let firstTimeUserPageWindow, loginWindow, signupWindow, signedInWindow, enterRoomIDWindow, roomWindow; +let firstTimeUserPageWindow, + loginWindow, + signupWindow, + signedInWindow, + enterRoomIDWindow, + roomWindow, + newRoomWindow; /* Electron App Events: */ @@ -91,9 +99,14 @@ app.on("window-all-closed", () => { }); // If no windows are open launch one -app.on("activate", () => { +app.on("activate", async () => { if (BrowserWindow.getAllWindows().length === 0) { - firstTimeUserPageWindow = firstTimeUserPage(); + let isFirstTimeUser = await checkIfFirstTimeUser(); + if (isFirstTimeUser) { + signedInWindow = createSignedInWindow(); + } else { + firstTimeUserPageWindow = firstTimeUserPage(); + } } }); @@ -124,10 +137,7 @@ ipcMain.handle("new-login:login", async function (event, data) { store.set("private_key", privateKey); - let user = await getUserWithToken(); - let user_id = user.id; - - await updateUserDetails(user_id, "public_key", publicKey); + await updateUserDetails("public_key", publicKey); loginWindow.close(); loginWindow = null; @@ -163,27 +173,25 @@ ipcMain.on("sign-out:signed_in", async function (event) { ipcMain.on("join-room-window:signed_in", async function (event) { signedInWindow.close(); signedInWindow = null; - enterRoomIDWindow = createEnterRoomIDWindow() + enterRoomIDWindow = createEnterRoomIDWindow(); }); - ipcMain.handle("join-new-room:join_room", async function (event, room_id) { let room = null; try { - room = await getRoomById(room_id) + room = await getRoomById(room_id); } catch (e) { - console.log("Skill gap") + console.log("Skill gap"); } if (room === null || room === undefined) { - return "Room not found (lil' dif in skill level ngl)" + return "Room not found (lil' dif in skill level ngl)"; } - enterRoomIDWindow.close() + enterRoomIDWindow.close(); enterRoomIDWindow = null; - connect_to_room(room.room_id) + connect_to_room(room.room_id); }); - // The real stuff async function connect_to_room(room_id) { let access_token = await getAccessToken( @@ -191,17 +199,30 @@ async function connect_to_room(room_id) { store.get("password") ); - roomWindow = createRoomWindow() + roomWindow = createRoomWindow(); ipcMain.handle("gimme-connection:room", async function (event) { - let user = await getUserWithToken(access_token) - let room_data = await getRoomById(room_id) + let user = await getUserWithToken(access_token); + let room_data = await getRoomById(room_id); data = { user: user, access_token: access_token, room_data: room_data, - } - return data - }) - -} \ No newline at end of file + }; + return data; + }); +} + +ipcMain.on("create-room:signed_in", function (event) { + signedInWindow.close(); + signedInWindow = null; + newRoomWindow = createNewRoomWindow(); +}); + +ipcMain.handle("new-room-create:create_room", async function (event, data) { + let room_id; + console.log(data); + room = await createNewRoom(data.room_name, data.room_description); + room_id = room.room_id; + return `Room created successfully: ${room_id}`; +}); diff --git a/src/scripts/requests.js b/src/scripts/requests.js index 150d65b..ad030cd 100644 --- a/src/scripts/requests.js +++ b/src/scripts/requests.js @@ -5,7 +5,7 @@ const { URLSearchParams } = require("url"); const store = new Store(); require("dotenv").config(); -const API_URL = process.env.API_URL; +const API_URL = "https://chatapi.fusionsid.xyz"; async function getAccessToken(username = null, password = null, admin = false) { let token; @@ -38,8 +38,6 @@ async function getAccessToken(username = null, password = null, admin = false) { } async function signupUser(data) { - let access_token = await getAccessToken(null, null, (admin = true)); - let keys = new rsa().generateKeyPair(); let publicKey = keys.exportKey("public"); let privateKey = keys.exportKey("private"); @@ -51,7 +49,6 @@ async function signupUser(data) { method: "POST", headers: { "Content-Type": "application/json", - Authorization: `Bearer ${access_token}`, }, body: JSON.stringify({ username: data.username, @@ -86,10 +83,10 @@ async function getUserWithToken(token) { return data; } -async function updateUserDetails(user_id, attribute, new_value) { - let access_token = await getAccessToken(null, null, (admin = true)); +async function updateUserDetails(attribute, new_value) { + let access_token = await getAccessToken(store.get("password"), store.get("username")); - await fetch(`${API_URL}/api/users/${user_id}`, { + await fetch(`${API_URL}/api/user/me`, { method: "PATCH", headers: { "Content-Type": "application/json", @@ -108,6 +105,28 @@ async function updateUserDetails(user_id, attribute, new_value) { return data; } +async function createNewRoom(room_name, room_description=null) { + let access_token = await getAccessToken(store.get("username"), store.get("password")); + + await fetch(`${API_URL}/api/chatroom/new`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${access_token}`, + }, + body: JSON.stringify({ + room_name: room_name, + room_description: room_description, + }), + }) + .then((response) => response.json()) + .then((json_data) => { + data = json_data; + }); + + return data; +} + async function getRoomById(room_id) { let access_token = await getAccessToken( store.get("username"), @@ -134,6 +153,7 @@ module.exports = { signupUser, getUserWithToken, updateUserDetails, + createNewRoom, getRoomById, }; diff --git a/src/scripts/windows.js b/src/scripts/windows.js index 139dcea..b0399ca 100644 --- a/src/scripts/windows.js +++ b/src/scripts/windows.js @@ -8,7 +8,13 @@ Menu.setApplicationMenu(mainMenu); /* Window Creation */ // Define for later because JS IS WEIRD AF -let firstTimeWindow, loginWindow, signedInWindow, signupWindow, enterRoomIDWindow, roomWindow; +let firstTimeWindow, + loginWindow, + signedInWindow, + signupWindow, + enterRoomIDWindow, + roomWindow, + newRoomWindow; // Creates first time user page, Launched if first time opening function firstTimeUserPage() { @@ -27,6 +33,22 @@ function firstTimeUserPage() { return firstTimeWindow; } +function createNewRoomWindow() { + newRoomWindow = new BrowserWindow({ + webPreferences: { + contextIsolation: false, + nodeIntegration: true, + }, + width: 1300, + height: 760, + }); + + // load in index.html file + newRoomWindow.loadFile(path.join(__dirname, "../html/create_room.html")); + + return newRoomWindow; +} + function createLoginWindow() { loginWindow = new BrowserWindow({ webPreferences: { @@ -113,5 +135,6 @@ module.exports = { createSignedInWindow, createSignupWindow, createEnterRoomIDWindow, - createRoomWindow + createRoomWindow, + createNewRoomWindow };