Skip to content

Commit 35f62b7

Browse files
authored
Merge pull request #128 from venomlib/fix-lid-migration
Fix lid migration
2 parents 866288f + 37876cc commit 35f62b7

11 files changed

Lines changed: 84 additions & 31 deletions

File tree

src/lib/wapi/business/send-message-with-buttons.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { createWidWrapper } from '../helper';
2+
13
window.WAPI.sendButtons = async function (chatId) {
24
var chat = Store.Chat.get(chatId);
35
var tempMsg = Object.create(chat.msgs.filter((msg) => msg.__x_isSentByMe)[0]);
@@ -103,12 +105,12 @@ window.WAPI.sendButtons = async function (chatId) {
103105
.then((t) => chat.msgs.add(t))
104106
.catch((e) => console.log(e))
105107
)
106-
.then((t) => {
108+
.then(async (t) => {
107109
var e = t[0];
108110
const s = Store.Base2;
109111
if (!s.BinaryProtocol)
110112
window.Store.Base2.BinaryProtocol = new window.Store.bp(11);
111-
var idUser = new Store.WidFactory.createWid(chatId);
113+
var idUser = await createWidWrapper(chatId);
112114
var k = Store.createMessageKey({
113115
...e,
114116
to: idUser,

src/lib/wapi/functions/get-chat.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
1+
import { createWidWrapper } from '../helper';
2+
13
export async function getChat(id) {
24
if (!id) {
35
return false;
46
}
57
id = typeof id == 'string' ? id : id._serialized;
6-
let found = Store.Chat.get(id);
8+
9+
let gate = window
10+
.require('WAWebLid1X1MigrationGating')
11+
.Lid1X1MigrationUtils.isLidMigrated();
12+
let found = false;
13+
if (gate) {
14+
console.info('Lid migration found');
15+
let chatWid = await createWidWrapper(id);
16+
found = await window.Store.FindOrCreateChat.findOrCreateLatestChat(chatWid).chat;
17+
} else {
18+
console.info('Lid migration not found');
19+
found = Store.Chat.get(id);
20+
}
21+
722
if (!found) {
23+
console.info('Validating Wid');
824
if (Store.CheckWid.validateWid(id)) {
925
const ConstructChat = new window.Store.UserConstructor(id, {
1026
intentionallyUsePrivateConstructor: !0
1127
});
1228
const chatWid = new Store.WidFactory.createWid(id);
29+
console.log('Adding chat');
1330
await Store.Chat.add(
1431
{
1532
createdLocally: true,
@@ -21,8 +38,7 @@ export async function getChat(id) {
2138
);
2239
found = Store.Chat.find(ConstructChat) || false;
2340
}
24-
}
25-
if (found) {
41+
} else {
2642
found.sendMessage = found.sendMessage
2743
? found.sendMessage
2844
: function () {

src/lib/wapi/functions/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ export { sendMessage } from './send-message';
5454
export { sendMessageOptions } from './sendMessageOptions';
5555
export { sendMessageWithTags } from './send-message-with-tags';
5656
export { sendMessageWithThumb } from './send-message-with-thumb';
57-
export { sendMessage2 } from './send-message2';
5857
export { sendSticker } from './send-sticker';
5958
export { sendVideoAsGif } from './send-video-as-gif';
6059
export { setMyName } from './set-my-name';

src/lib/wapi/functions/send-image-with-product.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { processFiles } from './process-files';
2-
import { base64ToFile } from '../helper';
2+
import { base64ToFile, createWidWrapper } from '../helper';
33

44
/**
55
* Sends product with product image to given chat id
@@ -38,7 +38,7 @@ export function sendImageWithProduct(
3838
caption
3939
};
4040

41-
const idUser = new Store.WidFactory.createWid(chatid);
41+
const idUser = await createWidWrapper(chatid);
4242
await Store.Chat.add(
4343
{
4444
createdLocally: true,

src/lib/wapi/functions/send-message.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { createWidWrapper } from '../helper/index.js';
2+
13
export async function sendMessage(
24
to,
35
content,
@@ -27,6 +29,7 @@ export async function sendMessage(
2729
return WAPI.scope(to, true, 404, 'It is necessary to number');
2830
}
2931

32+
createWidWrapper(to);
3033
const chat = checkNumber
3134
? await WAPI.sendExist(to)
3235
: await WAPI.returnChat(to);

src/lib/wapi/functions/send-message2.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/lib/wapi/functions/simulate-typing.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { createWidWrapper } from '../helper/index.js';
12
export async function startTyping(chatId) {
2-
const chat = window.Store.WidFactory.createWid(chatId);
3+
console.log(`startTyping: chatId ${chatId}`);
4+
const chat = createWidWrapper(chatId);
35
if (!chat) {
46
throw {
57
error: true,
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
export async function createWidWrapper(chatId) {
2+
let gate = window
3+
.require('WAWebLid1X1MigrationGating')
4+
.Lid1X1MigrationUtils.isLidMigrated();
5+
6+
let chatWid = window.Store.WidFactory.createWid(chatId);
7+
if (gate) {
8+
const isChannel = /@\w*newsletter\b/.test(chatId);
9+
10+
let chat;
11+
12+
if (isChannel) {
13+
try {
14+
chat = window.Store.NewsletterCollection.get(chatId);
15+
if (!chat) {
16+
await window.Store.ChannelUtils.loadNewsletterPreviewChat(chatId);
17+
chat = await window.Store.NewsletterCollection.find(chatWid);
18+
}
19+
} catch (err) {
20+
chat = null;
21+
}
22+
} else {
23+
chat = await window.Store.FindOrCreateChat.findOrCreateLatestChat(chatWid)
24+
.then((chat) => chat.chat)
25+
.catch(async (_) => {
26+
let actions;
27+
actions = [
28+
{
29+
type: 'add',
30+
phoneNumber: chatWid.user
31+
}
32+
];
33+
let query = window
34+
.require('WAWebContactSyncUtils')
35+
.constructUsyncDeltaQuery(actions);
36+
let result = await query.execute();
37+
chatWid = window.Store.WidFactory.createWid(result.list[0].lid);
38+
});
39+
}
40+
}
41+
return chatWid;
42+
}

src/lib/wapi/helper/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export { sleep } from './sleep';
77
export { injectConfig } from './inject-config';
88
export { filterObjects } from './filter-object';
99
export { filterModule } from './filter-module';
10+
export { createWidWrapper } from './fix-lid-migration';

src/lib/wapi/store/store-objects.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,16 @@ export const storeObjects = [
218218
conditions: (module) =>
219219
module.storeMessages && module.appendMessage ? module : null,
220220
},
221+
{
222+
id: 'ChannelUtils',
223+
conditions: (module) =>
224+
module.loadNewsletterPreviewChat ? module : null,
225+
},
226+
{
227+
id: 'FindOrCreateChat',
228+
conditions: (module) =>
229+
module.findOrCreateLatestChat ? module : null,
230+
},
221231
{
222232
id: 'createMessageKey',
223233
conditions: (module) =>

0 commit comments

Comments
 (0)