Merge branch 'master' into fix-attachments
This commit is contained in:
commit
086e3bf50b
20
package-lock.json
generated
20
package-lock.json
generated
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "mx-puppet-vk",
|
||||
"version": "0.4.0",
|
||||
"version": "0.5.1",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
@ -1296,9 +1296,9 @@
|
|||
}
|
||||
},
|
||||
"lodash": {
|
||||
"version": "4.17.20",
|
||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz",
|
||||
"integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA=="
|
||||
"version": "4.17.21",
|
||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
|
||||
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg=="
|
||||
},
|
||||
"lodash.camelcase": {
|
||||
"version": "4.3.0",
|
||||
|
@ -1853,9 +1853,9 @@
|
|||
"integrity": "sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY="
|
||||
},
|
||||
"postcss": {
|
||||
"version": "7.0.35",
|
||||
"resolved": "https://registry.npmjs.org/postcss/-/postcss-7.0.35.tgz",
|
||||
"integrity": "sha512-3QT8bBJeX/S5zKTTjTCIjRF3If4avAT6kqxcASlTWEtAFCb9NH0OUxNDfgZSWdP5fJnBYCMEWkIFfWeugjzYMg==",
|
||||
"version": "7.0.36",
|
||||
"resolved": "https://registry.npmjs.org/postcss/-/postcss-7.0.36.tgz",
|
||||
"integrity": "sha512-BebJSIUMwJHRH0HAQoxN4u1CN86glsrwsW0q7T+/m44eXOUAxSNdHRkNZPYz5vVUbg17hFgOQDE7fZk7li3pZw==",
|
||||
"requires": {
|
||||
"chalk": "^2.4.2",
|
||||
"source-map": "^0.6.1",
|
||||
|
@ -2691,9 +2691,9 @@
|
|||
"integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ=="
|
||||
},
|
||||
"y18n": {
|
||||
"version": "4.0.0",
|
||||
"resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.0.tgz",
|
||||
"integrity": "sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w=="
|
||||
"version": "4.0.3",
|
||||
"resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.3.tgz",
|
||||
"integrity": "sha512-JKhqTOwSrqNA1NY5lSztJ1GrBiUodLMmIZuLiDaMRJ+itFd+ABVE8XBjOvIWL+rSqNDC74LCSFmlb/U4UZ4hJQ=="
|
||||
},
|
||||
"yallist": {
|
||||
"version": "4.0.0",
|
||||
|
|
54
src/vk.ts
54
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,33 +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 = "";
|
||||
// Trying to find max size of image
|
||||
let widthMax: number = 0;
|
||||
let widthMaxUrl: string = "";
|
||||
let heightMax: number = 0;
|
||||
let heightMaxUrl: string = "";
|
||||
f["photo"]["sizes"].forEach((photoSize) => {
|
||||
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;
|
||||
}
|
||||
let biggestImage = this.getBiggestImage(
|
||||
f["photo"]["sizes"]
|
||||
);
|
||||
let url: string = biggestImage['url'] || "";
|
||||
|
||||
if (url === "") {
|
||||
log.error(`Image not found in ${f["photo"]}`);
|
||||
};
|
||||
await this.puppet.sendFileDetect(params, url);
|
||||
} else {
|
||||
await this.puppet.sendFileDetect(params, f["largeSizeUrl"]);
|
||||
|
@ -574,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"])
|
||||
|
@ -585,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"]);
|
||||
|
@ -595,6 +601,7 @@ export class VkPuppet {
|
|||
await this.puppet.sendMessage(params, opts);
|
||||
}
|
||||
break;
|
||||
|
||||
case AttachmentType.AUDIO:
|
||||
try {
|
||||
await this.puppet.sendAudio(params, f["url"]);
|
||||
|
@ -605,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"])
|
||||
|
@ -616,21 +624,25 @@ export class VkPuppet {
|
|||
await this.puppet.sendMessage(params, opts);
|
||||
}
|
||||
break;
|
||||
|
||||
case AttachmentType.LINK:
|
||||
await this.puppet.sendMessage(params, {
|
||||
body: `Link: ${f["link"]["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