mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
feat: add desktop + full history sync opts
This commit is contained in:
@@ -45,6 +45,7 @@ export const DEFAULT_CONNECTION_CONFIG: SocketConfig = {
|
||||
auth: undefined as any,
|
||||
downloadHistory: true,
|
||||
markOnlineOnConnect: true,
|
||||
syncFullHistory: false,
|
||||
linkPreviewImageThumbnailWidth: 192,
|
||||
transactionOpts: { maxCommitRetries: 10, delayBetweenTriesMs: 3000 },
|
||||
getMessage: async() => undefined
|
||||
|
||||
@@ -25,6 +25,7 @@ export const makeSocket = ({
|
||||
auth: authState,
|
||||
printQRInTerminal,
|
||||
defaultQueryTimeoutMs,
|
||||
syncFullHistory,
|
||||
transactionOpts
|
||||
}: SocketConfig) => {
|
||||
const ws = new WebSocket(waWebSocketUrl, undefined, {
|
||||
@@ -178,12 +179,14 @@ export const makeSocket = ({
|
||||
|
||||
const keyEnc = noise.processHandshake(handshake, creds.noiseKey)
|
||||
|
||||
const config = { version, browser, syncFullHistory }
|
||||
|
||||
let node: proto.IClientPayload
|
||||
if(!creds.me) {
|
||||
node = generateRegistrationNode(creds, { version, browser })
|
||||
node = generateRegistrationNode(creds, config)
|
||||
logger.info({ node }, 'not logged in, attempting registration...')
|
||||
} else {
|
||||
node = generateLoginNode(creds.me!.id, { version, browser })
|
||||
node = generateLoginNode(creds.me!.id, config)
|
||||
logger.info({ node }, 'logging in...')
|
||||
}
|
||||
|
||||
|
||||
@@ -34,6 +34,8 @@ export type SocketConfig = CommonSocketConfig & {
|
||||
msgRetryCounterMap?: MessageRetryMap
|
||||
/** width for link preview images */
|
||||
linkPreviewImageThumbnailWidth: number
|
||||
/** Should Baileys ask the phone for full history, will be received async */
|
||||
syncFullHistory: boolean
|
||||
/**
|
||||
* fetch a message from your store
|
||||
* implement this so that messages failed to send (solves the "this message can take a while" issue) can be retried
|
||||
|
||||
@@ -52,7 +52,7 @@ export const processHistoryMessage = (
|
||||
const curItem = recvChats[message.key.remoteJid!]
|
||||
const timestamp = toNumber(message.messageTimestamp)
|
||||
if(!message.key.fromMe && (!curItem || timestamp > curItem.lastMsgRecvTimestamp)) {
|
||||
recvChats[message.key.remoteJid!] = { lastMsgRecvTimestamp: timestamp }
|
||||
recvChats[chat.id] = { lastMsgRecvTimestamp: timestamp }
|
||||
// keep only the most recent message in the chat array
|
||||
chat.messages = [{ message }]
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import { Curve, hmacSign } from './crypto'
|
||||
import { encodeBigEndian } from './generics'
|
||||
import { createSignalIdentity } from './signal'
|
||||
|
||||
type ClientPayloadConfig = Pick<SocketConfig, 'version' | 'browser'>
|
||||
type ClientPayloadConfig = Pick<SocketConfig, 'version' | 'browser' | 'syncFullHistory'>
|
||||
|
||||
const getUserAgent = ({ version }: ClientPayloadConfig): proto.IUserAgent => {
|
||||
const osVersion = '0.1'
|
||||
@@ -31,16 +31,26 @@ const getUserAgent = ({ version }: ClientPayloadConfig): proto.IUserAgent => {
|
||||
}
|
||||
}
|
||||
|
||||
const getWebInfo = (): proto.IWebInfo => ({
|
||||
webSubPlatform: proto.WebInfo.WebInfoWebSubPlatform.WEB_BROWSER
|
||||
})
|
||||
const PLATFORM_MAP = {
|
||||
'Mac OS': proto.WebInfo.WebInfoWebSubPlatform.DARWIN,
|
||||
'Windows': proto.WebInfo.WebInfoWebSubPlatform.WIN32
|
||||
}
|
||||
|
||||
const getWebInfo = (config: ClientPayloadConfig): proto.IWebInfo => {
|
||||
let webSubPlatform = proto.WebInfo.WebInfoWebSubPlatform.WEB_BROWSER
|
||||
if(config.syncFullHistory && PLATFORM_MAP[config.browser[0]]) {
|
||||
webSubPlatform = PLATFORM_MAP[config.browser[0]]
|
||||
}
|
||||
|
||||
return { webSubPlatform }
|
||||
}
|
||||
|
||||
const getClientPayload = (config: ClientPayloadConfig): proto.IClientPayload => {
|
||||
return {
|
||||
connectType: proto.ClientPayload.ClientPayloadConnectType.WIFI_UNKNOWN,
|
||||
connectReason: proto.ClientPayload.ClientPayloadConnectReason.USER_ACTIVATED,
|
||||
userAgent: getUserAgent(config),
|
||||
webInfo: getWebInfo(),
|
||||
webInfo: getWebInfo(config),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,7 +85,7 @@ export const generateRegistrationNode = (
|
||||
},
|
||||
platformType: proto.DeviceProps.DevicePropsPlatformType[config.browser[1].toUpperCase()]
|
||||
|| proto.DeviceProps.DevicePropsPlatformType.UNKNOWN,
|
||||
requireFullSync: false,
|
||||
requireFullSync: config.syncFullHistory,
|
||||
}
|
||||
|
||||
const companionProto = proto.DeviceProps.encode(companion).finish()
|
||||
|
||||
Reference in New Issue
Block a user