forked from EvolutionAPI/evolution-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-multi-file-auth-state-prisma.ts
More file actions
213 lines (188 loc) · 5.68 KB
/
use-multi-file-auth-state-prisma.ts
File metadata and controls
213 lines (188 loc) · 5.68 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { prismaRepository } from '@api/server.module';
import { CacheService } from '@api/services/cache.service';
import { CacheConf, configService } from '@config/env.config';
import { Logger } from '@config/logger.config';
import { INSTANCE_DIR } from '@config/path.config';
import { AuthenticationState, BufferJSON, initAuthCreds, WAProto as proto } from 'baileys';
import fs from 'fs/promises';
import path from 'path';
const fixFileName = (file: string): string | undefined => {
if (!file) {
return undefined;
}
const replacedSlash = file.replace(/\//g, '__');
const replacedColon = replacedSlash.replace(/:/g, '-');
return replacedColon;
};
export async function keyExists(sessionId: string): Promise<any> {
try {
const key = await prismaRepository.session.findUnique({ where: { sessionId: sessionId } });
return !!key;
} catch {
return false;
}
}
export async function saveKey(sessionId: string, keyJson: any): Promise<any> {
const exists = await keyExists(sessionId);
try {
if (!exists)
return await prismaRepository.session.create({
data: {
sessionId: sessionId,
creds: JSON.stringify(keyJson),
},
});
await prismaRepository.session.update({
where: { sessionId: sessionId },
data: { creds: JSON.stringify(keyJson) },
});
} catch {
return null;
}
}
export async function getAuthKey(sessionId: string): Promise<any> {
try {
const register = await keyExists(sessionId);
if (!register) return null;
const auth = await prismaRepository.session.findUnique({ where: { sessionId: sessionId } });
return JSON.parse(auth?.creds);
} catch {
return null;
}
}
async function deleteAuthKey(sessionId: string): Promise<any> {
try {
const register = await keyExists(sessionId);
if (!register) return;
await prismaRepository.session.delete({ where: { sessionId: sessionId } });
} catch {
return;
}
}
async function fileExists(file: string): Promise<any> {
try {
const stat = await fs.stat(file);
if (stat.isFile()) return true;
} catch {
return;
}
}
const logger = new Logger('useMultiFileAuthStatePrisma');
export default async function useMultiFileAuthStatePrisma(
sessionId: string,
cache: CacheService,
): Promise<{
state: AuthenticationState;
saveCreds: () => Promise<void>;
removeCreds: () => Promise<void>;
}> {
const localFolder = path.join(INSTANCE_DIR, sessionId);
const localFile = (key: string) => path.join(localFolder, fixFileName(key) + '.json');
await fs.mkdir(localFolder, { recursive: true });
async function writeData(data: any, key: string): Promise<any> {
const dataString = JSON.stringify(data, BufferJSON.replacer);
const cacheConfig = configService.get<CacheConf>('CACHE');
if (key != 'creds') {
if (cacheConfig.REDIS.ENABLED) {
return await cache.hSet(sessionId, key, data);
} else {
await fs.writeFile(localFile(key), dataString);
return;
}
}
await saveKey(sessionId, dataString);
return;
}
async function readData(key: string): Promise<any> {
try {
let rawData;
const cacheConfig = configService.get<CacheConf>('CACHE');
if (key != 'creds') {
if (cacheConfig.REDIS.ENABLED) {
return await cache.hGet(sessionId, key);
} else {
if (!(await fileExists(localFile(key)))) return null;
rawData = await fs.readFile(localFile(key), { encoding: 'utf-8' });
return JSON.parse(rawData, BufferJSON.reviver);
}
} else {
rawData = await getAuthKey(sessionId);
}
const parsedData = JSON.parse(rawData, BufferJSON.reviver);
return parsedData;
} catch {
return null;
}
}
async function removeData(key: string): Promise<any> {
try {
const cacheConfig = configService.get<CacheConf>('CACHE');
if (key != 'creds') {
if (cacheConfig.REDIS.ENABLED) {
return await cache.hDelete(sessionId, key);
} else {
await fs.unlink(localFile(key));
}
} else {
await deleteAuthKey(sessionId);
}
} catch {
return;
}
}
async function removeCreds(): Promise<any> {
const cacheConfig = configService.get<CacheConf>('CACHE');
// Redis
try {
if (cacheConfig.REDIS.ENABLED) {
await cache.delete(sessionId);
logger.info({ action: 'redis.delete', sessionId });
return;
}
} catch (err) {
logger.warn({ action: 'redis.delete', sessionId, err });
}
logger.info({ action: 'auth.key.delete', sessionId });
await deleteAuthKey(sessionId);
}
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.create(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');
},
removeCreds,
};
}