-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sync online database of users (#171)
* add dep and update env vars * create turso db updating * sync db every 3 seconds * polish link commands * bump refresh interval to 6 seconds
- Loading branch information
Yoru
authored
Apr 13, 2024
1 parent
4f78f37
commit f7c60ed
Showing
9 changed files
with
74 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,7 @@ ACCESS_TOKEN= | |
CLIENT_ID= | ||
CLIENT_SECRET= | ||
CALLBACK_URL= | ||
TURSO_URL= | ||
TURSO_TOKEN= | ||
KEY= | ||
IV= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { getUsers, insertData } from "./database"; | ||
import { tursoClient as client } from "./initalize"; | ||
|
||
export async function syncUsersWithLocal(): Promise<void> { | ||
const usersOnline = (await getUsersOnline()).sort((a, b) => Number(a?.id) - Number(b?.id)); | ||
const usersLocalMap = new Map(getUsers().map((user) => [user?.id, user])); | ||
|
||
const max = Math.max(usersOnline.length, usersLocalMap.size); | ||
const maxWhich = usersOnline.length > usersLocalMap.size ? "online" : "local"; | ||
|
||
for (let i = 0; i < max; i++) { | ||
if (maxWhich === "local" && i >= usersLocalMap.size) continue; | ||
|
||
const online = usersOnline[i]; | ||
if (!online) continue; | ||
|
||
const localUser = usersLocalMap.get(online.id); | ||
if (!localUser) { | ||
insertData({ table: "users", id: online.id, data: [ { name: "banchoId", value: online.banchoId } ] }); | ||
console.log("Synced:", online); | ||
} | ||
} | ||
} | ||
|
||
export async function getUsersOnline(): Promise<Array<{ id: string, banchoId: number } | null>> { | ||
const { rows } = await client.execute("SELECT * FROM users;"); | ||
|
||
const response: Array<{ id: string, banchoId: number }> = []; | ||
for (let i = 0; i < rows.length; i++) { | ||
const row = rows[i]; | ||
response.push({ | ||
id: typeof row.id === "string" ? row.id.split(".")[0] : "", | ||
banchoId: typeof row.banchoId === "number" ? row.banchoId : -1 | ||
}); | ||
} | ||
|
||
return response; | ||
} | ||
|
||
export async function removeUserOnline(id: string | number): Promise<void> { | ||
await client.execute({ sql: "DELETE FROM users WHERE id = ?;", args: [id] }); | ||
} |