2022-12-14 12:29:42 +00:00
|
|
|
|
|
|
|
import os from 'os';
|
|
|
|
import dir from 'path';
|
|
|
|
import fs from 'fs';
|
2022-12-22 21:49:44 +00:00
|
|
|
import ShowWhatsAppService from '../services/WhatsappService/ShowWhatsAppService';
|
2023-01-01 23:40:00 +00:00
|
|
|
// import { insertOrUpeateWhatsCache } from './WhatsCache';
|
2022-12-22 21:49:44 +00:00
|
|
|
import autoRestore from './AutoRestore';
|
|
|
|
import Whatsapp from '../models/Whatsapp';
|
|
|
|
import { getIO } from '../libs/socket';
|
2022-12-14 12:29:42 +00:00
|
|
|
|
|
|
|
export const setRestoreControll = (obj: object) => {
|
|
|
|
|
|
|
|
const restoreInfoFile = dir.join(os.tmpdir(), `restoreInfo.json`);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (fs.existsSync(restoreInfoFile)) {
|
|
|
|
|
|
|
|
if (Array.isArray(obj)) {
|
|
|
|
|
|
|
|
fs.writeFileSync(restoreInfoFile, JSON.stringify(obj), "utf8");
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
const restoreInfo = fs.readFileSync(restoreInfoFile, { encoding: 'utf8', flag: 'r' });
|
|
|
|
|
|
|
|
let lstRestore: any = JSON.parse(restoreInfo)
|
|
|
|
|
|
|
|
lstRestore.push(obj)
|
|
|
|
|
|
|
|
fs.writeFileSync(restoreInfoFile, JSON.stringify(lstRestore), "utf8");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
console.log('restoreInfo.json file not found! It will be created.');
|
|
|
|
|
|
|
|
if (Array.isArray(obj)) {
|
|
|
|
|
|
|
|
fs.writeFileSync(restoreInfoFile, JSON.stringify(obj), "utf8");
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
fs.writeFileSync(restoreInfoFile, JSON.stringify([obj]), "utf8");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
console.log('There was an error on try to read the restoreInfo.json file: ', error)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export const shifRestoreControll = () => {
|
|
|
|
|
|
|
|
const restoreInfoFile = dir.join(os.tmpdir(), `restoreInfo.json`);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (fs.existsSync(restoreInfoFile)) {
|
|
|
|
|
|
|
|
const restoreInfo = fs.readFileSync(restoreInfoFile, { encoding: 'utf8', flag: 'r' });
|
|
|
|
|
|
|
|
let lstRestore: any = JSON.parse(restoreInfo)
|
|
|
|
|
|
|
|
let whatsapp: any = lstRestore.shift()
|
|
|
|
|
|
|
|
fs.writeFileSync(restoreInfoFile, JSON.stringify(lstRestore), "utf8");
|
|
|
|
|
|
|
|
return whatsapp
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
console.log('There was an error on try to read the restoreInfo.json file: ', error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export const delRestoreControllFile = () => {
|
|
|
|
|
|
|
|
const restoreInfoFile = dir.join(os.tmpdir(), `restoreInfo.json`);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (fs.existsSync(restoreInfoFile)) {
|
|
|
|
|
|
|
|
fs.unlinkSync(restoreInfoFile)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
console.log('restoreInfo.json file not found!');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
console.log('There was an error on try delete the restoreInfo.json file: ', error)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const getRestoreControll = () => {
|
|
|
|
|
|
|
|
const restoreInfoFile = dir.join(os.tmpdir(), `restoreInfo.json`);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (fs.existsSync(restoreInfoFile)) {
|
|
|
|
|
|
|
|
const restoreInfo = fs.readFileSync(restoreInfoFile, { encoding: 'utf8', flag: 'r' });
|
|
|
|
|
|
|
|
let lstRestore: any = JSON.parse(restoreInfo)
|
|
|
|
|
|
|
|
return lstRestore
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
console.log('restoreInfo.json file not found!');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
console.log('There was an error on try to read the restoreInfo.json file: ', error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
2022-12-22 21:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const _restore = async (whatsapp: Whatsapp, msg_file_title: string) => {
|
|
|
|
|
2022-12-23 16:35:07 +00:00
|
|
|
if (whatsapp.status != 'RESTORING' && whatsapp.status != 'qrcode') {
|
2022-12-22 21:49:44 +00:00
|
|
|
|
|
|
|
console.log('THE WHATSAAP ID: ', whatsapp.id, ' WILL BE RESTORED SOON!')
|
|
|
|
|
2022-12-30 05:07:13 +00:00
|
|
|
await whatsapp.update({ status: "RESTORING", });
|
|
|
|
|
2022-12-22 21:49:44 +00:00
|
|
|
const io = getIO();
|
|
|
|
|
|
|
|
io.emit("whatsappSession", {
|
|
|
|
action: "update",
|
|
|
|
session: whatsapp
|
|
|
|
});
|
|
|
|
|
2023-01-01 23:40:00 +00:00
|
|
|
// await insertOrUpeateWhatsCache(`whatsapp:${whatsapp.id}`, { status: "RESTORING", })
|
2022-12-22 21:49:44 +00:00
|
|
|
|
|
|
|
setTimeout(async () => await autoRestore(whatsapp.id, msg_file_title), 95000);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|