From b0bab717f9370a0be9767c6500df697cce083415 Mon Sep 17 00:00:00 2001 From: Cleiton Carvalho Date: Sun, 8 Oct 2023 16:43:26 -0300 Subject: [PATCH] feat: Added newsletter functions (close #1365) (#1386) Co-authored-by: Edgard --- package.json | 2 +- src/assert/assertChat.ts | 9 +- src/chat/functions/get.ts | 10 +- src/chat/functions/sendFileMessage.ts | 51 +++----- src/chat/functions/sendRawMessage.ts | 35 +++++- src/index.ts | 1 + src/newsletter/functions/create.ts | 71 +++++++++++ src/newsletter/functions/destroy.ts | 41 +++++++ src/newsletter/functions/edit.ts | 112 ++++++++++++++++++ src/newsletter/functions/ensureNewsletter.ts | 31 +++++ src/newsletter/functions/index.ts | 20 ++++ src/newsletter/functions/mute.ts | 49 ++++++++ src/newsletter/index.ts | 17 +++ .../functions/createNewsletterQuery.ts | 34 ++++++ src/whatsapp/functions/deleteNewsletter.ts | 30 +++++ .../functions/editNewsletterMetadataAction.ts | 43 +++++++ src/whatsapp/functions/getAsMms.ts | 9 -- src/whatsapp/functions/index.ts | 10 ++ src/whatsapp/functions/msgDataFromMsgModel.ts | 30 +++++ src/whatsapp/functions/muteNewsletter.ts | 32 +++++ .../functions/queryNewsletterMetadataByJid.ts | 46 +++++++ .../functions/sendNewsletterMessageJob.ts | 36 ++++++ src/whatsapp/functions/unmuteNewsletter.ts | 32 +++++ .../functions/updateNewsletterMsgRecord.ts | 32 +++++ src/whatsapp/functions/uploadMedia.ts | 30 +++++ src/whatsapp/models/ChatModel.ts | 1 + src/whatsapp/models/MsgModel.ts | 1 + src/whatsapp/stores.ts | 15 ++- 28 files changed, 776 insertions(+), 54 deletions(-) create mode 100644 src/newsletter/functions/create.ts create mode 100644 src/newsletter/functions/destroy.ts create mode 100644 src/newsletter/functions/edit.ts create mode 100644 src/newsletter/functions/ensureNewsletter.ts create mode 100644 src/newsletter/functions/index.ts create mode 100644 src/newsletter/functions/mute.ts create mode 100644 src/newsletter/index.ts create mode 100644 src/whatsapp/functions/createNewsletterQuery.ts create mode 100644 src/whatsapp/functions/deleteNewsletter.ts create mode 100644 src/whatsapp/functions/editNewsletterMetadataAction.ts create mode 100644 src/whatsapp/functions/msgDataFromMsgModel.ts create mode 100644 src/whatsapp/functions/muteNewsletter.ts create mode 100644 src/whatsapp/functions/queryNewsletterMetadataByJid.ts create mode 100644 src/whatsapp/functions/sendNewsletterMessageJob.ts create mode 100644 src/whatsapp/functions/unmuteNewsletter.ts create mode 100644 src/whatsapp/functions/updateNewsletterMsgRecord.ts create mode 100644 src/whatsapp/functions/uploadMedia.ts diff --git a/package.json b/package.json index df59590d07..c50ddd070f 100644 --- a/package.json +++ b/package.json @@ -86,6 +86,6 @@ "webpack-cli": "^5.1.4" }, "engines": { - "whatsapp-web": ">=2.2245.8-beta" + "whatsapp-web": ">=2.2321.6-beta" } } diff --git a/src/assert/assertChat.ts b/src/assert/assertChat.ts index 2c2c36cff7..eeb0126e34 100644 --- a/src/assert/assertChat.ts +++ b/src/assert/assertChat.ts @@ -16,7 +16,7 @@ import { chat as Chat } from '../'; import { WPPError } from '../util'; -import { ChatModel, Wid } from '../whatsapp'; +import { ChatModel, NewsletterStore, Wid } from '../whatsapp'; export class InvalidChat extends WPPError { constructor(readonly id: string | { _serialized: string }) { @@ -35,7 +35,12 @@ export async function assertFindChat(id: string | Wid): Promise { } export function assertGetChat(id: string | Wid): ChatModel { - const chat = (Chat as any).get(id); + let chat = null; + if (id.toString().includes('newsletter')) { + chat = NewsletterStore.get(id); + } else { + chat = (Chat as any).get(id); + } if (!chat) { throw new InvalidChat(id); diff --git a/src/chat/functions/get.ts b/src/chat/functions/get.ts index 1eac7d2bc2..1e599e5a8d 100644 --- a/src/chat/functions/get.ts +++ b/src/chat/functions/get.ts @@ -1,5 +1,5 @@ /*! - * Copyright 2021 WPPConnect Team + * Copyright 2023 WPPConnect Team * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,7 +15,7 @@ */ import { assertWid } from '../../assert'; -import { ChatModel, ChatStore, Wid } from '../../whatsapp'; +import { ChatModel, ChatStore, NewsletterStore, Wid } from '../../whatsapp'; /** * Find a chat by id @@ -24,5 +24,9 @@ import { ChatModel, ChatStore, Wid } from '../../whatsapp'; */ export function get(chatId: string | Wid): ChatModel | undefined { const wid = assertWid(chatId); - return ChatStore.get(wid); + if (wid.server === 'newsletter') { + return NewsletterStore.get(wid); + } else { + return ChatStore.get(wid); + } } diff --git a/src/chat/functions/sendFileMessage.ts b/src/chat/functions/sendFileMessage.ts index 5b9ffd6849..dc38ab5b9e 100644 --- a/src/chat/functions/sendFileMessage.ts +++ b/src/chat/functions/sendFileMessage.ts @@ -25,9 +25,8 @@ import { wrapModuleFunction } from '../../whatsapp/exportModule'; import { generateVideoThumbsAndDuration, isAnimatedWebp, - processRawAudioVideo, - processRawMedia, processRawSticker, + uploadMedia, } from '../../whatsapp/functions'; import { defaultSendMessageOptions, @@ -106,10 +105,6 @@ export interface AudioMessageOptions extends FileMessageOptions { waveform?: boolean; } -export interface PushToVideoMessageOptions extends FileMessageOptions { - type: 'ptv'; -} - export interface DocumentMessageOptions extends FileMessageOptions, MessageButtonsOptions { @@ -132,6 +127,7 @@ export interface VideoMessageOptions MessageButtonsOptions { type: 'video'; isGif?: boolean; + isPtv?: boolean; } /** @@ -216,7 +212,8 @@ export interface VideoMessageOptions * '[number]@c.us', * 'data:application/msword;base64,', * { - * type: 'ptv', + * type: 'video', + * isPtv: true, * } * ); * ``` @@ -233,7 +230,6 @@ export async function sendFileMessage( | ImageMessageOptions | VideoMessageOptions | StickerMessageOptions - | PushToVideoMessageOptions ): Promise { options = { ...defaultSendMessageOptions, @@ -258,7 +254,6 @@ export async function sendFileMessage( isPtt?: boolean; asDocument?: boolean; asGif?: boolean; - isPtv?: boolean; isAudio?: boolean; asSticker?: boolean; precomputedFields?: { @@ -279,8 +274,6 @@ export async function sendFileMessage( isViewOnce = options.isViewOnce; } else if (options.type === 'video') { rawMediaOptions.asGif = options.isGif; - } else if (options.type === 'ptv') { - rawMediaOptions.isPtv = true; } else if (options.type === 'document') { rawMediaOptions.asDocument = true; } else if (options.type === 'sticker') { @@ -309,6 +302,11 @@ export async function sendFileMessage( } await mediaPrep.waitForPrep(); + if ((options as any)?.isPtv) { + (mediaPrep as any)._mediaData.type = 'ptv'; + (mediaPrep as any)._mediaData.fullHeight = 1128; + (mediaPrep as any)._mediaData.fullWidth = 1128; + } debug(`sending message (${options.type}) with id ${rawMessage.id}`); const sendMsgResult = mediaPrep.sendToChat(chat, { @@ -432,32 +430,13 @@ webpack.onFullReady(() => { return result; }); - wrapModuleFunction(processRawMedia, async (func, ...args) => { + wrapModuleFunction(uploadMedia, async (func, ...args) => { const [data] = args; - - let result = await func(...args); - if ((args as any)[1]?.isPtv) { - result = await (processRawAudioVideo( - data as any, - false, - null, - false, - null, - data.type(), - true - ) as any); - } - - return result; - }); - - wrapModuleFunction(processRawAudioVideo, async (func, ...args) => { - const [data] = args; - const result = await func(...args); - - if (data.type() === 'video/mp4' && args[6]) { - result.type = 'ptv'; + if ((data as any).mediaType == 'ptv') { + (data as any).mediaType = 'video'; + return await func(data); + } else { + return await func(...args); } - return result; }); }); diff --git a/src/chat/functions/sendRawMessage.ts b/src/chat/functions/sendRawMessage.ts index 2f2e8de7c2..cee7bbbf8f 100644 --- a/src/chat/functions/sendRawMessage.ts +++ b/src/chat/functions/sendRawMessage.ts @@ -17,9 +17,16 @@ import Debug from 'debug'; import { assertFindChat, assertGetChat } from '../../assert'; +import { WPPError } from '../../util'; +import { MsgModel } from '../../whatsapp'; +import { ACK } from '../../whatsapp/enums'; import { addAndSendMessageEdit, addAndSendMsgToChat, + addNewsletterMsgsRecords, + msgDataFromMsgModel, + sendNewsletterMessageJob, + updateNewsletterMsgRecord, } from '../../whatsapp/functions'; import { defaultSendMessageOptions, @@ -60,7 +67,32 @@ export async function sendRawMessage( debug(`sending message (${rawMessage.type}) with id ${rawMessage.id}`); let result = null as any; - if (rawMessage.type === 'protocol' && rawMessage.subtype === 'message_edit') { + if (chat?.isNewsletter) { + if (rawMessage.type !== ('chat' || 'image' || 'video')) + throw new WPPError( + 'type_not_valid_for_newsletter', + 'Please, send a valid type for send message to newsletter. Valdi types: "chat", "image", "video"' + ); + const msg = new MsgModel(rawMessage as any); + await addNewsletterMsgsRecords([await msgDataFromMsgModel(msg)]); + const resultNewsletter = await sendNewsletterMessageJob({ + msgData: rawMessage, + newsletterJid: chat.id.toString(), + type: rawMessage.type == 'chat' ? 'text' : 'media', + }); + chat.msgs.add(msg); + + if (resultNewsletter.success) { + msg.t = resultNewsletter.ack.t; + msg.serverId = resultNewsletter.serverId; + } + msg.updateAck(ACK.SENT, true); + await updateNewsletterMsgRecord(msg); + result = [msg, 'OK']; + } else if ( + rawMessage.type === 'protocol' && + rawMessage.subtype === 'message_edit' + ) { const msg = await getMessageById(rawMessage.protocolMessageKey); await addAndSendMessageEdit(msg, rawMessage); result = [await getMessageById(rawMessage.protocolMessageKey), null]; @@ -70,7 +102,6 @@ export async function sendRawMessage( debug(`message ${rawMessage.id} queued`); const message = await result[0]; - if (options.waitForAck) { debug(`waiting ack for ${rawMessage.id}`); diff --git a/src/index.ts b/src/index.ts index bca5aa2455..849adc7208 100644 --- a/src/index.ts +++ b/src/index.ts @@ -40,6 +40,7 @@ export * as labels from './labels'; export * as profile from './profile'; export * as status from './status'; export * as util from './util'; +export * as newsletter from './newsletter'; export * as whatsapp from './whatsapp'; export * as order from './order'; diff --git a/src/newsletter/functions/create.ts b/src/newsletter/functions/create.ts new file mode 100644 index 0000000000..8970019720 --- /dev/null +++ b/src/newsletter/functions/create.ts @@ -0,0 +1,71 @@ +/*! + * Copyright 2023 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 { blobToBase64, convertToFile, downloadImage } from '../../util'; +import { createNewsletterQuery } from '../../whatsapp/functions'; + +export interface ResultCreateNewsletter { + idJid: string; + inviteCode: string; + inviteLink: string; + name: string; + state: string; + subscribersCount: number; + description: string | null; + timestamp: number; +} + +/** + * Create a newsletter + * + * @example + * ```javascript + * // To edit name + * WPP.newsletter.create('Name for your newsletter', { + * description: 'Description for that', + * picture: ' { + let pic = undefined; + if (opts?.picture) { + const file = await convertToFile(opts.picture); + pic = await blobToBase64(file); + ({ data: pic } = await downloadImage(pic, 'image/jpeg')); + } + const result = await createNewsletterQuery({ + name: name, + description: opts?.description || null, + picture: pic || null, + }); + + return { + idJid: result?.idJid, + inviteCode: result?.newsletterInviteLinkMetadataMixin.inviteCode, + inviteLink: `https://whatsapp.com/channel/${result?.newsletterInviteLinkMetadataMixin.inviteCode}`, + name: result?.newsletterNameMetadataMixin?.nameElementValue, + state: result?.newsletterStateMetadataMixin?.stateType, + subscribersCount: + result?.newsletterSubscribersMetadataMixin.subscribersCount, + description: opts?.description || null, + timestamp: result?.newsletterCreationTimeMetadataMixin?.creationTimeValue, + }; +} diff --git a/src/newsletter/functions/destroy.ts b/src/newsletter/functions/destroy.ts new file mode 100644 index 0000000000..7ac313b898 --- /dev/null +++ b/src/newsletter/functions/destroy.ts @@ -0,0 +1,41 @@ +/*! + * Copyright 2023 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 { WPPError } from '../../util'; +import { deleteNewsletter } from '../../whatsapp/functions'; + +/** + * Delete a newsletter + * + * @example + * ```javascript + * const code = WPP.newsletter.destroy('[newsletter-id]@newsletter'); + * ``` + * + * @category Newsletter + */ +export async function destroy(id: string): Promise { + if (!id || !id.includes('newsletter')) + throw new WPPError( + 'send_correctly_newsletter_id', + 'Please, send the correct newsletter ID.' + ); + try { + return await deleteNewsletter(id); + } catch (error) { + return false; + } +} diff --git a/src/newsletter/functions/edit.ts b/src/newsletter/functions/edit.ts new file mode 100644 index 0000000000..8821aad02d --- /dev/null +++ b/src/newsletter/functions/edit.ts @@ -0,0 +1,112 @@ +/*! + * Copyright 2023 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 { blobToBase64, convertToFile, downloadImage } from '../../util'; +import { + editNewsletterMetadataAction, + queryNewsletterMetadataByJid, +} from '../../whatsapp/functions'; +import { ensureNewsletter } from './ensureNewsletter'; + +export interface ResultCreateNewsletter { + idJid: string; + inviteCode: string; + inviteLink: string; + name: string; + state: string; + subscribersCount: number; + description: string | null; + timestamp: number; +} + +/** + * Edit the newsletter data + * + * @example + * ```javascript + * // To edit name + * const code = WPP.newsletter.edit('[newsletter-id]@newsletter', { + * name: 'New Name' + * }); + * + * // To edit description + * const code = WPP.newsletter.edit('[newsletter-id]@newsletter', { + * description: 'New description' + * }); + * + * // To change picture + * const code = WPP.newsletter.edit('[newsletter-id]@newsletter', { + * picture: '' + * }); + * + * // To remove picture + * const code = WPP.newsletter.edit('[newsletter-id]@newsletter', { + * picture: null + * }); + * ``` + * + * @category Newsletter + */ +export async function edit( + newsletterId: string, + opts: { + name?: string; + description?: string; + picture?: string; + } +): Promise { + let pic: string | undefined = undefined; + if (opts?.picture) { + const file = await convertToFile(opts.picture); + pic = await blobToBase64(file); + ({ data: pic } = await downloadImage(pic, 'image/jpeg')); + } + + await editNewsletterMetadataAction( + await ensureNewsletter(newsletterId), + { + editDescription: opts.description ? true : false, + editName: opts.name ? true : false, + editPicture: pic || opts.picture == null ? true : false, + }, + { + name: opts.name, + description: opts.description, + picture: opts.picture == null ? null : pic, + } + ); + + const result = await queryNewsletterMetadataByJid(newsletterId, { + picture: true, + description: true, + name: true, + state: true, + creationTime: true, + }); + return { + idJid: result?.idJid, + inviteCode: result?.newsletterInviteLinkMetadataMixin.inviteCode, + inviteLink: `https://whatsapp.com/channel/${result?.newsletterInviteLinkMetadataMixin.inviteCode}`, + name: result?.newsletterNameMetadataMixin?.nameElementValue, + state: result?.newsletterStateMetadataMixin?.stateType, + subscribersCount: + result?.newsletterSubscribersMetadataMixin.subscribersCount, + description: + result?.newsletterDescriptionMetadataMixin + ?.descriptionQueryDescriptionResponseMixin?.elementValue, + timestamp: result?.newsletterCreationTimeMetadataMixin?.creationTimeValue, + }; +} diff --git a/src/newsletter/functions/ensureNewsletter.ts b/src/newsletter/functions/ensureNewsletter.ts new file mode 100644 index 0000000000..571d5f4849 --- /dev/null +++ b/src/newsletter/functions/ensureNewsletter.ts @@ -0,0 +1,31 @@ +/*! + * 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. + */ + +import { assertGetChat } from '../../assert'; +import { WPPError } from '../../util'; +import { Wid } from '../../whatsapp'; + +export async function ensureNewsletter(newsletterId: string | Wid) { + const newsletterChat = assertGetChat(newsletterId); + + if (!newsletterChat.isNewsletter) { + throw new WPPError( + 'not_a_newsletter', + `Chat ${newsletterChat.id._serialized} is not a newsletter` + ); + } + return newsletterChat; +} diff --git a/src/newsletter/functions/index.ts b/src/newsletter/functions/index.ts new file mode 100644 index 0000000000..dd48ce6cf0 --- /dev/null +++ b/src/newsletter/functions/index.ts @@ -0,0 +1,20 @@ +/*! + * 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. + */ + +export { create } from './create'; +export { destroy } from './destroy'; +export { edit } from './edit'; +export { mute } from './mute'; diff --git a/src/newsletter/functions/mute.ts b/src/newsletter/functions/mute.ts new file mode 100644 index 0000000000..4c57a82760 --- /dev/null +++ b/src/newsletter/functions/mute.ts @@ -0,0 +1,49 @@ +/*! + * Copyright 2023 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 { ChatModel, NewsletterStore } from '../../whatsapp'; +import { muteNewsletter, unmuteNewsletter } from '../../whatsapp/functions'; +import { ensureNewsletter } from './ensureNewsletter'; + +/** + * Mute and unmute a newsletter + * + * @example + * // Mute + * ```javascript + * WPP.newsletter.mute('[newsletter-id]@newsletter', true); + * ``` + * + * // Unmute + * ```javascript + * WPP.newsletter.mute('[newsletter-id]@newsletter', false); + * ``` + * + * @category Newsletter + */ +export async function mute( + newsletterId: string, + value?: boolean +): Promise { + await ensureNewsletter(newsletterId); + if (value === false) { + await unmuteNewsletter([newsletterId]); + return NewsletterStore.get(newsletterId) as ChatModel; + } else { + await muteNewsletter([newsletterId]); + return NewsletterStore.get(newsletterId) as ChatModel; + } +} diff --git a/src/newsletter/index.ts b/src/newsletter/index.ts new file mode 100644 index 0000000000..15b554f601 --- /dev/null +++ b/src/newsletter/index.ts @@ -0,0 +1,17 @@ +/*! + * Copyright 2023 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 * from './functions'; diff --git a/src/whatsapp/functions/createNewsletterQuery.ts b/src/whatsapp/functions/createNewsletterQuery.ts new file mode 100644 index 0000000000..f0fd766598 --- /dev/null +++ b/src/whatsapp/functions/createNewsletterQuery.ts @@ -0,0 +1,34 @@ +/*! + * Copyright 2023 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 874705 + */ +export declare function createNewsletterQuery(opts: { + name: string; + description: string | null; + picture: string | null; //base64 string +}): any; + +exportModule( + exports, + { + createNewsletterQuery: 'createNewsletterQuery', + }, + (m) => m.createNewsletterQuery +); diff --git a/src/whatsapp/functions/deleteNewsletter.ts b/src/whatsapp/functions/deleteNewsletter.ts new file mode 100644 index 0000000000..363d3bfa1f --- /dev/null +++ b/src/whatsapp/functions/deleteNewsletter.ts @@ -0,0 +1,30 @@ +/*! + * Copyright 2023 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 698436 + */ +export declare function deleteNewsletter(id: string): any; + +exportModule( + exports, + { + deleteNewsletter: 'deleteNewsletter', + }, + (m) => m.deleteNewsletter +); diff --git a/src/whatsapp/functions/editNewsletterMetadataAction.ts b/src/whatsapp/functions/editNewsletterMetadataAction.ts new file mode 100644 index 0000000000..1f5534a53d --- /dev/null +++ b/src/whatsapp/functions/editNewsletterMetadataAction.ts @@ -0,0 +1,43 @@ +/*! + * Copyright 2023 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 { ChatModel } from '../models'; + +/** + * @whatsapp 172365 + */ +export declare function editNewsletterMetadataAction( + newsletter: ChatModel, + editAtts: { + editName?: boolean; + editDescription?: boolean; + editPicture?: boolean; + }, + data: { + name?: string; + description?: string; + picture?: string | null; + } +): any; + +exportModule( + exports, + { + editNewsletterMetadataAction: 'editNewsletterMetadataAction', + }, + (m) => m.editNewsletterMetadataAction +); diff --git a/src/whatsapp/functions/getAsMms.ts b/src/whatsapp/functions/getAsMms.ts index 91faaf1c61..6aaf9fb733 100644 --- a/src/whatsapp/functions/getAsMms.ts +++ b/src/whatsapp/functions/getAsMms.ts @@ -30,12 +30,3 @@ exportModule( }, (m) => m.getAsMms // >= 2.2310.4 ); - -/** - * @whatsapp < 2.2310.4 - */ -webpack.injectFallbackModule('getAsMms', { - getAsMms: (msg: MsgModel) => { - return msg.asMms; - }, -}); diff --git a/src/whatsapp/functions/index.ts b/src/whatsapp/functions/index.ts index 19eecb2037..5e98eb6e43 100644 --- a/src/whatsapp/functions/index.ts +++ b/src/whatsapp/functions/index.ts @@ -27,9 +27,12 @@ export * from './contactFunctions'; export * from './createFanoutMsgStanza'; export * from './createGroup'; export * from './createMsgProtobuf'; +export * from './createNewsletterQuery'; export * from './createOrUpdateReactions'; export * from './currencyForCountryShortcode'; +export * from './deleteNewsletter'; export * from './editBusinessProfile'; +export * from './editNewsletterMetadataAction'; export * from './encodeMaybeMediaType'; export * from './encryptAndSendGroupMsg'; export * from './encryptAndSendMsg'; @@ -71,7 +74,9 @@ export * from './joinGroupViaInvite'; export * from './markSeen'; export * from './mediaTypeFromProtobuf'; export * from './membershipApprovalRequestAction'; +export * from './msgDataFromMsgModel'; export * from './msgFindQuery'; +export * from './muteNewsletter'; export * from './processRawAudioVideo'; export * from './processRawMedia'; export * from './processRawSticker'; @@ -80,6 +85,7 @@ export * from './productVisibilitySet'; export * from './profilePic'; export * from './queryAllGroups'; export * from './queryGroupInviteCode'; +export * from './queryNewsletterMetadataByJid'; export * from './randomHex'; export * from './randomId'; export * from './resetGroupInviteCode'; @@ -90,6 +96,7 @@ export * from './sendDelete'; export * from './sendExitGroup'; export * from './sendGroupParticipants'; export * from './sendJoinGroupViaInvite'; +export * from './sendNewsletterMessageJob'; export * from './sendQueryExists'; export * from './sendQueryGroupInvite'; export * from './sendQueryGroupInviteCode'; @@ -104,9 +111,12 @@ export * from './status'; export * from './syncABPropsTask'; export * from './typeAttributeFromProtobuf'; export * from './unixTime'; +export * from './unmuteNewsletter'; export * from './updateCartEnabled'; export * from './updateDBForGroupAction'; +export * from './updateNewsletterMsgRecord'; export * from './updateParticipants'; +export * from './uploadMedia'; export * from './uploadProductImage'; export * from './uploadThumbnail'; export * from './upsertVotes'; diff --git a/src/whatsapp/functions/msgDataFromMsgModel.ts b/src/whatsapp/functions/msgDataFromMsgModel.ts new file mode 100644 index 0000000000..3400984f15 --- /dev/null +++ b/src/whatsapp/functions/msgDataFromMsgModel.ts @@ -0,0 +1,30 @@ +/*! + * Copyright 2023 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 { MsgModel } from '../models'; + +/** @whatsapp 678794 + */ +export declare function msgDataFromMsgModel(msg: MsgModel): Promise; + +exportModule( + exports, + { + msgDataFromMsgModel: 'msgDataFromMsgModel', + }, + (m) => m.msgDataFromMsgModel +); diff --git a/src/whatsapp/functions/muteNewsletter.ts b/src/whatsapp/functions/muteNewsletter.ts new file mode 100644 index 0000000000..49f57bf2a7 --- /dev/null +++ b/src/whatsapp/functions/muteNewsletter.ts @@ -0,0 +1,32 @@ +/*! + * Copyright 2023 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 736062 + */ +export declare function muteNewsletter( + newsletterIds: string[] +): Promise; + +exportModule( + exports, + { + muteNewsletter: 'muteNewsletter', + }, + (m) => m.muteNewsletter +); diff --git a/src/whatsapp/functions/queryNewsletterMetadataByJid.ts b/src/whatsapp/functions/queryNewsletterMetadataByJid.ts new file mode 100644 index 0000000000..d239e85906 --- /dev/null +++ b/src/whatsapp/functions/queryNewsletterMetadataByJid.ts @@ -0,0 +1,46 @@ +/*! + * Copyright 2023 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 657987 + */ +export declare function queryNewsletterMetadataByJid( + jid: string, + attributes: { + picture?: boolean; + subscribers?: boolean; + verification?: boolean; + description?: boolean; + inviteLink?: boolean; + name?: boolean; + creationTime?: boolean; + handle?: boolean; + privacy?: boolean; + linkedAccounts?: boolean; + membership?: boolean; + state?: boolean; + } +): any; + +exportModule( + exports, + { + queryNewsletterMetadataByJid: 'queryNewsletterMetadataByJid', + }, + (m) => m.queryNewsletterMetadataByJid +); diff --git a/src/whatsapp/functions/sendNewsletterMessageJob.ts b/src/whatsapp/functions/sendNewsletterMessageJob.ts new file mode 100644 index 0000000000..c567e9ed21 --- /dev/null +++ b/src/whatsapp/functions/sendNewsletterMessageJob.ts @@ -0,0 +1,36 @@ +/*! + * Copyright 2023 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 397995 + */ +export declare function sendNewsletterMessageJob(data: { + msgData: any; + newsletterJid: string; + type: string; +}): Promise<{ ack: { t: number }; serverId: number; success: boolean }>; + +exportModule( + exports, + { + sendNewsletterMessageJob: [ + 'sendNewsletterMessageJob', + 'sendNewsletterMessage', // <= 2.2326.x + ], + }, + (m) => m.sendNewsletterMessageJob || m.sendNewsletterMessage // <= 2.2326.x +); diff --git a/src/whatsapp/functions/unmuteNewsletter.ts b/src/whatsapp/functions/unmuteNewsletter.ts new file mode 100644 index 0000000000..60321efe2b --- /dev/null +++ b/src/whatsapp/functions/unmuteNewsletter.ts @@ -0,0 +1,32 @@ +/*! + * Copyright 2023 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 639735 + */ +export declare function unmuteNewsletter( + newsletterIds: string[] +): Promise; + +exportModule( + exports, + { + unmuteNewsletter: 'unmuteNewsletter', + }, + (m) => m.unmuteNewsletter +); diff --git a/src/whatsapp/functions/updateNewsletterMsgRecord.ts b/src/whatsapp/functions/updateNewsletterMsgRecord.ts new file mode 100644 index 0000000000..56a85bb65c --- /dev/null +++ b/src/whatsapp/functions/updateNewsletterMsgRecord.ts @@ -0,0 +1,32 @@ +/*! + * Copyright 2023 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 { MsgModel } from '../models'; + +/** @whatsapp 397995 + */ +export declare function updateNewsletterMsgRecord(msg: MsgModel): Promise; +export declare function addNewsletterMsgsRecords(msg: MsgModel[]): Promise; + +exportModule( + exports, + { + updateNewsletterMsgRecord: 'updateNewsletterMsgRecord', + addNewsletterMsgsRecords: 'addNewsletterMsgsRecords', + }, + (m) => m.updateNewsletterMsgRecord +); diff --git a/src/whatsapp/functions/uploadMedia.ts b/src/whatsapp/functions/uploadMedia.ts new file mode 100644 index 0000000000..01ac73b795 --- /dev/null +++ b/src/whatsapp/functions/uploadMedia.ts @@ -0,0 +1,30 @@ +/*! + * Copyright 2023 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 { ChatModel } from '..'; +import { exportModule } from '../exportModule'; + +/** @whatsapp 709089 + */ +export declare function uploadMedia(chat: ChatModel): Promise; + +exportModule( + exports, + { + uploadMedia: 'uploadMedia', + }, + (m) => m.uploadMedia +); diff --git a/src/whatsapp/models/ChatModel.ts b/src/whatsapp/models/ChatModel.ts index b2e6f56051..df6b38709b 100644 --- a/src/whatsapp/models/ChatModel.ts +++ b/src/whatsapp/models/ChatModel.ts @@ -90,6 +90,7 @@ interface Derived { isPSA: boolean; isGroup: boolean; isBroadcast: boolean; + isNewsletter: boolean; canUnread: boolean; hasUnread: boolean; optimisticUnreadCount?: any; diff --git a/src/whatsapp/models/MsgModel.ts b/src/whatsapp/models/MsgModel.ts index c769e878d2..acdfb4ea2b 100644 --- a/src/whatsapp/models/MsgModel.ts +++ b/src/whatsapp/models/MsgModel.ts @@ -34,6 +34,7 @@ import { interface Props { id: MsgKey; rowId?: any; + serverId?: number; body?: string; type?: string; subtype?: string | null; diff --git a/src/whatsapp/stores.ts b/src/whatsapp/stores.ts index 83ea0abf8a..22578c329b 100644 --- a/src/whatsapp/stores.ts +++ b/src/whatsapp/stores.ts @@ -1,5 +1,5 @@ /*! - * Copyright 2021 WPPConnect Team + * Copyright 2023 WPPConnect Team * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,6 +46,10 @@ export declare const CatalogStore: collections.CatalogCollection; * @whatsapp 669951 >= 2.2222.8 */ export declare const ChatStore: collections.ChatCollection; +/** @whatsapp 19380 + * @whatsapp 719380 >= 2.2222.8 + */ +export declare const NewsletterStore: collections.ChatCollection; /** @whatsapp 19380 * @whatsapp 719380 >= 2.2222.8 */ @@ -133,6 +137,7 @@ const storeNames = [ 'CartStore', 'CatalogStore', 'ChatStore', + 'NewsletterStore', 'ContactStore', 'EmojiVariantStore', 'GroupMetadataStore', @@ -197,3 +202,11 @@ exportModule( }, (m) => m.StickerPackCollection ); + +exportModule( + exports, + { + NewsletterStore: 'default.NewsletterCollection', + }, + (m) => m.default.NewsletterCollection +);