Handle more attachment types, closes #9

This commit is contained in:
Inex Code 2021-02-13 11:44:47 +00:00
parent fd02432559
commit 14e259dcd8

View file

@ -16,6 +16,7 @@ import { userInfo } from "os";
import { runInThisContext } from "vm"; import { runInThisContext } from "vm";
import { lookup } from "dns"; import { lookup } from "dns";
import { Converter } from "showdown"; import { Converter } from "showdown";
import { MessagesMessageAttachment } from "vk-io/lib/api/schemas/objects";
// here we create our log instance // here we create our log instance
const log = new Log("VKPuppet:vk"); const log = new Log("VKPuppet:vk");
@ -614,7 +615,25 @@ export class VkPuppet {
await this.puppet.sendMessage(params, opts); await this.puppet.sendMessage(params, opts);
} }
break; break;
case AttachmentType.LINK:
await this.puppet.sendMessage(params, {
body: `Link was sent: ${f["url"]}`,
});
break;
case AttachmentType.WALL:
await this.puppet.sendMessage(params, {
body: await this.renderWallPost(puppetId, f),
});
break;
case AttachmentType.WALL_REPLY:
await this.puppet.sendMessage(params, {
body: await this.renderWallPost(puppetId, f),
});
break;
default: default:
await this.puppet.sendMessage(params, {
body: `Unhandled attachment of type ${f.type}`,
});
break; break;
} }
} }
@ -698,7 +717,11 @@ export class VkPuppet {
case AttachmentType.DOCUMENT: case AttachmentType.DOCUMENT:
formatted += `> 📁 [File ${attachment["title"]}](${attachment["url"]})\n`; formatted += `> 📁 [File ${attachment["title"]}](${attachment["url"]})\n`;
break; break;
case AttachmentType.LINK:
formatted += `> 🔗 [ ${attachment["title"] ? attachment["title"] : attachment["url"]} ](${attachment["url"]})\n`;
break;
default: default:
formatted += `> ❓️ Unhandled attachment of type ${attachment.type}\n`;
break; break;
} }
}); });
@ -712,4 +735,37 @@ export class VkPuppet {
} }
return formatted; return formatted;
} }
public async renderWallPost(puppetId: number, post: MessagesMessageAttachment) {
const user = await this.getRemoteUser(puppetId, Number(post.fromId));
let formatted = `Forwarded post from [${user.name}](${user.externalUrl})\n`;
post.text?.split("\n").forEach((element) => {
formatted += `> ${element}\n`;
});
if (post.hasAttachments()) {
post.attachments.forEach((attachment) => {
switch (attachment.type) {
case AttachmentType.PHOTO:
formatted += `> 🖼️ [Photo](${attachment["largeSizeUrl"]})\n`;
break;
case AttachmentType.STICKER:
formatted += `> 🖼️ [Sticker](${attachment["imagesWithBackground"][4]["url"]})\n`;
break;
case AttachmentType.AUDIO_MESSAGE:
formatted += `> 🗣️ [Audio message](${attachment["oggUrl"]})\n`;
break;
case AttachmentType.DOCUMENT:
formatted += `> 📁 [File ${attachment["title"]}](${attachment["url"]})\n`;
break;
case AttachmentType.LINK:
formatted += `> 🔗 [ ${attachment["title"] ? attachment["title"] : attachment["url"]} ](${attachment["url"]})\n`;
break;
default:
formatted += `> ❓️ Unhandled attachment of type ${attachment.type}\n`;
break;
}
});
}
return formatted;
}
} }