command.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. }
  54. const link = match.link;
  55. let flag = false;
  56. lock.feed.forEach(fl => {
  57. if (fl === link)
  58. flag = true;
  59. });
  60. if (!flag)
  61. lock.feed.push(link);
  62. if (!lock.threads[link]) {
  63. lock.threads[link] = {
  64. offset: '0',
  65. subscribers: [],
  66. updatedAt: '',
  67. };
  68. }
  69. flag = false;
  70. lock.threads[link].subscribers.forEach(c => {
  71. if (c.chatID === chat.chatID && c.chatType === chat.chatType)
  72. flag = true;
  73. });
  74. if (!flag)
  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. return `已为此聊天订阅 ${link} 的媒体推文`;
  79. }
  80. exports.sub = sub;
  81. function unsub(chat, args, lock, lockfile) {
  82. if (args.length === 0) {
  83. return '找不到要退订媒体推文的链接。';
  84. }
  85. const match = parseLink(args[0]);
  86. if (!match) {
  87. return '链接格式有误。';
  88. }
  89. const link = match.link;
  90. if (!lock.threads[link]) {
  91. return '您没有订阅此链接的媒体推文。\n' + list(chat, args, lock);
  92. }
  93. let flag = false;
  94. lock.threads[link].subscribers.forEach((c, index) => {
  95. if (c.chatID === chat.chatID && c.chatType === chat.chatType) {
  96. flag = true;
  97. lock.threads[link].subscribers.splice(index, 1);
  98. }
  99. });
  100. if (flag) {
  101. fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock));
  102. logger.warn(`chat ${JSON.stringify(chat)} has unsubscribed ${link}`);
  103. return `已为此聊天退订 ${link} 的媒体推文`;
  104. }
  105. return '您没有订阅此链接的媒体推文。\n' + list(chat, args, lock);
  106. }
  107. exports.unsub = unsub;
  108. function list(chat, args, lock) {
  109. const links = [];
  110. Object.keys(lock.threads).forEach(key => {
  111. lock.threads[key].subscribers.forEach(c => {
  112. if (c.chatID === chat.chatID && c.chatType === chat.chatType)
  113. links.push(`${key} ${datetime_1.relativeDate(lock.threads[key].updatedAt)}`);
  114. });
  115. });
  116. return '此聊天中订阅媒体推文的链接:\n' + links.join('\n');
  117. }
  118. exports.list = list;