Skip to content

Commit

Permalink
Ensure we're not accidentally removing line breaks when stripping rep…
Browse files Browse the repository at this point in the history
…ly fallbacks

Fixes matrix-orgGH-737

Signed-off-by: Tadeusz „tadzik” Sośnierz <tadeusz@sosnierz.com>
  • Loading branch information
tadzik committed Mar 29, 2024
1 parent badc1eb commit 1921b50
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 41 deletions.
48 changes: 7 additions & 41 deletions src/BridgedRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ export class BridgedRoom {
// re-write the message so the matrixToSlack converter works as expected.
let newMessage = JSON.parse(JSON.stringify(message));
newMessage.content = message.content["m.new_content"];
newMessage = await this.stripMatrixReplyFallback(newMessage);
newMessage = substitutions.stripMatrixReplyFallback(newMessage);

const body = await substitutions.matrixToSlack(newMessage, this.main, this.SlackTeamId!);

Expand Down Expand Up @@ -443,8 +443,11 @@ export class BridgedRoom {
if (!this.slackWebhookUri && !this.botClient && !puppetedClient) { return false; }
const slackClient = puppetedClient || this.botClient;
const user = this.main.getOrCreateMatrixUser(message.sender);
message = await this.stripMatrixReplyFallback(message);

message = substitutions.stripMatrixReplyFallback(message);

const matrixToSlackResult = await substitutions.matrixToSlack(message, this.main, this.SlackTeamId!);

if (!matrixToSlackResult) {
// Could not handle content, dropped.
log.warn(`Dropped ${message.event_id}, message content could not be identified`);
Expand Down Expand Up @@ -994,7 +997,7 @@ export class BridgedRoom {
if (message.thread_ts !== undefined && message.text) {
let replyMEvent = await this.getReplyEvent(this.MatrixRoomId, message, this.SlackChannelId!);
if (replyMEvent) {
replyMEvent = await this.stripMatrixReplyFallback(replyMEvent);
replyMEvent = substitutions.stripMatrixReplyFallback(replyMEvent);
return await ghost.sendInThread(
this.MatrixRoomId, message.text, this.SlackChannelId!, eventTS, replyMEvent,
);
Expand Down Expand Up @@ -1048,7 +1051,7 @@ export class BridgedRoom {
let replyEvent = await this.getReplyEvent(
this.MatrixRoomId, message.message as unknown as ISlackMessageEvent, this.slackChannelId!,
);
replyEvent = await this.stripMatrixReplyFallback(replyEvent);
replyEvent = substitutions.stripMatrixReplyFallback(replyEvent);
if (replyEvent) {
const bodyFallback = ghost.getFallbackText(replyEvent);
const formattedFallback = ghost.getFallbackHtml(this.MatrixRoomId, replyEvent);
Expand Down Expand Up @@ -1152,43 +1155,6 @@ export class BridgedRoom {
return intent.getEvent(roomID, replyToEvent.eventId);
}

/*
Strip out reply fallbacks. Borrowed from
https://github.com/turt2live/matrix-js-bot-sdk/blob/master/src/preprocessors/RichRepliesPreprocessor.ts
*/
private async stripMatrixReplyFallback(event: any): Promise<any> {
if (!event.content?.body) {
return event;
}

let realHtml = event.content.formatted_body;
let realText = event.content.body || "";

if (event.content.format === "org.matrix.custom.html" && realHtml) {
const formattedBody = realHtml;
if (formattedBody.startsWith("<mx-reply>") && formattedBody.indexOf("</mx-reply>") !== -1) {
const parts = formattedBody.split("</mx-reply>");
realHtml = parts[1];
event.content.formatted_body = realHtml.trim();
}
}

let processedFallback = false;
for (const line of realText.split("\n")) {
if (line.startsWith("> ") && !processedFallback) {
continue;
} else if (!processedFallback) {
realText = line;
processedFallback = true;
} else {
realText += line + "\n";
}
}

event.content.body = realText.trim();
return event;
}

/*
Given an event which is in reply to something else return the event ID of the
top most event in the reply chain, i.e. the one without a relates to.
Expand Down
37 changes: 37 additions & 0 deletions src/substitutions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,43 @@ class Substitutions {
return `${file.url_private}?pub_secret=${pubSecret[1]}`;
}
}

/*
Strip out reply fallbacks. Borrowed from
https://github.com/turt2live/matrix-js-bot-sdk/blob/master/src/preprocessors/RichRepliesPreprocessor.ts
*/
public stripMatrixReplyFallback<T>(event: T & any): T {
if (!event.content?.body) {
return event;
}

let realHtml = event.content.formatted_body;
let realText = event.content.body || "";

if (event.content.format === "org.matrix.custom.html" && realHtml) {
const formattedBody = realHtml;
if (formattedBody.startsWith("<mx-reply>") && formattedBody.indexOf("</mx-reply>") !== -1) {
const parts = formattedBody.split("</mx-reply>");
realHtml = parts[1];
event.content.formatted_body = realHtml.trim();
}
}

let processedFallback = false;
for (const line of realText.split("\n")) {
if (line.startsWith("> ") && !processedFallback) {
continue;
} else if (!processedFallback) {
realText = line + "\n";
processedFallback = true;
} else {
realText += line + "\n";
}
}

event.content.body = realText.trim();
return event;
}
}

const substitutions = new Substitutions();
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/substitutionsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,12 @@ describe("Substitutions", () => {
expect(res).to.equal("@room hello");
});
});

describe("stripMatrixReplyFallback", () => {
it("should leave newlines intact in messages", () => {
let message = { content: { body: "foo\nbar" } };
message = substitutions.stripMatrixReplyFallback(message);
expect(message.content.body).to.equal("foo\nbar");
});
});
});

0 comments on commit 1921b50

Please sign in to comment.