mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
got rid of redundancies
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { MessageType, Mimetype, delay, promiseTimeout, WAMessage, WA_MESSAGE_STATUS_TYPE } from '../WAConnection/WAConnection'
|
import { MessageType, Mimetype, delay, promiseTimeout, WAMessage, WA_MESSAGE_STATUS_TYPE, MessageStatusUpdate } from '../WAConnection/WAConnection'
|
||||||
import {promises as fs} from 'fs'
|
import {promises as fs} from 'fs'
|
||||||
import * as assert from 'assert'
|
import * as assert from 'assert'
|
||||||
import { WAConnectionTest, testJid, sendAndRetreiveMessage } from './Common'
|
import { WAConnectionTest, testJid, sendAndRetreiveMessage } from './Common'
|
||||||
@@ -70,14 +70,14 @@ WAConnectionTest('Message Events', (conn) => {
|
|||||||
it('should deliver a message', async () => {
|
it('should deliver a message', async () => {
|
||||||
const waitForUpdate =
|
const waitForUpdate =
|
||||||
promiseTimeout(15000, resolve => {
|
promiseTimeout(15000, resolve => {
|
||||||
conn.on('message-update', message => {
|
conn.on('message-update', update => {
|
||||||
if (message.key.id === response.key.id) {
|
if (update.ids.includes(response.key.id)) {
|
||||||
resolve(message)
|
resolve(update)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}) as Promise<WAMessage>
|
}) as Promise<MessageStatusUpdate>
|
||||||
const response = await conn.sendMessage(testJid, 'My Name Jeff', MessageType.text)
|
const response = await conn.sendMessage(testJid, 'My Name Jeff', MessageType.text)
|
||||||
const m = await waitForUpdate
|
const m = await waitForUpdate
|
||||||
assert.ok (m.status >= WA_MESSAGE_STATUS_TYPE.DELIVERY_ACK)
|
assert.ok (m.type >= WA_MESSAGE_STATUS_TYPE.DELIVERY_ACK)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -254,7 +254,7 @@ export class WAConnection extends EventEmitter {
|
|||||||
this.msgCount += 1 // increment message count, it makes the 'epoch' field when sending binary messages
|
this.msgCount += 1 // increment message count, it makes the 'epoch' field when sending binary messages
|
||||||
return this.conn.send(m)
|
return this.conn.send(m)
|
||||||
}
|
}
|
||||||
protected async waitForConnection (waitForOpen: boolean) {
|
protected async waitForConnection (waitForOpen: boolean=true) {
|
||||||
if (!waitForOpen || this.state === 'open') return
|
if (!waitForOpen || this.state === 'open') return
|
||||||
|
|
||||||
const timeout = this.pendingRequestTimeoutMs
|
const timeout = this.pendingRequestTimeoutMs
|
||||||
@@ -298,6 +298,7 @@ export class WAConnection extends EventEmitter {
|
|||||||
|
|
||||||
Object.keys(this.callbacks).forEach(key => {
|
Object.keys(this.callbacks).forEach(key => {
|
||||||
if (!key.includes('function:')) {
|
if (!key.includes('function:')) {
|
||||||
|
this.log (`cancelling message wait: ${key}`, MessageLogLevel.info)
|
||||||
this.callbacks[key].errCallback(new Error('closed'))
|
this.callbacks[key].errCallback(new Error('closed'))
|
||||||
delete this.callbacks[key]
|
delete this.callbacks[key]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -129,6 +129,10 @@ export class WAConnection extends Base {
|
|||||||
chat.t = +chat.t
|
chat.t = +chat.t
|
||||||
chat.count = +chat.count
|
chat.count = +chat.count
|
||||||
chat.messages = []
|
chat.messages = []
|
||||||
|
|
||||||
|
const oldChat = this.chats.get(chat.jid)
|
||||||
|
oldChat && this.chats.delete (oldChat)
|
||||||
|
|
||||||
this.chats.insert (chat) // chats data (log json to see what it looks like)
|
this.chats.insert (chat) // chats data (log json to see what it looks like)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ export class WAConnection extends Base {
|
|||||||
if (!chat) return
|
if (!chat) return
|
||||||
|
|
||||||
this.emit ('message-update', update)
|
this.emit ('message-update', update)
|
||||||
this.chatUpdatedMessage (update.ids, update.type, chat)
|
this.chatUpdatedMessage (update.ids, update.type as number, chat)
|
||||||
}
|
}
|
||||||
this.registerCallback('Msg', func)
|
this.registerCallback('Msg', func)
|
||||||
this.registerCallback('MsgInfo', func)
|
this.registerCallback('MsgInfo', func)
|
||||||
@@ -194,7 +194,7 @@ export class WAConnection extends Base {
|
|||||||
to: message.key.remoteJid,
|
to: message.key.remoteJid,
|
||||||
ids: [message.key.id],
|
ids: [message.key.id],
|
||||||
timestamp: new Date(),
|
timestamp: new Date(),
|
||||||
type: -1
|
type: 'delete'
|
||||||
}
|
}
|
||||||
this.emit ('message-update', update)
|
this.emit ('message-update', update)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,16 @@ export class WAConnection extends Base {
|
|||||||
type: MessageType,
|
type: MessageType,
|
||||||
options: MessageOptions = {},
|
options: MessageOptions = {},
|
||||||
) {
|
) {
|
||||||
|
const content = await this.prepareMessageContent (
|
||||||
|
message,
|
||||||
|
type,
|
||||||
|
options
|
||||||
|
)
|
||||||
|
const preparedMessage = this.prepareMessageFromContent(id, content, options)
|
||||||
|
return preparedMessage
|
||||||
|
}
|
||||||
|
/** Prepares the message content */
|
||||||
|
async prepareMessageContent (message: string | WATextMessage | WALocationMessage | WAContactMessage | Buffer, type: MessageType, options: MessageOptions) {
|
||||||
let m: WAMessageContent = {}
|
let m: WAMessageContent = {}
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case MessageType.text:
|
case MessageType.text:
|
||||||
@@ -59,13 +69,13 @@ export class WAConnection extends Base {
|
|||||||
m.contactMessage = message as WAContactMessage
|
m.contactMessage = message as WAContactMessage
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
m = await this.prepareMediaMessage(message as Buffer, type, options)
|
m = await this.prepareMessageMedia(message as Buffer, type, options)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
return this.generateWAMessage(id, m, options)
|
return m
|
||||||
}
|
}
|
||||||
/** Prepare a media message for sending */
|
/** Prepare a media message for sending */
|
||||||
async prepareMediaMessage(buffer: Buffer, mediaType: MessageType, options: MessageOptions = {}) {
|
async prepareMessageMedia(buffer: Buffer, mediaType: MessageType, options: MessageOptions = {}) {
|
||||||
if (mediaType === MessageType.document && !options.mimetype) {
|
if (mediaType === MessageType.document && !options.mimetype) {
|
||||||
throw new Error('mimetype required to send a document')
|
throw new Error('mimetype required to send a document')
|
||||||
}
|
}
|
||||||
@@ -126,8 +136,8 @@ export class WAConnection extends Base {
|
|||||||
}
|
}
|
||||||
return message as WAMessageContent
|
return message as WAMessageContent
|
||||||
}
|
}
|
||||||
/** generates a WAMessage from the given content & options */
|
/** prepares a WAMessage for sending from the given content & options */
|
||||||
generateWAMessage(id: string, message: WAMessageContent, options: MessageOptions) {
|
prepareMessageFromContent(id: string, message: WAMessageContent, options: MessageOptions) {
|
||||||
if (!options.timestamp) options.timestamp = new Date() // set timestamp to now
|
if (!options.timestamp) options.timestamp = new Date() // set timestamp to now
|
||||||
|
|
||||||
// prevent an annoying bug (WA doesn't accept sending messages with '@c.us')
|
// prevent an annoying bug (WA doesn't accept sending messages with '@c.us')
|
||||||
|
|||||||
@@ -227,7 +227,7 @@ export class WAConnection extends Base {
|
|||||||
type: WAMessageProto.ProtocolMessage.PROTOCOL_MESSAGE_TYPE.REVOKE
|
type: WAMessageProto.ProtocolMessage.PROTOCOL_MESSAGE_TYPE.REVOKE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const waMessage = this.generateWAMessage (id, json, {})
|
const waMessage = this.prepareMessageFromContent (id, json, {})
|
||||||
await this.relayWAMessage (waMessage)
|
await this.relayWAMessage (waMessage)
|
||||||
return waMessage
|
return waMessage
|
||||||
}
|
}
|
||||||
@@ -254,7 +254,7 @@ export class WAConnection extends Base {
|
|||||||
if (score > 0) content[key].contextInfo = { forwardingScore: score, isForwarded: true }
|
if (score > 0) content[key].contextInfo = { forwardingScore: score, isForwarded: true }
|
||||||
else content[key].contextInfo = {}
|
else content[key].contextInfo = {}
|
||||||
|
|
||||||
const waMessage = this.generateWAMessage (id, content, {})
|
const waMessage = this.prepareMessageFromContent (id, content, {})
|
||||||
await this.relayWAMessage (waMessage)
|
await this.relayWAMessage (waMessage)
|
||||||
return waMessage
|
return waMessage
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -189,12 +189,6 @@ export enum Presence {
|
|||||||
recording = 'recording', // "recording..."
|
recording = 'recording', // "recording..."
|
||||||
paused = 'paused', // I have no clue
|
paused = 'paused', // I have no clue
|
||||||
}
|
}
|
||||||
/** Status of a message sent or received */
|
|
||||||
export enum MessageStatus {
|
|
||||||
sent = 'sent',
|
|
||||||
received = 'received',
|
|
||||||
read = 'read',
|
|
||||||
}
|
|
||||||
/** Set of message types that are supported by the library */
|
/** Set of message types that are supported by the library */
|
||||||
export enum MessageType {
|
export enum MessageType {
|
||||||
text = 'conversation',
|
text = 'conversation',
|
||||||
@@ -274,7 +268,7 @@ export interface MessageStatusUpdate {
|
|||||||
/** Message IDs read/delivered */
|
/** Message IDs read/delivered */
|
||||||
ids: string[]
|
ids: string[]
|
||||||
/** Status of the Message IDs */
|
/** Status of the Message IDs */
|
||||||
type: WA_MESSAGE_STATUS_TYPE
|
type: WA_MESSAGE_STATUS_TYPE | 'delete'
|
||||||
}
|
}
|
||||||
export enum GroupSettingChange {
|
export enum GroupSettingChange {
|
||||||
messageSend = 'announcement',
|
messageSend = 'announcement',
|
||||||
|
|||||||
Reference in New Issue
Block a user