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 { lookup } from "dns";
|
||||||
import { Converter } from "showdown";
|
import { Converter } from "showdown";
|
||||||
import { MessagesMessageAttachment } from "vk-io/lib/api/schemas/objects";
|
import { MessagesMessageAttachment } from "vk-io/lib/api/schemas/objects";
|
||||||
|
import { ElementFlags, OptionalTypeNode } from "typescript";
|
||||||
|
|
||||||
// here we create our log instance
|
// here we create our log instance
|
||||||
const log = new Log("VKPuppet:vk");
|
const log = new Log("VKPuppet:vk");
|
||||||
|
@ -487,6 +488,21 @@ export class VkPuppet {
|
||||||
// VK -> Matrix section //
|
// 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) {
|
public async handleVkMessage(puppetId: number, context: MessageContext) {
|
||||||
const p = this.puppets[puppetId];
|
const p = this.puppets[puppetId];
|
||||||
if (!p) {
|
if (!p) {
|
||||||
|
@ -536,32 +552,21 @@ export class VkPuppet {
|
||||||
const attachments = p.data.isUserToken
|
const attachments = p.data.isUserToken
|
||||||
? (await p.client.api.messages.getById({ message_ids: context.id })).items[0].attachments!
|
? (await p.client.api.messages.getById({ message_ids: context.id })).items[0].attachments!
|
||||||
: context.attachments;
|
: context.attachments;
|
||||||
|
|
||||||
for (const f of attachments) {
|
for (const f of attachments) {
|
||||||
switch (f.type) {
|
switch (f.type) {
|
||||||
case AttachmentType.PHOTO:
|
case AttachmentType.PHOTO:
|
||||||
try {
|
try {
|
||||||
if (p.data.isUserToken) {
|
if (p.data.isUserToken) {
|
||||||
// VK API is weird. Very weird.
|
// VK API is weird. Very weird.
|
||||||
let url: string = "";
|
let biggestImage = this.getBiggestImage(
|
||||||
f["photo"]["sizes"].forEach((element) => {
|
f["photo"]["sizes"]
|
||||||
if (element["type"] === "w") {
|
);
|
||||||
url = element["url"] || "";
|
let url: string = biggestImage['url'] || "";
|
||||||
}
|
|
||||||
});
|
|
||||||
if (url === "") {
|
if (url === "") {
|
||||||
f["photo"]["sizes"].forEach((element) => {
|
log.error(`Image not found in ${f["photo"]}`);
|
||||||
if (element["type"] === "z") {
|
};
|
||||||
url = element["url"] || "";
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (url === undefined) {
|
|
||||||
f["photo"]["sizes"].forEach((element) => {
|
|
||||||
if (element["type"] === "y") {
|
|
||||||
url = element["url"];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
await this.puppet.sendFileDetect(params, url);
|
await this.puppet.sendFileDetect(params, url);
|
||||||
} else {
|
} else {
|
||||||
await this.puppet.sendFileDetect(params, f["largeSizeUrl"]);
|
await this.puppet.sendFileDetect(params, f["largeSizeUrl"]);
|
||||||
|
@ -573,6 +578,7 @@ export class VkPuppet {
|
||||||
await this.puppet.sendMessage(params, opts);
|
await this.puppet.sendMessage(params, opts);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AttachmentType.STICKER:
|
case AttachmentType.STICKER:
|
||||||
try {
|
try {
|
||||||
p.data.isUserToken ? await this.puppet.sendFileDetect(params, f["sticker"]["images_with_background"][4]["url"])
|
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);
|
await this.puppet.sendMessage(params, opts);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AttachmentType.AUDIO_MESSAGE:
|
case AttachmentType.AUDIO_MESSAGE:
|
||||||
try {
|
try {
|
||||||
await this.puppet.sendAudio(params, f["oggUrl"]);
|
await this.puppet.sendAudio(params, f["oggUrl"]);
|
||||||
|
@ -594,6 +601,7 @@ export class VkPuppet {
|
||||||
await this.puppet.sendMessage(params, opts);
|
await this.puppet.sendMessage(params, opts);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AttachmentType.AUDIO:
|
case AttachmentType.AUDIO:
|
||||||
try {
|
try {
|
||||||
await this.puppet.sendAudio(params, f["url"]);
|
await this.puppet.sendAudio(params, f["url"]);
|
||||||
|
@ -604,6 +612,7 @@ export class VkPuppet {
|
||||||
await this.puppet.sendMessage(params, opts);
|
await this.puppet.sendMessage(params, opts);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AttachmentType.DOCUMENT:
|
case AttachmentType.DOCUMENT:
|
||||||
try {
|
try {
|
||||||
p.data.isUserToken ? await this.puppet.sendFileDetect(params, f["doc"]["url"], f["doc"]["title"])
|
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);
|
await this.puppet.sendMessage(params, opts);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AttachmentType.LINK:
|
case AttachmentType.LINK:
|
||||||
await this.puppet.sendMessage(params, {
|
await this.puppet.sendMessage(params, {
|
||||||
body: `Link was sent: ${f["url"]}`,
|
body: `Link was sent: ${f["url"]}`,
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AttachmentType.WALL:
|
case AttachmentType.WALL:
|
||||||
await this.puppet.sendMessage(params, {
|
await this.puppet.sendMessage(params, {
|
||||||
body: await this.renderWallPost(puppetId, f),
|
body: await this.renderWallPost(puppetId, f),
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AttachmentType.WALL_REPLY:
|
case AttachmentType.WALL_REPLY:
|
||||||
await this.puppet.sendMessage(params, {
|
await this.puppet.sendMessage(params, {
|
||||||
body: await this.renderWallPost(puppetId, f),
|
body: await this.renderWallPost(puppetId, f),
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
await this.puppet.sendMessage(params, {
|
await this.puppet.sendMessage(params, {
|
||||||
body: `Unhandled attachment of type ${f.type}`,
|
body: `Unhandled attachment of type ${f.type}`,
|
||||||
|
|
Loading…
Reference in a new issue