command.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import * as fs from 'fs';
  2. import * as path from 'path';
  3. import { relativeDate } from './datetime';
  4. import { getLogger } from './loggers';
  5. const logger = getLogger('command');
  6. function parseLink(link: string): { link: string, match: string[] } | undefined {
  7. let match = link.match(/twitter.com\/([^\/?#]+)\/lists\/([^\/?#]+)/);
  8. if (match) {
  9. link = `https://twitter.com/${match[1]}/lists/${match[2]}`;
  10. return {
  11. link,
  12. match: [match[1], match[2]],
  13. };
  14. }
  15. match = link.match(/twitter.com\/([^\/?#]+)/);
  16. if (match) {
  17. link = `https://twitter.com/${match[1]}`;
  18. return {
  19. link,
  20. match: [match[1]],
  21. };
  22. }
  23. match = link.match(/^([^\/?#]+)\/([^\/?#]+)$/);
  24. if (match) {
  25. link = `https://twitter.com/${match[1]}/lists/${match[2]}`;
  26. return {
  27. link,
  28. match: [match[1], match[2]],
  29. };
  30. }
  31. match = link.match(/^([^\/?#]+)$/);
  32. if (match) {
  33. link = `https://twitter.com/${match[1]}`;
  34. return {
  35. link,
  36. match: [match[1]],
  37. };
  38. }
  39. return undefined;
  40. }
  41. function sub(chat: IChat, args: string[], lock: ILock, lockfile: string): string {
  42. if (args.length === 0) {
  43. return '找不到要订阅的链接。';
  44. }
  45. const match = parseLink(args[0]);
  46. if (!match) {
  47. return `订阅链接格式错误:
  48. 示例:
  49. https://twitter.com/Saito_Shuka
  50. https://twitter.com/rikakomoe/lists/lovelive`;
  51. }
  52. const link = match.link;
  53. let flag = false;
  54. lock.feed.forEach(fl => {
  55. if (fl === link) flag = true;
  56. });
  57. if (!flag) lock.feed.push(link);
  58. if (!lock.threads[link]) {
  59. lock.threads[link] = {
  60. offset: '0',
  61. subscribers: [],
  62. updatedAt: '',
  63. };
  64. }
  65. flag = false;
  66. lock.threads[link].subscribers.forEach(c => {
  67. if (c.chatID === chat.chatID && c.chatType === chat.chatType) flag = true;
  68. });
  69. if (!flag) lock.threads[link].subscribers.push(chat);
  70. logger.warn(`chat ${JSON.stringify(chat)} has subscribed ${link}`);
  71. fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock));
  72. return `已为此聊天订阅 ${link}`;
  73. }
  74. function unsub(chat: IChat, args: string[], lock: ILock, lockfile: string): string {
  75. if (args.length === 0) {
  76. return '找不到要退订的链接。';
  77. }
  78. const match = parseLink(args[0]);
  79. if (!match) {
  80. return '链接格式有误。';
  81. }
  82. const link = match.link;
  83. if (!lock.threads[link]) {
  84. return '您没有订阅此链接。\n' + list(chat, args, lock);
  85. }
  86. let flag = false;
  87. lock.threads[link].subscribers.forEach((c, index) => {
  88. if (c.chatID === chat.chatID && c.chatType === chat.chatType) {
  89. flag = true;
  90. lock.threads[link].subscribers.splice(index, 1);
  91. }
  92. });
  93. if (flag) {
  94. fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock));
  95. logger.warn(`chat ${JSON.stringify(chat)} has unsubscribed ${link}`);
  96. return `已为此聊天退订 ${link}`;
  97. }
  98. return '您没有订阅此链接。\n' + list(chat, args, lock);
  99. }
  100. function list(chat: IChat, args: string[], lock: ILock): string {
  101. const links = [];
  102. Object.keys(lock.threads).forEach(key => {
  103. lock.threads[key].subscribers.forEach(c => {
  104. if (c.chatID === chat.chatID && c.chatType === chat.chatType) links.push(`${key} ${relativeDate(lock.threads[key].updatedAt)}`);
  105. });
  106. });
  107. return '此聊天中订阅的链接:\n' + links.join('\n');
  108. }
  109. export { sub, list, unsub };