feat: add "strictNullChecks"

This commit is contained in:
Adhiraj Singh
2022-07-08 10:38:25 +05:30
parent 7426b7aa2f
commit 40a1e268aa
42 changed files with 350 additions and 339 deletions

View File

@@ -27,7 +27,7 @@ const makeAuthSocket = (config: LegacySocketConfig) => {
const socket = makeSocket(config)
const { ws } = socket
let curveKeys: CurveKeyPair
let initTimeout: NodeJS.Timeout
let initTimeout: NodeJS.Timeout | undefined
ws.on('phone-connection', ({ value: phoneConnected }) => {
updateState({ legacy: { ...state.legacy, phoneConnected } })
@@ -133,7 +133,7 @@ const makeAuthSocket = (config: LegacySocketConfig) => {
generateKeysForAuth(ref, ttl)
}
})()
let loginTag: string
let loginTag: string | undefined
if(canDoLogin) {
updateEncKeys()
// if we have the info to restore a closed session

View File

@@ -114,9 +114,9 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
{
tag: 'read',
attrs: {
jid: fromMessage.remoteJid,
jid: fromMessage.remoteJid!,
count: count.toString(),
index: fromMessage.id,
index: fromMessage.id!,
owner: fromMessage.fromMe ? 'true' : 'false'
}
}
@@ -124,7 +124,7 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
[ WAMetric.read, WAFlag.ignore ]
)
if(config.emitOwnEvents) {
ev.emit('chats.update', [{ id: fromMessage.remoteJid, unreadCount: count < 0 ? -1 : 0 }])
ev.emit('chats.update', [{ id: fromMessage.remoteJid!, unreadCount: count < 0 ? -1 : 0 }])
}
}
@@ -225,10 +225,11 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
})
// User Profile Name Updates
socketEvents.on('CB:Conn,pushname', json => {
const { legacy: { user }, connection } = state
if(connection === 'open' && json[1].pushname !== user.name) {
user.name = json[1].pushname
ev.emit('connection.update', { legacy: { ...state.legacy, user } })
const { legacy, connection } = state
const { user } = legacy!
if(connection === 'open' && json[1].pushname !== user!.name) {
user!.name = json[1].pushname
ev.emit('connection.update', { legacy: { ...legacy!, user } })
}
})
// read updates
@@ -354,7 +355,7 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
}
))
} else if('markRead' in modification) {
const indexKey = getIndexKey(modification.lastMessages)
const indexKey = getIndexKey(modification.lastMessages)!
return chatRead(indexKey, modification.markRead ? 0 : -1)
} else if('delete' in modification) {
chatAttrs.type = 'delete'
@@ -363,7 +364,7 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
if('lastMessages' in modification) {
const indexKey = getIndexKey(modification.lastMessages)
if(indexKey) {
chatAttrs.index = indexKey.id
chatAttrs.index = indexKey.id!
chatAttrs.owner = indexKey.fromMe ? 'true' : 'false'
}
}
@@ -409,7 +410,7 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
content: [
{
tag: 'presence',
attrs: { type: type, to: toJid }
attrs: { type: type, to: toJid! }
}
]
}
@@ -463,7 +464,7 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
if(config.emitOwnEvents) {
const user = { ...state.legacy!.user!, name }
ev.emit('connection.update', { legacy: {
...state.legacy, user
...state.legacy!, user
} })
ev.emit('contacts.update', [{ id: user.id, name }])
}
@@ -493,11 +494,11 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
const { eurl } = await this.setQuery ([query], [WAMetric.picture, 136], tag) as { eurl: string, status: number }
if(config.emitOwnEvents) {
if(jid === user.id) {
if(jid === user?.id) {
user.imgUrl = eurl
ev.emit('connection.update', {
legacy: {
...state.legacy,
...state.legacy!,
user
}
})

View File

@@ -23,11 +23,11 @@ const makeGroupsSocket = (config: LegacySocketConfig) => {
{
tag: 'group',
attrs: {
author: state.legacy?.user?.id,
author: state.legacy?.user?.id!,
id: tag,
type: type,
jid: jid,
subject: subject,
jid: jid!,
subject: subject!,
},
content: participants ?
participants.map(jid => (
@@ -96,7 +96,7 @@ const makeGroupsSocket = (config: LegacySocketConfig) => {
id: jid,
owner: attrs?.creator,
creation: +attrs?.create,
subject: null,
subject: '',
desc,
participants
}
@@ -146,8 +146,8 @@ const makeGroupsSocket = (config: LegacySocketConfig) => {
* @param participants people to include in the group
*/
groupCreate: async(title: string, participants: string[]) => {
const response = await groupQuery('create', null, title, participants) as WAGroupCreateResponse
const gid = response.gid
const response = await groupQuery('create', undefined, title, participants) as WAGroupCreateResponse
const gid = response.gid!
let metadata: GroupMetadata
try {
metadata = await groupMetadataFull(gid)
@@ -199,11 +199,11 @@ const makeGroupsSocket = (config: LegacySocketConfig) => {
const metadata = await groupMetadataFull(jid)
const node: BinaryNode = {
tag: 'description',
attrs: { id: generateMessageID(), prev: metadata?.descId },
attrs: { id: generateMessageID(), prev: metadata?.descId! },
content: Buffer.from(description, 'utf-8')
}
const response = await groupQuery ('description', jid, null, null, [node])
const response = await groupQuery('description', jid, undefined, undefined, [node])
ev.emit('groups.update', [ { id: jid, desc: description } ])
return response
},
@@ -213,7 +213,7 @@ const makeGroupsSocket = (config: LegacySocketConfig) => {
* @param participants the people to add
*/
groupParticipantsUpdate: async(id: string, participants: string[], action: ParticipantAction) => {
const result: GroupModificationResponse = await groupQuery(action, id, null, participants)
const result: GroupModificationResponse = await groupQuery(action, id, undefined, participants)
const jids = Object.keys(result.participants || {})
ev.emit('group-participants.update', { id, participants: jids, action })
return Object.keys(result.participants || {}).map(
@@ -237,7 +237,6 @@ const makeGroupsSocket = (config: LegacySocketConfig) => {
const metadata: GroupMetadata = {
subject: result.name,
id: jid,
creation: undefined,
owner: state.legacy?.user?.id,
participants: result.recipients!.map(({ id }) => (
{ id: jidNormalizedUser(id), isAdmin: false, isSuperAdmin: false }

View File

@@ -47,7 +47,7 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
count: number,
cursor?: WAMessageCursor
) => {
let key: WAMessageKey
let key: WAMessageKey | undefined
if(cursor) {
key = 'before' in cursor ? cursor.before : cursor.after
}
@@ -61,7 +61,7 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
jid: jid,
kind: !cursor || 'before' in cursor ? 'before' : 'after',
count: count.toString(),
index: key?.id,
index: key?.id!,
owner: key?.fromMe === false ? 'false' : 'true',
}
},
@@ -84,9 +84,9 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
tag: 'query',
attrs: {
type: 'media',
index: message.key.id,
index: message.key.id!,
owner: message.key.fromMe ? 'true' : 'false',
jid: message.key.remoteJid,
jid: message.key.remoteJid!,
epoch: currentEpoch().toString()
}
},
@@ -158,7 +158,7 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
{
// the key of the deleted message is updated
update: { message: null, key: message.key, messageStubType },
key
key: key!
}
])
return
@@ -167,7 +167,7 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
chatUpdate.ephemeralExpiration = protocolMessage.ephemeralExpiration
if(isJidGroup(jid)) {
emitGroupUpdate({ ephemeralDuration: protocolMessage.ephemeralExpiration || null })
emitGroupUpdate({ ephemeralDuration: protocolMessage.ephemeralExpiration || 0 })
}
break
@@ -188,18 +188,18 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
switch (message.messageStubType) {
case WAMessageStubType.CHANGE_EPHEMERAL_SETTING:
chatUpdate.ephemeralSettingTimestamp = message.messageTimestamp
chatUpdate.ephemeralExpiration = +message.messageStubParameters[0]
chatUpdate.ephemeralExpiration = +message.messageStubParameters![0]
if(isJidGroup(jid)) {
emitGroupUpdate({ ephemeralDuration: +message.messageStubParameters[0] || null })
emitGroupUpdate({ ephemeralDuration: +(message.messageStubParameters?.[0] || 0) })
}
break
case WAMessageStubType.GROUP_PARTICIPANT_LEAVE:
case WAMessageStubType.GROUP_PARTICIPANT_REMOVE:
participants = message.messageStubParameters.map (jidNormalizedUser)
participants = message.messageStubParameters!.map (jidNormalizedUser)
emitParticipantsUpdate('remove')
// mark the chat read only if you left the group
if(participants.includes(user.id)) {
if(participants.includes(user!.id)) {
chatUpdate.readOnly = true
}
@@ -207,24 +207,24 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
case WAMessageStubType.GROUP_PARTICIPANT_ADD:
case WAMessageStubType.GROUP_PARTICIPANT_INVITE:
case WAMessageStubType.GROUP_PARTICIPANT_ADD_REQUEST_JOIN:
participants = message.messageStubParameters.map (jidNormalizedUser)
if(participants.includes(user.id)) {
participants = message.messageStubParameters!.map (jidNormalizedUser)
if(participants.includes(user!.id)) {
chatUpdate.readOnly = null
}
emitParticipantsUpdate('add')
break
case WAMessageStubType.GROUP_CHANGE_ANNOUNCE:
const announce = message.messageStubParameters[0] === 'on'
const announce = message.messageStubParameters?.[0] === 'on'
emitGroupUpdate({ announce })
break
case WAMessageStubType.GROUP_CHANGE_RESTRICT:
const restrict = message.messageStubParameters[0] === 'on'
const restrict = message.messageStubParameters?.[0] === 'on'
emitGroupUpdate({ restrict })
break
case WAMessageStubType.GROUP_CHANGE_SUBJECT:
case WAMessageStubType.GROUP_CREATE:
chatUpdate.name = message.messageStubParameters[0]
chatUpdate.name = message.messageStubParameters?.[0]
emitGroupUpdate({ subject: chatUpdate.name })
break
}
@@ -275,9 +275,9 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
}
]
}
const isMsgToMe = areJidsSameUser(message.key.remoteJid, state.legacy.user?.id || '')
const isMsgToMe = areJidsSameUser(message.key.remoteJid!, state.legacy?.user?.id || '')
const flag = isMsgToMe ? WAFlag.acknowledge : WAFlag.ignore // acknowledge when sending message to oneself
const mID = message.key.id
const mID = message.key.id!
const finalState = isMsgToMe ? WAMessageStatus.READ : WAMessageStatus.SERVER_ACK
message.status = WAMessageStatus.PENDING
@@ -498,7 +498,7 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
search: txt,
count: count.toString(),
page: page.toString(),
jid: inJid
jid: inJid!
}
},
binaryTag: [24, WAFlag.ignore],
@@ -515,7 +515,7 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
content: AnyMessageContent,
options: MiscMessageGenerationOptions & { waitForAck?: boolean } = { waitForAck: true }
) => {
const userJid = state.legacy.user?.id
const userJid = state.legacy?.user?.id
if(
typeof content === 'object' &&
'disappearingMessagesInChat' in content &&
@@ -530,7 +530,7 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
await setQuery([
{
tag: 'group',
attrs: { id: tag, jid, type: 'prop', author: userJid },
attrs: { id: tag, jid, type: 'prop', author: userJid! },
content: [
{ tag: 'ephemeral', attrs: { value: value.toString() } }
]
@@ -542,7 +542,7 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
content,
{
logger,
userJid: userJid,
userJid: userJid!,
getUrlInfo: generateUrlInfo,
upload: waUploadToServer,
mediaCache: config.mediaCache,
@@ -550,7 +550,7 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
}
)
await relayMessage(msg, { waitForAck: options.waitForAck })
await relayMessage(msg, { waitForAck: !!options.waitForAck })
return msg
}
}

View File

@@ -43,7 +43,7 @@ export const makeSocket = ({
let authInfo: { encKey: Buffer, macKey: Buffer }
let keepAliveReq: NodeJS.Timeout
let phoneCheckInterval: NodeJS.Timeout
let phoneCheckInterval: NodeJS.Timeout | undefined
let phoneCheckListeners = 0
const phoneConnectionChanged = (value: boolean) => {
@@ -267,10 +267,10 @@ export const makeSocket = ({
return result as any
} finally {
requiresPhoneConnection && clearPhoneCheckInterval()
cancelPhoneChecker && cancelPhoneChecker()
cancelPhoneChecker! && cancelPhoneChecker!()
ws.off(`TAG:${tag}`, onRecv)
ws.off('ws-close', onErr) // if the socket closes, you'll never receive the message
ws.off(`TAG:${tag}`, onRecv!)
ws.off('ws-close', onErr!) // if the socket closes, you'll never receive the message
}
})(),
cancelToken: () => {
@@ -290,7 +290,7 @@ export const makeSocket = ({
{ json, timeoutMs, expect200, tag, longTag, binaryTag, requiresPhoneConnection }: SocketQueryOptions
) => {
tag = tag || generateMessageTag(longTag)
const { promise, cancelToken } = waitForMessage(tag, requiresPhoneConnection, timeoutMs)
const { promise, cancelToken } = waitForMessage(tag, !!requiresPhoneConnection, timeoutMs)
try {
await sendNode({ json, tag, binaryTag })
} catch(error) {