Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions shared/constants/platform-specific/push.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ type DataNewMessage = DataCommon & {
convID?: string
t: string | number
m: string
username?: string
}
type DataNewMessageSilent2 = DataCommon & {
type: 'chat.newmessageSilent_2'
t: string | number
c?: string
m: string
username?: string
}
type DataFollow = DataCommon & {
type: 'follow'
Expand All @@ -43,6 +45,7 @@ type DataFollow = DataCommon & {
type DataChatExtension = DataCommon & {
type: 'chat.extension'
convID?: string
username?: string
}
type Data = DataReadMessage | DataNewMessage | DataNewMessageSilent2 | DataFollow | DataChatExtension

Expand Down Expand Up @@ -94,6 +97,7 @@ const normalizePush = (_n?: object): T.Push.PushNotification | undefined => {
? {
conversationIDKey: T.Chat.stringToConversationIDKey(data.convID),
membersType: anyToConversationMembersType(data.t),
recipientUsername: data.username,
type: 'chat.newmessage',
unboxPayload: data.m || '',
userInteraction,
Expand All @@ -106,6 +110,7 @@ const normalizePush = (_n?: object): T.Push.PushNotification | undefined => {
return {
conversationIDKey: T.Chat.stringToConversationIDKey(data.c),
membersType,
recipientUsername: data.username,
type: 'chat.newmessageSilent_2',
unboxPayload: data.m || '',
}
Expand All @@ -124,6 +129,7 @@ const normalizePush = (_n?: object): T.Push.PushNotification | undefined => {
return data.convID
? {
conversationIDKey: T.Chat.stringToConversationIDKey(data.convID),
recipientUsername: data.username,
type: 'chat.extension',
}
: undefined
Expand Down
44 changes: 41 additions & 3 deletions shared/constants/push.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,40 @@ export const useState_ = Z.createZustand<State>((set, get) => {
try {
logger.info('[Push]: ' + notification.type || 'unknown')

const switchToRecipientIfNeeded = async (recipientUsername?: string): Promise<boolean> => {
if (!recipientUsername) {
return true
}
const currentUsername = C.useCurrentUserState.getState().username
if (recipientUsername === currentUsername) {
return true
}

logger.info(`[Push] notification is for different user: ${recipientUsername} vs ${currentUsername}`)

const configuredAccounts = C.useConfigState.getState().configuredAccounts
const targetAccount = configuredAccounts.find(acc => acc.username === recipientUsername)

if (!targetAccount) {
logger.info(`[Push] recipient account not found in configured accounts`)
return false
}

if (!targetAccount.hasStoredSecret) {
logger.info(`[Push] recipient account has no stored secrets, cannot auto-login`)
return false
}

logger.info(`[Push] auto-switching to account ${targetAccount.username}`)
const setUserSwitching = C.useConfigState.getState().dispatch.setUserSwitching
const login = C.useConfigState.getState().dispatch.login
setUserSwitching(true)
login(targetAccount.username, '')
await C.timeoutPromise(1500)

return true
}

switch (notification.type) {
case 'chat.readmessage':
logger.info('[Push] read message')
Expand All @@ -157,7 +191,9 @@ export const useState_ = Z.createZustand<State>((set, get) => {
// entirely handled by go on ios and in onNotification on Android
break
case 'chat.newmessage':
await handleLoudMessage(notification)
if (await switchToRecipientIfNeeded(notification.recipientUsername)) {
await handleLoudMessage(notification)
}
break
case 'follow':
// We only care if the user clicked while in session
Expand All @@ -169,8 +205,10 @@ export const useState_ = Z.createZustand<State>((set, get) => {
break
case 'chat.extension':
{
const {conversationIDKey} = notification
C.getConvoState(conversationIDKey).dispatch.navigateToThread('extension')
const {conversationIDKey, recipientUsername} = notification
if (await switchToRecipientIfNeeded(recipientUsername)) {
C.getConvoState(conversationIDKey).dispatch.navigateToThread('extension')
}
}
break
case 'settings.contacts':
Expand Down
5 changes: 4 additions & 1 deletion shared/constants/types/push.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ export type PushNotification =
| {
conversationIDKey: ChatTypes.ConversationIDKey
membersType: RPCChatTypes.ConversationMembersType
recipientUsername?: string
type: 'chat.newmessageSilent_2'
unboxPayload: string
}
| {
conversationIDKey: ChatTypes.ConversationIDKey
membersType?: RPCChatTypes.ConversationMembersType
recipientUsername?: string
type: 'chat.newmessage'
unboxPayload: string
userInteraction: boolean
Expand All @@ -27,8 +29,9 @@ export type PushNotification =
username: string
}
| {
type: 'chat.extension'
conversationIDKey: ChatTypes.ConversationIDKey
recipientUsername?: string
type: 'chat.extension'
}
| {
type: 'settings.contacts'
Expand Down