adding chat features, admin features and bug fixes
This commit is contained in:
@@ -127,6 +127,143 @@ async function pollLoginFlow(pollEndpoint: string, pollToken: string): Promise<L
|
||||
}
|
||||
}
|
||||
|
||||
interface NextcloudChatMessage {
|
||||
id: number;
|
||||
token: string;
|
||||
actorType: string;
|
||||
actorId: string;
|
||||
actorDisplayName: string;
|
||||
message: string;
|
||||
timestamp: number;
|
||||
messageType: string;
|
||||
systemMessage: string;
|
||||
}
|
||||
|
||||
async function getAllConversations(loginName: string, appPassword: string): Promise<NextcloudConversation[]> {
|
||||
const baseUrl = environment.nextcloudUrl;
|
||||
if (!baseUrl || !isValidServiceUrl(baseUrl)) {
|
||||
throw new Error('NEXTCLOUD_URL is not configured or is not a valid service URL');
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await axios.get(
|
||||
`${baseUrl}/ocs/v2.php/apps/spreed/api/v4/room?format=json`,
|
||||
{
|
||||
headers: {
|
||||
'Authorization': `Basic ${Buffer.from(loginName + ':' + appPassword).toString('base64')}`,
|
||||
'OCS-APIRequest': 'true',
|
||||
'Accept': 'application/json',
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
const rooms: any[] = response.data?.ocs?.data ?? [];
|
||||
return rooms
|
||||
.filter((r: any) => r.type !== 4)
|
||||
.sort((a: any, b: any) => (b.lastActivity ?? 0) - (a.lastActivity ?? 0))
|
||||
.map((r: any) => ({
|
||||
token: r.token,
|
||||
displayName: r.displayName,
|
||||
unreadMessages: r.unreadMessages ?? 0,
|
||||
lastActivity: r.lastActivity ?? 0,
|
||||
lastMessage: r.lastMessage
|
||||
? {
|
||||
text: r.lastMessage.message ?? '',
|
||||
author: r.lastMessage.actorDisplayName ?? '',
|
||||
timestamp: r.lastMessage.timestamp ?? 0,
|
||||
}
|
||||
: null,
|
||||
type: r.type,
|
||||
url: `${baseUrl}/call/${r.token}`,
|
||||
}));
|
||||
} catch (error) {
|
||||
if (axios.isAxiosError(error) && error.response?.status === 401) {
|
||||
const err = new Error('Nextcloud app password is invalid');
|
||||
(err as any).code = 'NEXTCLOUD_AUTH_INVALID';
|
||||
throw err;
|
||||
}
|
||||
if (axios.isAxiosError(error)) {
|
||||
logger.error('Nextcloud getAllConversations failed', {
|
||||
status: error.response?.status,
|
||||
statusText: error.response?.statusText,
|
||||
});
|
||||
throw new Error(`Nextcloud API error: ${error.response?.status ?? 'unknown'}`);
|
||||
}
|
||||
logger.error('NextcloudService.getAllConversations failed', { error });
|
||||
throw new Error('Failed to fetch Nextcloud conversations');
|
||||
}
|
||||
}
|
||||
|
||||
async function getMessages(token: string, loginName: string, appPassword: string): 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 response = await axios.get(
|
||||
`${baseUrl}/ocs/v2.php/apps/spreed/api/v4/chat/${encodeURIComponent(token)}`,
|
||||
{
|
||||
params: { lookIntoFuture: 0, limit: 50, setReadMarker: 0 },
|
||||
headers: {
|
||||
'Authorization': `Basic ${Buffer.from(loginName + ':' + appPassword).toString('base64')}`,
|
||||
'OCS-APIRequest': 'true',
|
||||
'Accept': 'application/json',
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
const messages: any[] = response.data?.ocs?.data ?? [];
|
||||
return messages.map((m: any) => ({
|
||||
id: m.id,
|
||||
token: m.token,
|
||||
actorType: m.actorType,
|
||||
actorId: m.actorId,
|
||||
actorDisplayName: m.actorDisplayName,
|
||||
message: m.message,
|
||||
timestamp: m.timestamp,
|
||||
messageType: m.messageType ?? '',
|
||||
systemMessage: m.systemMessage ?? '',
|
||||
}));
|
||||
}
|
||||
|
||||
async function sendMessage(token: string, message: string, loginName: string, appPassword: string): Promise<void> {
|
||||
const baseUrl = environment.nextcloudUrl;
|
||||
if (!baseUrl || !isValidServiceUrl(baseUrl)) {
|
||||
throw new Error('NEXTCLOUD_URL is not configured or is not a valid service URL');
|
||||
}
|
||||
|
||||
await axios.post(
|
||||
`${baseUrl}/ocs/v2.php/apps/spreed/api/v4/chat/${encodeURIComponent(token)}`,
|
||||
{ message },
|
||||
{
|
||||
headers: {
|
||||
'Authorization': `Basic ${Buffer.from(loginName + ':' + appPassword).toString('base64')}`,
|
||||
'OCS-APIRequest': 'true',
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async function markAsRead(token: string, loginName: string, appPassword: string): Promise<void> {
|
||||
const baseUrl = environment.nextcloudUrl;
|
||||
if (!baseUrl || !isValidServiceUrl(baseUrl)) {
|
||||
throw new Error('NEXTCLOUD_URL is not configured or is not a valid service URL');
|
||||
}
|
||||
|
||||
await axios.delete(
|
||||
`${baseUrl}/ocs/v2.php/apps/spreed/api/v4/chat/${encodeURIComponent(token)}/read`,
|
||||
{
|
||||
headers: {
|
||||
'Authorization': `Basic ${Buffer.from(loginName + ':' + appPassword).toString('base64')}`,
|
||||
'OCS-APIRequest': 'true',
|
||||
'Accept': 'application/json',
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async function getConversations(loginName: string, appPassword: string): Promise<ConversationsResult> {
|
||||
const baseUrl = environment.nextcloudUrl;
|
||||
if (!baseUrl || !isValidServiceUrl(baseUrl)) {
|
||||
@@ -193,4 +330,5 @@ async function getConversations(loginName: string, appPassword: string): Promise
|
||||
}
|
||||
}
|
||||
|
||||
export default { initiateLoginFlow, pollLoginFlow, getConversations };
|
||||
export type { NextcloudChatMessage };
|
||||
export default { initiateLoginFlow, pollLoginFlow, getConversations, getAllConversations, getMessages, sendMessage, markAsRead };
|
||||
|
||||
Reference in New Issue
Block a user