command.ts 3.3 KB

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