Skip to content

Commit

Permalink
fix(notifications): use notification state API to check whether to di…
Browse files Browse the repository at this point in the history
…smiss callbox

Signed-off-by: Maksim Sukharev <[email protected]>
  • Loading branch information
Antreesy committed Dec 11, 2024
1 parent 9d308d1 commit 3f69fb1
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/callbox/renderer/callbox.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import axios from '@nextcloud/axios'
import { getCurrentUser } from '@nextcloud/auth'
import { generateOcsUrl } from '@nextcloud/router'
import { appData } from '../../app/AppData.js'

import type { AxiosError } from '@nextcloud/axios'

// @talk/src/types/openapi/openapi.ts/operations['call-get-peers-for-call']['responses'][200]['content']['application/json']
// TODO: find a way to import from @talk without type errors on CI
Expand All @@ -30,6 +33,22 @@ type CallGetParticipantsForCallResponse = {
}
}

type CallNotificationStateResponse = {
ocs: {
meta: {
status: string
statuscode: number
message?: string
totalitems?: string
itemsperpage?: string
}
data: unknown
}
}

// TODO: this should be wrapped in a function to be used in separate callbox window
const getSupportCallNotificationStateApi = () => appData.capabilities?.spreed?.features?.includes('call-notification-state-api')

/**
* Get participants of a call in a conversation
* @param token - Conversation token
Expand All @@ -39,6 +58,14 @@ async function getCallParticipants(token: string) {
return response.data.ocs.data
}

/**
* Get call notification state in a conversation
* @param token - Conversation token
*/
async function getCallNotificationState(token: string) {
return axios.get<CallNotificationStateResponse>(generateOcsUrl('apps/spreed/api/v4/call/{token}/notification-state', { token }))
}

/**
* Check if the current user has joined the call
* @param token - Conversation token
Expand All @@ -49,6 +76,28 @@ async function hasCurrentUserJoinedCall(token: string) {
if (!user) {
throw new Error('Cannot check whether current join the call - no current user found')
}

if (getSupportCallNotificationStateApi()) {
try {
const response = await getCallNotificationState(token)
if (response.data.ocs.meta.statuscode === 201) {
// status code 201 returned, call missed
return null
} else {
// status code 200 returned, user not joined yet and call notification is valid
return false
}
} catch (exception) {
if ((exception as AxiosError)?.response?.status === 404) {
// status code 404 returned, user joined call already
console.debug(exception)
return true
} else {
throw exception
}
}
}

const participants = await getCallParticipants(token)
if (!participants.length) {
return null
Expand Down

0 comments on commit 3f69fb1

Please sign in to comment.