Skip to content

Commit dc73fb3

Browse files
committed
2 parents b6ec733 + 9ba93d6 commit dc73fb3

7 files changed

Lines changed: 72 additions & 46 deletions

File tree

index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,13 +882,17 @@ declare namespace WAWebJS {
882882
AUTHENTICATED = 'authenticated',
883883
AUTHENTICATION_FAILURE = 'auth_failure',
884884
READY = 'ready',
885+
CHAT_REMOVED = 'chat_removed',
886+
CHAT_ARCHIVED = 'chat_archived',
885887
MESSAGE_RECEIVED = 'message',
886888
MESSAGE_CIPHERTEXT = 'message_ciphertext',
887889
MESSAGE_CREATE = 'message_create',
888890
MESSAGE_REVOKED_EVERYONE = 'message_revoke_everyone',
889891
MESSAGE_REVOKED_ME = 'message_revoke_me',
890892
MESSAGE_ACK = 'message_ack',
891893
MESSAGE_EDIT = 'message_edit',
894+
UNREAD_COUNT = 'unread_count',
895+
MESSAGE_REACTION = 'message_reaction',
892896
MEDIA_UPLOADED = 'media_uploaded',
893897
CONTACT_CHANGED = 'contact_changed',
894898
GROUP_JOIN = 'group_join',
@@ -900,6 +904,7 @@ declare namespace WAWebJS {
900904
CODE_RECEIVED = 'code',
901905
CODE_FAILED = 'code_failed',
902906
LOADING_SCREEN = 'loading_screen',
907+
CALL = 'call',
903908
DISCONNECTED = 'disconnected',
904909
STATE_CHANGED = 'change_state',
905910
BATTERY_CHANGED = 'change_battery',
@@ -1719,6 +1724,8 @@ declare namespace WAWebJS {
17191724
lastMessage: Message,
17201725
/** Indicates if the Chat is pinned */
17211726
pinned: boolean,
1727+
/** Indicates if the Chat is locked */
1728+
isLocked: boolean,
17221729

17231730
/** Archives this chat */
17241731
archive: () => Promise<void>,

src/Client.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,15 @@ class Client extends EventEmitter {
317317
await this.pupPage.evaluate(() => {
318318
window.AuthStore.AppState.on('change:state', (_AppState, state) => { window.onAuthAppStateChangedEvent(state); });
319319
window.AuthStore.AppState.on('change:hasSynced', () => { window.onAppStateHasSyncedEvent(); });
320-
window.AuthStore.Cmd.on('offline_progress_update', () => {
320+
window.AuthStore.Cmd.on('offline_progress_update_from_bridge', () => {
321321
window.onOfflineProgressUpdateEvent(window.AuthStore.OfflineMessageHandler.getOfflineDeliveryProgress());
322322
});
323323
window.AuthStore.Cmd.on('logout', async () => {
324324
await window.onLogoutEvent();
325325
});
326+
window.AuthStore.Cmd.on('logout_from_bridge', async () => {
327+
await window.onLogoutEvent();
328+
});
326329
});
327330
}
328331

@@ -1151,6 +1154,7 @@ class Client extends EventEmitter {
11511154
sendMediaAsDocument: options.sendMediaAsDocument,
11521155
sendMediaAsHd: options.sendMediaAsHd,
11531156
caption: options.caption,
1157+
isCaptionByUser: options.caption ? true : false,
11541158
quotedMessageId: options.quotedMessageId,
11551159
parseVCards: options.parseVCards !== false,
11561160
mentionedJidList: options.mentions || [],
@@ -2124,7 +2128,7 @@ class Client extends EventEmitter {
21242128
* @returns {Promise<boolean>} Returns true if the operation completed successfully, false otherwise
21252129
*/
21262130
async deleteChannel(channelId) {
2127-
return await this.client.pupPage.evaluate(async (channelId) => {
2131+
return await this.pupPage.evaluate(async (channelId) => {
21282132
const channel = await window.WWebJS.getChat(channelId, { getAsModel: false });
21292133
if (!channel) return false;
21302134
try {

src/authStrategies/RemoteAuth.js

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,28 @@ class RemoteAuth extends BaseAuthStrategy {
102102
}
103103

104104
async storeRemoteSession(options) {
105-
/* Compress & Store Session */
106105
const pathExists = await this.isValidPath(this.userDataDir);
107-
if (pathExists) {
108-
await this.compressSession();
109-
await this.store.save({session: path.join(this.dataPath, this.sessionName)});
110-
await fs.promises.unlink(path.join(this.dataPath, `${this.sessionName}.zip`));
111-
await fs.promises.rm(`${this.tempDir}`, {
112-
recursive: true,
113-
force: true,
114-
maxRetries: this.rmMaxRetries,
115-
}).catch(() => {});
106+
if (!pathExists) return;
107+
108+
let compressedSessionPath;
109+
try {
110+
compressedSessionPath = await this.compressSession();
111+
await this.store.save({ session: path.join(this.dataPath, this.sessionName) });
116112
if(options && options.emit) this.client.emit(Events.REMOTE_SESSION_SAVED);
113+
} finally {
114+
const paths = [
115+
this.tempDir,
116+
...(compressedSessionPath ? [compressedSessionPath] : [])
117+
];
118+
await Promise.allSettled(
119+
paths.map((p) =>
120+
fs.promises.rm(p, {
121+
recursive: true,
122+
force: true,
123+
maxRetries: this.rmMaxRetries,
124+
})
125+
)
126+
);
117127
}
118128
}
119129

@@ -142,20 +152,26 @@ class RemoteAuth extends BaseAuthStrategy {
142152
}
143153

144154
async compressSession() {
145-
const archive = archiver('zip');
146-
const stream = fs.createWriteStream(path.join(this.dataPath, `${this.sessionName}.zip`));
155+
const stageDefaultPath = path.join(this.tempDir, 'Default');
156+
const userDataDefaultPath = path.join(this.userDataDir, 'Default');
147157

148-
await fs.copy(this.userDataDir, this.tempDir).catch(() => {});
149-
await this.deleteMetadata();
150-
return new Promise((resolve, reject) => {
151-
archive
152-
.directory(this.tempDir, false)
153-
.on('error', err => reject(err))
154-
.pipe(stream);
158+
await fs.emptyDir(stageDefaultPath);
159+
await this.copyByRequiredDirs(userDataDefaultPath, stageDefaultPath);
155160

156-
stream.on('close', () => resolve());
161+
const archive = archiver('zip');
162+
const outPath = path.join(this.dataPath, `${this.sessionName}.zip`);
163+
const out = fs.createWriteStream(outPath);
164+
165+
await new Promise((resolve, reject) => {
166+
out.once('close', resolve);
167+
out.once('error', reject);
168+
archive.once('error', reject);
169+
170+
archive.pipe(out);
171+
archive.directory(this.tempDir, false);
157172
archive.finalize();
158173
});
174+
return outPath;
159175
}
160176

161177
async unCompressSession(compressedSessionPath) {
@@ -171,25 +187,16 @@ class RemoteAuth extends BaseAuthStrategy {
171187
await fs.promises.unlink(compressedSessionPath);
172188
}
173189

174-
async deleteMetadata() {
175-
const sessionDirs = [this.tempDir, path.join(this.tempDir, 'Default')];
176-
for (const dir of sessionDirs) {
177-
const sessionFiles = await fs.promises.readdir(dir);
178-
for (const element of sessionFiles) {
179-
if (!this.requiredDirs.includes(element)) {
180-
const dirElement = path.join(dir, element);
181-
const stats = await fs.promises.lstat(dirElement);
182-
183-
if (stats.isDirectory()) {
184-
await fs.promises.rm(dirElement, {
185-
recursive: true,
186-
force: true,
187-
maxRetries: this.rmMaxRetries,
188-
}).catch(() => {});
189-
} else {
190-
await fs.promises.unlink(dirElement).catch(() => {});
191-
}
192-
}
190+
async copyByRequiredDirs(from, to) {
191+
for (const d of this.requiredDirs) {
192+
const src = path.join(from, d);
193+
if (await this.isValidPath(src)) {
194+
const dest = path.join(to, path.basename(src));
195+
await fs.promises.cp(src, dest, {
196+
recursive: true,
197+
force: true,
198+
errorOnExist: false
199+
});
193200
}
194201
}
195202
}

src/structures/Chat.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ class Chat extends Base {
6363
*/
6464
this.pinned = !!data.pin;
6565

66+
/**
67+
* Indicates if the Chat is locked
68+
* @type {boolean}
69+
*/
70+
this.isLocked = data.isLocked;
71+
6672
/**
6773
* Indicates if the chat is muted or not
6874
* @type {boolean}

src/structures/GroupChat.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ class GroupChat extends Chat {
274274
async setDescription(description) {
275275
const success = await this.client.pupPage.evaluate(async (chatId, description) => {
276276
const chatWid = window.Store.WidFactory.createWid(chatId);
277-
let descId = window.Store.GroupMetadata.get(chatWid).descId;
277+
const chat = await window.WWebJS.getChat(chatId, { getAsModel: false });
278+
let descId = chat.groupMetadata.descId;
278279
let newId = await window.Store.MsgKey.newId();
279280
try {
280281
await window.Store.GroupUtils.setGroupDescription(chatWid, description, newId, descId);

src/util/Injected/Store.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ exports.ExposeStore = () => {
123123

124124
window.Store.NumberInfo = {
125125
...window.require('WAPhoneUtils'),
126-
...window.require('WAPhoneFindCC')
126+
...window.require('WAPhoneFindCC'),
127+
...window.require('WAWebPhoneUtils')
127128
};
128129
window.Store.ForwardUtils = {
129130
...window.require('WAWebChatForwardMessage')

src/util/Injected/Utils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,10 +573,10 @@ exports.LoadUtils = () => {
573573

574574
if (isChannel) {
575575
try {
576-
chat = window.Store.WAWebNewsletterMetadataCollection.get(chatId);
576+
chat = window.Store.WAWebNewsletterCollection.get(chatId);
577577
if (!chat) {
578578
await window.Store.ChannelUtils.loadNewsletterPreviewChat(chatId);
579-
chat = await window.Store.WAWebNewsletterMetadataCollection.find(chatWid);
579+
chat = await window.Store.WAWebNewsletterCollection.find(chatWid);
580580
}
581581
} catch (err) {
582582
chat = null;
@@ -639,7 +639,7 @@ exports.LoadUtils = () => {
639639
};
640640

641641
window.WWebJS.getChannels = async () => {
642-
const channels = window.Store.WAWebNewsletterMetadataCollection.getModelsArray();
642+
const channels = window.Store.WAWebNewsletterCollection.getModelsArray();
643643
const channelPromises = channels?.map((channel) => window.WWebJS.getChatModel(channel, { isChannel: true }));
644644
return await Promise.all(channelPromises);
645645
};

0 commit comments

Comments
 (0)