Refactored image url search, fixed url for link attachments

This commit is contained in:
Alexey Murz Korepov 2021-02-17 08:45:26 +03:00
parent 0ae82e5473
commit 40c7caf0ab
1 changed files with 23 additions and 22 deletions

View File

@ -543,24 +543,25 @@ export class VkPuppet {
if (p.data.isUserToken) { if (p.data.isUserToken) {
// VK API is weird. Very weird. // VK API is weird. Very weird.
let url: string = ""; let url: string = "";
f["photo"]["sizes"].forEach((element) => { // Trying to find max size of image
if (element["type"] === "w") { let widthMax: number = 0;
url = element["url"] || ""; 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 (url === "") { if (widthMax > 0 && widthMax > heightMax) {
f["photo"]["sizes"].forEach((element) => { url = widthMaxUrl;
if (element["type"] === "z") { } else {
url = element["url"] || ""; url = heightMaxUrl;
}
});
}
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 {
@ -568,7 +569,7 @@ export class VkPuppet {
} }
} catch (err) { } catch (err) {
const opts: IMessageEvent = { const opts: IMessageEvent = {
body: `Image was sent: ${f["largeSizeUrl"]}`, body: `Image: ${f["image"]["largeSizeUrl"]}`,
}; };
await this.puppet.sendMessage(params, opts); await this.puppet.sendMessage(params, opts);
} }
@ -579,7 +580,7 @@ export class VkPuppet {
: await this.puppet.sendFileDetect(params, f["imagesWithBackground"][4]["url"]); : await this.puppet.sendFileDetect(params, f["imagesWithBackground"][4]["url"]);
} catch (err) { } catch (err) {
const opts: IMessageEvent = { const opts: IMessageEvent = {
body: `Sticker was sent: ${f["imagesWithBackground"][4]["url"]}`, body: `Sticker: ${f["imagesWithBackground"][4]["url"]}`,
}; };
await this.puppet.sendMessage(params, opts); await this.puppet.sendMessage(params, opts);
} }
@ -589,7 +590,7 @@ export class VkPuppet {
await this.puppet.sendAudio(params, f["oggUrl"]); await this.puppet.sendAudio(params, f["oggUrl"]);
} catch (err) { } catch (err) {
const opts: IMessageEvent = { const opts: IMessageEvent = {
body: `Audio message was sent: ${f["url"]}`, body: `Audio message: ${f["url"]}`,
}; };
await this.puppet.sendMessage(params, opts); await this.puppet.sendMessage(params, opts);
} }
@ -599,7 +600,7 @@ export class VkPuppet {
await this.puppet.sendAudio(params, f["url"]); await this.puppet.sendAudio(params, f["url"]);
} catch (err) { } catch (err) {
const opts: IMessageEvent = { const opts: IMessageEvent = {
body: `Music was sent: ${f["title"]} by ${f["artist"]} ${f["url"]}`, body: `Audio: ${f["title"]} by ${f["artist"]} ${f["url"]}`,
}; };
await this.puppet.sendMessage(params, opts); await this.puppet.sendMessage(params, opts);
} }
@ -610,14 +611,14 @@ export class VkPuppet {
: await this.puppet.sendFileDetect(params, f["url"], f["title"]); : await this.puppet.sendFileDetect(params, f["url"], f["title"]);
} catch (err) { } catch (err) {
const opts: IMessageEvent = { const opts: IMessageEvent = {
body: `Document was sent: ${f["url"]}`, body: `Document: ${f["url"]}`,
}; };
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: ${f["link"]["url"]}`,
}); });
break; break;
case AttachmentType.WALL: case AttachmentType.WALL: