Rewrite the restricted iterators and reorder the forward references. [skip build]
Checks / checks (push) Successful in 2m11s
Trigger cinny-mobile / dispatch (push) Successful in 2s

This commit is contained in:
2026-11-01 21:05:47 +11:00
parent 5477242661
commit 664b7eb739
25 changed files with 472 additions and 437 deletions
+125 -122
View File
@@ -55,125 +55,6 @@ export function setPaarrotNavigate(navigate: (path: string) => void) {
console.log('Paarrot API: Navigate function registered');
}
/**
* Initialize the Paarrot API handler
* This should be called once during app startup
*/
export function initPaarrotAPI(matrixClient: MatrixClient) {
// Check if we're running in Electron
if (!(window as any).electron?.api?.onAction) {
console.log('Paarrot API: Not running in Electron, skipping API initialization');
return;
}
// Prevent duplicate registrations
if (isInitialized) {
console.log('Paarrot API: Already initialized, skipping');
return;
}
isInitialized = true;
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);
try {
let result;
switch (action.action) {
case 'get-status':
result = await getStatus(matrixClient);
break;
case 'toggle-mute':
result = await toggleMute(matrixClient);
break;
case 'set-mute':
result = await setMute(matrixClient, action.params.muted);
break;
case 'toggle-deafen':
result = await toggleDeafen(matrixClient);
break;
case 'set-deafen':
result = await setDeafen(matrixClient, action.params.deafened);
break;
case 'change-channel':
result = await changeChannel(matrixClient, action.params.roomId);
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-current':
result = await sendMessageCurrent(matrixClient, action.params.message);
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-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-unreads-groups':
result = await getUnreadsByScope(matrixClient, 'groups');
break;
case 'get-unreads-dms':
result = await getUnreadsByScope(matrixClient, 'dms');
break;
case 'get-unreads-combined':
result = await getUnreadsByScope(matrixClient, 'combined');
break;
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',
});
}
},
);
console.log('Paarrot API: Handler initialized');
}
/**
* Get the currently active room ID
* Extracts room ID from the current URL pathname
@@ -198,7 +79,8 @@ function getCurrentRoomId(): string | null {
/^\/[^/]+\/([^/]+)/, // /:spaceIdOrAlias/:roomIdOrAlias (space rooms)
];
for (const pattern of patterns) {
for (let i = 0; i < patterns.length; i += 1) {
const pattern = patterns[i];
const match = pathname.match(pattern);
if (match && match[1]) {
// Decode URI component in case the room ID was encoded
@@ -591,7 +473,8 @@ async function getMessagesByScope(matrixClient: any, scope: MessageScope, limit?
const rooms = getRoomsForScope(matrixClient, scope);
const candidates: ApiMessage[] = [];
for (const room of rooms) {
for (let i = 0; i < rooms.length; i += 1) {
const room = rooms[i];
candidates.push(...collectRecentMessages(room, limitNum));
}
@@ -629,7 +512,8 @@ async function getUnreadsByScope(matrixClient: any, scope: MessageScope) {
};
}> = [];
for (const room of rooms) {
for (let i = 0; i < rooms.length; i += 1) {
const room = rooms[i];
if (
getNotificationType(matrixClient, room.roomId) !== NotificationType.Mute &&
(roomHaveNotification(room) || roomHaveUnread(matrixClient, room))
@@ -674,4 +558,123 @@ async function getUnreadsByScope(matrixClient: any, scope: MessageScope) {
};
}
/**
* Initialize the Paarrot API handler
* This should be called once during app startup
*/
export function initPaarrotAPI(matrixClient: MatrixClient) {
// Check if we're running in Electron
if (!(window as any).electron?.api?.onAction) {
console.log('Paarrot API: Not running in Electron, skipping API initialization');
return;
}
// Prevent duplicate registrations
if (isInitialized) {
console.log('Paarrot API: Already initialized, skipping');
return;
}
isInitialized = true;
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);
try {
let result;
switch (action.action) {
case 'get-status':
result = await getStatus(matrixClient);
break;
case 'toggle-mute':
result = await toggleMute(matrixClient);
break;
case 'set-mute':
result = await setMute(matrixClient, action.params.muted);
break;
case 'toggle-deafen':
result = await toggleDeafen(matrixClient);
break;
case 'set-deafen':
result = await setDeafen(matrixClient, action.params.deafened);
break;
case 'change-channel':
result = await changeChannel(matrixClient, action.params.roomId);
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-current':
result = await sendMessageCurrent(matrixClient, action.params.message);
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-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-unreads-groups':
result = await getUnreadsByScope(matrixClient, 'groups');
break;
case 'get-unreads-dms':
result = await getUnreadsByScope(matrixClient, 'dms');
break;
case 'get-unreads-combined':
result = await getUnreadsByScope(matrixClient, 'combined');
break;
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',
});
}
},
);
console.log('Paarrot API: Handler initialized');
}
export default { initPaarrotAPI };