Skip to content

Commit cae4a97

Browse files
committed
Save room users and avatars to encrypted db
1 parent 8992a00 commit cae4a97

File tree

5 files changed

+103
-42
lines changed

5 files changed

+103
-42
lines changed

src/backend/account.cjs

+10-20
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const file = join(userDataDir, 'misc.db')
77
const adapter = new JSONFile(file)
88
const db = new Low(adapter)
99
const dbPath = userDataDir + '/SQLmessages.db'
10-
const { getGroups, loadBlockList, loadKeys, loadDB } = require('./database.cjs')
10+
const { getGroups, loadBlockList, loadKeys, loadDB, saveRoomUser, loadRoomUsers } = require('./database.cjs')
1111
const fs = require('fs')
1212

1313
const Store = require('electron-store')
@@ -23,26 +23,16 @@ ipcMain.on('set-avatar', (e, data) => {
2323
})
2424

2525

26-
ipcMain.on('save-avatar', (e, data) => {
27-
// let list = store.get('avatars') ?? []
28-
// if (list.some(a => a.address === data.address)) {
29-
// const update = list.filter(a => a.address !== data.address)
30-
// list = update
31-
// }
32-
// list.push({avatar: data.avatar.toString('base64'), address: data.address})
33-
// store.set({
34-
// avatars: list
35-
// })
26+
ipcMain.on('save-room-user', (e, data) => {
27+
saveRoomUser(data)
3628
})
3729

38-
function get_room_avatars() {
39-
const list = store.get('avatars') ?? []
40-
const avatars = []
41-
// for (const a of list) {
42-
// const item = {avatar: Buffer.from(a.avatar, 'base64'), address: a.address}
43-
// avatars.push(item)
44-
// }
45-
return avatars
30+
ipcMain.handle('get-room-users', async (e, key) => {
31+
return await get_room_users(key)
32+
})
33+
34+
async function get_room_users(key) {
35+
return await loadRoomUsers(key)
4636
};
4737

4838
ipcMain.handle('get-avatar', () => {
@@ -143,7 +133,7 @@ class Account {
143133
const banned = store.get('banned') ?? []
144134
const usersBanned = store.get('bannedUsers') ?? []
145135
const files = store.get('files') ?? []
146-
const avatars = get_room_avatars()
136+
const avatars = []
147137

148138
this.sender('wallet-started', [
149139
this.node,

src/backend/database.cjs

+59-13
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const createTables = () => {
7474
blockListTable()
7575
groupChannelsMessagesTable()
7676
roomKeysTable()
77+
roomUserTable()
7778
}
7879

7980
const welcomeAddress =
@@ -285,30 +286,75 @@ const roomKeysTable = () => {
285286
})
286287
}
287288

289+
const roomUserTable = () => {
290+
291+
const roomUsers = `CREATE TABLE IF NOT EXISTS roomusers (
292+
name TEXT NOT NULL,
293+
address TEXT NOT NULL,
294+
room TEXT NOT NULL,
295+
avatar TEXT,
296+
lastseen INT DEFAULT 0,
297+
PRIMARY KEY (address, room)
298+
)`;
299+
300+
return new Promise(
301+
(resolve, reject) => {
302+
database.prepare(roomUsers).run()
303+
},
304+
() => {
305+
resolve()
306+
})
307+
308+
}
309+
288310

289311

290312
//DATABASE REQUESTS
291313

292314
const loadGroups = () => {
293315
const rows = []
294-
const getAllGroups = `SELECT * FROM pgroups`
295-
const groups = database.prepare(getAllGroups).all()
316+
const getAllGroups = `SELECT * FROM pgroups`
317+
const groups = database.prepare(getAllGroups).all()
296318

297-
for(const group of groups) {
298-
rows.push(group)
299-
}
300-
return rows
319+
for(const group of groups) {
320+
rows.push(group)
321+
}
322+
return rows
301323
}
302324

303325
const loadRooms = () => {
304326
const rows = []
305-
const getAllRooms = `SELECT * FROM rooms`
306-
const rooms = database.prepare(getAllRooms).all()
327+
const getAllRooms = `SELECT * FROM rooms`
328+
const rooms = database.prepare(getAllRooms).all()
307329

308-
for(const room of rooms) {
309-
rows.push(room)
310-
}
311-
return rows
330+
for(const room of rooms) {
331+
rows.push(room)
332+
}
333+
return rows
334+
}
335+
336+
const loadRoomUsers = async (key) => {
337+
const rows = []
338+
const getAllUsers = `
339+
SELECT *
340+
FROM roomusers
341+
WHERE room = ?
342+
`
343+
const users = database.prepare(getAllUsers)
344+
for(const user of users.iterate(key)) {
345+
rows.push(user)
346+
}
347+
return rows
348+
}
349+
350+
const saveRoomUser = (user) => {
351+
console.log("Save this user", user.name)
352+
try {
353+
database.prepare('REPLACE INTO roomusers (name, address, room, avatar, lastseen) VALUES (?, ?, ?, ?, ?)')
354+
.run(user.name, user.address, user.room,user.avatar.toString('base64'), Date.now())
355+
}catch (e) {
356+
console.log("Error saving user:", e)
357+
}
312358
}
313359

314360
const loadRoomKeys = () => {
@@ -937,4 +983,4 @@ process.on('SIGINT', async () => process.exit(128 + 2));
937983
process.on('SIGTERM', async () => process.exit(128 + 15));
938984

939985

940-
module.exports = {saveHash, roomMessageExists, getLatestRoomHashes, loadRoomKeys, removeRoom, getRooms ,addRoomKeys, firstContact, welcomeMessage, loadDB, loadGroups, loadRooms, loadKeys, getGroups, saveGroupMsg, unBlockContact, blockContact, removeMessages, removeContact, removeGroup, addGroup, loadBlockList, getConversation, getConversations, loadKnownTxs, getMessages, getGroupReply, printGroup, saveMsg, saveThisContact, groupMessageExists, messageExists, getContacts, getChannels, deleteMessage, addRoom}
986+
module.exports = {loadRoomUsers, saveRoomUser, saveHash, roomMessageExists, getLatestRoomHashes, loadRoomKeys, removeRoom, getRooms ,addRoomKeys, firstContact, welcomeMessage, loadDB, loadGroups, loadRooms, loadKeys, getGroups, saveGroupMsg, unBlockContact, blockContact, removeMessages, removeContact, removeGroup, addGroup, loadBlockList, getConversation, getConversations, loadKnownTxs, getMessages, getGroupReply, printGroup, saveMsg, saveThisContact, groupMessageExists, messageExists, getContacts, getChannels, deleteMessage, addRoom}

src/backend/preload.cjs

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ const WINDOW_API = {
5353
getRooms: async () => {
5454
return await ipcRenderer.invoke('get-rooms')
5555
},
56+
getRoomUsers: async (key) => {
57+
return await ipcRenderer.invoke('get-room-users', key)
58+
},
5659
printRoom: async (room, page) => {
5760
return await ipcRenderer.invoke('print-group', room, page)
5861
},

src/lib/stores/storeFunctions.svelte

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script>
22
import {boards, groups, notify, user, webRTC, messageWallet, beam, misc, swarm, rooms} from '$lib/stores/user.js'
3-
import { roomMessages } from './roommsgs'
3+
import { roomMessages } from './roommsgs'
44
55
window.api.receive('peer-disconnected', async (data) => {
66
peer_disconnected(data)
@@ -64,18 +64,24 @@ import { roomMessages } from './roommsgs'
6464
add_user(data, joined)
6565
}
6666
67-
const make_avatar = async (data, address) => {
67+
const make_avatar = async (data, address, key, name) => {
6868
if (!data || data.length === 0) return false
6969
const blob = new Blob( [ data ]);
7070
const avatar = URL.createObjectURL( blob );
7171
const user = {avatar, address}
72+
73+
//Replace with updated avatar.
74+
if ($rooms.avatars.some(a => a.address === address)) {
75+
$rooms.avatars = $rooms.avatars.filter(a => a.address !== address)
76+
}
77+
7278
$rooms.avatars.push(user)
7379
$rooms.avatars = $rooms.avatars
74-
window.api.send('save-avatar', {address, avatar: data})
80+
window.api.send('save-room-user', {address, avatar: data, room: key, name})
7581
}
7682
7783
async function add_user(data, joined) {
78-
const avatar = await make_avatar(data.avatar, data.address)
84+
const avatar = await make_avatar(data.avatar, data.address, joined.key, data.name)
7985
const user = {
8086
message: "Joined the lobby",
8187
grp: joined.key,

src/routes/rooms/components/RoomList.svelte

+21-5
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,30 @@ const printRoom = async (room) => {
8989
readMessage(room)
9090
}
9191
92-
//Function to filer array of active users on board.
93-
function filterActiveHugins() {
94-
let uniq = {}
95-
const arr = $roomMessages
96-
$rooms.activeHugins = arr.filter((obj) => !uniq[obj.address] && (uniq[obj.address] = true))
92+
//Function to get all users in a room.
93+
async function filterActiveHugins() {
94+
const users = await window.api.getRoomUsers($swarm.activeSwarm.key)
95+
console.log("getRoomUsers", users)
96+
const all = []
97+
for (const u of users) {
98+
const user = {address: u.address, room: u.room, name: u.name}
99+
make_avatar(u.avatar, u.address)
100+
all.push(user)
101+
}
102+
$rooms.activeHugins = all
97103
104+
98105
}
99106
107+
const make_avatar = (data, address) => {
108+
if (!data || data.length === 0) return
109+
if ($rooms.avatar.some(a => a.address === address)) return
110+
const blob = new Blob( [ data ]);
111+
const avatar = URL.createObjectURL( blob );
112+
const usr = {avatar, address}
113+
$rooms.avatars.push(usr)
114+
$rooms.avatars = $rooms.avatars
115+
}
100116
//Print our conversations from DBs
101117
async function printRooms() {
102118
roomList = await window.api.getRooms()

0 commit comments

Comments
 (0)