UNPKG

@juzi/wechaty

Version:

Wechaty is a RPA SDK for Chatbot Makers.

119 lines 3.96 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UrlLinkImpl = void 0; /** * Wechaty Chatbot SDK - https://github.com/wechaty/wechaty * * @copyright 2016 Huan LI (李卓桓) <https://github.com/huan>, and * Wechaty Contributors <https://github.com/wechaty>. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ const url_1 = __importDefault(require("url")); const open_graph_js_1 = require("../helper-functions/open-graph.js"); const validation_js_1 = require("../user-mixins/validation.js"); const wechatify_js_1 = require("../user-mixins/wechatify.js"); const config_js_1 = require("../config.js"); class UrlLinkMixin extends (0, wechatify_js_1.wechatifyMixinBase)() { payload; /** * * Create from URL * */ static async create(url, fallback) { config_js_1.log.verbose('UrlLink', 'create(%s)', url); const meta = await (0, open_graph_js_1.openGraph)(url); let description; let thumbnailUrl; let title; if (meta.image) { if (typeof meta.image === 'string') { thumbnailUrl = meta.image; } else if (Array.isArray(meta.image)) { thumbnailUrl = meta.image[0]; } else { if (Array.isArray(meta.image.url)) { thumbnailUrl = meta.image.url[0]; } else if (meta.image.url) { thumbnailUrl = meta.image.url; } } } if (Array.isArray(meta.title)) { title = meta.title[0]; } else { title = meta.title; } if (Array.isArray(meta.description)) { description = meta.description[0]; } else if (meta.description) { description = meta.description; } else { description = title; } if (thumbnailUrl && !thumbnailUrl.startsWith('http')) { const resolvedUrl = new url_1.default.URL(thumbnailUrl, url); thumbnailUrl = resolvedUrl.toString(); } const payload = { description: description || fallback?.description || '', thumbnailUrl: thumbnailUrl || fallback?.thumbnailUrl || '', title: title || fallback?.title || '', url, }; return new this(payload); } /* * @hideconstructor */ constructor(payload) { super(); this.payload = payload; config_js_1.log.verbose('UrlLink', 'constructor()'); // Huan(202110): it is ok to create a raw one without wechaty instance // guardWechatifyClass.call(this, UrlLink) } toString() { return `UrlLink<${this.payload.url}>`; } url() { return this.payload.url; } title() { return this.payload.title; } thumbnailUrl() { return this.payload.thumbnailUrl; } description() { return this.payload.description; } thumbnailFile() { return this.payload.thumbnailFileBox; } } class UrlLinkImpl extends (0, validation_js_1.validationMixin)(UrlLinkMixin)() { } exports.UrlLinkImpl = UrlLinkImpl; //# sourceMappingURL=url-link.js.map