-
Notifications
You must be signed in to change notification settings - Fork 153
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
feat: add Matrix -> Discord reactions #877
Open
mangofeet
wants to merge
6
commits into
matrix-org:develop
Choose a base branch
from
mangofeet:matrix-reactions
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ae4fa54
add matrix -> discord reactions
mangofeet 9472a10
Apply suggestions from code review
mangofeet ded5ef1
support reaction redaction from Matrix -> Discord
mangofeet 3c32f23
handle thumbs up edge case
mangofeet 28ced77
add changelog file
mangofeet 56a75aa
add an initial test
mangofeet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Adds one-way reaction support from Matrix -> Discord. | ||
|
||
This will only respond to the first reaction of each emoji and is intended for cases where the bridge is used by a single matrix user. | ||
|
||
Set `bridge.enableMatrixReactions` to `true` to enable this feature |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import { DiscordClientFactory } from "./clientfactory"; | |
import { DiscordStore } from "./store"; | ||
import { DbEmoji } from "./db/dbdataemoji"; | ||
import { DbEvent } from "./db/dbdataevent"; | ||
import { DbReaction } from "./db/dbdatareaction"; | ||
import { DiscordMessageProcessor } from "./discordmessageprocessor"; | ||
import { IDiscordMessageParserResult } from "@mx-puppet/matrix-discord-parser"; | ||
import { MatrixEventProcessor, MatrixEventProcessorOpts, IMatrixEventProcessorResult } from "./matrixeventprocessor"; | ||
|
@@ -656,12 +657,20 @@ export class DiscordBot { | |
log.info(`Got redact request for ${event.redacts}`); | ||
log.verbose(`Event:`, event); | ||
|
||
const storeEvent = await this.store.Get(DbEvent, {matrix_id: `${event.redacts};${event.room_id}`}); | ||
const storeEvent = await this.store.Get(DbEvent, { matrix_id: `${event.redacts};${event.room_id}` }); | ||
const storeReaction = await this.store.Get(DbReaction, { matrix_id: `${event.redacts};${event.room_id}` }); | ||
|
||
if (!storeEvent || !storeEvent.Result) { | ||
log.warn(`Could not redact because the event was not in the store.`); | ||
return; | ||
if (storeEvent && storeEvent.Result) { | ||
return this.ProcessMatrixRedactEvent(event, storeEvent); | ||
} else if (storeReaction && storeReaction.Result) { | ||
return this.ProcessMatrixRedactReaction(event, storeReaction); | ||
} | ||
|
||
log.warn(`Could not redact because the event was not in the store.`); | ||
return; | ||
} | ||
|
||
public async ProcessMatrixRedactEvent(event: IMatrixEvent, storeEvent: DbEvent) { | ||
log.info(`Redact event matched ${storeEvent.ResultCount} entries`); | ||
while (storeEvent.Next()) { | ||
log.info(`Deleting discord msg ${storeEvent.DiscordId}`); | ||
|
@@ -680,6 +689,86 @@ export class DiscordBot { | |
} | ||
} | ||
|
||
public async ProcessMatrixRedactReaction(event: IMatrixEvent, storeReaction: DbReaction) { | ||
log.info(`Redact reaction matched ${storeReaction.ResultCount} entries`); | ||
while (storeReaction.Next()) { | ||
log.info(`Deleting discord reaction ${storeReaction.DiscordId}`); | ||
const result = await this.LookupRoom(storeReaction.GuildId, storeReaction.ChannelId, event.sender); | ||
const chan = result.channel; | ||
|
||
const msg = await chan.messages.fetch(storeReaction.DiscordId); | ||
try { | ||
this.channelLock.set(msg.channel.id); | ||
|
||
const reaction = msg.reactions.resolve(storeReaction.Emoji) | ||
if (!reaction) { | ||
log.warn(`Could not find reaction for emoji ${storeReaction.Emoji}`) | ||
continue | ||
} | ||
await reaction.users.remove() | ||
this.channelLock.release(msg.channel.id); | ||
log.info(`Deleted reaction`); | ||
} catch (ex) { | ||
log.warn(`Failed to delete message`, ex); | ||
} | ||
} | ||
} | ||
|
||
public async ProcessMatrixReaction(event: IMatrixEvent) { | ||
if (!this.config.bridge.enableMatrixReactions) { | ||
return; | ||
} | ||
log.info(`Got reaction request`); | ||
log.verbose(`Event:`, event); | ||
|
||
if (!event.content) return; | ||
if (!event.content['m.relates_to']) return; | ||
|
||
const relatesTo = event.content['m.relates_to']; | ||
|
||
let emoji = relatesTo.key | ||
if (emoji.indexOf("👍") >=0 ) { | ||
emoji = "👍" | ||
} | ||
Comment on lines
+729
to
+732
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After reading this if statement, I'm assuming that this is related to the skin-tones? If so, you may want to do proper parsing of those, as more than thumbs up is affected. There's also country flags which use a similar concept. |
||
|
||
const matrixID = `${relatesTo.event_id};${event.room_id}` | ||
const storeEvent = await this.store.Get(DbEvent, { matrix_id: matrixID }); | ||
|
||
if (!storeEvent || !storeEvent.Result) { | ||
log.warn(`Could not react because the event was not in the store.`); | ||
return; | ||
} | ||
log.info(`React event matched ${storeEvent.ResultCount} entries`); | ||
while (storeEvent.Next()) { | ||
log.info(`Reacting to Discord msg ${storeEvent.DiscordId}`); | ||
const result = await this.LookupRoom(storeEvent.GuildId, storeEvent.ChannelId, event.sender); | ||
const chan = result.channel; | ||
|
||
const msg = await chan.messages.fetch(storeEvent.DiscordId); | ||
try { | ||
this.channelLock.set(msg.channel.id); | ||
await msg.react(emoji); | ||
this.channelLock.release(msg.channel.id); | ||
log.info(`Reacted to message`); | ||
} catch (ex) { | ||
log.warn(`Failed to react to message`, ex); | ||
} | ||
|
||
try { | ||
const dbReaction = new DbReaction() | ||
dbReaction.MatrixId = `${event.event_id};${event.room_id}`; | ||
dbReaction.DiscordId = storeEvent.DiscordId; | ||
dbReaction.ChannelId = storeEvent.ChannelId; | ||
dbReaction.GuildId = storeEvent.GuildId; | ||
dbReaction.Emoji = emoji; | ||
await this.store.Insert(dbReaction); | ||
} catch (ex) { | ||
log.warn(`Failed to store reaction event`) | ||
log.verbose(ex) | ||
} | ||
} | ||
} | ||
|
||
public OnUserQuery(userId: string): boolean { | ||
return false; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
Copyright 2017, 2018 matrix-appservice-discord | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { DiscordStore } from "../store"; | ||
import { ISqlCommandParameters } from "./connector"; | ||
import { IDbDataMany } from "./dbdatainterface"; | ||
|
||
export class DbReaction implements IDbDataMany { | ||
public MatrixId: string; | ||
public DiscordId: string; | ||
public GuildId: string; | ||
public ChannelId: string; | ||
public Result: boolean; | ||
public Emoji: string; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
private rows: any[]; | ||
|
||
get ResultCount(): number { | ||
return this.rows.length; | ||
} | ||
|
||
public async RunQuery(store: DiscordStore, params: ISqlCommandParameters): Promise<void> { | ||
this.rows = []; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let rowsM: any[] | null = null; | ||
if (params.matrix_id && params.emoji) { | ||
rowsM = await store.db.All(` | ||
SELECT * | ||
FROM reaction_store | ||
WHERE matrix_id = $id AND emoji = $emoji`, { | ||
id: params.matrix_id, | ||
emoji: params.emoji, | ||
}); | ||
} else if (params.matrix_id) { | ||
rowsM = await store.db.All(` | ||
SELECT * | ||
FROM reaction_store | ||
WHERE matrix_id = $id`, { | ||
id: params.matrix_id | ||
}); | ||
} else if (params.discord_id) { | ||
rowsM = await store.db.All(` | ||
SELECT * | ||
FROM reaction_store | ||
WHERE discord_id = $id`, { | ||
id: params.discord_id, | ||
}); | ||
} else { | ||
throw new Error("Unknown/incorrect id given as a param"); | ||
} | ||
|
||
for (const rowM of rowsM) { | ||
const row = { | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
discord_id: rowM.discord_id, | ||
matrix_id: rowM.matrix_id, | ||
emoji: rowM.emoji, | ||
/* eslint-enable @typescript-eslint/naming-convention */ | ||
}; | ||
for (const rowD of await store.db.All(` | ||
SELECT * | ||
FROM discord_msg_store | ||
WHERE msg_id = $id`, { | ||
id: rowM.discord_id, | ||
})) { | ||
this.rows.push({ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
...row, | ||
guild_id: rowD.guild_id, | ||
channel_id: rowD.channel_id, | ||
/* eslint-enable @typescript-eslint/naming-convention */ | ||
}); | ||
} | ||
} | ||
this.Result = this.rows.length !== 0; | ||
} | ||
|
||
public Next(): boolean { | ||
if (!this.Result || this.ResultCount === 0) { | ||
return false; | ||
} | ||
const item = this.rows.shift(); | ||
this.MatrixId = item.matrix_id; | ||
this.DiscordId = item.discord_id; | ||
this.Emoji = item.emoji; | ||
this.GuildId = item.guild_id; | ||
this.ChannelId = item.channel_id; | ||
return true; | ||
} | ||
|
||
public async Insert(store: DiscordStore): Promise<void> { | ||
await store.db.Run(` | ||
INSERT INTO reaction_store | ||
(matrix_id,discord_id,emoji) | ||
VALUES ($matrix_id,$discord_id,$emoji);`, { | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
discord_id: this.DiscordId, | ||
matrix_id: this.MatrixId, | ||
emoji: this.Emoji, | ||
/* eslint-enable @typescript-eslint/naming-convention */ | ||
}); | ||
// Check if the discord item exists? | ||
const msgExists = await store.db.Get(` | ||
SELECT * | ||
FROM discord_msg_store | ||
WHERE msg_id = $id`, { | ||
id: this.DiscordId, | ||
}) != null; | ||
if (msgExists) { | ||
return; | ||
} | ||
return store.db.Run(` | ||
INSERT INTO discord_msg_store | ||
(msg_id, guild_id, channel_id) | ||
VALUES ($msg_id, $guild_id, $channel_id);`, { | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
channel_id: this.ChannelId, | ||
guild_id: this.GuildId, | ||
msg_id: this.DiscordId, | ||
/* eslint-enable @typescript-eslint/naming-convention */ | ||
}); | ||
} | ||
|
||
public async Update(store: DiscordStore): Promise<void> { | ||
throw new Error("Update is not implemented"); | ||
} | ||
|
||
public async Delete(store: DiscordStore): Promise<void> { | ||
await store.db.Run(` | ||
DELETE FROM reaction_store | ||
WHERE matrix_id = $matrix_id | ||
AND discord_id = $discord_id;`, { | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
discord_id: this.DiscordId, | ||
matrix_id: this.MatrixId, | ||
/* eslint-enable @typescript-eslint/naming-convention */ | ||
}); | ||
return store.db.Run(` | ||
DELETE FROM discord_msg_store | ||
WHERE msg_id = $discord_id;`, { | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
discord_id: this.DiscordId, | ||
/* eslint-enable @typescript-eslint/naming-convention */ | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
Copyright 2017, 2018 matrix-appservice-discord | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { DiscordStore } from "../../store"; | ||
import { IDbSchema } from "./dbschema"; | ||
|
||
export class Schema implements IDbSchema { | ||
public description = "create event_store table"; | ||
public async run(store: DiscordStore): Promise<void> { | ||
await store.createTable(` | ||
CREATE TABLE reaction_store ( | ||
matrix_id TEXT NOT NULL, | ||
discord_id TEXT NOT NULL, | ||
emoji TEXT NOT NULL, | ||
PRIMARY KEY(matrix_id, discord_id) | ||
);`, "event_store"); | ||
} | ||
|
||
public async rollBack(store: DiscordStore): Promise<void> { | ||
await store.db.Run( | ||
`DROP TABLE IF EXISTS reaction_store;`, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice optimisation for having to build the first step less often. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wasn't sure if this should go in it's own PR, I was going to revert it, but yeah, it does speed up building by quite a bit when trying to test changes on a remote machine instead of locally.
EDIT: There's a way to make it even faster by adding just the
package.json
andyarn.lock
files, doing the yarn install, then adding the source code and doing the build separately