refactor: use constant state & authInfo on legacy

This commit is contained in:
Adhiraj Singh
2021-12-18 19:45:58 +05:30
parent 1e97c7714e
commit 37c07937d7
4 changed files with 18 additions and 18 deletions

View File

@@ -258,9 +258,9 @@ const makeAuthSocket = (config: LegacySocketConfig) => {
return { return {
...socket, ...socket,
state,
authInfo,
ev, ev,
getState: () => state,
getAuthInfo: () => authInfo,
waitForConnection, waitForConnection,
canLogin, canLogin,
logout logout

View File

@@ -13,7 +13,7 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
setQuery, setQuery,
query, query,
sendNode, sendNode,
getState state
} = sock } = sock
const chatsDebounceTimeout = debouncedTimeout(10_000, () => sendChatsQuery(1)) const chatsDebounceTimeout = debouncedTimeout(10_000, () => sendChatsQuery(1))
@@ -212,10 +212,10 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
}) })
// User Profile Name Updates // User Profile Name Updates
socketEvents.on('CB:Conn,pushname', json => { socketEvents.on('CB:Conn,pushname', json => {
const { legacy: { user }, connection } = getState() const { legacy: { user }, connection } = state
if(connection === 'open' && json[1].pushname !== user.name) { if(connection === 'open' && json[1].pushname !== user.name) {
user.name = json[1].pushname user.name = json[1].pushname
ev.emit('connection.update', { legacy: { ...getState().legacy, user } }) ev.emit('connection.update', { legacy: { ...state.legacy, user } })
} }
}) })
// read updates // read updates
@@ -397,7 +397,7 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
} }
] ]
) )
ev.emit('contacts.update', [{ id: getState().legacy!.user!.id, status }]) ev.emit('contacts.update', [{ id: state.legacy!.user!.id, status }])
return response return response
}, },
/** Updates business profile. */ /** Updates business profile. */
@@ -420,9 +420,9 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
)) as any as {status: number, pushname: string} )) as any as {status: number, pushname: string}
if(config.emitOwnEvents) { if(config.emitOwnEvents) {
const user = { ...getState().legacy!.user!, name } const user = { ...state.legacy!.user!, name }
ev.emit('connection.update', { legacy: { ev.emit('connection.update', { legacy: {
...getState().legacy, user ...state.legacy, user
} }) } })
ev.emit('contacts.update', [{ id: user.id, name }]) ev.emit('contacts.update', [{ id: user.id, name }])
} }
@@ -446,7 +446,7 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
] ]
} }
const user = getState().legacy?.user const user = state.legacy?.user
const { eurl } = await this.setQuery ([query], [WAMetric.picture, 136], tag) as { eurl: string, status: number } const { eurl } = await this.setQuery ([query], [WAMetric.picture, 136], tag) as { eurl: string, status: number }
if(config.emitOwnEvents) { if(config.emitOwnEvents) {
@@ -454,7 +454,7 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
user.imgUrl = eurl user.imgUrl = eurl
ev.emit('connection.update', { ev.emit('connection.update', {
legacy: { legacy: {
...getState().legacy, ...state.legacy,
user user
} }
}) })

View File

@@ -13,7 +13,7 @@ const makeGroupsSocket = (config: LegacySocketConfig) => {
generateMessageTag, generateMessageTag,
currentEpoch, currentEpoch,
setQuery, setQuery,
getState state
} = sock } = sock
/** Generic function for group queries */ /** Generic function for group queries */
@@ -23,7 +23,7 @@ const makeGroupsSocket = (config: LegacySocketConfig) => {
{ {
tag: 'group', tag: 'group',
attrs: { attrs: {
author: getState().legacy?.user?.id, author: state.legacy?.user?.id,
id: tag, id: tag,
type: type, type: type,
jid: jid, jid: jid,
@@ -217,7 +217,7 @@ const makeGroupsSocket = (config: LegacySocketConfig) => {
subject: result.name, subject: result.name,
id: jid, id: jid,
creation: undefined, creation: undefined,
owner: getState().legacy?.user?.id, owner: state.legacy?.user?.id,
participants: result.recipients!.map(({ id }) => ( participants: result.recipients!.map(({ id }) => (
{ id: jidNormalizedUser(id), isAdmin: false, isSuperAdmin: false } { id: jidNormalizedUser(id), isAdmin: false, isSuperAdmin: false }
)) ))

View File

@@ -22,7 +22,7 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
generateMessageTag, generateMessageTag,
currentEpoch, currentEpoch,
setQuery, setQuery,
getState state
} = sock } = sock
let mediaConn: Promise<MediaConnInfo> let mediaConn: Promise<MediaConnInfo>
@@ -161,7 +161,7 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
// check if the message is an action // check if the message is an action
if (message.messageStubType) { if (message.messageStubType) {
const { user } = getState().legacy! const { user } = state.legacy!
//let actor = jidNormalizedUser (message.participant) //let actor = jidNormalizedUser (message.participant)
let participants: string[] let participants: string[]
const emitParticipantsUpdate = (action: ParticipantAction) => ( const emitParticipantsUpdate = (action: ParticipantAction) => (
@@ -257,7 +257,7 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
} }
] ]
} }
const isMsgToMe = areJidsSameUser(message.key.remoteJid, getState().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 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 const finalState = isMsgToMe ? WAMessageStatus.READ : WAMessageStatus.SERVER_ACK
@@ -353,7 +353,7 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
} }
const keyPartial = { const keyPartial = {
remoteJid: jidNormalizedUser(attributes.to), remoteJid: jidNormalizedUser(attributes.to),
fromMe: areJidsSameUser(attributes.from, getState().legacy?.user?.id || ''), fromMe: areJidsSameUser(attributes.from, state.legacy?.user?.id || ''),
} }
const updates = ids.map<MessageInfoUpdate>(id => ({ const updates = ids.map<MessageInfoUpdate>(id => ({
key: { ...keyPartial, id }, key: { ...keyPartial, id },
@@ -491,7 +491,7 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
content: AnyMessageContent, content: AnyMessageContent,
options: MiscMessageGenerationOptions & { waitForAck?: boolean } = { waitForAck: true } options: MiscMessageGenerationOptions & { waitForAck?: boolean } = { waitForAck: true }
) => { ) => {
const userJid = getState().legacy.user?.id const userJid = state.legacy.user?.id
if( if(
typeof content === 'object' && typeof content === 'object' &&
'disappearingMessagesInChat' in content && 'disappearingMessagesInChat' in content &&