command.js 3.6 KB

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