"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.view = exports.unsub = exports.list = exports.sub = exports.parseCmd = void 0; const fs = require("fs"); const path = require("path"); const datetime_1 = require("./datetime"); const loggers_1 = require("./loggers"); const twitter_1 = require("./twitter"); const utils_1 = require("./utils"); const logger = loggers_1.getLogger('command'); function parseCmd(message) { message = message.trim(); message = message.replace('\\\\', '\\0x5c'); message = message.replace('\\\"', '\\0x22'); message = message.replace('\\\'', '\\0x27'); const strs = message.match(/'[\s\S]*?'|(?:\S+=)?"[\s\S]*?"|\S+/mg); const cmd = (strs === null || strs === void 0 ? void 0 : strs.length) ? strs[0].length ? strs[0].substring(0, 1) === '/' ? strs[0].substring(1) : '' : '' : ''; const args = (strs !== null && strs !== void 0 ? strs : []).slice(1).map(arg => { arg = arg.replace(/^(\S+=)?["']+(?!.*=)|["']+$/g, '$1'); arg = arg.replace('\\0x27', '\\\''); arg = arg.replace('\\0x22', '\\\"'); arg = arg.replace('\\0x5c', '\\\\'); return arg; }); return { cmd, args, }; } exports.parseCmd = parseCmd; function linkFinder(userName, chat, lock) { const normalizedLink = twitter_1.linkBuilder({ userName }); const link = Object.keys(lock.threads).find(realLink => normalizedLink === realLink.replace(/\/@/, '/').toLowerCase()); if (!link) return [null, -1]; const index = lock.threads[link].subscribers.findIndex(({ chatID, chatType }) => chat.chatID === chatID && chat.chatType === chatType); return [link, index]; } function sub(chat, args, reply, lock, lockfile) { if (chat.chatType === "temp") { return reply('请先添加机器人为好友。'); } if (args.length === 0) { return reply('找不到要订阅的链接。'); } const matched = twitter_1.parseLink(args[0]); if (!matched) { return reply(`订阅链接格式错误: 示例: https://www.instagram.com/tomoyo_kurosawa_/ https://www.instagram.com/p/B6GHRSmgV-7/`); } let offset = '0'; const subscribeTo = (link, config = {}) => { const { id, msg = `已为此聊天订阅 ${link}` } = config; if (id) { lock.feed.push(link); lock.threads[link] = { id, offset, subscribers: [], updatedAt: '', }; } lock.threads[link].subscribers.push(chat); logger.warn(`chat ${JSON.stringify(chat)} has subscribed ${link}`); fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock)); reply(msg); }; const tryFindSub = (userName) => { const [realLink, index] = linkFinder(userName, chat, lock); if (index > -1) { reply('此聊天已订阅此链接。'); return true; } if (realLink) { subscribeTo(realLink); return true; } return false; }; const newSub = (userName) => { const link = twitter_1.linkBuilder(matched); let msg; if (offset !== '0') { msg = `已为此聊天订阅 ${link} 并回溯到 ${args[0].replace(/\?.*/, '')}(含)之后的第一条动态`; } return subscribeTo(link, { id: Number(userName.split(':')[1]), msg }); }; if (matched.postUrlSegment) { offset = utils_1.BigNumOps.plus(twitter_1.urlSegmentToId(matched.postUrlSegment), '-1'); twitter_1.getPostOwner(matched.postUrlSegment).then(userName => { delete matched.postUrlSegment; matched.userName = userName.split(':')[0]; if (!tryFindSub(userName)) newSub(userName); }).catch((parsedErr) => { reply(parsedErr.message); }); } else if (!tryFindSub(matched.userName)) { twitter_1.ScreenNameNormalizer.normalizeLive(matched.userName).then(userName => { if (!userName) return reply(`找不到用户 ${matched.userName.replace(/^@?(.*)$/, '@$1')}。`); else newSub(userName); }); } } exports.sub = sub; function unsub(chat, args, reply, lock, lockfile) { var _a; if (chat.chatType === "temp") { return reply('请先添加机器人为好友。'); } if (args.length === 0) { return reply('找不到要退订的链接。'); } const match = (_a = twitter_1.parseLink(args[0])) === null || _a === void 0 ? void 0 : _a.userName; if (!match) { return reply('链接格式有误。'); } const [link, index] = linkFinder(match, chat, lock); if (index === -1) return list(chat, args, msg => reply('您没有订阅此链接。\n' + msg), lock); else { lock.threads[link].subscribers.splice(index, 1); fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock)); logger.warn(`chat ${JSON.stringify(chat)} has unsubscribed ${link}`); return reply(`已为此聊天退订 ${link}`); } } exports.unsub = unsub; function list(chat, _, reply, lock) { if (chat.chatType === "temp") { return reply('请先添加机器人为好友。'); } const links = []; Object.keys(lock.threads).forEach(key => { if (lock.threads[key].subscribers.find(({ chatID, chatType }) => chat.chatID === chatID && chat.chatType === chatType)) links.push(`${key} ${datetime_1.relativeDate(lock.threads[key].updatedAt)}`); }); return reply('此聊天中订阅的 Instagram 动态链接:\n' + links.join('\n')); } exports.list = list; function view(chat, args, reply) { var _a; if (args.length === 0) { return reply('找不到要查看的链接。'); } const match = twitter_1.isValidUrlSegment(args[0]) && args[0] || ((_a = twitter_1.parseLink(args[0])) === null || _a === void 0 ? void 0 : _a.postUrlSegment); if (!match) { return reply('链接格式有误。'); } try { twitter_1.sendPost(match, chat); } catch (e) { reply('机器人尚未加载完毕,请稍后重试。'); } } exports.view = view;