command.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const fs = require("fs");
  4. const path = require("path");
  5. const datetime_1 = require("./datetime");
  6. const loggers_1 = require("./loggers");
  7. const logger = loggers_1.getLogger('command');
  8. function parseLink(link) {
  9. let match = link.match(/twitter.com\/([^\/?#]+)\/lists\/([^\/?#]+)/);
  10. if (match) {
  11. link = `https://twitter.com/${match[1]}/lists/${match[2]}`;
  12. return {
  13. link,
  14. match: [match[1], match[2]],
  15. };
  16. }
  17. match = link.match(/twitter.com\/([^\/?#]+)/);
  18. if (match) {
  19. link = `https://twitter.com/${match[1]}`;
  20. return {
  21. link,
  22. match: [match[1]],
  23. };
  24. }
  25. match = link.match(/^([^\/?#]+)\/([^\/?#]+)$/);
  26. if (match) {
  27. link = `https://twitter.com/${match[1]}/lists/${match[2]}`;
  28. return {
  29. link,
  30. match: [match[1], match[2]],
  31. };
  32. }
  33. match = link.match(/^([^\/?#]+)$/);
  34. if (match) {
  35. link = `https://twitter.com/${match[1]}`;
  36. return {
  37. link,
  38. match: [match[1]],
  39. };
  40. }
  41. return undefined;
  42. }
  43. function sub(chat, args, lock, lockfile) {
  44. if (args.length === 0) {
  45. return '找不到要订阅的链接。';
  46. }
  47. const match = parseLink(args[0]);
  48. if (!match) {
  49. return `订阅链接格式错误:
  50. 示例:
  51. https://twitter.com/Saito_Shuka
  52. https://twitter.com/rikakomoe/lists/lovelive`;
  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;