command.ts 5.7 KB

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