Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pin Messages (all platform support) #959

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/Socket/messages-send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import NodeCache from 'node-cache'
import { proto } from '../../WAProto'
import { DEFAULT_CACHE_TTLS, WA_DEFAULT_EPHEMERAL } from '../Defaults'
import { AnyMessageContent, MediaConnInfo, MessageReceiptType, MessageRelayOptions, MiscMessageGenerationOptions, SocketConfig, WAMessageKey } from '../Types'
import { aggregateMessageKeysNotFromMe, assertMediaContent, bindWaitForEvent, decryptMediaRetryData, encodeSignedDeviceIdentity, encodeWAMessage, encryptMediaRetryRequest, extractDeviceJids, generateMessageIDV2, generateWAMessage, getStatusCodeForMediaRetry, getUrlFromDirectPath, getWAUploadToServer, parseAndInjectE2ESessions, unixTimestampSeconds } from '../Utils'
import { aggregateMessageKeysNotFromMe, assertMediaContent, bindWaitForEvent, decryptMediaRetryData, encodeSignedDeviceIdentity, encodeWAMessage, encryptMediaRetryRequest, extractDeviceJids, generateMessageIDV2, generateWAMessage, getStatusCodeForMediaRetry, getUrlFromDirectPath, getWAUploadToServer, normalizeMessageContent, parseAndInjectE2ESessions, unixTimestampSeconds } from '../Utils'
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'
Expand Down Expand Up @@ -362,6 +362,8 @@ export const makeMessagesSocket = (config: SocketConfig) => {
}
}

const extraAttrs = {}

if(participant) {
// when the retry request is not for a group
// only send to the specific device that asked for a retry
Expand All @@ -377,6 +379,14 @@ export const makeMessagesSocket = (config: SocketConfig) => {
await authState.keys.transaction(
async() => {
const mediaType = getMediaType(message)
if(mediaType) {
extraAttrs['mediatype'] = mediaType
}

if(normalizeMessageContent(message)?.pinInChatMessage) {
extraAttrs['decrypt-fail'] = 'hide'
}

if(isGroup || isStatus) {
const [groupData, senderKeyMap] = await Promise.all([
(async() => {
Expand Down Expand Up @@ -445,7 +455,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {

await assertSessions(senderKeyJids, false)

const result = await createParticipantNodes(senderKeyJids, senderKeyMsg, mediaType ? { mediatype: mediaType } : undefined)
const result = await createParticipantNodes(senderKeyJids, senderKeyMsg, extraAttrs)
shouldIncludeDeviceIdentity = shouldIncludeDeviceIdentity || result.shouldIncludeDeviceIdentity

participants.push(...result.nodes)
Expand Down Expand Up @@ -496,8 +506,8 @@ export const makeMessagesSocket = (config: SocketConfig) => {
{ nodes: meNodes, shouldIncludeDeviceIdentity: s1 },
{ nodes: otherNodes, shouldIncludeDeviceIdentity: s2 }
] = await Promise.all([
createParticipantNodes(meJids, meMsg, mediaType ? { mediatype: mediaType } : undefined),
createParticipantNodes(otherJids, message, mediaType ? { mediatype: mediaType } : undefined)
createParticipantNodes(meJids, meMsg, extraAttrs),
createParticipantNodes(otherJids, message, extraAttrs)
])
participants.push(...meNodes)
participants.push(...otherNodes)
Expand Down Expand Up @@ -674,7 +684,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
}

content.directPath = media.directPath
content.url = getUrlFromDirectPath(content.directPath!)
content.url = getUrlFromDirectPath(content.directPath)

logger.debug({ directPath: media.directPath, key: result.key }, 'media update successful')
} catch(err) {
Expand Down Expand Up @@ -745,6 +755,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
)
const isDeleteMsg = 'delete' in content && !!content.delete
const isEditMsg = 'edit' in content && !!content.edit
const isPinMsg = 'pin' in content && !!content.pin
const additionalAttributes: BinaryNodeAttributes = { }
// required for delete
if(isDeleteMsg) {
Expand All @@ -756,6 +767,8 @@ export const makeMessagesSocket = (config: SocketConfig) => {
}
} else if(isEditMsg) {
additionalAttributes.edit = '1'
} else if(isPinMsg) {
additionalAttributes.edit = '2'
}

if('cachedGroupMetadata' in options) {
Expand Down
8 changes: 8 additions & 0 deletions src/Types/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ export type AnyRegularMessageContent = (
| {
listReply: Omit<proto.Message.IListResponseMessage, 'contextInfo'>
}
| {
pin: WAMessageKey
type: proto.PinInChat.Type
/**
* 24 hours, 7 days, 30 days
*/
time?: 86400 | 604800 | 2592000
}
| {
product: WASendableProduct
businessOwnerJid?: string
Expand Down
13 changes: 11 additions & 2 deletions src/Utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export const prepareWAMessageMedia = async(
!!uploadData.media.url &&
!!options.mediaCache && (
// generate the key
mediaType + ':' + uploadData.media.url!.toString()
mediaType + ':' + uploadData.media.url.toString()
)

if(mediaType === 'document' && !uploadData.fileName) {
Expand Down Expand Up @@ -394,6 +394,15 @@ export const generateWAMessageContent = async(
(message.disappearingMessagesInChat ? WA_DEFAULT_EPHEMERAL : 0) :
message.disappearingMessagesInChat
m = prepareDisappearingMessageSettingContent(exp)
} else if('pin' in message) {
m.pinInChatMessage = {}
m.messageContextInfo = {}

m.pinInChatMessage.key = message.pin
m.pinInChatMessage.type = message.type
m.pinInChatMessage.senderTimestampMs = Date.now()

m.messageContextInfo.messageAddOnDurationInSecs = message.type === 1 ? message.time || 86400 : 0
} else if('buttonReply' in message) {
switch (message.type) {
case 'template':
Expand Down Expand Up @@ -678,7 +687,7 @@ export const extractMessageContent = (content: WAMessageContent | undefined | nu
content = normalizeMessageContent(content)

if(content?.buttonsMessage) {
return extractFromTemplateMessage(content.buttonsMessage!)
return extractFromTemplateMessage(content.buttonsMessage)
}

if(content?.templateMessage?.hydratedFourRowTemplate) {
Expand Down
Loading