feat: handle unarchiveChats setting

This commit is contained in:
Adhiraj Singh
2022-04-07 11:29:57 +05:30
parent ebf4aa6772
commit 0d94315776
5 changed files with 28 additions and 11 deletions

View File

@@ -145,7 +145,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
// process message and emit events // process message and emit events
const newEvents = await processMessage( const newEvents = await processMessage(
msg, msg,
{ historyCache, meId, keyStore: authState.keys, logger, treatCiphertextMessagesAsReal } { historyCache, meId, accountSettings: authState.creds.accountSettings, keyStore: authState.keys, logger, treatCiphertextMessagesAsReal }
) )
// send ack for history message // send ack for history message

View File

@@ -27,6 +27,11 @@ export type SignalCreds = {
readonly registrationId: number readonly registrationId: number
} }
export type AccountSettings = {
/** unarchive chats when a new message is received */
unarchiveChats: boolean
}
export type AuthenticationCreds = SignalCreds & { export type AuthenticationCreds = SignalCreds & {
readonly noiseKey: KeyPair readonly noiseKey: KeyPair
readonly advSecretKey: string readonly advSecretKey: string
@@ -40,6 +45,7 @@ export type AuthenticationCreds = SignalCreds & {
nextPreKeyId: number nextPreKeyId: number
lastAccountSyncTimestamp?: number lastAccountSyncTimestamp?: number
accountSettings: AccountSettings
} }
export type SignalDataTypeMap = { export type SignalDataTypeMap = {

View File

@@ -108,7 +108,10 @@ export const initAuthCreds = (): AuthenticationCreds => {
nextPreKeyId: 1, nextPreKeyId: 1,
firstUnuploadedPreKeyId: 1, firstUnuploadedPreKeyId: 1,
serverHasPreKeys: false serverHasPreKeys: false,
accountSettings: {
unarchiveChats: false
}
} }
} }

View File

@@ -583,11 +583,13 @@ export const processSyncActions = (
name: action.contactAction!.fullName name: action.contactAction!.fullName
} }
} else if(action?.pushNameSetting) { } else if(action?.pushNameSetting) {
map['creds.update'] = { map['creds.update'] = map['creds.update'] || { }
me: { ...me, name: action?.pushNameSetting?.name! } map['creds.update'].me = { ...me, name: action?.pushNameSetting?.name! }
}
} else if(action?.pinAction) { } else if(action?.pinAction) {
update.pin = action.pinAction?.pinned ? toNumber(action.timestamp) : undefined update.pin = action.pinAction?.pinned ? toNumber(action.timestamp) : undefined
} else if(action?.unarchiveChatsSetting) {
map['creds.update'] = map['creds.update'] || { }
map['creds.update'].accountSettings = { unarchiveChats: !!action.unarchiveChatsSetting.unarchiveChats }
} else { } else {
logger.warn({ action, id }, 'unprocessable update') logger.warn({ action, id }, 'unprocessable update')
} }

View File

@@ -1,20 +1,21 @@
import type { Logger } from 'pino' import type { Logger } from 'pino'
import { proto } from '../../WAProto' import { proto } from '../../WAProto'
import { BaileysEventMap, Chat, GroupMetadata, ParticipantAction, SignalKeyStoreWithTransaction, WAMessageStubType } from '../Types' import { AccountSettings, BaileysEventMap, Chat, GroupMetadata, ParticipantAction, SignalKeyStoreWithTransaction, WAMessageStubType } from '../Types'
import { downloadAndProcessHistorySyncNotification, normalizeMessageContent, toNumber } from '../Utils' import { downloadAndProcessHistorySyncNotification, normalizeMessageContent, toNumber } from '../Utils'
import { areJidsSameUser, jidNormalizedUser } from '../WABinary' import { areJidsSameUser, jidNormalizedUser } from '../WABinary'
type ProcessMessageContext = { type ProcessMessageContext = {
historyCache: Set<string>, historyCache: Set<string>
meId: string, meId: string
keyStore: SignalKeyStoreWithTransaction, keyStore: SignalKeyStoreWithTransaction
accountSettings: AccountSettings
logger?: Logger logger?: Logger
treatCiphertextMessagesAsReal?: boolean treatCiphertextMessagesAsReal?: boolean
} }
const processMessage = async( const processMessage = async(
message: proto.IWebMessageInfo, message: proto.IWebMessageInfo,
{ historyCache, meId, keyStore, logger, treatCiphertextMessagesAsReal }: ProcessMessageContext { historyCache, meId, keyStore, accountSettings, logger, treatCiphertextMessagesAsReal }: ProcessMessageContext
) => { ) => {
const map: Partial<BaileysEventMap<any>> = { } const map: Partial<BaileysEventMap<any>> = { }
@@ -30,9 +31,14 @@ const processMessage = async(
&& !normalizedContent?.reactionMessage && !normalizedContent?.reactionMessage
) { ) {
chat.conversationTimestamp = toNumber(message.messageTimestamp) chat.conversationTimestamp = toNumber(message.messageTimestamp)
if(!message.key.fromMe) { // only increment unread count if not CIPHERTEXT and from another person
if(!message.key.fromMe && !!message.messageStubType) {
chat.unreadCount = (chat.unreadCount || 0) + 1 chat.unreadCount = (chat.unreadCount || 0) + 1
} }
if(accountSettings?.unarchiveChats) {
chat.archive = false
}
} }
const content = normalizeMessageContent(message.message) const content = normalizeMessageContent(message.message)