Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Push Matrix->Slack messages through the same queue as Slack->Matrix #790

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
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
40 changes: 24 additions & 16 deletions src/BridgedRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,17 @@
public MatrixRoomActive: boolean;
private recentSlackMessages: string[] = [];

private slackSendLock: Promise<unknown> = Promise.resolve();
private bridgingQueue: Promise<unknown> = Promise.resolve();

private waitingForJoin?: Promise<void>;
private waitingForJoinResolve?: () => void;

private async pushToBridgingQueue<T>(fn: () => Promise<T>): Promise<T> {
return new Promise((resolve) => {
this.bridgingQueue = this.bridgingQueue.finally(() => fn().then(resolve));

Check failure on line 169 in src/BridgedRoom.ts

View workflow job for this annotation

GitHub Actions / lint

Promise returned in function argument where a void return was expected

Check failure on line 169 in src/BridgedRoom.ts

View workflow job for this annotation

GitHub Actions / lint

Functions that return promises must be async
});
}

/**
* True if this instance has changed from the version last read/written to the RoomStore.
*/
Expand Down Expand Up @@ -241,13 +247,13 @@
id: `INTEG-${this.inboundId}`,
matrix_id: this.matrixRoomId,
remote: {
id: this.slackChannelId!,

Check warning on line 250 in src/BridgedRoom.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
name: this.slackChannelName!,

Check warning on line 251 in src/BridgedRoom.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
slack_team_id: this.slackTeamId!,

Check warning on line 252 in src/BridgedRoom.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
slack_type: this.slackType!,

Check warning on line 253 in src/BridgedRoom.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
slack_private: this.isPrivate!,

Check warning on line 254 in src/BridgedRoom.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
webhook_uri: this.slackWebhookUri!,

Check warning on line 255 in src/BridgedRoom.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
puppet_owner: this.puppetOwner!,

Check warning on line 256 in src/BridgedRoom.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
},
remote_id: this.inboundId,
};
Expand All @@ -256,7 +262,7 @@
}

public async getClientForRequest(userId: string): Promise<{id: string, client: WebClient}|null> {
const puppet = await this.main.clientFactory.getClientForUserWithId(this.SlackTeamId!, userId);

Check warning on line 265 in src/BridgedRoom.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
if (puppet) {
return puppet;
}
Expand All @@ -269,7 +275,7 @@
return null;
}

public async onMatrixReaction(message: any): Promise<void> {

Check warning on line 278 in src/BridgedRoom.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
const relatesTo = message.content["m.relates_to"];
const eventStore = this.main.datastore;
const event = await eventStore.getEventByMatrixId(message.room_id, relatesTo.event_id);
Expand Down Expand Up @@ -528,7 +534,6 @@
body.as_user = true;
delete body.username;
}
let res: ChatPostMessageResponse;
const chatPostMessageArgs = {
...body,
// Ensure that text is defined, even for attachments.
Expand All @@ -537,21 +542,25 @@
unfurl_links: true,
};

try {
res = await slackClient.chat.postMessage(chatPostMessageArgs) as ChatPostMessageResponse;
} catch (ex) {
const platformError = ex as WebAPIPlatformError;
if (platformError.data?.error === "not_in_channel") {
await slackClient.conversations.join({
channel: chatPostMessageArgs.channel,
});
const res = await this.pushToBridgingQueue(async () => {
let res: ChatPostMessageResponse;

Check failure on line 546 in src/BridgedRoom.ts

View workflow job for this annotation

GitHub Actions / lint

'res' is already declared in the upper scope on line 545 column 15
try {
res = await slackClient.chat.postMessage(chatPostMessageArgs) as ChatPostMessageResponse;
} else {
throw ex;
} catch (ex) {
const platformError = ex as WebAPIPlatformError;
if (platformError.data?.error === "not_in_channel") {
await slackClient.conversations.join({
channel: chatPostMessageArgs.channel,
});
res = await slackClient.chat.postMessage(chatPostMessageArgs) as ChatPostMessageResponse;
} else {
throw ex;
}
}
}

this.addRecentSlackMessage(res.ts);
this.addRecentSlackMessage(res.ts);
return res;
});

this.main.incCounter(METRIC_SENT_MESSAGES, {side: "remote"});
// Log activity, but don't await the answer or throw errors
Expand Down Expand Up @@ -710,7 +719,7 @@
if (ghostChanged) {
await this.main.fixDMMetadata(this, ghost);
}
this.slackSendLock = this.slackSendLock.then(() => {
await this.pushToBridgingQueue(async () => {
// Check again
if (this.recentSlackMessages.includes(message.ts)) {
// We sent this, ignore
Expand All @@ -720,7 +729,6 @@
log.warn(`Failed to handle slack message ${message.ts} for ${this.MatrixRoomId} ${this.slackChannelId}`, ex);
});
});
await this.slackSendLock;
} catch (err) {
log.error("Failed to process event");
log.error(err);
Expand Down
Loading