update nextcloud handling
This commit is contained in:
@@ -113,7 +113,18 @@ class NextcloudController {
|
||||
res.status(400).json({ success: false, message: 'Room token fehlt' });
|
||||
return;
|
||||
}
|
||||
const messages = await nextcloudService.getMessages(token, credentials.loginName, credentials.appPassword);
|
||||
const lookIntoFuture = req.query.lookIntoFuture === '1';
|
||||
const lastKnownMessageId = req.query.lastKnownMessageId
|
||||
? parseInt(req.query.lastKnownMessageId as string, 10)
|
||||
: undefined;
|
||||
const timeout = req.query.timeout
|
||||
? Math.min(parseInt(req.query.timeout as string, 10), 25)
|
||||
: 25;
|
||||
const messages = await nextcloudService.getMessages(token, credentials.loginName, credentials.appPassword, {
|
||||
lookIntoFuture,
|
||||
lastKnownMessageId,
|
||||
timeout,
|
||||
});
|
||||
res.status(200).json({ success: true, data: messages });
|
||||
} catch (error: any) {
|
||||
if (error?.code === 'NEXTCLOUD_AUTH_INVALID') {
|
||||
|
||||
@@ -195,25 +195,53 @@ async function getAllConversations(loginName: string, appPassword: string): Prom
|
||||
}
|
||||
}
|
||||
|
||||
async function getMessages(token: string, loginName: string, appPassword: string): Promise<NextcloudChatMessage[]> {
|
||||
interface GetMessagesOptions {
|
||||
lookIntoFuture?: boolean;
|
||||
lastKnownMessageId?: number;
|
||||
timeout?: number;
|
||||
}
|
||||
|
||||
async function getMessages(token: string, loginName: string, appPassword: string, options?: GetMessagesOptions): Promise<NextcloudChatMessage[]> {
|
||||
const baseUrl = environment.nextcloudUrl;
|
||||
if (!baseUrl || !isValidServiceUrl(baseUrl)) {
|
||||
throw new Error('NEXTCLOUD_URL is not configured or is not a valid service URL');
|
||||
}
|
||||
|
||||
const lookIntoFuture = options?.lookIntoFuture ?? false;
|
||||
const ncTimeout = options?.timeout ?? 25;
|
||||
|
||||
const params: Record<string, any> = {
|
||||
lookIntoFuture: lookIntoFuture ? 1 : 0,
|
||||
limit: lookIntoFuture ? 100 : 50,
|
||||
setReadMarker: 0,
|
||||
};
|
||||
|
||||
if (lookIntoFuture) {
|
||||
params.lastKnownMessageId = options?.lastKnownMessageId ?? 0;
|
||||
params.timeout = ncTimeout;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await httpClient.get(
|
||||
`${baseUrl}/ocs/v2.php/apps/spreed/api/v1/chat/${encodeURIComponent(token)}`,
|
||||
{
|
||||
params: { lookIntoFuture: 0, limit: 50, setReadMarker: 0 },
|
||||
params,
|
||||
headers: {
|
||||
'Authorization': `Basic ${Buffer.from(loginName + ':' + appPassword).toString('base64')}`,
|
||||
'OCS-APIRequest': 'true',
|
||||
'Accept': 'application/json',
|
||||
},
|
||||
...(lookIntoFuture && {
|
||||
timeout: (ncTimeout + 5) * 1000,
|
||||
validateStatus: (s: number) => (s >= 200 && s < 300) || s === 304,
|
||||
}),
|
||||
},
|
||||
);
|
||||
|
||||
if (response.status === 304) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const messages: any[] = response.data?.ocs?.data ?? [];
|
||||
return messages.map((m: any) => ({
|
||||
id: m.id,
|
||||
@@ -384,5 +412,5 @@ async function getConversations(loginName: string, appPassword: string): Promise
|
||||
}
|
||||
}
|
||||
|
||||
export type { NextcloudChatMessage };
|
||||
export type { NextcloudChatMessage, GetMessagesOptions };
|
||||
export default { initiateLoginFlow, pollLoginFlow, getConversations, getAllConversations, getMessages, sendMessage, markAsRead };
|
||||
|
||||
Reference in New Issue
Block a user