Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public async GetEmbedForReply(
event: IMatrixEvent,
channel: Discord.TextChannel,
): Promise {
if (!event.content) {
event.content = {};
}
const relatesTo = event.content["m.relates_to"];
let eventId = "";
if (relatesTo && relatesTo["m.in_reply_to"]) {
eventId = relatesTo["m.in_reply_to"].event_id;
} else {
return;
}
const intent = this.bridge.getIntent();
// Try to get the event.
try {
const sourceEvent = await intent.getEvent(event.room_id, eventId);
sourceEvent.content.body = sourceEvent.content.body || "Reply with unknown content";
const replyEmbed = (await this.EventToEmbed(sourceEvent, channel, false)).messageEmbed;
// if we reply to a discord member, ping them!
if (this.bridge.getBot().isRemoteUser(sourceEvent.sender)) {
const uid = new MatrixUser(sourceEvent.sender.replace("@", "")).localpart.substring("_discord".length);
replyEmbed.addField("ping", `<@${uid}>`);
static getParentEventId(ev) {
if (!ev || ev.isRedacted()) return;
// XXX: For newer relations (annotations, replacements, etc.), we now
// have a `getRelation` helper on the event, and you might assume it
// could be used here for replies as well... However, the helper
// currently assumes the relation has a `rel_type`, which older replies
// do not, so this block is left as-is for now.
const mRelatesTo = ev.getWireContent()['m.relates_to'];
if (mRelatesTo && mRelatesTo['m.in_reply_to']) {
const mInReplyTo = mRelatesTo['m.in_reply_to'];
if (mInReplyTo && mInReplyTo['event_id']) return mInReplyTo['event_id'];
}
}
private async findParentReply(message: any, depth: number = 0): Promise {
const MAX_DEPTH = 10;
// Extract the referenced event
if (!message.content) { return message.event_id; }
if (!message.content["m.relates_to"]) { return message.event_id; }
if (!message.content["m.relates_to"]["m.in_reply_to"]) { return message.event_id; }
const parentEventId = message.content["m.relates_to"]["m.in_reply_to"].event_id;
if (!parentEventId) { return message.event_id; }
if (depth > MAX_DEPTH) {
return parentEventId; // We have hit our depth limit, use this one.
}
const intent = await this.getIntentForRoom(message.room_id);
const nextEvent = await intent.getClient().fetchRoomEvent(message.room_id, parentEventId);
return this.findParentReply(nextEvent, depth++);
}
BridgedRoom.prototype.findParentReply = async function(message) {
// Extract the referenced event
if (!message["content"]) return message.event_id;
if (!message["content"]["m.relates_to"]) return message.event_id;
if (!message["content"]["m.relates_to"]["m.in_reply_to"]) return message.event_id;
const parentEventId = message["content"]["m.relates_to"]["m.in_reply_to"]["event_id"];
if (!parentEventId) return message.event_id;
// Get the previous event
const intent = this._main.getBotIntent();
const nextEvent = await intent.getClient().fetchRoomEvent(message.room_id, parentEventId);
return this.findParentReply(nextEvent);
};
static getParentEventId(ev) {
if (!ev || ev.isRedacted()) return;
// XXX: For newer relations (annotations, replacements, etc.), we now
// have a `getRelation` helper on the event, and you might assume it
// could be used here for replies as well... However, the helper
// currently assumes the relation has a `rel_type`, which older replies
// do not, so this block is left as-is for now.
const mRelatesTo = ev.getWireContent()['m.relates_to'];
if (mRelatesTo && mRelatesTo['m.in_reply_to']) {
const mInReplyTo = mRelatesTo['m.in_reply_to'];
if (mInReplyTo && mInReplyTo['event_id']) return mInReplyTo['event_id'];
}
}
public async GetEmbedForReply(
event: IMatrixEvent,
channel: Discord.TextChannel,
): Promise {
if (!event.content) {
event.content = {};
}
const relatesTo = event.content["m.relates_to"];
let eventId = "";
if (relatesTo && relatesTo["m.in_reply_to"]) {
eventId = relatesTo["m.in_reply_to"].event_id;
} else {
return;
}
const intent = this.bridge.getIntent();
// Try to get the event.
try {
const sourceEvent = await intent.getEvent(event.room_id, eventId);
sourceEvent.content.body = sourceEvent.content.body || "Reply with unknown content";
const replyEmbed = (await this.EventToEmbed(sourceEvent, channel, false)).messageEmbed;
// if we reply to a discord member, ping them!
if (this.bridge.getBot().isRemoteUser(sourceEvent.sender)) {
const uid = new MatrixUser(sourceEvent.sender.replace("@", "")).localpart.substring("_discord".length);
replyEmbed.addField("ping", `<@${uid}>`);
}
private async findParentReply(message: any, depth: number = 0): Promise {
const MAX_DEPTH = 10;
// Extract the referenced event
if (!message.content) { return message.event_id; }
if (!message.content["m.relates_to"]) { return message.event_id; }
if (!message.content["m.relates_to"]["m.in_reply_to"]) { return message.event_id; }
const parentEventId = message.content["m.relates_to"]["m.in_reply_to"].event_id;
if (!parentEventId) { return message.event_id; }
if (depth > MAX_DEPTH) {
return parentEventId; // We have hit our depth limit, use this one.
}
const intent = await this.getIntentForRoom(message.room_id);
const nextEvent = await intent.getClient().fetchRoomEvent(message.room_id, parentEventId);
return this.findParentReply(nextEvent, depth++);
}
BridgedRoom.prototype.findParentReply = async function(message) {
// Extract the referenced event
if (!message["content"]) return message.event_id;
if (!message["content"]["m.relates_to"]) return message.event_id;
if (!message["content"]["m.relates_to"]["m.in_reply_to"]) return message.event_id;
const parentEventId = message["content"]["m.relates_to"]["m.in_reply_to"]["event_id"];
if (!parentEventId) return message.event_id;
// Get the previous event
const intent = this._main.getBotIntent();
const nextEvent = await intent.getClient().fetchRoomEvent(message.room_id, parentEventId);
return this.findParentReply(nextEvent);
};