command.js 3.6 KB

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