Skip to content

Commit

Permalink
fix: Removed fallback for WhatsApp WEB < 2.2307.10
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Feb 21, 2023
1 parent c273e3e commit 57f901a
Show file tree
Hide file tree
Showing 15 changed files with 14 additions and 467 deletions.
6 changes: 6 additions & 0 deletions src/chat/events/eventTypes.ts
Expand Up @@ -85,6 +85,9 @@ export interface ChatEventTypes {
degrees?: number;
shareDuration: number;
};
/**
* @deprecated Temporary unsuported by WhatsApp Web Multi-Device
*/
'chat.live_location_update': {
id: Wid;
lastUpdated: number;
Expand All @@ -96,6 +99,9 @@ export interface ChatEventTypes {
degrees?: number;
comment: string;
};
/**
* @deprecated Temporary unsuported by WhatsApp Web Multi-Device
*/
'chat.live_location_end': {
id: Wid;
chat: Wid;
Expand Down
87 changes: 1 addition & 86 deletions src/chat/events/registerLiveLocationUpdateEvent.ts
Expand Up @@ -14,65 +14,12 @@
* limitations under the License.
*/

import { config } from '../../config';
import { internalEv } from '../../eventEmitter';
import * as webpack from '../../webpack';
import {
ChatStore,
LiveLocationStore,
MsgModel,
MsgStore,
Wid,
} from '../../whatsapp';
import { unixTime } from '../../whatsapp/functions';
import { MsgModel, MsgStore } from '../../whatsapp';

webpack.onInjected(() => registerLiveLocationUpdateEvent());

interface LocationUpdate {
type: 'update';
accuracy?: number;
body: string;
degrees?: number;
elapsed: number;
jid: Wid;
lat: number;
lng: number;
speed?: number;
}

interface LocationDisable {
type: 'disable';
chat: Wid;
jid: Wid;
seq: number;
}

function processLocation(e: LocationUpdate | LocationDisable) {
if (e.type === 'update') {
internalEv.emit('chat.live_location_update', {
id: e.jid,
lastUpdated: unixTime() - e.elapsed,
elapsed: e.elapsed,
lat: e.lat,
lng: e.lng,
accuracy: e.accuracy,
speed: e.speed,
degrees: e.degrees,
comment: e.body,
});
return;
}

if (e.type === 'disable') {
internalEv.emit('chat.live_location_end', {
id: e.jid,
chat: e.chat,
seq: e.seq,
});
return;
}
}

function registerLiveLocationUpdateEvent() {
/**
* For each new live detection, fecth initial data and start update handler
Expand All @@ -94,38 +41,6 @@ function registerLiveLocationUpdateEvent() {
degrees: msg.degrees,
shareDuration: msg.shareDuration!,
});

LiveLocationStore.update(msg.chat!.id)
.then((liveLocation) => {
liveLocation.startViewingMap();
})
.catch(() => null);
});
});

/**
* Start for all active chats
*/
internalEv.once('conn.main_ready', () => {
const chats = ChatStore.getModelsArray().slice(0, config.liveLocationLimit);
chats.forEach((chat) => {
LiveLocationStore.update(chat.id)
.then((liveLocation) => {
liveLocation.startViewingMap();
})
.catch(() => null);
});
});

/**
* Wrap LiveLocationStore.handle to get all LiveLocation events
* By default, only "loaded" messages process this events
*/
const originalHandle = LiveLocationStore.handle;
LiveLocationStore.handle = (updateList: any[]) => {
for (const update of updateList) {
processLocation(update);
}
return originalHandle.call(originalHandle, updateList);
};
}
4 changes: 2 additions & 2 deletions src/chat/functions/editMessage.ts
Expand Up @@ -16,7 +16,7 @@

import { WPPError } from '../../util';
import { MsgKey } from '../../whatsapp';
import { canEditMessage } from '../../whatsapp/functions';
import { canEditMsg } from '../../whatsapp/functions';
import {
defaultSendMessageOptions,
LinkPreviewOptions,
Expand Down Expand Up @@ -51,7 +51,7 @@ export async function editMessage(

const msg = await getMessageById(msgId);

const canEdit = canEditMessage(msg);
const canEdit = canEditMsg(msg);
if (!canEdit) {
throw new WPPError(`edit_message_error`, `Cannot edit this message`);
}
Expand Down
61 changes: 0 additions & 61 deletions src/whatsapp/collections/LiveLocationCollection.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/whatsapp/collections/index.ts
Expand Up @@ -34,7 +34,6 @@ export * from './EmojiVariantCollection';
export * from './GroupMetadataCollection';
export * from './LabelCollection';
export * from './LabelItemCollection';
export * from './LiveLocationCollection';
export * from './MsgCollection';
export * from './MsgInfoCollection';
export * from './MsgInfoParticipantCollection';
Expand Down
Expand Up @@ -14,30 +14,18 @@
* limitations under the License.
*/

import * as webpack from '../../webpack';
import { exportModule } from '../exportModule';
import { MsgModel } from '../models';

/**
* @whatsapp 591988 >= 2.2244.5
* @whatsapp 755707 >= 2.2307.10
*/
export declare function canEditMessage(msg: MsgModel): boolean;
export declare function canEditMsg(msg: MsgModel): boolean;

exportModule(
exports,
{
canEditMessage: 'canEditMessage',
canEditMsg: 'canEditMsg',
},
(m) => m.canEditMessage
(m) => m.canEditMsg
);

webpack.injectFallbackModule('canEditMessage', {
canEditMessage: (msg: MsgModel) => {
if (!msg.isSentByMe) return false;
if (msg.type !== 'chat') return false;
if (msg.isForwarded) return false;
if ('out' !== msg.self) return false;
if (new Date().getTime() / 1e3 > msg.t! + 900) return false;
return true;
},
});
7 changes: 0 additions & 7 deletions src/whatsapp/functions/canReplyMsg.ts
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import * as webpack from '../../webpack';
import { exportModule } from '../exportModule';
import { MsgModel } from '../models';

Expand All @@ -30,9 +29,3 @@ exportModule(
},
(m) => m.canReplyMsg
);

webpack.injectFallbackModule('canReplyMsg', {
canReplyMsg: (msg: MsgModel) => {
return msg.canReply?.() || false;
},
});
10 changes: 0 additions & 10 deletions src/whatsapp/functions/getQuotedMsgObj.ts
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import * as webpack from '../../webpack';
import { exportModule } from '../exportModule';
import { MsgModel } from '../models';

Expand All @@ -30,12 +29,3 @@ exportModule(
},
(m) => m.getQuotedMsgObj
);

/**
* @whatsapp < 2.2245.8
*/
webpack.injectFallbackModule('getQuotedMsgObj', {
getQuotedMsgObj: (msg: MsgModel) => {
return msg.quotedMsgObj?.() || null;
},
});
13 changes: 0 additions & 13 deletions src/whatsapp/functions/getSearchContext.ts
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import * as webpack from '../../webpack';
import { exportModule } from '../exportModule';
import { MsgKey, MsgLoad } from '../misc';
import { ChatModel, MsgModel } from '../models';
Expand Down Expand Up @@ -42,15 +41,3 @@ exportModule(
},
(m) => m.getSearchContext
);

webpack.injectFallbackModule('getSearchContext', {
getSearchContext: (
chat: ChatModel,
msg: MsgModel | MsgKey,
options?: {
isQuotedMsgAvailable: boolean;
}
) => {
return chat.getSearchContext?.(msg, options);
},
});
3 changes: 1 addition & 2 deletions src/whatsapp/functions/index.ts
Expand Up @@ -17,7 +17,7 @@
export * from './addAndSendMsgToChat';
export * from './blockContact';
export * from './calculateFilehashFromBlob';
export * from './canEditMessage';
export * from './canEditMsg';
export * from './canReplyMsg';
export * from './collections';
export * from './createGroup';
Expand Down Expand Up @@ -61,7 +61,6 @@ export * from './queryGroupInviteCode';
export * from './randomHex';
export * from './randomId';
export * from './resetGroupInviteCode';
export * from './sendCallSignalingMsg';
export * from './sendClear';
export * from './sendCreateCommunity';
export * from './sendCreateGroup';
Expand Down
59 changes: 0 additions & 59 deletions src/whatsapp/functions/sendCallSignalingMsg.ts

This file was deleted.

0 comments on commit 57f901a

Please sign in to comment.