lansenger-cli 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/commands/chat.js +110 -13
  2. package/package.json +1 -1
@@ -2,6 +2,25 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.registerChatCommands = registerChatCommands;
4
4
  const utils_1 = require("../utils");
5
+ function splitMonths(startUs, endUs) {
6
+ const results = [];
7
+ const startDt = startUs ? new Date(startUs / 1000000) : new Date(2020, 0, 1);
8
+ const endDt = new Date(endUs / 1000000);
9
+ let year = startDt.getFullYear();
10
+ let month = startDt.getMonth();
11
+ while (new Date(year, month, 1) <= endDt) {
12
+ const lastDay = new Date(year, month + 1, 0).getDate();
13
+ const msStart = Math.floor(new Date(year, month, 1).getTime() * 1000);
14
+ const msEnd = Math.floor(new Date(year, month, lastDay, 23, 59, 59).getTime() * 1000);
15
+ results.push([msStart, msEnd]);
16
+ month++;
17
+ if (month > 11) {
18
+ month = 0;
19
+ year++;
20
+ }
21
+ }
22
+ return results;
23
+ }
5
24
  function registerChatCommands(program) {
6
25
  const cmd = program.command("chat").description("Chat list and message history");
7
26
  cmd
@@ -22,7 +41,22 @@ function registerChatCommands(program) {
22
41
  user_token: opts.userToken || undefined,
23
42
  });
24
43
  (0, utils_1.checkError)(result);
25
- (0, utils_1.outputResult)(result);
44
+ if (result.success) {
45
+ (0, utils_1.outputResult)(result);
46
+ if (result.staff_infos) {
47
+ (0, utils_1.outputList)(result.staff_infos, ["Staff ID", "Name", "Sectors"], (s) => [
48
+ s.staff_id || "", s.staff_name || "", String(s.sector_names || ""),
49
+ ]);
50
+ }
51
+ if (result.group_infos) {
52
+ (0, utils_1.outputList)(result.group_infos, ["Group ID", "Name"], (g) => [
53
+ g.group_id || "", g.group_name || "",
54
+ ]);
55
+ }
56
+ }
57
+ else {
58
+ (0, utils_1.outputResult)(result);
59
+ }
26
60
  });
27
61
  cmd
28
62
  .command("messages")
@@ -34,20 +68,83 @@ function registerChatCommands(program) {
34
68
  .option("--start <start>", "Start time in microseconds", "0")
35
69
  .option("--end <end>", "End time in microseconds", "0")
36
70
  .option("--sender-id <senderId>", "Filter by sender staffId", "")
71
+ .option("--split-month", "Auto-split query by month when range > 1 month", false)
72
+ .option("--progress", "Show pagination progress (pages/messages fetched)", false)
37
73
  .option("--user-token <token>", "User token", "")
38
74
  .action(async (opts) => {
39
75
  const client = (0, utils_1.getClient)();
40
- const result = await client.fetchChatMessages({
41
- staff_id: opts.staffId || undefined,
42
- group_id: opts.groupId || undefined,
43
- page_size: parseInt(opts.size),
44
- base_version: opts.version,
45
- start_time: parseInt(opts.start),
46
- end_time: parseInt(opts.end),
47
- sender_id: opts.senderId || undefined,
48
- user_token: opts.userToken || undefined,
49
- });
50
- (0, utils_1.checkError)(result);
51
- (0, utils_1.outputResult)(result);
76
+ if (!opts.splitMonth) {
77
+ const result = await client.fetchChatMessages({
78
+ staff_id: opts.staffId || undefined,
79
+ group_id: opts.groupId || undefined,
80
+ page_size: parseInt(opts.size),
81
+ base_version: opts.version,
82
+ start_time: parseInt(opts.start),
83
+ end_time: parseInt(opts.end),
84
+ sender_id: opts.senderId || undefined,
85
+ user_token: opts.userToken || undefined,
86
+ });
87
+ (0, utils_1.checkError)(result);
88
+ if (result.success) {
89
+ (0, utils_1.outputResult)(result);
90
+ if (result.messages) {
91
+ (0, utils_1.outputList)(result.messages, ["Time", "Sender", "Type"], (m) => [
92
+ m.send_time || "", m.sender || "", m.message_type || "",
93
+ ]);
94
+ }
95
+ }
96
+ else {
97
+ (0, utils_1.outputResult)(result);
98
+ }
99
+ return;
100
+ }
101
+ const allMessages = [];
102
+ let pageCount = 0;
103
+ let msgCount = 0;
104
+ const startUs = parseInt(opts.start) || 0;
105
+ const endUs = parseInt(opts.end) || Math.floor(Date.now() * 1000);
106
+ const months = splitMonths(startUs, endUs);
107
+ for (let i = 0; i < months.length; i++) {
108
+ const [msStart, msEnd] = months[i];
109
+ let cursor = "0";
110
+ while (true) {
111
+ const result = await client.fetchChatMessages({
112
+ staff_id: opts.staffId || undefined,
113
+ group_id: opts.groupId || undefined,
114
+ page_size: parseInt(opts.size),
115
+ base_version: cursor,
116
+ start_time: msStart,
117
+ end_time: msEnd,
118
+ sender_id: opts.senderId || undefined,
119
+ user_token: opts.userToken || undefined,
120
+ });
121
+ if (!result.success) {
122
+ (0, utils_1.outputResult)(result);
123
+ return;
124
+ }
125
+ pageCount++;
126
+ msgCount += result.messages ? result.messages.length : 0;
127
+ if (result.messages)
128
+ allMessages.push(...result.messages);
129
+ if (opts.progress && !utils_1.jsonOutput) {
130
+ console.log(`Month ${i + 1}/${months.length} | Page ${pageCount} | ${msgCount} messages total`);
131
+ }
132
+ if (!(result.messages && result.has_more))
133
+ break;
134
+ cursor = String(result.last_version || "");
135
+ }
136
+ }
137
+ if (utils_1.jsonOutput) {
138
+ console.log(JSON.stringify(allMessages.map(m => m.toDict ? m.toDict() : m), null, 2));
139
+ return;
140
+ }
141
+ if (opts.progress) {
142
+ console.log(`Done: ${pageCount} pages, ${msgCount} messages across ${months.length} months`);
143
+ }
144
+ if (allMessages.length) {
145
+ (0, utils_1.outputList)(allMessages, ["Time", "Sender", "Type"], (m) => [
146
+ m.send_time || "", m.sender || "", m.message_type || "",
147
+ ]);
148
+ }
52
149
  });
53
150
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lansenger-cli",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "CLI for Lansenger (蓝信) — send messages, manage groups, staff, departments, calendars, todos, and more",
5
5
  "main": "dist/main.js",
6
6
  "bin": {