command.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.view = exports.unsub = exports.list = exports.sub = 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 logger = loggers_1.getLogger('command');
  10. function parseLink(link) {
  11. let match = link.match(/twitter.com\/([^\/?#]+)\/lists\/([^\/?#]+)/) ||
  12. link.match(/^([^\/?#]+)\/([^\/?#]+)$/);
  13. if (match)
  14. return [match[1], `/lists/${match[2]}`];
  15. match =
  16. link.match(/twitter.com\/([^\/?#]+)\/status\/(\d+)/);
  17. if (match)
  18. return [match[1], `/status/${match[2]}`];
  19. match =
  20. link.match(/twitter.com\/([^\/?#]+)/) ||
  21. link.match(/^([^\/?#]+)$/);
  22. if (match)
  23. return [match[1]];
  24. return;
  25. }
  26. function linkBuilder(userName, more = '') {
  27. if (!userName)
  28. return;
  29. return `https://twitter.com/${userName}${more}`;
  30. }
  31. function linkFinder(checkedMatch, chat, lock) {
  32. var _a;
  33. const normalizedLink = linkBuilder(twitter_1.ScreenNameNormalizer.normalize(checkedMatch[0]), (_a = checkedMatch[1]) === null || _a === void 0 ? void 0 : _a.toLowerCase());
  34. const link = Object.keys(lock.threads).find(realLink => normalizedLink === realLink.replace(/\/@/, '/').toLowerCase());
  35. if (!link)
  36. return [null, -1];
  37. const index = lock.threads[link].subscribers.findIndex(({ chatID, chatType }) => chat.chatID === chatID && chat.chatType === chatType);
  38. return [link, index];
  39. }
  40. function sub(chat, args, reply, lock, lockfile) {
  41. if (args.length === 0) {
  42. return reply('找不到要订阅的链接。');
  43. }
  44. const match = parseLink(args[0]);
  45. if (!match) {
  46. return reply(`订阅链接格式错误:
  47. 示例:
  48. https://twitter.com/Saito_Shuka
  49. https://twitter.com/rikakomoe/lists/lovelive
  50. https://twitter.com/TomoyoKurosawa/status/1294613494860361729`);
  51. }
  52. let offset = '0';
  53. if (match[1]) {
  54. const matchStatus = match[1].match(/\/status\/(\d+)/);
  55. if (matchStatus) {
  56. offset = String(matchStatus[1] - 1);
  57. delete match[1];
  58. }
  59. }
  60. const subscribeTo = (link, config = {}) => {
  61. const { addNew = false, msg = `已为此聊天订阅 ${link}` } = config;
  62. if (addNew) {
  63. lock.feed.push(link);
  64. lock.threads[link] = {
  65. offset,
  66. subscribers: [],
  67. updatedAt: '',
  68. };
  69. }
  70. lock.threads[link].subscribers.push(chat);
  71. logger.warn(`chat ${JSON.stringify(chat)} has subscribed ${link}`);
  72. fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock));
  73. reply(msg);
  74. };
  75. const [realLink, index] = linkFinder(match, chat, lock);
  76. if (index > -1)
  77. return reply('此聊天已订阅此链接。');
  78. if (realLink)
  79. return subscribeTo(realLink);
  80. const [rawUserName, more] = match;
  81. if (rawUserName.toLowerCase() === 'i' && (more === null || more === void 0 ? void 0 : more.match(/lists\/(\d+)/))) {
  82. return subscribeTo(linkBuilder('i', more), { addNew: true });
  83. }
  84. twitter_1.ScreenNameNormalizer.normalizeLive(rawUserName).then(userName => {
  85. if (!userName)
  86. return reply(`找不到用户 ${rawUserName.replace(/^@?(.*)$/, '@$1')}。`);
  87. const link = linkBuilder(userName, more);
  88. const msg = (offset === '0') ?
  89. undefined :
  90. `已为此聊天订阅 ${link} 并回溯到此动态 ID(含)之后的第一条动态。
  91. (参见:https://blog.twitter.com/engineering/en_us/a/2010/announcing-snowflake.html)`;
  92. subscribeTo(link, { addNew: true, msg });
  93. });
  94. }
  95. exports.sub = sub;
  96. function unsub(chat, args, reply, lock, lockfile) {
  97. if (args.length === 0) {
  98. return reply('找不到要退订的链接。');
  99. }
  100. const match = parseLink(args[0]);
  101. if (!match) {
  102. return reply('链接格式有误。');
  103. }
  104. const [link, index] = linkFinder(match, chat, lock);
  105. if (index === -1)
  106. return list(chat, args, msg => reply('您没有订阅此链接。\n' + msg), lock);
  107. else {
  108. lock.threads[link].subscribers.splice(index, 1);
  109. fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock));
  110. logger.warn(`chat ${JSON.stringify(chat)} has unsubscribed ${link}`);
  111. return reply(`已为此聊天退订 ${link}`);
  112. }
  113. }
  114. exports.unsub = unsub;
  115. function list(chat, _, reply, lock) {
  116. const links = [];
  117. Object.keys(lock.threads).forEach(key => {
  118. if (lock.threads[key].subscribers.find(({ chatID, chatType }) => chat.chatID === chatID && chat.chatType === chatType))
  119. links.push(`${key} ${datetime_1.relativeDate(lock.threads[key].updatedAt)}`);
  120. });
  121. return reply('此聊天中订阅的链接:\n' + links.join('\n'));
  122. }
  123. exports.list = list;
  124. function view(chat, args, reply) {
  125. if (args.length === 0) {
  126. return reply('找不到要查看的链接。');
  127. }
  128. const match = args[0].match(/^(?:.*twitter.com\/[^\/?#]+\/status\/)?(\d+)/);
  129. if (!match) {
  130. return reply('链接格式有误。');
  131. }
  132. try {
  133. twitter_1.sendTweet(match[1], chat);
  134. }
  135. catch (e) {
  136. reply('推特机器人尚未加载完毕,请稍后重试。');
  137. }
  138. }
  139. exports.view = view;