chore: more unification of APIs

This commit is contained in:
Adhiraj Singh
2021-12-18 17:01:16 +05:30
parent d09025e8dd
commit 1e97c7714e

View File

@@ -28,7 +28,7 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
}) })
) )
const fetchImageUrl = async(jid: string) => { const profilePictureUrl = async(jid: string) => {
const response = await query({ const response = await query({
json: ['query', 'ProfilePicThumb', jid], json: ['query', 'ProfilePicThumb', jid],
expect200: false, expect200: false,
@@ -63,7 +63,7 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
ev.emit('chats.update', [ { id: jid, archive: false } ]) ev.emit('chats.update', [ { id: jid, archive: false } ])
break break
case 'pin': case 'pin':
ev.emit('chats.update', [ { id: jid, pin: +attributes.pin } ]) ev.emit('chats.update', [ { id: jid, pin: attributes.pin ? +attributes.pin : null } ])
break break
case 'star': case 'star':
case 'unstar': case 'unstar':
@@ -104,6 +104,26 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
return { id, presences: { [participant]: presence } } return { id, presences: { [participant]: presence } }
} }
const chatRead = async(fromMessage: WAMessageKey, count: number) => {
await setQuery (
[
{
tag: 'read',
attrs: {
jid: fromMessage.remoteJid,
count: count.toString(),
index: fromMessage.id,
owner: fromMessage.fromMe ? 'true' : 'false'
}
}
],
[ WAMetric.read, WAFlag.ignore ]
)
if(config.emitOwnEvents) {
ev.emit('chats.update', [{ id: fromMessage.remoteJid, unreadCount: count < 0 ? -1 : 0 }])
}
}
ev.on('connection.update', async({ connection }) => { ev.on('connection.update', async({ connection }) => {
if(connection !== 'open') return if(connection !== 'open') return
try { try {
@@ -216,7 +236,7 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
socketEvents.on('CB:Cmd,type:picture', async json => { socketEvents.on('CB:Cmd,type:picture', async json => {
json = json[1] json = json[1]
const id = jidNormalizedUser(json.jid) const id = jidNormalizedUser(json.jid)
const imgUrl = await fetchImageUrl(id).catch(() => '') const imgUrl = await profilePictureUrl(id).catch(() => '')
ev.emit('contacts.update', [ { id, imgUrl } ]) ev.emit('contacts.update', [ { id, imgUrl } ])
}) })
@@ -254,25 +274,8 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
return { return {
...sock, ...sock,
sendChatsQuery, sendChatsQuery,
fetchImageUrl, profilePictureUrl,
chatRead: async(fromMessage: WAMessageKey, count: number) => { chatRead,
await setQuery (
[
{ tag: 'read',
attrs: {
jid: fromMessage.remoteJid,
count: count.toString(),
index: fromMessage.id,
owner: fromMessage.fromMe ? 'true' : 'false'
}
}
],
[ WAMetric.read, WAFlag.ignore ]
)
if(config.emitOwnEvents) {
ev.emit ('chats.update', [{ id: fromMessage.remoteJid, unreadCount: count < 0 ? -1 : 0 }])
}
},
/** /**
* Modify a given chat (archive, pin etc.) * Modify a given chat (archive, pin etc.)
* @param jid the ID of the person/group you are modifiying * @param jid the ID of the person/group you are modifiying
@@ -300,7 +303,7 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
} }
} else if('clear' in modification) { } else if('clear' in modification) {
chatAttrs.type = 'clear' chatAttrs.type = 'clear'
chatAttrs.modify_tag = Math.round(Math.random ()*1000000).toString() chatAttrs.modify_tag = Math.round(Math.random()*1000000).toString()
if(modification.clear !== 'all') { if(modification.clear !== 'all') {
data = modification.clear.messages.map(({ id, fromMe }) => ( data = modification.clear.messages.map(({ id, fromMe }) => (
{ {
@@ -317,6 +320,8 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
attrs: { owner: (!!fromMe).toString(), index: id } attrs: { owner: (!!fromMe).toString(), index: id }
} }
)) ))
} else if('markRead' in modification) {
return chatRead(index!, modification.markRead ? 0 : -1)
} }
if(index) { if(index) {
@@ -326,8 +331,10 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
const node = { tag: 'chat', attrs: chatAttrs, content: data } const node = { tag: 'chat', attrs: chatAttrs, content: data }
const response = await setQuery([node], [ WAMetric.chat, WAFlag.ignore ]) const response = await setQuery([node], [ WAMetric.chat, WAFlag.ignore ])
// apply it and emit events if(config.emitOwnEvents) {
executeChatModification(node) // apply it and emit events
executeChatModification(node)
}
return response return response
}, },
/** /**