command.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.view = exports.unsub = exports.list = exports.sub = exports.parseCmd = void 0;
  4. const fs = require("fs");
  5. const path = require("path");
  6. const datetime_1 = require("./datetime");
  7. const loggers_1 = require("./loggers");
  8. const twitter_1 = require("./twitter");
  9. const utils_1 = require("./utils");
  10. const logger = loggers_1.getLogger('command');
  11. function parseCmd(message) {
  12. message = message.trim();
  13. message = message.replace('\\\\', '\\0x5c');
  14. message = message.replace('\\\"', '\\0x22');
  15. message = message.replace('\\\'', '\\0x27');
  16. const strs = message.match(/'[\s\S]*?'|(?:\S+=)?"[\s\S]*?"|\S+/mg);
  17. const cmd = (strs === null || strs === void 0 ? void 0 : strs.length) ? strs[0].length ? strs[0].substring(0, 1) === '/' ? strs[0].substring(1) : '' : '' : '';
  18. const args = (strs !== null && strs !== void 0 ? strs : []).slice(1).map(arg => {
  19. arg = arg.replace(/^(\S+=)?["']+(?!.*=)|["']+$/g, '$1');
  20. arg = arg.replace('\\0x27', '\\\'');
  21. arg = arg.replace('\\0x22', '\\\"');
  22. arg = arg.replace('\\0x5c', '\\\\');
  23. return arg;
  24. });
  25. return {
  26. cmd,
  27. args,
  28. };
  29. }
  30. exports.parseCmd = parseCmd;
  31. function linkFinder(userName, chat, lock) {
  32. const normalizedLink = twitter_1.linkBuilder({ userName });
  33. const link = Object.keys(lock.threads).find(realLink => normalizedLink === realLink.replace(/\/@/, '/').toLowerCase());
  34. if (!link)
  35. return [null, -1];
  36. const index = lock.threads[link].subscribers.findIndex(({ chatID, chatType }) => chat.chatID === chatID && chat.chatType === chatType);
  37. return [link, index];
  38. }
  39. function sub(chat, args, reply, lock, lockfile) {
  40. if (chat.chatType === "temp") {
  41. return reply('请先添加机器人为好友。');
  42. }
  43. if (args.length === 0) {
  44. return reply('找不到要订阅的链接。');
  45. }
  46. const matched = twitter_1.parseLink(args[0]);
  47. if (!matched) {
  48. return reply(`订阅链接格式错误:
  49. 示例:
  50. https://www.instagram.com/tomoyo_kurosawa_/
  51. https://www.instagram.com/p/B6GHRSmgV-7/`);
  52. }
  53. let offset = '0';
  54. const subscribeTo = (link, config = {}) => {
  55. const { id, msg = `已为此聊天订阅 ${link}` } = config;
  56. if (id) {
  57. lock.feed.push(link);
  58. lock.threads[link] = {
  59. id,
  60. offset,
  61. subscribers: [],
  62. updatedAt: '',
  63. };
  64. }
  65. lock.threads[link].subscribers.push(chat);
  66. logger.warn(`chat ${JSON.stringify(chat)} has subscribed ${link}`);
  67. fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock));
  68. reply(msg);
  69. };
  70. const tryFindSub = (userName) => {
  71. const [realLink, index] = linkFinder(userName, chat, lock);
  72. if (index > -1) {
  73. reply('此聊天已订阅此链接。');
  74. return true;
  75. }
  76. if (realLink) {
  77. subscribeTo(realLink);
  78. return true;
  79. }
  80. return false;
  81. };
  82. const newSub = (userName) => {
  83. const link = twitter_1.linkBuilder(matched);
  84. let msg;
  85. if (offset !== '0') {
  86. msg = `已为此聊天订阅 ${link} 并回溯到 ${args[0].replace(/\?.*/, '')}(含)之后的第一条动态`;
  87. }
  88. return subscribeTo(link, { id: Number(userName.split(':')[1]), msg });
  89. };
  90. if (matched.postUrlSegment) {
  91. offset = utils_1.BigNumOps.plus(twitter_1.urlSegmentToId(matched.postUrlSegment), '-1');
  92. twitter_1.getPostOwner(matched.postUrlSegment).then(userName => {
  93. delete matched.postUrlSegment;
  94. matched.userName = userName.split(':')[0];
  95. if (!tryFindSub(userName))
  96. newSub(userName);
  97. }).catch((parsedErr) => {
  98. reply(parsedErr.message);
  99. });
  100. }
  101. else if (!tryFindSub(matched.userName)) {
  102. twitter_1.ScreenNameNormalizer.normalizeLive(matched.userName).then(userName => {
  103. if (!userName)
  104. return reply(`找不到用户 ${matched.userName.replace(/^@?(.*)$/, '@$1')}。`);
  105. else
  106. newSub(userName);
  107. });
  108. }
  109. }
  110. exports.sub = sub;
  111. function unsub(chat, args, reply, lock, lockfile) {
  112. var _a;
  113. if (chat.chatType === "temp") {
  114. return reply('请先添加机器人为好友。');
  115. }
  116. if (args.length === 0) {
  117. return reply('找不到要退订的链接。');
  118. }
  119. const match = (_a = twitter_1.parseLink(args[0])) === null || _a === void 0 ? void 0 : _a.userName;
  120. if (!match) {
  121. return reply('链接格式有误。');
  122. }
  123. const [link, index] = linkFinder(match, chat, lock);
  124. if (index === -1)
  125. return list(chat, args, msg => reply('您没有订阅此链接。\n' + msg), lock);
  126. else {
  127. lock.threads[link].subscribers.splice(index, 1);
  128. fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock));
  129. logger.warn(`chat ${JSON.stringify(chat)} has unsubscribed ${link}`);
  130. return reply(`已为此聊天退订 ${link}`);
  131. }
  132. }
  133. exports.unsub = unsub;
  134. function list(chat, _, reply, lock) {
  135. if (chat.chatType === "temp") {
  136. return reply('请先添加机器人为好友。');
  137. }
  138. const links = [];
  139. Object.keys(lock.threads).forEach(key => {
  140. if (lock.threads[key].subscribers.find(({ chatID, chatType }) => chat.chatID === chatID && chat.chatType === chatType))
  141. links.push(`${key} ${datetime_1.relativeDate(lock.threads[key].updatedAt)}`);
  142. });
  143. return reply('此聊天中订阅的 Instagram 动态链接:\n' + links.join('\n'));
  144. }
  145. exports.list = list;
  146. function view(chat, args, reply) {
  147. var _a;
  148. if (args.length === 0) {
  149. return reply('找不到要查看的链接。');
  150. }
  151. const match = twitter_1.isValidUrlSegment(args[0]) && args[0] || ((_a = twitter_1.parseLink(args[0])) === null || _a === void 0 ? void 0 : _a.postUrlSegment);
  152. if (!match) {
  153. return reply('链接格式有误。');
  154. }
  155. try {
  156. twitter_1.sendPost(match, chat);
  157. }
  158. catch (e) {
  159. reply('机器人尚未加载完毕,请稍后重试。');
  160. }
  161. }
  162. exports.view = view;