Skip to content

Commit

Permalink
fix: Fixed WPP.chat.sendFileMessage for animated stickers (fix #432)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Nov 4, 2022
1 parent ee779e3 commit 6c69da7
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Expand Up @@ -2,7 +2,8 @@
"[javascript]": {
"editor.maxTokenizationLineLength": 2500,
"editor.bracketPairColorization.enabled": false,
"editor.guides.bracketPairs": false
"editor.guides.bracketPairs": false,
"editor.stickyScroll.enabled": false
},
"search.useIgnoreFiles": false,
"search.exclude": {
Expand Down
8 changes: 5 additions & 3 deletions src/chat/functions/downloadMedia.ts
Expand Up @@ -44,12 +44,14 @@ export async function downloadMedia(id: string): Promise<Blob> {

let blob: Blob | null = null;

if (msg.mediaData.mediaBlob) {
blob = msg.mediaData.mediaBlob.forceToBlob();
} else if (msg.mediaData.filehash) {
if (msg.mediaData.filehash) {
blob = MediaBlobCache.get(msg.mediaData.filehash);
}

if (!blob && msg.mediaData.mediaBlob) {
blob = msg.mediaData.mediaBlob.forceToBlob();
}

if (!blob && msg.mediaObject?.type === 'VIDEO') {
try {
msg.type = 'document';
Expand Down
24 changes: 22 additions & 2 deletions src/chat/functions/sendFileMessage.ts
Expand Up @@ -17,12 +17,16 @@
import Debug from 'debug';

import { assertFindChat, assertGetChat } from '../../assert';
import { getVideoInfoFromBuffer } from '../../util';
import { blobToArrayBuffer, getVideoInfoFromBuffer } from '../../util';
import { convertToFile } from '../../util/convertToFile';
import * as webpack from '../../webpack';
import { MediaPrep, MsgModel, OpaqueData } from '../../whatsapp';
import { wrapModuleFunction } from '../../whatsapp/exportModule';
import { generateVideoThumbsAndDuration } from '../../whatsapp/functions';
import {
generateVideoThumbsAndDuration,
isAnimatedWebp,
processRawSticker,
} from '../../whatsapp/functions';
import {
defaultSendMessageOptions,
RawMessage,
Expand Down Expand Up @@ -329,4 +333,20 @@ webpack.onReady(() => {
throw error;
}
});

wrapModuleFunction(processRawSticker, async (func, ...args) => {
const [data] = args;
const result = await func(...args);

if (data.type() === 'image/webp') {
const blob = data.forceToBlob();
const buffer = await blobToArrayBuffer(blob);

if (isAnimatedWebp(buffer)) {
result.mediaBlob = await OpaqueData.createFromData(blob, data.type());
}
}

return result;
});
});
27 changes: 27 additions & 0 deletions src/util/blobToArrayBuffer.ts
@@ -0,0 +1,27 @@
/*!
* Copyright 2021 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export function blobToArrayBuffer(blob: Blob): Promise<ArrayBuffer> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = function () {
resolve(reader.result as ArrayBuffer);
};
reader.onabort = reject;
reader.onerror = reject;
reader.readAsArrayBuffer(blob);
});
}
1 change: 1 addition & 0 deletions src/util/index.ts
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

export * from './blobToArrayBuffer';
export * from './blobToBase64';
export * from './convertToFile';
export * from './createWid';
Expand Down
2 changes: 2 additions & 0 deletions src/whatsapp/functions/index.ts
Expand Up @@ -36,11 +36,13 @@ export * from './getOrGenerate';
export * from './getSearchContext';
export * from './groupParticipants';
export * from './handleAck';
export * from './isAnimatedWebp';
export * from './isAuthenticated';
export * from './isRegistered';
export * from './markSeen';
export * from './mediaTypeFromProtobuf';
export * from './msgFindQuery';
export * from './processRawSticker';
export * from './products';
export * from './productVisibilitySet';
export * from './profilePic';
Expand Down
30 changes: 30 additions & 0 deletions src/whatsapp/functions/isAnimatedWebp.ts
@@ -0,0 +1,30 @@
/*!
* Copyright 2022 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { exportModule } from '../exportModule';

/**
* @whatsapp 176819 >= 2.2242.6
*/
export declare function isAnimatedWebp(data: ArrayBuffer): boolean;

exportModule(
exports,
{
isAnimatedWebp: 'isAnimatedWebp',
},
(m) => m.isAnimatedWebp
);
37 changes: 37 additions & 0 deletions src/whatsapp/functions/processRawSticker.ts
@@ -0,0 +1,37 @@
/*!
* Copyright 2022 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { exportModule } from '../exportModule';
import { OpaqueData } from '../misc';

/**
* @whatsapp 232294 >= 2.2242.6
*/
export declare function processRawSticker(data: OpaqueData): Promise<{
type: 'sticker';
mediaBlob: OpaqueData;
mimetype: string;
fullWidth: number;
fullHeight: number;
}>;

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

0 comments on commit 6c69da7

Please sign in to comment.