Wrap up connection + in memory store

This commit is contained in:
Adhiraj Singh
2021-07-09 20:35:07 +05:30
parent 5be4a9cc2c
commit 89cf8004e9
27 changed files with 4637 additions and 1317 deletions

View File

@@ -1,7 +1,3 @@
import type KeyedDB from "@adiwajshing/keyed-db";
import type { proto } from '../../WAMessage/WAMessage'
import type { GroupMetadata } from "./GroupMetadata";
/** set of statuses visible to other people; see updatePresence() in WhatsAppWeb.Send */
export enum Presence {
unavailable = 'unavailable', // "offline"
@@ -37,8 +33,25 @@ export interface Chat {
ephemeral?: string
// Baileys added properties
messages: KeyedDB<proto.IWebMessageInfo, string>
imgUrl?: string
presences?: { [k: string]: PresenceData }
metadata?: GroupMetadata
}
}
export type ChatModification =
{ archive: boolean } |
{
/** pin at current timestamp, or provide timestamp of pin to remove */
pin: true | { remove: number }
} |
{
/** mute for duration, or provide timestamp of mute to remove*/
mute: number | { remove: number }
} |
{
clear: 'all' | { messages: { id: string, fromMe?: boolean }[] }
} |
{
star: {
messages: { id: string, fromMe?: boolean }[],
star: boolean
}
}

View File

@@ -12,4 +12,5 @@ export interface Contact {
short?: string
// Baileys Added
imgUrl?: string
status?: string
}

View File

@@ -2,6 +2,8 @@ import { Contact } from "./Contact";
export type GroupParticipant = (Contact & { isAdmin: boolean; isSuperAdmin: boolean })
export type ParticipantAction = 'add' | 'remove' | 'promote' | 'demote'
export interface GroupMetadata {
id: string
owner: string
@@ -16,4 +18,16 @@ export interface GroupMetadata {
announce?: 'true' | 'false'
// Baileys modified array
participants: GroupParticipant[]
}
export interface WAGroupCreateResponse {
status: number
gid?: string
participants?: [{ [key: string]: any }]
}
export interface GroupModificationResponse {
status: number
participants?: { [key: string]: any }
}

135
src/Types/Message.ts Normal file
View File

@@ -0,0 +1,135 @@
import type { Agent } from "https"
import type { Logger } from "pino"
import type { URL } from "url"
import { proto } from '../../WAMessage/WAMessage'
// export the WAMessage Prototypes
export { proto as WAMessageProto }
export type WAMessage = proto.WebMessageInfo
export type WAMessageContent = proto.IMessage
export type WAContactMessage = proto.ContactMessage
export type WAContactsArrayMessage = proto.ContactsArrayMessage
export type WAMessageKey = proto.IMessageKey
export type WATextMessage = proto.ExtendedTextMessage
export type WAContextInfo = proto.IContextInfo
export type WALocationMessage = proto.LocationMessage
export type WAGenericMediaMessage = proto.IVideoMessage | proto.IImageMessage | proto.IAudioMessage | proto.IDocumentMessage | proto.IStickerMessage
export import WAMessageStubType = proto.WebMessageInfo.WebMessageInfoStubType
export import WAMessageStatus = proto.WebMessageInfo.WebMessageInfoStatus
export type WAMediaUpload = Buffer | { url: URL | string }
/** Set of message types that are supported by the library */
export type MessageType = keyof proto.Message
export type MediaConnInfo = {
auth: string
ttl: number
hosts: {
hostname: string
}[]
fetchDate: Date
}
/** Reverse stub type dictionary */
export const WA_MESSAGE_STUB_TYPES = function () {
const types = WAMessageStubType
const dict: Record<number, string> = {}
Object.keys(types).forEach(element => dict[ types[element] ] = element)
return dict
}()
export interface WAUrlInfo {
'canonical-url': string
'matched-text': string
title: string
description: string
jpegThumbnail?: Buffer
}
// types to generate WA messages
type Mentionable = {
/** list of jids that are mentioned in the accompanying text */
mentions?: string[]
}
export type MediaType = 'image' | 'video' | 'sticker' | 'audio' | 'document'
export type AnyMediaMessageContent = (
({
image: WAMediaUpload
caption?: string
jpegThumbnail?: string
} & Mentionable) |
({
video: WAMediaUpload
caption?: string
gifPlayback?: boolean
jpegThumbnail?: string
} & Mentionable) | {
audio: WAMediaUpload
/** if set to true, will send as a `voice note` */
pttAudio?: boolean
/** optionally tell the duration of the audio */
seconds?: number
} | {
sticker: WAMediaUpload
} | {
document: WAMediaUpload
mimetype: string
fileName?: string
}) &
{ mimetype?: string }
export type AnyRegularMessageContent =
string |
({
text: string
}
& Mentionable) |
AnyMediaMessageContent |
{
contacts: {
displayName?: string
contacts: WAContactMessage[]
}
} |
{
location: WALocationMessage
}
export type AnyMessageContent = AnyRegularMessageContent | {
forward: WAMessage
force?: boolean
} | {
delete: WAMessageKey
} | {
disappearingMessagesInChat: boolean | number
}
export type MiscMessageGenerationOptions = {
/** Force message id */
messageId?: string
/** optional, if you want to manually set the timestamp of the message */
timestamp?: Date
/** the message you want to quote */
quoted?: WAMessage
}
export type MessageGenerationOptionsFromContent = MiscMessageGenerationOptions & {
userJid: string
ephemeralOptions?: {
expiration: number | string
eph_setting_ts: number | string
}
}
export type MediaGenerationOptions = {
logger?: Logger
agent?: Agent
getMediaOptions: (refresh: boolean) => Promise<MediaConnInfo>
}
export type MessageContentGenerationOptions = MediaGenerationOptions & {
getUrlInfo?: (text: string) => Promise<WAUrlInfo>
}
export type MessageGenerationOptions = MessageContentGenerationOptions & MessageGenerationOptionsFromContent
export type MessageUpdateType = 'prepend' | 'append' | 'notify' | 'last'
export interface MessageInfo {
reads: {jid: string, t: string}[]
deliveries: {jid: string, t: string}[]
}

View File

@@ -3,15 +3,21 @@ export * from './GroupMetadata'
export * from './Chat'
export * from './Contact'
export * from './Store'
export * from './Message'
import type EventEmitter from "events"
import type { Agent } from "https"
import type { Logger } from "pino"
import type { URL } from "url"
import type BinaryNode from "../BinaryNode"
import { AnyAuthenticationCredentials } from './Auth'
import { AnyAuthenticationCredentials, AuthenticationCredentials } from './Auth'
import { Chat } from './Chat'
import { Contact } from './Contact'
import { ConnectionState } from './Store'
import { GroupMetadata, ParticipantAction } from './GroupMetadata'
import { MessageUpdateType, WAMessage } from './Message'
/** used for binary messages */
export enum WAMetric {
debugLog = 1,
@@ -117,7 +123,7 @@ export type SocketQueryOptions = SocketSendMessageOptions & {
}
export enum DisconnectReason {
connectionClosedIntentionally = 428,
connectionClosed = 428,
connectionReplaced = 440,
connectionLost = 408,
timedOut = 408,
@@ -131,6 +137,35 @@ export type WAInitResponse = {
status: 200
}
export interface WABroadcastListInfo {
status: number
name: string
recipients?: {id: string}[]
}
type WABusinessHoursConfig = {
day_of_week: string
mode: string
open_time?: number
close_time?: number
}
export type WABusinessProfile = {
description: string
email: string
business_hours: {
timezone: string
config?: WABusinessHoursConfig[]
business_config?: WABusinessHoursConfig[]
}
website: string[]
categories: {
id: string
localized_display_name: string
}[]
wid?: string
}
export type QueryOptions = SocketQueryOptions & {
waitForOpen?: boolean
maxRetries?: number
@@ -140,6 +175,23 @@ export type CurveKeyPair = { private: Uint8Array; public: Uint8Array }
export type BaileysEventMap = {
'connection.update': Partial<ConnectionState>
'credentials.update': AuthenticationCredentials
'chats.upsert': { chats: Chat[], type: 'set' | 'upsert' }
'chats.update': Partial<Chat>[]
'chats.delete': string[]
'contacts.upsert': { contacts: Contact[], type: 'set' | 'upsert' }
'contacts.update': Partial<Contact>[]
'messages.delete': { jid: string, ids: string[] } | { jid: string, all: true }
'messages.update': Partial<WAMessage>[]
'messages.upsert': { messages: WAMessage[], type: MessageUpdateType }
'groups.update': Partial<GroupMetadata>[]
'group-participants.update': { jid: string, participants: string[], action: ParticipantAction }
'blocklist.update': { blocklist: string[], type: 'add' | 'remove' | 'set' }
}
export interface BaileysEventEmitter extends EventEmitter {
on<T extends keyof BaileysEventMap>(event: T, listener: (arg: BaileysEventMap[T]) => void): this