Skip to content
Extraits de code Groupes Projets
Non vérifiée Valider 82995165 rédigé par Sorunome's avatar Sorunome
Parcourir les fichiers

handle replies to media stuffs

parent ed12b5cb
Branches
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -137,21 +137,12 @@ export class MatrixEventProcessor { ...@@ -137,21 +137,12 @@ export class MatrixEventProcessor {
} }
public async HandleAttachment(event: IMatrixEvent, mxClient: MatrixClient): Promise<string|Discord.FileOptions> { public async HandleAttachment(event: IMatrixEvent, mxClient: MatrixClient): Promise<string|Discord.FileOptions> {
if (!event.content) { if (!this.HasAttachment(event)) {
event.content = {}; return "";
} }
const hasAttachment = [ if (!event.content) {
"m.image", event.content = {};
"m.audio",
"m.video",
"m.file",
"m.sticker",
].includes(event.content.msgtype as string) || [
"m.sticker",
].includes(event.type);
if (!hasAttachment) {
return "";
} }
if (!event.content.info) { if (!event.content.info) {
...@@ -211,6 +202,19 @@ export class MatrixEventProcessor { ...@@ -211,6 +202,19 @@ export class MatrixEventProcessor {
} }
replyEmbed.setTimestamp(new Date(sourceEvent.origin_server_ts)); replyEmbed.setTimestamp(new Date(sourceEvent.origin_server_ts));
if (this.HasAttachment(sourceEvent)) {
const mxClient = this.bridge.getClientFactory().getClientAs();
const url = mxClient.mxcUrlToHttp(sourceEvent.content.url);
if (["m.image", "m.sticker"].includes(sourceEvent.content.msgtype as string)
|| sourceEvent.type === "m.sticker") {
// we have an image reply
replyEmbed.setImage(url);
} else {
const name = this.GetFilenameForMediaEvent(sourceEvent.content);
replyEmbed.description = `[${name}](${url})`;
}
}
return replyEmbed; return replyEmbed;
} catch (ex) { } catch (ex) {
log.warn("Failed to handle reply, showing a unknown embed:", ex); log.warn("Failed to handle reply, showing a unknown embed:", ex);
...@@ -222,6 +226,23 @@ export class MatrixEventProcessor { ...@@ -222,6 +226,23 @@ export class MatrixEventProcessor {
return embed; return embed;
} }
private HasAttachment(event: IMatrixEvent): boolean {
if (!event.content) {
event.content = {};
}
const hasAttachment = [
"m.image",
"m.audio",
"m.video",
"m.file",
"m.sticker",
].includes(event.content.msgtype as string) || [
"m.sticker",
].includes(event.type);
return hasAttachment;
}
private async SetEmbedAuthor(embed: Discord.RichEmbed, sender: string, profile?: IMatrixEvent | null) { private async SetEmbedAuthor(embed: Discord.RichEmbed, sender: string, profile?: IMatrixEvent | null) {
const intent = this.bridge.getIntent(); const intent = this.bridge.getIntent();
let displayName = sender; let displayName = sender;
......
...@@ -124,6 +124,24 @@ function createMatrixEventProcessor(): MatrixEventProcessor { ...@@ -124,6 +124,24 @@ function createMatrixEventProcessor(): MatrixEventProcessor {
}, },
sender: "@_discord_1234:localhost", sender: "@_discord_1234:localhost",
}; };
} else if (eventId === "$image:localhost") {
return {
content: {
body: "fox.jpg",
msgtype: "m.image",
url: "mxc://fox/localhost",
},
sender: "@fox:localhost",
};
} else if (eventId === "$file:localhost") {
return {
content: {
body: "package.zip",
msgtype: "m.file",
url: "mxc://package/localhost",
},
sender: "@fox:localhost",
};
} }
return null; return null;
}, },
...@@ -699,5 +717,38 @@ This is the reply`, ...@@ -699,5 +717,38 @@ This is the reply`,
} }
expect(foundField).to.be.true; expect(foundField).to.be.true;
}); });
it("should handle replies to images", async () => {
const processor = createMatrixEventProcessor();
const result = await processor.GetEmbedForReply({
content: {
"body": "Test",
"m.relates_to": {
"m.in_reply_to": {
event_id: "$image:localhost",
},
},
},
sender: "@test:localhost",
type: "m.room.message",
} as IMatrixEvent, mockChannel as any);
expect(result!.image!.url!).to.be.equal("https://fox/localhost");
expect(result!.description).to.be.equal("fox.jpg");
});
it("should handle replies to files", async () => {
const processor = createMatrixEventProcessor();
const result = await processor.GetEmbedForReply({
content: {
"body": "Test",
"m.relates_to": {
"m.in_reply_to": {
event_id: "$file:localhost",
},
},
},
sender: "@test:localhost",
type: "m.room.message",
} as IMatrixEvent, mockChannel as any);
expect(result!.description).to.be.equal("[package.zip](https://package/localhost)");
});
}); });
}); });
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment