-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathusePrismaDBAuthStore.js
More file actions
162 lines (137 loc) · 4.55 KB
/
Copy pathusePrismaDBAuthStore.js
File metadata and controls
162 lines (137 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import fs from 'fs/promises'
import path from 'path'
import { WAProto as proto, initAuthCreds, BufferJSON } from "@whiskeysockets/baileys"
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
const fixFileName = (file) => {
if (!file) {
return undefined;
}
const replacedSlash = file.replace(/\//g, '__');
const replacedColon = replacedSlash.replace(/:/g, '-');
return replacedColon;
};
export async function keyExists(sessionID) {
try {
let key = await prisma.session.findUnique({ where: { sessionID: sessionID } })
return !!key
} catch (error) {
console.log(`${error}`)
return false
}
}
export async function saveKey(sessionID, keyJson) {
const jaExiste = await keyExists(sessionID)
try {
if (!jaExiste) return await prisma.session.create({ data: { sessionID: sessionID, creds: JSON.stringify(keyJson) } });
await prisma.session.update({ where: { sessionID: sessionID }, data: { creds: JSON.stringify(keyJson) } })
} catch (error) {
console.log(`${error}`)
return null
}
}
export async function getAuthKey(sessionID) {
try {
let registro = await keyExists(sessionID)
if (!registro) return null
let auth = await prisma.session.findUnique({ where: { sessionID: sessionID } })
return JSON.parse(auth?.creds)
} catch (error) {
console.log(`${error}`)
return null
}
}
async function deleteAuthKey(sessionID) {
try {
let registro = await keyExists(sessionID)
if (!registro) return;
await prisma.session.delete({ where: { sessionID: sessionID } })
} catch (error) {
console.log('2', `${error}`)
}
}
async function fileExists(file) {
try {
const stat = await fs.stat(file);
if (stat.isFile()) return true
} catch (error) {
return;
}
}
export default async function usePrismaDBAuthStore(sessionID) {
const localFolder = path.join(process.cwd(), 'sessions', sessionID)
const localFile = (key) => path.join(localFolder, (fixFileName(key) + '.json'))
await fs.mkdir(localFolder, { recursive: true });
async function writeData(data, key) {
const dataString = JSON.stringify(data, BufferJSON.replacer);
if (key != 'creds') {
await fs.writeFile(localFile(key), dataString)
return;
}
await saveKey(sessionID, dataString)
return;
};
async function readData(key) {
try {
let rawData;
if (key != 'creds') {
if (!(await fileExists(localFile(key)))) return null;
rawData = await fs.readFile(localFile(key), { encoding: 'utf-8' })
} else {
rawData = (await getAuthKey(sessionID))
}
const parsedData = JSON.parse(rawData, BufferJSON.reviver);
return parsedData;
} catch (error) {
return null;
}
}
async function removeData(key) {
try {
if (key != 'creds') {
await fs.unlink(localFile(key))
} else {
await deleteAuthKey(sessionID)
}
} catch (error) {
return;
}
}
let creds = await readData('creds');
if (!creds) {
creds = initAuthCreds();
await writeData(creds, 'creds');
}
return {
state: {
creds,
keys: {
get: async (type, ids) => {
const data = {};
await Promise.all(ids.map(async (id) => {
let value = await readData(`${type}-${id}`);
if (type === 'app-state-sync-key' && value) {
value = proto.Message.AppStateSyncKeyData.fromObject(value);
}
data[id] = value;
}));
return data;
},
set: async (data) => {
const tasks = [];
for (const category in data) {
for (const id in data[category]) {
const value = data[category][id];
const key = `${category}-${id}`;
tasks.push(value ? writeData(value, key) : removeData(key));
}
}
await Promise.all(tasks);
}
}
},
saveCreds: () => {
return writeData(creds, 'creds');
}
};
}