Merge branch 'master' into fix-attachments

This commit is contained in:
Inex Code 2021-06-21 17:58:29 +03:00 committed by GitHub
commit 086e3bf50b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 31 deletions

20
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "mx-puppet-vk", "name": "mx-puppet-vk",
"version": "0.4.0", "version": "0.5.1",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
@ -1296,9 +1296,9 @@
} }
}, },
"lodash": { "lodash": {
"version": "4.17.20", "version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
"integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==" "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg=="
}, },
"lodash.camelcase": { "lodash.camelcase": {
"version": "4.3.0", "version": "4.3.0",
@ -1853,9 +1853,9 @@
"integrity": "sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY=" "integrity": "sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY="
}, },
"postcss": { "postcss": {
"version": "7.0.35", "version": "7.0.36",
"resolved": "https://registry.npmjs.org/postcss/-/postcss-7.0.35.tgz", "resolved": "https://registry.npmjs.org/postcss/-/postcss-7.0.36.tgz",
"integrity": "sha512-3QT8bBJeX/S5zKTTjTCIjRF3If4avAT6kqxcASlTWEtAFCb9NH0OUxNDfgZSWdP5fJnBYCMEWkIFfWeugjzYMg==", "integrity": "sha512-BebJSIUMwJHRH0HAQoxN4u1CN86glsrwsW0q7T+/m44eXOUAxSNdHRkNZPYz5vVUbg17hFgOQDE7fZk7li3pZw==",
"requires": { "requires": {
"chalk": "^2.4.2", "chalk": "^2.4.2",
"source-map": "^0.6.1", "source-map": "^0.6.1",
@ -2691,9 +2691,9 @@
"integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==" "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ=="
}, },
"y18n": { "y18n": {
"version": "4.0.0", "version": "4.0.3",
"resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.0.tgz", "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.3.tgz",
"integrity": "sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==" "integrity": "sha512-JKhqTOwSrqNA1NY5lSztJ1GrBiUodLMmIZuLiDaMRJ+itFd+ABVE8XBjOvIWL+rSqNDC74LCSFmlb/U4UZ4hJQ=="
}, },
"yallist": { "yallist": {
"version": "4.0.0", "version": "4.0.0",

View File

@ -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,33 +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(
// Trying to find max size of image f["photo"]["sizes"]
let widthMax: number = 0; );
let widthMaxUrl: string = ""; let url: string = biggestImage['url'] || "";
let heightMax: number = 0;
let heightMaxUrl: string = ""; if (url === "") {
f["photo"]["sizes"].forEach((photoSize) => { log.error(`Image not found in ${f["photo"]}`);
if (photoSize["width"] > widthMax) { };
widthMax = photoSize["width"];
widthMaxUrl = photoSize["url"];
}
if (photoSize["height"] > widthMax) {
heightMax = photoSize["height"];
heightMaxUrl = photoSize["url"];
}
});
if (widthMax > 0 && widthMax > heightMax) {
url = widthMaxUrl;
} else {
url = heightMaxUrl;
}
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"]);
@ -574,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"])
@ -585,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"]);
@ -595,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"]);
@ -605,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"])
@ -616,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: ${f["link"]["url"]}`, body: `Link: ${f["link"]["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}`,