diff --git a/centralUserBackend/src/databaseTables.ts b/centralUserBackend/src/databaseTables.ts index ce71f55..3b787e7 100644 --- a/centralUserBackend/src/databaseTables.ts +++ b/centralUserBackend/src/databaseTables.ts @@ -2,8 +2,7 @@ import sql from "./database.ts"; await sql`CREATE TABLE IF NOT EXISTS cub_user_sessions ( did TEXT, - intermediate_frontend_access_token TEXT NULL, - session_tokens TEXT[] DEFAULT '{}', + session_token TEXT, login_ips TEXT[] DEFAULT '{}', - login_timestamps TIMESTAMP[] DEFAULT '{}' + login_timestamps TIMESTAMP[] DEFAULT '{}' )`; diff --git a/frontend/src/routes/app/community.tsx b/frontend/src/routes/app/community.tsx index c9fa609..d21a886 100644 --- a/frontend/src/routes/app/community.tsx +++ b/frontend/src/routes/app/community.tsx @@ -1,36 +1,60 @@ import UKButton from "@ewsgit/uikit-solid/src/components/button/UKButton.jsx"; import { SocketMessageType } from "@harmonychat/generalcommunicationinstance/src/socket/types/socketMessageType"; -import { type Component, onMount, type ParentProps } from "solid-js"; +import { type Component, createSignal, For, onMount, type ParentProps, Show } from "solid-js"; const Community: Component = (props) => { - let socket: WebSocket; + const [socket, setSocket] = createSignal(undefined); + const [cids, setCids] = createSignal([]); onMount(() => { const gciWebsocket = new WebSocket("wss://127.0.0.1/gci/websocket"); - socket = gciWebsocket; gciWebsocket.addEventListener("open", () => { + setSocket(gciWebsocket); gciWebsocket.send(JSON.stringify({ type: SocketMessageType.ConnectionPing })); + gciWebsocket.send(JSON.stringify({ type: SocketMessageType.ListParticipatingCommunities })); }); // Listen for messages gciWebsocket.addEventListener("message", (event) => { - console.log("Message from server", JSON.parse(event.data)); + const parsedMessage = JSON.parse(event.data); + console.log("Message from server", parsedMessage); + + const messageType = parsedMessage.type as SocketMessageType; + const messageContent = parsedMessage.content; + + switch (messageType) { + case SocketMessageType.ListParticipatingCommunities: { + setCids(messageContent); + break; + } + case SocketMessageType.CreateCommunity: { + gciWebsocket.send(JSON.stringify({ type: SocketMessageType.ListParticipatingCommunities })); + break; + } + } }); }); return ( -
- Test CommunityLayout - {props.children} - { - socket.send(JSON.stringify({ type: SocketMessageType.CreateCommunity, content: { displayName: "Sample Community Name" } })); - }} - > - Create Community - -
+ +
+ Test CommunityLayout + {props.children} + { + socket()!.send(JSON.stringify({ type: SocketMessageType.CreateCommunity, content: { displayName: "Sample Community Name" } })); + }} + > + Create Community + + + {(cid) => { + return
{cid}
; + }} +
+
+
); }; diff --git a/frontend/uikit-solid b/frontend/uikit-solid deleted file mode 160000 index e7e5041..0000000 --- a/frontend/uikit-solid +++ /dev/null @@ -1 +0,0 @@ -Subproject commit e7e5041e376c737e386f86c8cc585f8e3ff805d0 diff --git a/generalCommunicationInstance/src/options.ts b/generalCommunicationInstance/src/options.ts new file mode 100644 index 0000000..4d05444 --- /dev/null +++ b/generalCommunicationInstance/src/options.ts @@ -0,0 +1 @@ +export const MAXIMUM_COMMUNITIES_PER_USER = 200; diff --git a/generalCommunicationInstance/src/socket/messages/create_community.ts b/generalCommunicationInstance/src/socket/messages/create_community.ts index 36b4e0e..3c4888b 100644 --- a/generalCommunicationInstance/src/socket/messages/create_community.ts +++ b/generalCommunicationInstance/src/socket/messages/create_community.ts @@ -1,5 +1,6 @@ import { z } from "zod"; import sql from "../../database.ts"; +import { MAXIMUM_COMMUNITIES_PER_USER } from "../../options.ts"; import RolePermissions from "../../types/rolePermissions.ts"; import createUniqueId from "../../uid.ts"; import createMessageHandler from "../createSocketMessageHandler.ts"; @@ -11,18 +12,23 @@ export default createMessageHandler({ async onMessage(content, socket) { console.log(content); + if ((await sql`SELECT cid FROM gci_community_members WHERE uid = ${socket.user.uid}`).length >= MAXIMUM_COMMUNITIES_PER_USER) { + socket.reply(SocketMessageType.Error, { message: "You are in too many communities (200)" }); + return; + } + const cid = createUniqueId(); const transactionSuccess = await sql.begin(async (s) => { await s`INSERT INTO gci_communities (cid, members, display_name, owner) VALUES (${cid}, ${[socket.user.uid]}, ${content.displayName}, ${socket.user.uid})`; + await s`UPDATE gci_users SET member_of_communities = array_append(member_of_communities, ${cid}) WHERE uid = ${socket.user.uid}`; const OWNER_ROLE_ID = 0; await s`INSERT INTO gci_community_members (cid, uid, roles) VALUES (${cid}, ${socket.user.uid}, ${[OWNER_ROLE_ID]})`; await s`INSERT INTO gci_community_roles (cid, role_id, display_name, apply_permissions, name_color) VALUES (${cid}, ${OWNER_ROLE_ID}, ${"Owner"}, ${[RolePermissions.ADMINISTRATOR]}, ${"#998844"})`; - await s`UPDATE gci_users SET member_of_communities = array_append(member_of_communities, ${cid}) WHERE uid = ${socket.user.uid}`; return true; }); - if (transactionSuccess) socket.reply(SocketMessageType.ConfirmCommunityCreated, { cid }); + if (transactionSuccess) socket.reply(SocketMessageType.CreateCommunity, { cid }); }, }); diff --git a/generalCommunicationInstance/src/socket/messages/list_participating_communities.ts b/generalCommunicationInstance/src/socket/messages/list_participating_communities.ts new file mode 100644 index 0000000..d14928b --- /dev/null +++ b/generalCommunicationInstance/src/socket/messages/list_participating_communities.ts @@ -0,0 +1,14 @@ +import { z } from "zod"; +import sql from "../../database.ts"; +import createMessageHandler from "../createSocketMessageHandler.ts"; +import { SocketMessageType } from "../types/socketMessageType.ts"; + +export default createMessageHandler({ + type: SocketMessageType.ListParticipatingCommunities, + schema: z.undefined(), + async onMessage(_, socket) { + const cids = (await sql`SELECT cid FROM gci_community_members WHERE uid = ${socket.user.uid}`).map((output) => output.cid); + + socket.reply(SocketMessageType.ListParticipatingCommunities, cids ?? []); + }, +}); diff --git a/generalCommunicationInstance/src/socket/types/socketMessageType.ts b/generalCommunicationInstance/src/socket/types/socketMessageType.ts index 52ae9fc..7573c17 100644 --- a/generalCommunicationInstance/src/socket/types/socketMessageType.ts +++ b/generalCommunicationInstance/src/socket/types/socketMessageType.ts @@ -4,6 +4,7 @@ export enum SocketMessageType { ConnectionPong = "connection_pong", JoinCommunity = "join_community", CreateCommunity = "create_community", - ConfirmCommunityCreated = "confirm_community_created", + ListParticipatingCommunities = "list_participating_communities", LeaveCommunity = "leave_community", + Error = "error", } diff --git a/generalCommunicationInstance/src/uid.ts b/generalCommunicationInstance/src/uid.ts index 72669e5..15a4339 100644 --- a/generalCommunicationInstance/src/uid.ts +++ b/generalCommunicationInstance/src/uid.ts @@ -3,5 +3,7 @@ let lastGeneratedIdSuffix = 0; export default function createUniqueId() { if (lastGeneratedIdSuffix > 9999) lastGeneratedIdSuffix = 0; - return Date.now().toString().padStart(16, "0") + lastGeneratedIdSuffix.toString().padStart(8, "0"); + return ( + Date.now().toString().padStart(16, "0") + (process.env.HARMONY_GCI_NODE_ID || "0").padStart(4, "0") + lastGeneratedIdSuffix.toString().padStart(8, "0") + ); }