Fix sending images from vk to matrix for user tokens (#17)
This commit is contained in:
parent
60ff20ecca
commit
45836d23e7
51
src/vk.ts
51
src/vk.ts
|
@ -17,6 +17,7 @@ import { runInThisContext } from "vm";
|
|||
import { lookup } from "dns";
|
||||
import { Converter } from "showdown";
|
||||
import { MessagesMessageAttachment } from "vk-io/lib/api/schemas/objects";
|
||||
import { ElementFlags, OptionalTypeNode } from "typescript";
|
||||
|
||||
// here we create our log instance
|
||||
const log = new Log("VKPuppet:vk");
|
||||
|
@ -487,6 +488,21 @@ export class VkPuppet {
|
|||
// VK -> Matrix section //
|
||||
//////////////////////////
|
||||
|
||||
public getBiggestImage(images: Array<object>): any {
|
||||
let maxImageResolution = 0;
|
||||
let biggestImage: any = null;
|
||||
images.forEach(
|
||||
function(image: object) {
|
||||
if (maxImageResolution < (image["width"] + image["height"])) {
|
||||
maxImageResolution = image["width"] + image["height"];
|
||||
biggestImage = image;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return biggestImage;
|
||||
};
|
||||
|
||||
public async handleVkMessage(puppetId: number, context: MessageContext) {
|
||||
const p = this.puppets[puppetId];
|
||||
if (!p) {
|
||||
|
@ -536,32 +552,21 @@ export class VkPuppet {
|
|||
const attachments = p.data.isUserToken
|
||||
? (await p.client.api.messages.getById({ message_ids: context.id })).items[0].attachments!
|
||||
: context.attachments;
|
||||
|
||||
for (const f of attachments) {
|
||||
switch (f.type) {
|
||||
case AttachmentType.PHOTO:
|
||||
try {
|
||||
if (p.data.isUserToken) {
|
||||
// VK API is weird. Very weird.
|
||||
let url: string = "";
|
||||
f["photo"]["sizes"].forEach((element) => {
|
||||
if (element["type"] === "w") {
|
||||
url = element["url"] || "";
|
||||
}
|
||||
});
|
||||
let biggestImage = this.getBiggestImage(
|
||||
f["photo"]["sizes"]
|
||||
);
|
||||
let url: string = biggestImage['url'] || "";
|
||||
|
||||
if (url === "") {
|
||||
f["photo"]["sizes"].forEach((element) => {
|
||||
if (element["type"] === "z") {
|
||||
url = element["url"] || "";
|
||||
}
|
||||
});
|
||||
}
|
||||
if (url === undefined) {
|
||||
f["photo"]["sizes"].forEach((element) => {
|
||||
if (element["type"] === "y") {
|
||||
url = element["url"];
|
||||
}
|
||||
});
|
||||
}
|
||||
log.error(`Image not found in ${f["photo"]}`);
|
||||
};
|
||||
await this.puppet.sendFileDetect(params, url);
|
||||
} else {
|
||||
await this.puppet.sendFileDetect(params, f["largeSizeUrl"]);
|
||||
|
@ -573,6 +578,7 @@ export class VkPuppet {
|
|||
await this.puppet.sendMessage(params, opts);
|
||||
}
|
||||
break;
|
||||
|
||||
case AttachmentType.STICKER:
|
||||
try {
|
||||
p.data.isUserToken ? await this.puppet.sendFileDetect(params, f["sticker"]["images_with_background"][4]["url"])
|
||||
|
@ -584,6 +590,7 @@ export class VkPuppet {
|
|||
await this.puppet.sendMessage(params, opts);
|
||||
}
|
||||
break;
|
||||
|
||||
case AttachmentType.AUDIO_MESSAGE:
|
||||
try {
|
||||
await this.puppet.sendAudio(params, f["oggUrl"]);
|
||||
|
@ -594,6 +601,7 @@ export class VkPuppet {
|
|||
await this.puppet.sendMessage(params, opts);
|
||||
}
|
||||
break;
|
||||
|
||||
case AttachmentType.AUDIO:
|
||||
try {
|
||||
await this.puppet.sendAudio(params, f["url"]);
|
||||
|
@ -604,6 +612,7 @@ export class VkPuppet {
|
|||
await this.puppet.sendMessage(params, opts);
|
||||
}
|
||||
break;
|
||||
|
||||
case AttachmentType.DOCUMENT:
|
||||
try {
|
||||
p.data.isUserToken ? await this.puppet.sendFileDetect(params, f["doc"]["url"], f["doc"]["title"])
|
||||
|
@ -615,21 +624,25 @@ export class VkPuppet {
|
|||
await this.puppet.sendMessage(params, opts);
|
||||
}
|
||||
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:
|
||||
await this.puppet.sendMessage(params, {
|
||||
body: `Unhandled attachment of type ${f.type}`,
|
||||
|
|
Loading…
Reference in a new issue