Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/services/TelegramService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ function parseTelegramMessage(content: string): {
} else if (trimmed.startsWith("chat_id:")) {
chatId = trimmed.replace("chat_id:", "").trim();
} else if (trimmed.startsWith("content:")) {
messageText = trimmed.replace("content:", "").trim();
const contentIndex = lines.indexOf(line);
const firstLine = trimmed.replace("content:", "").trim();
const remainingLines = lines.slice(contentIndex + 1);
messageText = [firstLine, ...remainingLines].join("\n").trim();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using lines.indexOf(line) inside this loop can be inefficient, as it performs a search on each iteration where the content: line is checked. This could lead to O(n^2) complexity in a worst-case scenario. It might also be brittle if a previous line in the message has the exact same content as the content: line.

A more robust and efficient approach would be to use a for loop that provides an index, which avoids the need for indexOf.

Example:

for (let i = 0; i < lines.length; i++) {
    const line = lines[i];
    const trimmed = line.trim();
    // ... other if/else if branches
    if (trimmed.startsWith("content:")) {
        const firstLine = trimmed.replace("content:", "").trim();
        const remainingLines = lines.slice(i + 1);
        messageText = [firstLine, ...remainingLines].join('\n').trim();
        foundContent = true;
        break;
    }
}

This would require changing the for...of loop on line 44. While this is outside the current diff, it's a recommended improvement for robustness and performance.

foundContent = true;
break;
}
}

Expand Down
Loading