chore: format everything

This commit is contained in:
canove
2025-05-06 12:10:19 -03:00
parent 04afa20244
commit fa706d0b50
76 changed files with 8241 additions and 7142 deletions

View File

@@ -1,7 +1,17 @@
import { Boom } from '@hapi/boom'
import { proto } from '../../WAProto'
import { SignalRepository, WAMessageKey } from '../Types'
import { areJidsSameUser, BinaryNode, isJidBroadcast, isJidGroup, isJidMetaIa, isJidNewsletter, isJidStatusBroadcast, isJidUser, isLidUser } from '../WABinary'
import {
areJidsSameUser,
BinaryNode,
isJidBroadcast,
isJidGroup,
isJidMetaIa,
isJidNewsletter,
isJidStatusBroadcast,
isJidUser,
isLidUser
} from '../WABinary'
import { unpadRandomMax16 } from './generics'
import { ILogger } from './logger'
@@ -24,17 +34,20 @@ export const NACK_REASONS = {
DBOperationFailed: 552
}
type MessageType = 'chat' | 'peer_broadcast' | 'other_broadcast' | 'group' | 'direct_peer_status' | 'other_status' | 'newsletter'
type MessageType =
| 'chat'
| 'peer_broadcast'
| 'other_broadcast'
| 'group'
| 'direct_peer_status'
| 'other_status'
| 'newsletter'
/**
* Decode the received node as a message.
* @note this will only parse the message, not decrypt it
*/
export function decodeMessageNode(
stanza: BinaryNode,
meId: string,
meLid: string
) {
export function decodeMessageNode(stanza: BinaryNode, meId: string, meLid: string) {
let msgType: MessageType
let chatId: string
let author: string
@@ -47,9 +60,9 @@ export function decodeMessageNode(
const isMe = (jid: string) => areJidsSameUser(jid, meId)
const isMeLid = (jid: string) => areJidsSameUser(jid, meLid)
if(isJidUser(from) || isLidUser(from)) {
if(recipient && !isJidMetaIa(recipient)) {
if(!isMe(from) && !isMeLid(from)) {
if (isJidUser(from) || isLidUser(from)) {
if (recipient && !isJidMetaIa(recipient)) {
if (!isMe(from) && !isMeLid(from)) {
throw new Boom('receipient present, but msg not from me', { data: stanza })
}
@@ -60,21 +73,21 @@ export function decodeMessageNode(
msgType = 'chat'
author = from
} else if(isJidGroup(from)) {
if(!participant) {
} else if (isJidGroup(from)) {
if (!participant) {
throw new Boom('No participant in group message')
}
msgType = 'group'
author = participant
chatId = from
} else if(isJidBroadcast(from)) {
if(!participant) {
} else if (isJidBroadcast(from)) {
if (!participant) {
throw new Boom('No participant in group message')
}
const isParticipantMe = isMe(participant)
if(isJidStatusBroadcast(from)) {
if (isJidStatusBroadcast(from)) {
msgType = isParticipantMe ? 'direct_peer_status' : 'other_status'
} else {
msgType = isParticipantMe ? 'peer_broadcast' : 'other_broadcast'
@@ -82,7 +95,7 @@ export function decodeMessageNode(
chatId = from
author = participant
} else if(isJidNewsletter(from)) {
} else if (isJidNewsletter(from)) {
msgType = 'newsletter'
chatId = from
author = from
@@ -107,7 +120,7 @@ export function decodeMessageNode(
broadcast: isJidBroadcast(from)
}
if(key.fromMe) {
if (key.fromMe) {
fullMessage.status = proto.WebMessageInfo.Status.SERVER_ACK
}
@@ -132,19 +145,19 @@ export const decryptMessageNode = (
author,
async decrypt() {
let decryptables = 0
if(Array.isArray(stanza.content)) {
for(const { tag, attrs, content } of stanza.content) {
if(tag === 'verified_name' && content instanceof Uint8Array) {
if (Array.isArray(stanza.content)) {
for (const { tag, attrs, content } of stanza.content) {
if (tag === 'verified_name' && content instanceof Uint8Array) {
const cert = proto.VerifiedNameCertificate.decode(content)
const details = proto.VerifiedNameCertificate.Details.decode(cert.details!)
fullMessage.verifiedBizName = details.verifiedName
}
if(tag !== 'enc' && tag !== 'plaintext') {
if (tag !== 'enc' && tag !== 'plaintext') {
continue
}
if(!(content instanceof Uint8Array)) {
if (!(content instanceof Uint8Array)) {
continue
}
@@ -155,53 +168,52 @@ export const decryptMessageNode = (
try {
const e2eType = tag === 'plaintext' ? 'plaintext' : attrs.type
switch (e2eType) {
case 'skmsg':
msgBuffer = await repository.decryptGroupMessage({
group: sender,
authorJid: author,
msg: content
})
break
case 'pkmsg':
case 'msg':
const user = isJidUser(sender) ? sender : author
msgBuffer = await repository.decryptMessage({
jid: user,
type: e2eType,
ciphertext: content
})
break
case 'plaintext':
msgBuffer = content
break
default:
throw new Error(`Unknown e2e type: ${e2eType}`)
case 'skmsg':
msgBuffer = await repository.decryptGroupMessage({
group: sender,
authorJid: author,
msg: content
})
break
case 'pkmsg':
case 'msg':
const user = isJidUser(sender) ? sender : author
msgBuffer = await repository.decryptMessage({
jid: user,
type: e2eType,
ciphertext: content
})
break
case 'plaintext':
msgBuffer = content
break
default:
throw new Error(`Unknown e2e type: ${e2eType}`)
}
let msg: proto.IMessage = proto.Message.decode(e2eType !== 'plaintext' ? unpadRandomMax16(msgBuffer) : msgBuffer)
let msg: proto.IMessage = proto.Message.decode(
e2eType !== 'plaintext' ? unpadRandomMax16(msgBuffer) : msgBuffer
)
msg = msg.deviceSentMessage?.message || msg
if(msg.senderKeyDistributionMessage) {
if (msg.senderKeyDistributionMessage) {
//eslint-disable-next-line max-depth
try {
try {
await repository.processSenderKeyDistributionMessage({
authorJid: author,
item: msg.senderKeyDistributionMessage
})
} catch(err) {
} catch (err) {
logger.error({ key: fullMessage.key, err }, 'failed to decrypt message')
}
}
}
if(fullMessage.message) {
if (fullMessage.message) {
Object.assign(fullMessage.message, msg)
} else {
fullMessage.message = msg
}
} catch(err) {
logger.error(
{ key: fullMessage.key, err },
'failed to decrypt message'
)
} catch (err) {
logger.error({ key: fullMessage.key, err }, 'failed to decrypt message')
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT
fullMessage.messageStubParameters = [err.message]
}
@@ -209,7 +221,7 @@ export const decryptMessageNode = (
}
// if nothing was found to decrypt
if(!decryptables) {
if (!decryptables) {
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT
fullMessage.messageStubParameters = [NO_MESSAGE_FOUND_ERROR_TEXT]
}