Apply the auto-fixable lint rules and the formatter, and drop the Vite build artifacts.
This commit is contained in:
+112
-116
@@ -1,9 +1,9 @@
|
||||
/**
|
||||
* Paarrot API Handler
|
||||
*
|
||||
*
|
||||
* This module handles API actions from the Electron API server.
|
||||
* It receives actions via IPC and executes them on the Matrix client.
|
||||
*
|
||||
*
|
||||
* To use this, import and call initPaarrotAPI() early in your app initialization.
|
||||
*/
|
||||
|
||||
@@ -76,102 +76,100 @@ export function initPaarrotAPI(matrixClient: MatrixClient) {
|
||||
console.log('Paarrot API: Initializing API handler');
|
||||
|
||||
// Listen for API actions from the Electron API server
|
||||
(window as any).electron.api.onAction(async (action: {
|
||||
action: string;
|
||||
params: any;
|
||||
responseChannel: string;
|
||||
}) => {
|
||||
console.log('Paarrot API: Received action:', action.action, action.params);
|
||||
(window as any).electron.api.onAction(
|
||||
async (action: { action: string; params: any; responseChannel: string }) => {
|
||||
console.log('Paarrot API: Received action:', action.action, action.params);
|
||||
|
||||
try {
|
||||
let result;
|
||||
try {
|
||||
let result;
|
||||
|
||||
switch (action.action) {
|
||||
case 'get-status':
|
||||
result = await getStatus(matrixClient);
|
||||
break;
|
||||
switch (action.action) {
|
||||
case 'get-status':
|
||||
result = await getStatus(matrixClient);
|
||||
break;
|
||||
|
||||
case 'toggle-mute':
|
||||
result = await toggleMute(matrixClient);
|
||||
break;
|
||||
case 'toggle-mute':
|
||||
result = await toggleMute(matrixClient);
|
||||
break;
|
||||
|
||||
case 'set-mute':
|
||||
result = await setMute(matrixClient, action.params.muted);
|
||||
break;
|
||||
case 'set-mute':
|
||||
result = await setMute(matrixClient, action.params.muted);
|
||||
break;
|
||||
|
||||
case 'toggle-deafen':
|
||||
result = await toggleDeafen(matrixClient);
|
||||
break;
|
||||
case 'toggle-deafen':
|
||||
result = await toggleDeafen(matrixClient);
|
||||
break;
|
||||
|
||||
case 'set-deafen':
|
||||
result = await setDeafen(matrixClient, action.params.deafened);
|
||||
break;
|
||||
case 'set-deafen':
|
||||
result = await setDeafen(matrixClient, action.params.deafened);
|
||||
break;
|
||||
|
||||
case 'change-channel':
|
||||
result = await changeChannel(matrixClient, action.params.roomId);
|
||||
break;
|
||||
case 'change-channel':
|
||||
result = await changeChannel(matrixClient, action.params.roomId);
|
||||
break;
|
||||
|
||||
case 'get-channels':
|
||||
result = await getChannels(matrixClient);
|
||||
break;
|
||||
case 'get-channels':
|
||||
result = await getChannels(matrixClient);
|
||||
break;
|
||||
|
||||
case 'send-message':
|
||||
result = await sendMessage(matrixClient, action.params.roomId, action.params.message);
|
||||
break;
|
||||
case 'send-message':
|
||||
result = await sendMessage(matrixClient, action.params.roomId, action.params.message);
|
||||
break;
|
||||
|
||||
case 'send-message-current':
|
||||
result = await sendMessageCurrent(matrixClient, action.params.message);
|
||||
break;
|
||||
case 'send-message-current':
|
||||
result = await sendMessageCurrent(matrixClient, action.params.message);
|
||||
break;
|
||||
|
||||
case 'get-current-room':
|
||||
result = await getCurrentRoom(matrixClient);
|
||||
break;
|
||||
case 'get-current-room':
|
||||
result = await getCurrentRoom(matrixClient);
|
||||
break;
|
||||
|
||||
case 'get-messages-groups':
|
||||
result = await getMessagesByScope(matrixClient, 'groups', action.params.limit);
|
||||
break;
|
||||
case 'get-messages-groups':
|
||||
result = await getMessagesByScope(matrixClient, 'groups', action.params.limit);
|
||||
break;
|
||||
|
||||
case 'get-messages-dms':
|
||||
result = await getMessagesByScope(matrixClient, 'dms', action.params.limit);
|
||||
break;
|
||||
case 'get-messages-dms':
|
||||
result = await getMessagesByScope(matrixClient, 'dms', action.params.limit);
|
||||
break;
|
||||
|
||||
case 'get-messages-combined':
|
||||
result = await getMessagesByScope(matrixClient, 'combined', action.params.limit);
|
||||
break;
|
||||
case 'get-messages-combined':
|
||||
result = await getMessagesByScope(matrixClient, 'combined', action.params.limit);
|
||||
break;
|
||||
|
||||
case 'get-unreads-groups':
|
||||
result = await getUnreadsByScope(matrixClient, 'groups');
|
||||
break;
|
||||
case 'get-unreads-groups':
|
||||
result = await getUnreadsByScope(matrixClient, 'groups');
|
||||
break;
|
||||
|
||||
case 'get-unreads-dms':
|
||||
result = await getUnreadsByScope(matrixClient, 'dms');
|
||||
break;
|
||||
case 'get-unreads-dms':
|
||||
result = await getUnreadsByScope(matrixClient, 'dms');
|
||||
break;
|
||||
|
||||
case 'get-unreads-combined':
|
||||
result = await getUnreadsByScope(matrixClient, 'combined');
|
||||
break;
|
||||
case 'get-unreads-combined':
|
||||
result = await getUnreadsByScope(matrixClient, 'combined');
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Error(`Unknown action: ${action.action}`);
|
||||
default:
|
||||
throw new Error(`Unknown action: ${action.action}`);
|
||||
}
|
||||
|
||||
// Send success response back
|
||||
(window as any).electron.api.sendResponse(action.responseChannel, {
|
||||
success: true,
|
||||
data: result,
|
||||
});
|
||||
|
||||
console.log('Paarrot API: Action completed successfully:', action.action);
|
||||
} catch (error: any) {
|
||||
console.error('Paarrot API: Action failed:', action.action, error);
|
||||
|
||||
// Send error response back
|
||||
(window as any).electron.api.sendResponse(action.responseChannel, {
|
||||
success: false,
|
||||
error: error.message || 'Unknown error',
|
||||
});
|
||||
}
|
||||
|
||||
// Send success response back
|
||||
(window as any).electron.api.sendResponse(action.responseChannel, {
|
||||
success: true,
|
||||
data: result,
|
||||
});
|
||||
|
||||
console.log('Paarrot API: Action completed successfully:', action.action);
|
||||
} catch (error: any) {
|
||||
console.error('Paarrot API: Action failed:', action.action, error);
|
||||
|
||||
// Send error response back
|
||||
(window as any).electron.api.sendResponse(action.responseChannel, {
|
||||
success: false,
|
||||
error: error.message || 'Unknown error',
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
console.log('Paarrot API: Handler initialized');
|
||||
}
|
||||
@@ -181,11 +179,11 @@ export function initPaarrotAPI(matrixClient: MatrixClient) {
|
||||
*/
|
||||
async function getStatus(matrixClient: MatrixClient) {
|
||||
const currentRoom = getCurrentRoomId();
|
||||
|
||||
|
||||
// Get actual mute/deafen state from CallService if available
|
||||
const callService = getCallService();
|
||||
const activeCall = callService?.getActiveCall();
|
||||
|
||||
|
||||
return {
|
||||
muted: activeCall?.isMuted ?? false,
|
||||
deafened: activeCall?.isDeafened ?? false,
|
||||
@@ -200,14 +198,14 @@ async function getStatus(matrixClient: MatrixClient) {
|
||||
*/
|
||||
async function toggleMute(matrixClient: MatrixClient) {
|
||||
const callService = getCallService();
|
||||
|
||||
|
||||
if (!callService) {
|
||||
return {
|
||||
muted: false,
|
||||
message: 'CallService not initialized - no active call',
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
const activeCall = callService.getActiveCall();
|
||||
if (!activeCall) {
|
||||
return {
|
||||
@@ -215,10 +213,10 @@ async function toggleMute(matrixClient: MatrixClient) {
|
||||
message: 'No active call to mute/unmute',
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// Toggle mute state
|
||||
const newMuteState = callService.toggleMute();
|
||||
|
||||
|
||||
return {
|
||||
muted: newMuteState,
|
||||
};
|
||||
@@ -229,14 +227,14 @@ async function toggleMute(matrixClient: MatrixClient) {
|
||||
*/
|
||||
async function setMute(matrixClient: MatrixClient, muted: boolean) {
|
||||
const callService = getCallService();
|
||||
|
||||
|
||||
if (!callService) {
|
||||
return {
|
||||
muted,
|
||||
message: 'CallService not initialized - no active call',
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
const activeCall = callService.getActiveCall();
|
||||
if (!activeCall) {
|
||||
return {
|
||||
@@ -244,12 +242,12 @@ async function setMute(matrixClient: MatrixClient, muted: boolean) {
|
||||
message: 'No active call to mute/unmute',
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// Set mute to desired state if it's different
|
||||
if (activeCall.isMuted !== muted) {
|
||||
callService.toggleMute();
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
muted,
|
||||
};
|
||||
@@ -260,14 +258,14 @@ async function setMute(matrixClient: MatrixClient, muted: boolean) {
|
||||
*/
|
||||
async function toggleDeafen(matrixClient: MatrixClient) {
|
||||
const callService = getCallService();
|
||||
|
||||
|
||||
if (!callService) {
|
||||
return {
|
||||
deafened: false,
|
||||
message: 'CallService not initialized - no active call',
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
const activeCall = callService.getActiveCall();
|
||||
if (!activeCall) {
|
||||
return {
|
||||
@@ -275,10 +273,10 @@ async function toggleDeafen(matrixClient: MatrixClient) {
|
||||
message: 'No active call to deafen/undeafen',
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// Toggle deafen state
|
||||
const newDeafenState = callService.toggleDeafen();
|
||||
|
||||
|
||||
return {
|
||||
deafened: newDeafenState,
|
||||
};
|
||||
@@ -289,14 +287,14 @@ async function toggleDeafen(matrixClient: MatrixClient) {
|
||||
*/
|
||||
async function setDeafen(matrixClient: MatrixClient, deafened: boolean) {
|
||||
const callService = getCallService();
|
||||
|
||||
|
||||
if (!callService) {
|
||||
return {
|
||||
deafened,
|
||||
message: 'CallService not initialized - no active call',
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
const activeCall = callService.getActiveCall();
|
||||
if (!activeCall) {
|
||||
return {
|
||||
@@ -304,12 +302,12 @@ async function setDeafen(matrixClient: MatrixClient, deafened: boolean) {
|
||||
message: 'No active call to deafen/undeafen',
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// Set deafen to desired state if it's different
|
||||
if (activeCall.isDeafened !== deafened) {
|
||||
callService.toggleDeafen();
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
deafened,
|
||||
};
|
||||
@@ -325,7 +323,9 @@ async function changeChannel(matrixClient: any, roomId: string) {
|
||||
}
|
||||
|
||||
if (!navigateFunction) {
|
||||
throw new Error('Navigate function not registered. Call setPaarrotNavigate from your router setup.');
|
||||
throw new Error(
|
||||
'Navigate function not registered. Call setPaarrotNavigate from your router setup.',
|
||||
);
|
||||
}
|
||||
|
||||
// Determine the appropriate path based on room type
|
||||
@@ -334,7 +334,7 @@ async function changeChannel(matrixClient: any, roomId: string) {
|
||||
|
||||
// Check if it's a direct message
|
||||
const isDirect = room.guessDMUserId() !== null;
|
||||
|
||||
|
||||
if (isDirect) {
|
||||
// Navigate to direct message
|
||||
path = getDirectRoomPath(roomIdOrAlias);
|
||||
@@ -368,11 +368,11 @@ async function getChannels(matrixClient: any) {
|
||||
// Extract server domain from roomId (!localpart:domain.tld)
|
||||
const serverMatch = room.roomId.match(/:(.+)$/);
|
||||
const server = serverMatch ? serverMatch[1] : null;
|
||||
|
||||
|
||||
return {
|
||||
roomId: room.roomId,
|
||||
name: room.name || 'Unnamed Room',
|
||||
server: server,
|
||||
server,
|
||||
isDirect: room.getMyMembership() === 'invite' ? false : room.guessDMUserId() !== null,
|
||||
avatar: room.getMxcAvatarUrl() || null,
|
||||
};
|
||||
@@ -435,7 +435,7 @@ async function getCurrentRoom(matrixClient: any) {
|
||||
return {
|
||||
roomId: room.roomId,
|
||||
name: room.name || 'Unnamed Room',
|
||||
server: server,
|
||||
server,
|
||||
avatar: room.getMxcAvatarUrl() || null,
|
||||
isDirect: room.guessDMUserId() !== null,
|
||||
};
|
||||
@@ -466,7 +466,7 @@ function parseMessageLimit(limit?: number | string): number {
|
||||
const parsedLimit = Number(limit);
|
||||
return Math.min(
|
||||
Math.max(Number.isFinite(parsedLimit) ? Math.floor(parsedLimit) : DEFAULT_MESSAGE_LIMIT, 1),
|
||||
MAX_MESSAGE_LIMIT
|
||||
MAX_MESSAGE_LIMIT,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -477,7 +477,7 @@ function parseMessageLimit(limit?: number | string): number {
|
||||
function collectRecentMessages(room: any, limit: number): ApiMessage[] {
|
||||
const liveEvents = room.getLiveTimeline?.()?.getEvents?.() ?? [];
|
||||
const isDirect = isDirectRoom(room);
|
||||
const roomId = room.roomId;
|
||||
const { roomId } = room;
|
||||
const roomName = room.name || 'Unnamed Room';
|
||||
const messages: ApiMessage[] = [];
|
||||
|
||||
@@ -533,7 +533,7 @@ function collectRecentMessages(room: any, limit: number): ApiMessage[] {
|
||||
|
||||
function getRoomsForScope(matrixClient: any, scope: MessageScope) {
|
||||
const rooms = (matrixClient?.getRooms?.() || []).filter(
|
||||
(room: any) => !isSpaceLikeRoom(room) && room.getMyMembership?.() === 'join'
|
||||
(room: any) => !isSpaceLikeRoom(room) && room.getMyMembership?.() === 'join',
|
||||
);
|
||||
|
||||
if (scope === 'groups') {
|
||||
@@ -548,11 +548,7 @@ function getRoomsForScope(matrixClient: any, scope: MessageScope) {
|
||||
/**
|
||||
* Get recent messages across groups, DMs, or both (combined).
|
||||
*/
|
||||
async function getMessagesByScope(
|
||||
matrixClient: any,
|
||||
scope: MessageScope,
|
||||
limit?: number | string
|
||||
) {
|
||||
async function getMessagesByScope(matrixClient: any, scope: MessageScope, limit?: number | string) {
|
||||
const limitNum = parseMessageLimit(limit);
|
||||
const rooms = getRoomsForScope(matrixClient, scope);
|
||||
|
||||
@@ -650,8 +646,8 @@ function getCurrentRoomId(): string | null {
|
||||
try {
|
||||
// Get the pathname from URL
|
||||
// Support both normal routing and hash routing
|
||||
let pathname = window.location.pathname;
|
||||
|
||||
let { pathname } = window.location;
|
||||
|
||||
// If using hash router, parse from hash
|
||||
if (window.location.hash) {
|
||||
const hashMatch = window.location.hash.match(/#(.+)/);
|
||||
@@ -659,19 +655,19 @@ function getCurrentRoomId(): string | null {
|
||||
pathname = hashMatch[1];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// URL patterns:
|
||||
// /home/:roomIdOrAlias/:eventId?/
|
||||
// /direct/:roomIdOrAlias/:eventId?/
|
||||
// /:spaceIdOrAlias/:roomIdOrAlias/:eventId?/
|
||||
|
||||
|
||||
// Try to match room patterns
|
||||
const patterns = [
|
||||
/^\/home\/([^/]+)/, // /home/:roomIdOrAlias
|
||||
/^\/direct\/([^/]+)/, // /direct/:roomIdOrAlias
|
||||
/^\/[^/]+\/([^/]+)/, // /:spaceIdOrAlias/:roomIdOrAlias (space rooms)
|
||||
];
|
||||
|
||||
|
||||
for (const pattern of patterns) {
|
||||
const match = pathname.match(pattern);
|
||||
if (match && match[1]) {
|
||||
@@ -679,7 +675,7 @@ function getCurrentRoomId(): string | null {
|
||||
return decodeURIComponent(match[1]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
} catch (error) {
|
||||
console.error('Paarrot API: Error getting current room ID:', error);
|
||||
|
||||
Reference in New Issue
Block a user