|
| 1 | +import { dbVars } from '../database'; |
| 2 | + |
| 3 | +import sqliteService from '../sqlite.js'; |
| 4 | + |
| 5 | +const mutualGraph = { |
| 6 | + async getMutualGraphSnapshot() { |
| 7 | + const snapshot = new Map(); |
| 8 | + if (!dbVars.userPrefix) { |
| 9 | + return snapshot; |
| 10 | + } |
| 11 | + const friendTable = `${dbVars.userPrefix}_mutual_graph_friends`; |
| 12 | + const linkTable = `${dbVars.userPrefix}_mutual_graph_links`; |
| 13 | + await sqliteService.execute((dbRow) => { |
| 14 | + const friendId = dbRow[0]; |
| 15 | + if (friendId && !snapshot.has(friendId)) { |
| 16 | + snapshot.set(friendId, []); |
| 17 | + } |
| 18 | + }, `SELECT friend_id FROM ${friendTable}`); |
| 19 | + await sqliteService.execute((dbRow) => { |
| 20 | + const friendId = dbRow[0]; |
| 21 | + const mutualId = dbRow[1]; |
| 22 | + if (!friendId || !mutualId) { |
| 23 | + return; |
| 24 | + } |
| 25 | + let list = snapshot.get(friendId); |
| 26 | + if (!list) { |
| 27 | + list = []; |
| 28 | + snapshot.set(friendId, list); |
| 29 | + } |
| 30 | + list.push(mutualId); |
| 31 | + }, `SELECT friend_id, mutual_id FROM ${linkTable}`); |
| 32 | + return snapshot; |
| 33 | + }, |
| 34 | + |
| 35 | + async saveMutualGraphSnapshot(entries) { |
| 36 | + if (!dbVars.userPrefix) { |
| 37 | + return; |
| 38 | + } |
| 39 | + const friendTable = `${dbVars.userPrefix}_mutual_graph_friends`; |
| 40 | + const linkTable = `${dbVars.userPrefix}_mutual_graph_links`; |
| 41 | + const pairs = entries instanceof Map ? entries : new Map(); |
| 42 | + await sqliteService.executeNonQuery('BEGIN'); |
| 43 | + try { |
| 44 | + await sqliteService.executeNonQuery(`DELETE FROM ${friendTable}`); |
| 45 | + await sqliteService.executeNonQuery(`DELETE FROM ${linkTable}`); |
| 46 | + if (pairs.size === 0) { |
| 47 | + await sqliteService.executeNonQuery('COMMIT'); |
| 48 | + return; |
| 49 | + } |
| 50 | + let friendValues = ''; |
| 51 | + let edgeValues = ''; |
| 52 | + pairs.forEach((mutualIds, friendId) => { |
| 53 | + if (!friendId) { |
| 54 | + return; |
| 55 | + } |
| 56 | + const safeFriendId = friendId.replace(/'/g, "''"); |
| 57 | + friendValues += `('${safeFriendId}'),`; |
| 58 | + let collection = []; |
| 59 | + if (Array.isArray(mutualIds)) { |
| 60 | + collection = mutualIds; |
| 61 | + } else if (mutualIds instanceof Set) { |
| 62 | + collection = Array.from(mutualIds); |
| 63 | + } |
| 64 | + for (const mutual of collection) { |
| 65 | + if (!mutual) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + const safeMutualId = String(mutual).replace(/'/g, "''"); |
| 69 | + edgeValues += `('${safeFriendId}', '${safeMutualId}'),`; |
| 70 | + } |
| 71 | + }); |
| 72 | + if (friendValues) { |
| 73 | + friendValues = friendValues.slice(0, -1); |
| 74 | + await sqliteService.executeNonQuery( |
| 75 | + `INSERT OR REPLACE INTO ${friendTable} (friend_id) VALUES ${friendValues}` |
| 76 | + ); |
| 77 | + } |
| 78 | + if (edgeValues) { |
| 79 | + edgeValues = edgeValues.slice(0, -1); |
| 80 | + await sqliteService.executeNonQuery( |
| 81 | + `INSERT OR REPLACE INTO ${linkTable} (friend_id, mutual_id) VALUES ${edgeValues}` |
| 82 | + ); |
| 83 | + } |
| 84 | + await sqliteService.executeNonQuery('COMMIT'); |
| 85 | + } catch (err) { |
| 86 | + await sqliteService.executeNonQuery('ROLLBACK'); |
| 87 | + throw err; |
| 88 | + } |
| 89 | + } |
| 90 | +}; |
| 91 | + |
| 92 | +export { mutualGraph }; |
0 commit comments