Skip to content

Commit

Permalink
fix(fix/buttons-deprecation): initial/final commit (#956)
Browse files Browse the repository at this point in the history
  • Loading branch information
PurpShell authored Aug 14, 2024
1 parent e915010 commit 35f6d75
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 138 deletions.
50 changes: 3 additions & 47 deletions src/Socket/messages-send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { aggregateMessageKeysNotFromMe, assertMediaContent, bindWaitForEvent, de
import { getUrlInfo } from '../Utils/link-preview'
import { areJidsSameUser, BinaryNode, BinaryNodeAttributes, getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, isJidUser, jidDecode, jidEncode, jidNormalizedUser, JidWithDevice, S_WHATSAPP_NET } from '../WABinary'
import { makeGroupsSocket } from './groups'
import ListType = proto.Message.ListMessage.ListType;

export const makeMessagesSocket = (config: SocketConfig) => {
const {
Expand Down Expand Up @@ -306,7 +305,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
const relayMessage = async(
jid: string,
message: proto.IMessage,
{ messageId: msgId, participant, additionalAttributes, useUserDevicesCache, cachedGroupMetadata, statusJidList }: MessageRelayOptions
{ messageId: msgId, participant, additionalAttributes, additionalNodes, useUserDevicesCache, cachedGroupMetadata, statusJidList }: MessageRelayOptions
) => {
const meId = authState.creds.me!.id

Expand Down Expand Up @@ -519,20 +518,8 @@ export const makeMessagesSocket = (config: SocketConfig) => {
logger.debug({ jid }, 'adding device identity')
}

const buttonType = getButtonType(message)
if(buttonType) {
(stanza.content as BinaryNode[]).push({
tag: 'biz',
attrs: { },
content: [
{
tag: buttonType,
attrs: getButtonArgs(message),
}
]
})

logger.debug({ jid }, 'adding business node')
if(additionalNodes && additionalNodes.length > 0) {
(stanza.content as BinaryNode[]).push(...additionalNodes)
}

logger.debug({ msgId }, `sending message to ${participants.length} devices`)
Expand Down Expand Up @@ -576,36 +563,6 @@ export const makeMessagesSocket = (config: SocketConfig) => {
}
}

const getButtonType = (message: proto.IMessage) => {
if(message.buttonsMessage) {
return 'buttons'
} else if(message.buttonsResponseMessage) {
return 'buttons_response'
} else if(message.interactiveResponseMessage) {
return 'interactive_response'
} else if(message.listMessage) {
return 'list'
} else if(message.listResponseMessage) {
return 'list_response'
}
}

const getButtonArgs = (message: proto.IMessage): BinaryNode['attrs'] => {
if(message.templateMessage) {
// TODO: Add attributes
return {}
} else if(message.listMessage) {
const type = message.listMessage.listType
if(!type) {
throw new Boom('Expected list type inside message')
}

return { v: '2', type: ListType[type].toLowerCase() }
} else {
return {}
}
}

const getPrivacyTokens = async(jids: string[]) => {
const t = unixTimestampSeconds().toString()
const result = await query({
Expand Down Expand Up @@ -647,7 +604,6 @@ export const makeMessagesSocket = (config: SocketConfig) => {
relayMessage,
sendReceipt,
sendReceipts,
getButtonArgs,
readMessages,
refreshMediaConn,
waUploadToServer,
Expand Down
32 changes: 7 additions & 25 deletions src/Types/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Readable } from 'stream'
import type { URL } from 'url'
import { proto } from '../../WAProto'
import { MEDIA_HKDF_KEY_MAPPING } from '../Defaults'
import { BinaryNode } from '../WABinary'
import type { GroupMetadata } from './GroupMetadata'
import { CacheStore } from './Socket'

Expand Down Expand Up @@ -60,29 +61,9 @@ type ViewOnce = {
viewOnce?: boolean
}

type Buttonable = {
/** add buttons to the message */
buttons?: proto.Message.ButtonsMessage.IButton[]
}
type Templatable = {
/** add buttons to the message (conflicts with normal buttons)*/
templateButtons?: proto.IHydratedTemplateButton[]

footer?: string
}
type Editable = {
edit?: WAMessageKey
}
type Listable = {
/** Sections of the List */
sections?: proto.Message.ListMessage.ISection[]

/** Title of a List Message only */
title?: string

/** Text of the bnutton on the list (required) */
buttonText?: string
}
type WithDimensions = {
width?: number
height?: number
Expand Down Expand Up @@ -111,15 +92,15 @@ export type AnyMediaMessageContent = (
image: WAMediaUpload
caption?: string
jpegThumbnail?: string
} & Mentionable & Contextable & Buttonable & Templatable & WithDimensions)
} & Mentionable & Contextable & WithDimensions)
| ({
video: WAMediaUpload
caption?: string
gifPlayback?: boolean
jpegThumbnail?: string
/** if set to true, will send as a `video note` */
ptv?: boolean
} & Mentionable & Contextable & Buttonable & Templatable & WithDimensions)
} & Mentionable & Contextable & WithDimensions)
| {
audio: WAMediaUpload
/** if set to true, will send as a `voice note` */
Expand All @@ -135,7 +116,7 @@ export type AnyMediaMessageContent = (
mimetype: string
fileName?: string
caption?: string
} & Contextable & Buttonable & Templatable))
} & Contextable))
& { mimetype?: string } & Editable

export type ButtonReplyInfo = {
Expand All @@ -153,11 +134,11 @@ export type AnyRegularMessageContent = (
text: string
linkPreview?: WAUrlInfo | null
}
& Mentionable & Contextable & Buttonable & Templatable & Listable & Editable)
& Mentionable & Contextable & Editable)
| AnyMediaMessageContent
| ({
poll: PollMessageOptions
} & Mentionable & Contextable & Buttonable & Templatable & Editable)
} & Mentionable & Contextable & Editable)
| {
contacts: {
displayName?: string
Expand Down Expand Up @@ -207,6 +188,7 @@ export type MessageRelayOptions = MinimalRelayOptions & {
participant?: { jid: string, count: number }
/** additional attributes to add to the WA binary node */
additionalAttributes?: { [_: string]: string }
additionalNodes?: BinaryNode[]
/** should we use the devices cache, or fetch afresh from the server; default assumed to be "true" */
useUserDevicesCache?: boolean
/** jid list of participants for status@broadcast */
Expand Down
66 changes: 0 additions & 66 deletions src/Utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ const MessageTypeProto = {
'document': WAProto.Message.DocumentMessage,
} as const

const ButtonType = proto.Message.ButtonsMessage.HeaderType

/**
* Uses a regex to test whether the string contains a URL, and returns the URL if it does.
* @param text eg. hello https://google.com
Expand Down Expand Up @@ -487,70 +485,6 @@ export const generateWAMessageContent = async(
)
}

if('buttons' in message && !!message.buttons) {
const buttonsMessage: proto.Message.IButtonsMessage = {
buttons: message.buttons!.map(b => ({ ...b, type: proto.Message.ButtonsMessage.Button.Type.RESPONSE }))
}
if('text' in message) {
buttonsMessage.contentText = message.text
buttonsMessage.headerType = ButtonType.EMPTY
} else {
if('caption' in message) {
buttonsMessage.contentText = message.caption
}

const type = Object.keys(m)[0].replace('Message', '').toUpperCase()
buttonsMessage.headerType = ButtonType[type]

Object.assign(buttonsMessage, m)
}

if('footer' in message && !!message.footer) {
buttonsMessage.footerText = message.footer
}

m = { buttonsMessage }
} else if('templateButtons' in message && !!message.templateButtons) {
const msg: proto.Message.TemplateMessage.IHydratedFourRowTemplate = {
hydratedButtons: message.templateButtons
}

if('text' in message) {
msg.hydratedContentText = message.text
} else {

if('caption' in message) {
msg.hydratedContentText = message.caption
}

Object.assign(msg, m)
}

if('footer' in message && !!message.footer) {
msg.hydratedFooterText = message.footer
}

m = {
templateMessage: {
fourRowTemplate: msg,
hydratedTemplate: msg
}
}
}

if('sections' in message && !!message.sections) {
const listMessage: proto.Message.IListMessage = {
sections: message.sections,
buttonText: message.buttonText,
title: message.title,
footerText: message.footer,
description: message.text,
listType: proto.Message.ListMessage.ListType.SINGLE_SELECT
}

m = { listMessage }
}

if('viewOnce' in message && !!message.viewOnce) {
m = { viewOnceMessage: { message: m } }
}
Expand Down

0 comments on commit 35f6d75

Please sign in to comment.