cc98-cli 0.2.0 → 0.3.0
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.
- package/CHANGELOG.md +14 -0
- package/README.md +4 -2
- package/dist/api/client.d.ts +26 -15
- package/dist/api/client.d.ts.map +1 -1
- package/dist/api/client.js +96 -30
- package/dist/api/client.js.map +1 -1
- package/dist/api/endpoints.d.ts +14 -0
- package/dist/api/endpoints.d.ts.map +1 -1
- package/dist/api/endpoints.js +21 -1
- package/dist/api/endpoints.js.map +1 -1
- package/dist/cli/commands/board.d.ts.map +1 -1
- package/dist/cli/commands/board.js +15 -0
- package/dist/cli/commands/board.js.map +1 -1
- package/dist/cli/commands/me.d.ts.map +1 -1
- package/dist/cli/commands/me.js +18 -3
- package/dist/cli/commands/me.js.map +1 -1
- package/dist/cli/commands/message.d.ts.map +1 -1
- package/dist/cli/commands/message.js +10 -0
- package/dist/cli/commands/message.js.map +1 -1
- package/dist/cli/commands/post.d.ts.map +1 -1
- package/dist/cli/commands/post.js +12 -0
- package/dist/cli/commands/post.js.map +1 -1
- package/dist/cli/commands/topic.d.ts.map +1 -1
- package/dist/cli/commands/topic.js +41 -5
- package/dist/cli/commands/topic.js.map +1 -1
- package/dist/cli/commands/user.d.ts.map +1 -1
- package/dist/cli/commands/user.js +12 -0
- package/dist/cli/commands/user.js.map +1 -1
- package/dist/cli/router.js +8 -1
- package/dist/cli/router.js.map +1 -1
- package/dist/tui/app-new.d.ts +2 -0
- package/dist/tui/app-new.d.ts.map +1 -0
- package/dist/tui/app-new.js +2589 -0
- package/dist/tui/app-new.js.map +1 -0
- package/dist/tui/app-old.d.ts +2 -0
- package/dist/tui/app-old.d.ts.map +1 -0
- package/dist/tui/app-old.js +2589 -0
- package/dist/tui/app-old.js.map +1 -0
- package/dist/tui/app.d.ts.map +1 -1
- package/dist/tui/app.js +14 -1724
- package/dist/tui/app.js.map +1 -1
- package/dist/tui/borders.d.ts +183 -0
- package/dist/tui/borders.d.ts.map +1 -0
- package/dist/tui/borders.js +143 -0
- package/dist/tui/borders.js.map +1 -0
- package/dist/tui/cached-client.d.ts +23 -0
- package/dist/tui/cached-client.d.ts.map +1 -1
- package/dist/tui/cached-client.js +88 -0
- package/dist/tui/cached-client.js.map +1 -1
- package/dist/tui/components/content.d.ts +11 -0
- package/dist/tui/components/content.d.ts.map +1 -0
- package/dist/tui/components/content.js +129 -0
- package/dist/tui/components/content.js.map +1 -0
- package/dist/tui/components/header.d.ts +7 -0
- package/dist/tui/components/header.d.ts.map +1 -0
- package/dist/tui/components/header.js +38 -0
- package/dist/tui/components/header.js.map +1 -0
- package/dist/tui/components/index.d.ts +8 -0
- package/dist/tui/components/index.d.ts.map +1 -0
- package/dist/tui/components/index.js +9 -0
- package/dist/tui/components/index.js.map +1 -0
- package/dist/tui/components/layout.d.ts +3 -0
- package/dist/tui/components/layout.d.ts.map +1 -0
- package/dist/tui/components/layout.js +464 -0
- package/dist/tui/components/layout.js.map +1 -0
- package/dist/tui/components/overview.d.ts +7 -0
- package/dist/tui/components/overview.d.ts.map +1 -0
- package/dist/tui/components/overview.js +20 -0
- package/dist/tui/components/overview.js.map +1 -0
- package/dist/tui/components/sidebar.d.ts +7 -0
- package/dist/tui/components/sidebar.d.ts.map +1 -0
- package/dist/tui/components/sidebar.js +58 -0
- package/dist/tui/components/sidebar.js.map +1 -0
- package/dist/tui/components/status.d.ts +8 -0
- package/dist/tui/components/status.d.ts.map +1 -0
- package/dist/tui/components/status.js +36 -0
- package/dist/tui/components/status.js.map +1 -0
- package/dist/tui/components/types.d.ts +17 -0
- package/dist/tui/components/types.d.ts.map +1 -0
- package/dist/tui/components/types.js +3 -0
- package/dist/tui/components/types.js.map +1 -0
- package/dist/tui/components/utils.d.ts +5 -0
- package/dist/tui/components/utils.d.ts.map +1 -0
- package/dist/tui/components/utils.js +68 -0
- package/dist/tui/components/utils.js.map +1 -0
- package/dist/tui/controller.d.ts +61 -0
- package/dist/tui/controller.d.ts.map +1 -0
- package/dist/tui/controller.js +1118 -0
- package/dist/tui/controller.js.map +1 -0
- package/dist/tui/helpers.d.ts +25 -0
- package/dist/tui/helpers.d.ts.map +1 -0
- package/dist/tui/helpers.js +240 -0
- package/dist/tui/helpers.js.map +1 -0
- package/dist/tui/keymap/actions.d.ts +9 -0
- package/dist/tui/keymap/actions.d.ts.map +1 -0
- package/dist/tui/keymap/actions.js +208 -0
- package/dist/tui/keymap/actions.js.map +1 -0
- package/dist/tui/keymap/bindings.d.ts +5 -0
- package/dist/tui/keymap/bindings.d.ts.map +1 -0
- package/dist/tui/keymap/bindings.js +138 -0
- package/dist/tui/keymap/bindings.js.map +1 -0
- package/dist/tui/keymap/index.d.ts +4 -0
- package/dist/tui/keymap/index.d.ts.map +1 -0
- package/dist/tui/keymap/index.js +5 -0
- package/dist/tui/keymap/index.js.map +1 -0
- package/dist/tui/keymap/types.d.ts +17 -0
- package/dist/tui/keymap/types.d.ts.map +1 -0
- package/dist/tui/keymap/types.js +3 -0
- package/dist/tui/keymap/types.js.map +1 -0
- package/dist/tui/navigation.d.ts +4 -0
- package/dist/tui/navigation.d.ts.map +1 -0
- package/dist/tui/navigation.js +20 -0
- package/dist/tui/navigation.js.map +1 -0
- package/dist/tui/renderer.d.ts +6 -0
- package/dist/tui/renderer.d.ts.map +1 -0
- package/dist/tui/renderer.js +302 -0
- package/dist/tui/renderer.js.map +1 -0
- package/dist/tui/state/index.d.ts +3 -0
- package/dist/tui/state/index.d.ts.map +1 -0
- package/dist/tui/state/index.js +4 -0
- package/dist/tui/state/index.js.map +1 -0
- package/dist/tui/state/store.d.ts +4 -0
- package/dist/tui/state/store.d.ts.map +1 -0
- package/dist/tui/state/store.js +61 -0
- package/dist/tui/state/store.js.map +1 -0
- package/dist/tui/state/types.d.ts +137 -0
- package/dist/tui/state/types.d.ts.map +1 -0
- package/dist/tui/state/types.js +3 -0
- package/dist/tui/state/types.js.map +1 -0
- package/dist/tui/topic-reader.d.ts +10 -0
- package/dist/tui/topic-reader.d.ts.map +1 -0
- package/dist/tui/topic-reader.js +167 -0
- package/dist/tui/topic-reader.js.map +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -2
- /package/{docs/images → images}/tui.jpg +0 -0
|
@@ -0,0 +1,1118 @@
|
|
|
1
|
+
import { checkForUpdate } from "../update.js";
|
|
2
|
+
import { appVersion } from "../version.js";
|
|
3
|
+
import { navItems, settingsItems } from "./navigation.js";
|
|
4
|
+
import { getStatus } from "./state/store.js";
|
|
5
|
+
import { asArray, asNumber, asObject, chatItem, chatMessageItems, flattenBoards, genericItem, historyItem, isAbortError, jsonPreviewLines, loadChatUserNames, mapLimit, noticeItem, overviewStats, topicItem, unreadStats, userItem } from "./helpers.js";
|
|
6
|
+
import { appendTopicPosts, buildTopicReader, currentTopicPost, findTopicPostByFloor, jumpRelativeTopicFloor } from "./topic-reader.js";
|
|
7
|
+
export class TuiController {
|
|
8
|
+
state;
|
|
9
|
+
client;
|
|
10
|
+
tokenStore;
|
|
11
|
+
render;
|
|
12
|
+
close;
|
|
13
|
+
nextSignal;
|
|
14
|
+
abortCurrent;
|
|
15
|
+
loadVersion = 0;
|
|
16
|
+
constructor(state, client, tokenStore, render, close, nextSignal, abortCurrent) {
|
|
17
|
+
this.state = state;
|
|
18
|
+
this.client = client;
|
|
19
|
+
this.tokenStore = tokenStore;
|
|
20
|
+
this.render = render;
|
|
21
|
+
this.close = close;
|
|
22
|
+
this.nextSignal = nextSignal;
|
|
23
|
+
this.abortCurrent = abortCurrent;
|
|
24
|
+
}
|
|
25
|
+
async load(force = false) {
|
|
26
|
+
const version = ++this.loadVersion;
|
|
27
|
+
const signal = this.nextSignal();
|
|
28
|
+
const nav = navItems[this.state.navIndex] ?? navItems[0];
|
|
29
|
+
this.state.viewTitle = nav.label;
|
|
30
|
+
this.state.loading = true;
|
|
31
|
+
this.state.error = undefined;
|
|
32
|
+
this.state.itemIndex = 0;
|
|
33
|
+
this.state.scroll = 0;
|
|
34
|
+
this.state.mode = nav.id === "settings" && this.state.mode === "settings" ? "settings" : "list";
|
|
35
|
+
if (this.state.mode === "settings") {
|
|
36
|
+
this.state.focus = "content";
|
|
37
|
+
}
|
|
38
|
+
this.state.items = [];
|
|
39
|
+
this.state.stats = [];
|
|
40
|
+
this.state.topic = undefined;
|
|
41
|
+
this.state.parentList = undefined;
|
|
42
|
+
this.state.currentBoard = undefined;
|
|
43
|
+
this.state.currentChat = undefined;
|
|
44
|
+
this.render();
|
|
45
|
+
try {
|
|
46
|
+
this.state.account = await this.tokenStore.getCurrentAccountName();
|
|
47
|
+
const next = await this.loadView(nav.id, force, signal);
|
|
48
|
+
if (version !== this.loadVersion)
|
|
49
|
+
return;
|
|
50
|
+
this.state.viewTitle = next.title;
|
|
51
|
+
this.state.items = next.items;
|
|
52
|
+
this.state.stats = next.stats;
|
|
53
|
+
if (next.overview) {
|
|
54
|
+
this.state.overview = next.overview;
|
|
55
|
+
}
|
|
56
|
+
this.state.status = next.status ?? "";
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
if (isAbortError(error) || version !== this.loadVersion)
|
|
60
|
+
return;
|
|
61
|
+
this.state.error = error instanceof Error ? error.message : String(error);
|
|
62
|
+
this.state.items = [];
|
|
63
|
+
this.state.stats = [];
|
|
64
|
+
}
|
|
65
|
+
finally {
|
|
66
|
+
if (version === this.loadVersion) {
|
|
67
|
+
this.state.loading = false;
|
|
68
|
+
this.render();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
handleKey(key) {
|
|
73
|
+
if (this.state.inputMode) {
|
|
74
|
+
this.handleInputKey(key);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (key === "\u0003" || key === "q") {
|
|
78
|
+
this.close();
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (key === "?") {
|
|
82
|
+
this.state.modal = this.state.modal === "help" ? null : "help";
|
|
83
|
+
this.render();
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (this.state.modal) {
|
|
87
|
+
this.handleModalKey(key);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (this.state.mode === "topic") {
|
|
91
|
+
this.handleTopicKey(key);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (this.state.mode === "settings") {
|
|
95
|
+
this.handleSettingsKey(key);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (this.state.focus === "nav") {
|
|
99
|
+
this.handleNavKey(key);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
this.handleContentKey(key);
|
|
103
|
+
}
|
|
104
|
+
handleInputKey(key) {
|
|
105
|
+
if (key === "\x1b") {
|
|
106
|
+
this.state.inputMode = false;
|
|
107
|
+
this.state.inputValue = "";
|
|
108
|
+
this.render();
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (key === "\r") {
|
|
112
|
+
this.state.inputCallback?.(this.state.inputValue);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (key === "\x7f") {
|
|
116
|
+
this.state.inputValue = this.state.inputValue.slice(0, -1);
|
|
117
|
+
this.render();
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (key.length === 1 && key >= " ") {
|
|
121
|
+
this.state.inputValue += key;
|
|
122
|
+
this.render();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
handleModalKey(key) {
|
|
126
|
+
if (this.state.modal === "help" || this.state.modal === "info") {
|
|
127
|
+
if (key === "\x1b" || key === "\r" || key === "h" || key === "\x1b[D" || key === "?") {
|
|
128
|
+
this.closeModal();
|
|
129
|
+
}
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
if (this.state.modal === "search") {
|
|
133
|
+
this.handleSearchKey(key);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (this.state.modal === "user") {
|
|
137
|
+
this.handleUserModalKey(key);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (this.state.modal === "menu") {
|
|
141
|
+
this.handleMenuKey(key);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
handleSearchKey(key) {
|
|
145
|
+
if (key === "\x1b") {
|
|
146
|
+
this.state.modal = null;
|
|
147
|
+
this.state.searchQuery = "";
|
|
148
|
+
this.render();
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
if (key === "\t") {
|
|
152
|
+
this.state.searchMode = this.state.searchMode === "topics" ? "users" : "topics";
|
|
153
|
+
this.state.searchResults = [];
|
|
154
|
+
this.state.itemIndex = 0;
|
|
155
|
+
this.render();
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if ((key === "j" || key === "\x1b[B") && this.state.searchResults.length > 0) {
|
|
159
|
+
this.state.itemIndex = Math.min(this.state.searchResults.length - 1, this.state.itemIndex + 1);
|
|
160
|
+
this.render();
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if ((key === "k" || key === "\x1b[A") && this.state.searchResults.length > 0) {
|
|
164
|
+
this.state.itemIndex = Math.max(0, this.state.itemIndex - 1);
|
|
165
|
+
this.render();
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (key === "\r") {
|
|
169
|
+
const selected = this.state.searchResults[this.state.itemIndex];
|
|
170
|
+
if (selected) {
|
|
171
|
+
this.state.modal = null;
|
|
172
|
+
void this.activateContentItem(selected, this.nextSignal());
|
|
173
|
+
}
|
|
174
|
+
else if (this.state.searchQuery.trim()) {
|
|
175
|
+
void this.performSearch(this.nextSignal());
|
|
176
|
+
}
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
if (key === "\x7f") {
|
|
180
|
+
this.state.searchQuery = this.state.searchQuery.slice(0, -1);
|
|
181
|
+
this.state.searchResults = [];
|
|
182
|
+
this.state.itemIndex = 0;
|
|
183
|
+
this.render();
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
if (key.length === 1 && key >= " ") {
|
|
187
|
+
this.state.searchQuery += key;
|
|
188
|
+
this.state.searchResults = [];
|
|
189
|
+
this.state.itemIndex = 0;
|
|
190
|
+
this.render();
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
handleUserModalKey(key) {
|
|
194
|
+
if (key === "\x1b") {
|
|
195
|
+
this.closeModal();
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
if (key === "f" && this.state.userDetail) {
|
|
199
|
+
void this.toggleFollow();
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
if (key === "m" && this.state.userDetail) {
|
|
203
|
+
const user = this.state.userDetail;
|
|
204
|
+
this.state.inputMode = true;
|
|
205
|
+
this.state.inputPrompt = `发送私信给 ${user.name}: `;
|
|
206
|
+
this.state.inputValue = "";
|
|
207
|
+
this.state.inputCallback = (value) => {
|
|
208
|
+
this.state.inputMode = false;
|
|
209
|
+
this.state.inputValue = "";
|
|
210
|
+
if (value.trim()) {
|
|
211
|
+
void this.sendPrivateMessage(user.userId, value.trim());
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
this.render();
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
this.render();
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
handleMenuKey(key) {
|
|
221
|
+
if (key === "j" || key === "\x1b[B") {
|
|
222
|
+
this.state.menuIndex = Math.min(Math.max(0, this.state.menuItems.length - 1), this.state.menuIndex + 1);
|
|
223
|
+
this.render();
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (key === "k" || key === "\x1b[A") {
|
|
227
|
+
this.state.menuIndex = Math.max(0, this.state.menuIndex - 1);
|
|
228
|
+
this.render();
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
if (key === "h" || key === "\x1b[D" || key === "\x1b" || key === "o") {
|
|
232
|
+
this.state.modal = null;
|
|
233
|
+
this.render();
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
if (key === "\r" || key === "l" || key === "\x1b[C") {
|
|
237
|
+
const selected = this.state.menuItems[this.state.menuIndex];
|
|
238
|
+
this.state.modal = null;
|
|
239
|
+
if (selected?.action === "refresh")
|
|
240
|
+
void this.refresh();
|
|
241
|
+
if (selected?.action === "back")
|
|
242
|
+
this.leave();
|
|
243
|
+
this.render();
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
handleTopicKey(key) {
|
|
247
|
+
if (/^\d$/.test(key) && this.state.topic) {
|
|
248
|
+
this.state.topic.floorInput = `${this.state.topic.floorInput}${key}`.slice(0, 6);
|
|
249
|
+
this.state.status = `跳转到 ${this.state.topic.floorInput} 楼:Enter 确认 Esc 取消`;
|
|
250
|
+
this.render();
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
if (key === "\x7f" && this.state.topic?.floorInput) {
|
|
254
|
+
this.state.topic.floorInput = this.state.topic.floorInput.slice(0, -1);
|
|
255
|
+
this.state.status = this.state.topic.floorInput ? `跳转到 ${this.state.topic.floorInput} 楼:Enter 确认 Esc 取消` : getStatus(this.state);
|
|
256
|
+
this.render();
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
if (key === "\r" && this.state.topic?.floorInput) {
|
|
260
|
+
const floor = Number(this.state.topic.floorInput);
|
|
261
|
+
this.state.topic.floorInput = "";
|
|
262
|
+
if (Number.isInteger(floor) && floor > 0)
|
|
263
|
+
void this.jumpToTopicFloor(floor, this.nextSignal());
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
if ((key === "]" || key === "】") && this.state.topic) {
|
|
267
|
+
this.state.scroll = jumpRelativeTopicFloor(this.state.topic, this.state.scroll, 1);
|
|
268
|
+
this.state.status = getStatus(this.state);
|
|
269
|
+
this.render();
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
if ((key === "[" || key === "【") && this.state.topic) {
|
|
273
|
+
this.state.scroll = jumpRelativeTopicFloor(this.state.topic, this.state.scroll, -1);
|
|
274
|
+
this.state.status = getStatus(this.state);
|
|
275
|
+
this.render();
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (key === "\x1b" && this.state.topic?.floorInput) {
|
|
279
|
+
this.state.topic.floorInput = "";
|
|
280
|
+
this.state.status = getStatus(this.state);
|
|
281
|
+
this.render();
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (key === "h" || key === "\x1b[D" || key === "\x1b") {
|
|
285
|
+
this.leave();
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
if (key === "j" || key === "\x1b[B") {
|
|
289
|
+
const maxScroll = Math.max(0, (this.state.topic?.lines.length ?? 0) - 1);
|
|
290
|
+
const wasAtEnd = this.state.scroll >= maxScroll;
|
|
291
|
+
this.state.scroll = Math.min(maxScroll, this.state.scroll + 1);
|
|
292
|
+
this.render();
|
|
293
|
+
if (wasAtEnd && this.state.topic?.hasMore && !this.state.loadingMore) {
|
|
294
|
+
void this.loadNextTopicPage(this.nextSignal(), true);
|
|
295
|
+
}
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
if (key === "k" || key === "\x1b[A") {
|
|
299
|
+
this.state.scroll = Math.max(0, this.state.scroll - 1);
|
|
300
|
+
this.render();
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
if (key === "n" || key === " ") {
|
|
304
|
+
void this.loadNextTopicPage(this.nextSignal());
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
if (key === "r" && this.state.topic) {
|
|
308
|
+
void this.openTopic(this.state.topic.topicId, true, this.nextSignal());
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
if (key === "s")
|
|
312
|
+
void this.toggleFavorite();
|
|
313
|
+
if (key === "l")
|
|
314
|
+
void this.reactToCurrentPost(true);
|
|
315
|
+
if (key === "d")
|
|
316
|
+
void this.reactToCurrentPost(false);
|
|
317
|
+
if (key === "u")
|
|
318
|
+
void this.showCurrentUser(this.nextSignal());
|
|
319
|
+
if (key === "v")
|
|
320
|
+
void this.showTopicVote(this.nextSignal());
|
|
321
|
+
if (key === "a")
|
|
322
|
+
void this.showPostReactionState(this.nextSignal());
|
|
323
|
+
if (key === "o")
|
|
324
|
+
this.openMenu();
|
|
325
|
+
}
|
|
326
|
+
handleSettingsKey(key) {
|
|
327
|
+
if (key === "j" || key === "\x1b[B") {
|
|
328
|
+
this.state.itemIndex = Math.min(settingsItems.length - 1, this.state.itemIndex + 1);
|
|
329
|
+
this.render();
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
if (key === "k" || key === "\x1b[A") {
|
|
333
|
+
this.state.itemIndex = Math.max(0, this.state.itemIndex - 1);
|
|
334
|
+
this.render();
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
if (key === "h" || key === "\x1b[D" || key === "\x1b") {
|
|
338
|
+
this.state.mode = "list";
|
|
339
|
+
this.state.focus = "nav";
|
|
340
|
+
this.state.status = getStatus(this.state);
|
|
341
|
+
this.render();
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
if (key === "l" || key === "\x1b[C" || key === "\r") {
|
|
345
|
+
void this.activateSetting(settingsItems[this.state.itemIndex]);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
handleNavKey(key) {
|
|
349
|
+
if (key === "j" || key === "\x1b[B") {
|
|
350
|
+
this.state.navIndex = Math.min(navItems.length - 1, this.state.navIndex + 1);
|
|
351
|
+
void this.load();
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
if (key === "k" || key === "\x1b[A") {
|
|
355
|
+
this.state.navIndex = Math.max(0, this.state.navIndex - 1);
|
|
356
|
+
void this.load();
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
if (key === "l" || key === "\x1b[C" || key === "\r") {
|
|
360
|
+
if (!this.state.loading && this.state.items.length > 0) {
|
|
361
|
+
if (navItems[this.state.navIndex]?.id === "settings")
|
|
362
|
+
this.state.mode = "settings";
|
|
363
|
+
this.state.focus = "content";
|
|
364
|
+
this.state.itemIndex = 0;
|
|
365
|
+
this.state.status = getStatus(this.state);
|
|
366
|
+
this.render();
|
|
367
|
+
}
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
if (key === "r")
|
|
371
|
+
void this.load(true);
|
|
372
|
+
}
|
|
373
|
+
handleContentKey(key) {
|
|
374
|
+
if (key === "j" || key === "\x1b[B") {
|
|
375
|
+
this.state.itemIndex = Math.min(Math.max(0, this.state.items.length - 1), this.state.itemIndex + 1);
|
|
376
|
+
this.render();
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
if (key === "k" || key === "\x1b[A") {
|
|
380
|
+
this.state.itemIndex = Math.max(0, this.state.itemIndex - 1);
|
|
381
|
+
this.render();
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
if (key === "h" || key === "\x1b[D" || key === "\x1b") {
|
|
385
|
+
this.leave();
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
if (key === "l" || key === "\x1b[C" || key === "\r") {
|
|
389
|
+
const selected = this.state.items[this.state.itemIndex];
|
|
390
|
+
if (selected) {
|
|
391
|
+
void this.activateContentItem(selected, this.nextSignal());
|
|
392
|
+
}
|
|
393
|
+
else {
|
|
394
|
+
this.state.status = "当前条目不可进入";
|
|
395
|
+
this.render();
|
|
396
|
+
}
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
if ((key === "n" || key === " ") && this.state.currentChat) {
|
|
400
|
+
void this.loadNextChatPage(this.nextSignal());
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
if (key === "r")
|
|
404
|
+
void this.refresh();
|
|
405
|
+
if (key === "/")
|
|
406
|
+
this.openSearch();
|
|
407
|
+
if (key === "o")
|
|
408
|
+
this.openMenu();
|
|
409
|
+
}
|
|
410
|
+
leave() {
|
|
411
|
+
this.abortCurrent();
|
|
412
|
+
if (this.state.mode === "topic") {
|
|
413
|
+
this.state.mode = "list";
|
|
414
|
+
this.state.focus = "content";
|
|
415
|
+
this.state.status = "";
|
|
416
|
+
this.render();
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
if (this.state.parentList) {
|
|
420
|
+
this.restoreParentList();
|
|
421
|
+
this.render();
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
this.state.focus = "nav";
|
|
425
|
+
this.state.status = "";
|
|
426
|
+
this.render();
|
|
427
|
+
}
|
|
428
|
+
refresh() {
|
|
429
|
+
if (this.state.currentBoard) {
|
|
430
|
+
void this.openBoard(this.state.currentBoard.boardId, this.state.currentBoard.title, true, this.nextSignal(), false);
|
|
431
|
+
}
|
|
432
|
+
else if (this.state.currentChat) {
|
|
433
|
+
void this.openChat(this.state.currentChat.userId, this.state.currentChat.title, true, this.nextSignal(), false);
|
|
434
|
+
}
|
|
435
|
+
else {
|
|
436
|
+
void this.load(true);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
openSearch() {
|
|
440
|
+
this.state.modal = "search";
|
|
441
|
+
this.state.searchQuery = "";
|
|
442
|
+
this.state.searchResults = [];
|
|
443
|
+
this.state.searchMode = "topics";
|
|
444
|
+
this.state.itemIndex = 0;
|
|
445
|
+
this.render();
|
|
446
|
+
}
|
|
447
|
+
openMenu() {
|
|
448
|
+
this.state.modal = "menu";
|
|
449
|
+
this.state.menuItems = this.getMenuItems();
|
|
450
|
+
this.state.menuIndex = 0;
|
|
451
|
+
this.render();
|
|
452
|
+
}
|
|
453
|
+
getMenuItems() {
|
|
454
|
+
if (this.state.mode === "topic") {
|
|
455
|
+
return [
|
|
456
|
+
{ label: "刷新", key: "r", action: "refresh" },
|
|
457
|
+
{ label: "返回列表", key: "h", action: "back" }
|
|
458
|
+
];
|
|
459
|
+
}
|
|
460
|
+
if (this.state.mode === "list") {
|
|
461
|
+
const items = [{ label: "刷新", key: "r", action: "refresh" }];
|
|
462
|
+
if (this.state.currentBoard)
|
|
463
|
+
items.push({ label: "返回版面列表", key: "h", action: "back" });
|
|
464
|
+
return items;
|
|
465
|
+
}
|
|
466
|
+
return [];
|
|
467
|
+
}
|
|
468
|
+
closeModal() {
|
|
469
|
+
this.state.modal = null;
|
|
470
|
+
this.state.infoTitle = undefined;
|
|
471
|
+
this.state.infoLines = [];
|
|
472
|
+
this.state.userDetail = undefined;
|
|
473
|
+
this.render();
|
|
474
|
+
}
|
|
475
|
+
async loadView(view, force, signal) {
|
|
476
|
+
switch (view) {
|
|
477
|
+
case "hot": {
|
|
478
|
+
const [index, unread] = await Promise.all([
|
|
479
|
+
this.client.getForumIndex(force, signal),
|
|
480
|
+
this.client.getUnreadCount(force, signal)
|
|
481
|
+
]);
|
|
482
|
+
const indexObject = asObject(index);
|
|
483
|
+
const unreadObject = asObject(unread);
|
|
484
|
+
const hotTopics = asArray(indexObject.hotTopic ?? indexObject.manualHotTopic);
|
|
485
|
+
return {
|
|
486
|
+
title: "十大",
|
|
487
|
+
items: hotTopics.map((topic) => topicItem(topic)),
|
|
488
|
+
stats: unreadStats(unreadObject),
|
|
489
|
+
overview: overviewStats(indexObject, unreadObject)
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
case "new": {
|
|
493
|
+
const topics = asArray(await this.client.getNewTopics(0, 12, force, signal));
|
|
494
|
+
return { title: "最新", items: topics.map((topic) => topicItem(topic)), stats: [{ title: "新帖流", detail: `${topics.length} 条` }] };
|
|
495
|
+
}
|
|
496
|
+
case "boards": {
|
|
497
|
+
const sections = asArray(await this.client.getAllBoards(force, signal));
|
|
498
|
+
const boards = flattenBoards(sections);
|
|
499
|
+
return {
|
|
500
|
+
title: "版面",
|
|
501
|
+
items: boards.slice(0, 24),
|
|
502
|
+
stats: [{ title: "分区", detail: `${sections.length}` }, { title: "版面", detail: `${boards.length}` }],
|
|
503
|
+
status: "版面:j/k 选择 l 进入版面 h 返回 r 刷新"
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
case "following": {
|
|
507
|
+
const topics = asArray(await this.client.getFolloweeTopics(0, 12, force, signal));
|
|
508
|
+
return {
|
|
509
|
+
title: "关注",
|
|
510
|
+
items: topics.map((topic) => topicItem(topic)),
|
|
511
|
+
stats: [{ title: "关注动态", detail: `${topics.length} 条` }, { title: "缓存", detail: "30s" }],
|
|
512
|
+
status: "关注:j/k 选择 l 打开帖子 h 返回 r 刷新"
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
case "favorite": {
|
|
516
|
+
const [meRaw, sectionsRaw, topicFavorites] = await Promise.all([
|
|
517
|
+
this.client.getMe(force, signal),
|
|
518
|
+
this.client.getAllBoards(false, signal),
|
|
519
|
+
this.client.getFavoriteTopics(0, 6, 1, 0, force, signal)
|
|
520
|
+
]);
|
|
521
|
+
const customBoards = asArray(asObject(meRaw).customBoards).filter((id) => typeof id === "number");
|
|
522
|
+
const allBoards = flattenBoards(asArray(sectionsRaw));
|
|
523
|
+
const boardById = new Map(allBoards.filter((board) => board.boardId !== undefined).map((board) => [board.boardId, board]));
|
|
524
|
+
const topicGroups = await mapLimit(customBoards, 3, async (boardId) => {
|
|
525
|
+
const board = boardById.get(boardId);
|
|
526
|
+
const topics = asArray(await this.client.getBoardTopics(boardId, 0, 3, false, force, signal));
|
|
527
|
+
return topics.map((topic) => topicItem(topic, board));
|
|
528
|
+
});
|
|
529
|
+
const boardTopics = topicGroups.flat().sort((left, right) => (right.sortTime ?? 0) - (left.sortTime ?? 0)).slice(0, 12);
|
|
530
|
+
return {
|
|
531
|
+
title: "收藏",
|
|
532
|
+
items: [
|
|
533
|
+
{ title: "收藏主题", meta: "topic/me/favorite", detail: "打开收藏夹主题列表", action: "favorite-topics" },
|
|
534
|
+
{ title: "收藏分组", meta: "me/favorite-topic-group", detail: "查看收藏夹分组", action: "favorite-groups" },
|
|
535
|
+
...asArray(topicFavorites).slice(0, 6).map((topic) => topicItem(topic)),
|
|
536
|
+
...boardTopics
|
|
537
|
+
],
|
|
538
|
+
stats: [
|
|
539
|
+
{ title: "收藏版面", detail: `${customBoards.length} 个` },
|
|
540
|
+
{ title: "收藏主题", detail: `${asArray(topicFavorites).length} 条` },
|
|
541
|
+
{ title: "版面主题", detail: `${boardTopics.length} 条` }
|
|
542
|
+
],
|
|
543
|
+
status: "收藏:j/k 选择 l 打开 h 返回 r 刷新"
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
case "messages": {
|
|
547
|
+
const [unread, recent] = await Promise.all([
|
|
548
|
+
this.client.getUnreadCount(force, signal),
|
|
549
|
+
this.client.getRecentChats(0, 10, force, signal)
|
|
550
|
+
]);
|
|
551
|
+
const chats = asArray(recent);
|
|
552
|
+
const names = await loadChatUserNames(this.client, chats, force, signal);
|
|
553
|
+
return {
|
|
554
|
+
title: "消息",
|
|
555
|
+
items: chats.length > 0 ? chats.map((chat) => chatItem(chat, names)) : [{ title: "暂无最近私信", meta: "recent-contact-users" }],
|
|
556
|
+
stats: unreadStats(asObject(unread)),
|
|
557
|
+
status: "消息:j/k 选择 l 打开会话 h 返回 r 刷新"
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
case "notices": {
|
|
561
|
+
const unread = asObject(await this.client.getUnreadCount(force, signal));
|
|
562
|
+
return {
|
|
563
|
+
title: "通知",
|
|
564
|
+
items: [
|
|
565
|
+
{ title: "系统通知", meta: `${unread.systemCount ?? 0} 未读`, detail: "查看系统通知列表", action: "notices:system" },
|
|
566
|
+
{ title: "@ 通知", meta: `${unread.atCount ?? 0} 未读`, detail: "查看提到我的通知", action: "notices:at" },
|
|
567
|
+
{ title: "回复通知", meta: `${unread.replyCount ?? 0} 未读`, detail: "查看回复我的通知", action: "notices:reply" }
|
|
568
|
+
],
|
|
569
|
+
stats: unreadStats(unread),
|
|
570
|
+
status: "通知:j/k 选择 l 打开列表 h 返回 r 刷新"
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
case "me": {
|
|
574
|
+
const [me, cacheStats] = await Promise.all([this.client.getMe(force, signal), this.client.getCacheStats()]);
|
|
575
|
+
const meObject = asObject(me);
|
|
576
|
+
return {
|
|
577
|
+
title: "我的",
|
|
578
|
+
items: [
|
|
579
|
+
{ title: String(meObject.name ?? "当前账号"), meta: `#${meObject.id ?? "?"}`, detail: String(meObject.levelTitle ?? meObject.groupName ?? ""), userId: asNumber(meObject.id) },
|
|
580
|
+
{ title: "我的最近主题", meta: "me/recent-topic", detail: "查看自己最近发布或回复的主题", action: "recent-topics" },
|
|
581
|
+
{ title: "浏览历史", meta: "me/browsing-record", detail: "查看最近浏览过的主题", action: "browse-history" },
|
|
582
|
+
{ title: "每日签到", meta: "me/signin", detail: "执行签到", action: "signin" }
|
|
583
|
+
],
|
|
584
|
+
stats: [
|
|
585
|
+
{ title: "登录状态", detail: "已登录" },
|
|
586
|
+
{ title: "缓存", detail: `${cacheStats.fileCacheEntries} 文件` }
|
|
587
|
+
],
|
|
588
|
+
status: "我的:j/k 选择 l 打开 h 返回 r 刷新"
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
case "more":
|
|
592
|
+
return {
|
|
593
|
+
title: "更多",
|
|
594
|
+
items: [
|
|
595
|
+
{ title: "随机主题", meta: "topic/random-recent", detail: "随机读取一组最近主题", action: "random-topics" },
|
|
596
|
+
{ title: "我的最近主题", meta: "me/recent-topic", detail: "查看自己最近发布或回复的主题", action: "recent-topics" },
|
|
597
|
+
{ title: "浏览历史", meta: "me/browsing-record", detail: "查看最近浏览过的主题", action: "browse-history" },
|
|
598
|
+
{ title: "收藏主题", meta: "topic/me/favorite", detail: "查看收藏夹主题", action: "favorite-topics" },
|
|
599
|
+
{ title: "收藏更新", meta: "topic/me/favorite?order=1", detail: "查看收藏主题更新", action: "favorite-updates" },
|
|
600
|
+
{ title: "收藏分组", meta: "me/favorite-topic-group", detail: "查看收藏夹分组", action: "favorite-groups" },
|
|
601
|
+
{ title: "粉丝列表", meta: "me/follower", detail: "查看关注我的用户", action: "followers" },
|
|
602
|
+
{ title: "关注列表", meta: "me/followee", detail: "查看我关注的用户", action: "followees" },
|
|
603
|
+
{ title: "全站统计", meta: "card.cc98.org/api/collection/stat", detail: "查看论坛全站统计", action: "card-stat" },
|
|
604
|
+
{ title: "评分原因: 普通", meta: "post/rating-reason?type=0", detail: "查看普通评分原因", action: "rate-reasons:0" },
|
|
605
|
+
{ title: "评分原因: 管理", meta: "post/rating-reason?type=1", detail: "查看管理评分原因", action: "rate-reasons:1" }
|
|
606
|
+
],
|
|
607
|
+
stats: [{ title: "只读入口", detail: "11 个" }, { title: "写入", detail: "不含发帖/回帖" }],
|
|
608
|
+
status: "更多:j/k 选择 l 打开只读内容 h 返回 r 刷新"
|
|
609
|
+
};
|
|
610
|
+
case "settings": {
|
|
611
|
+
const cacheStats = await this.client.getCacheStats();
|
|
612
|
+
return {
|
|
613
|
+
title: "设置",
|
|
614
|
+
items: [...settingsItems],
|
|
615
|
+
stats: [{ title: "缓存", detail: `${cacheStats.fileCacheEntries} 文件` }, { title: "版本", detail: `v${appVersion}` }],
|
|
616
|
+
status: "设置:j/k 选择 l 执行 h 返回"
|
|
617
|
+
};
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
async activateSetting(selected) {
|
|
622
|
+
if (!selected)
|
|
623
|
+
return;
|
|
624
|
+
if (selected.meta === "help") {
|
|
625
|
+
this.state.modal = "help";
|
|
626
|
+
this.render();
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
if (selected.meta === "cache") {
|
|
630
|
+
this.state.status = "正在清理缓存...";
|
|
631
|
+
this.render();
|
|
632
|
+
try {
|
|
633
|
+
await this.client.clearCache();
|
|
634
|
+
this.state.status = "缓存已清理";
|
|
635
|
+
await this.load(true);
|
|
636
|
+
}
|
|
637
|
+
catch {
|
|
638
|
+
this.state.status = "缓存清理失败";
|
|
639
|
+
this.render();
|
|
640
|
+
}
|
|
641
|
+
return;
|
|
642
|
+
}
|
|
643
|
+
if (selected.meta === "update") {
|
|
644
|
+
this.state.status = "正在检查 GitHub Release...";
|
|
645
|
+
this.render();
|
|
646
|
+
try {
|
|
647
|
+
const result = await checkForUpdate();
|
|
648
|
+
this.state.status = result.message;
|
|
649
|
+
}
|
|
650
|
+
catch (error) {
|
|
651
|
+
this.state.status = error instanceof Error ? error.message : "检查更新失败";
|
|
652
|
+
}
|
|
653
|
+
this.render();
|
|
654
|
+
return;
|
|
655
|
+
}
|
|
656
|
+
this.state.status = selected.meta === "logout" ? "退出登录功能开发中..." : "账号切换功能开发中...";
|
|
657
|
+
this.render();
|
|
658
|
+
}
|
|
659
|
+
async activateContentItem(selected, signal) {
|
|
660
|
+
if (selected.topicId !== undefined) {
|
|
661
|
+
await this.openTopic(selected.topicId, false, signal);
|
|
662
|
+
return;
|
|
663
|
+
}
|
|
664
|
+
if (selected.boardId !== undefined) {
|
|
665
|
+
await this.openBoard(selected.boardId, selected.title, false, signal);
|
|
666
|
+
return;
|
|
667
|
+
}
|
|
668
|
+
if (selected.chatUserId !== undefined) {
|
|
669
|
+
await this.openChat(selected.chatUserId, selected.title, false, signal);
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
if (selected.userId !== undefined) {
|
|
673
|
+
await this.showUserDetailById(selected.userId, signal);
|
|
674
|
+
return;
|
|
675
|
+
}
|
|
676
|
+
if (selected.action?.startsWith("notices:")) {
|
|
677
|
+
await this.openNoticeList(selected.action.split(":")[1], signal);
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
if (selected.action) {
|
|
681
|
+
await this.runReadOnlyAction(selected.action, signal);
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
this.state.status = "当前条目不可进入";
|
|
685
|
+
this.render();
|
|
686
|
+
}
|
|
687
|
+
async openTopic(topicId, force, signal) {
|
|
688
|
+
this.state.mode = "topic";
|
|
689
|
+
this.state.loading = true;
|
|
690
|
+
this.state.error = undefined;
|
|
691
|
+
this.state.status = "";
|
|
692
|
+
this.state.topic = undefined;
|
|
693
|
+
this.state.scroll = 0;
|
|
694
|
+
this.render();
|
|
695
|
+
try {
|
|
696
|
+
const [topicRaw, postsRaw] = await Promise.all([
|
|
697
|
+
this.client.getTopic(topicId, force, signal),
|
|
698
|
+
this.client.getTopicPosts(topicId, 0, 10, force, signal)
|
|
699
|
+
]);
|
|
700
|
+
this.state.topic = buildTopicReader(topicId, asObject(topicRaw), asArray(postsRaw), 10);
|
|
701
|
+
this.state.loading = false;
|
|
702
|
+
this.state.status = "";
|
|
703
|
+
}
|
|
704
|
+
catch (error) {
|
|
705
|
+
if (!isAbortError(error)) {
|
|
706
|
+
this.state.error = error instanceof Error ? error.message : String(error);
|
|
707
|
+
this.state.loading = false;
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
this.render();
|
|
711
|
+
}
|
|
712
|
+
async openBoard(boardId, title, force, signal, pushParent = true) {
|
|
713
|
+
if (pushParent)
|
|
714
|
+
this.snapshotParent();
|
|
715
|
+
this.state.loading = true;
|
|
716
|
+
this.state.error = undefined;
|
|
717
|
+
this.state.viewTitle = title;
|
|
718
|
+
this.state.focus = "content";
|
|
719
|
+
this.state.currentBoard = { boardId, title };
|
|
720
|
+
this.state.itemIndex = 0;
|
|
721
|
+
this.state.scroll = 0;
|
|
722
|
+
this.render();
|
|
723
|
+
try {
|
|
724
|
+
const topics = asArray(await this.client.getBoardTopics(boardId, 0, 20, false, force, signal));
|
|
725
|
+
this.state.items = topics.map((topic) => topicItem(topic, { title, boardId }));
|
|
726
|
+
this.state.stats = [{ title: "主题", detail: `${topics.length} 条` }];
|
|
727
|
+
this.state.status = `版面 ${title}: j/k 选择 l 打开帖子 h 返回`;
|
|
728
|
+
}
|
|
729
|
+
catch (error) {
|
|
730
|
+
if (!isAbortError(error))
|
|
731
|
+
this.state.error = error instanceof Error ? error.message : String(error);
|
|
732
|
+
}
|
|
733
|
+
finally {
|
|
734
|
+
this.state.loading = false;
|
|
735
|
+
this.render();
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
async openChat(userId, title, force, signal, pushParent = true) {
|
|
739
|
+
if (pushParent)
|
|
740
|
+
this.snapshotParent();
|
|
741
|
+
this.state.loading = true;
|
|
742
|
+
this.state.error = undefined;
|
|
743
|
+
this.state.viewTitle = `私信: ${title}`;
|
|
744
|
+
this.state.focus = "content";
|
|
745
|
+
this.state.itemIndex = 0;
|
|
746
|
+
this.state.scroll = 0;
|
|
747
|
+
this.render();
|
|
748
|
+
try {
|
|
749
|
+
const messages = asArray(await this.client.getChatHistory(userId, 0, 10, force, signal));
|
|
750
|
+
this.state.items = chatMessageItems(messages, title, userId);
|
|
751
|
+
this.state.currentChat = { userId, title, loaded: messages.length, size: 10, hasMore: messages.length >= 10 };
|
|
752
|
+
this.state.stats = [{ title: "会话", detail: title }, { title: "消息", detail: `${messages.length}` }];
|
|
753
|
+
this.state.status = "私信:n 加载更多 h 返回";
|
|
754
|
+
}
|
|
755
|
+
catch (error) {
|
|
756
|
+
if (!isAbortError(error))
|
|
757
|
+
this.state.error = error instanceof Error ? error.message : String(error);
|
|
758
|
+
}
|
|
759
|
+
finally {
|
|
760
|
+
this.state.loading = false;
|
|
761
|
+
this.render();
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
async loadNextChatPage(signal) {
|
|
765
|
+
const chat = this.state.currentChat;
|
|
766
|
+
if (!chat?.hasMore || this.state.loadingMore)
|
|
767
|
+
return;
|
|
768
|
+
this.state.loadingMore = true;
|
|
769
|
+
this.render();
|
|
770
|
+
try {
|
|
771
|
+
const messages = asArray(await this.client.getChatHistory(chat.userId, chat.loaded, chat.size, false, signal));
|
|
772
|
+
this.state.items.push(...chatMessageItems(messages, chat.title, chat.userId));
|
|
773
|
+
chat.loaded += messages.length;
|
|
774
|
+
chat.hasMore = messages.length >= chat.size;
|
|
775
|
+
this.state.status = chat.hasMore ? "已加载更多私信" : "已到底";
|
|
776
|
+
}
|
|
777
|
+
catch (error) {
|
|
778
|
+
if (!isAbortError(error))
|
|
779
|
+
this.state.status = error instanceof Error ? error.message : "加载失败";
|
|
780
|
+
}
|
|
781
|
+
finally {
|
|
782
|
+
this.state.loadingMore = false;
|
|
783
|
+
this.render();
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
async loadNextTopicPage(signal, quiet = false) {
|
|
787
|
+
const topic = this.state.topic;
|
|
788
|
+
if (!topic?.hasMore || this.state.loadingMore)
|
|
789
|
+
return;
|
|
790
|
+
this.state.loadingMore = true;
|
|
791
|
+
if (!quiet)
|
|
792
|
+
this.render();
|
|
793
|
+
try {
|
|
794
|
+
const posts = asArray(await this.client.getTopicPosts(topic.topicId, topic.loaded, topic.size, false, signal));
|
|
795
|
+
appendTopicPosts(topic, posts);
|
|
796
|
+
this.state.status = topic.hasMore ? "已加载下一页" : "已到底";
|
|
797
|
+
}
|
|
798
|
+
catch (error) {
|
|
799
|
+
if (!isAbortError(error))
|
|
800
|
+
this.state.status = error instanceof Error ? error.message : "加载失败";
|
|
801
|
+
}
|
|
802
|
+
finally {
|
|
803
|
+
this.state.loadingMore = false;
|
|
804
|
+
this.render();
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
async jumpToTopicFloor(floor, signal) {
|
|
808
|
+
const topic = this.state.topic;
|
|
809
|
+
if (!topic)
|
|
810
|
+
return;
|
|
811
|
+
const loaded = findTopicPostByFloor(topic, floor);
|
|
812
|
+
if (loaded) {
|
|
813
|
+
this.state.scroll = loaded.lineStart;
|
|
814
|
+
this.state.status = getStatus(this.state);
|
|
815
|
+
this.render();
|
|
816
|
+
return;
|
|
817
|
+
}
|
|
818
|
+
while (topic.hasMore && !findTopicPostByFloor(topic, floor)) {
|
|
819
|
+
await this.loadNextTopicPage(signal, true);
|
|
820
|
+
}
|
|
821
|
+
const post = findTopicPostByFloor(topic, floor);
|
|
822
|
+
this.state.scroll = post?.lineStart ?? this.state.scroll;
|
|
823
|
+
this.state.status = post ? getStatus(this.state) : `未找到 ${floor} 楼`;
|
|
824
|
+
this.render();
|
|
825
|
+
}
|
|
826
|
+
async runReadOnlyAction(action, signal) {
|
|
827
|
+
switch (action) {
|
|
828
|
+
case "random-topics": {
|
|
829
|
+
const topics = asArray(await this.client.getRandomTopics(10, true, signal));
|
|
830
|
+
this.openReadOnlyList("随机主题", topics.map((topic) => topicItem(topic)), [{ title: "主题", detail: `${topics.length}` }]);
|
|
831
|
+
return;
|
|
832
|
+
}
|
|
833
|
+
case "recent-topics": {
|
|
834
|
+
const topics = asArray(await this.client.getRecentTopics(undefined, 0, 11, false, signal));
|
|
835
|
+
this.openReadOnlyList("我的最近主题", topics.map((topic) => topicItem(topic)), [{ title: "主题", detail: `${topics.length}` }]);
|
|
836
|
+
return;
|
|
837
|
+
}
|
|
838
|
+
case "browse-history": {
|
|
839
|
+
const topics = asArray(await this.client.getBrowseHistory(0, 11, false, signal));
|
|
840
|
+
this.openReadOnlyList("浏览历史", topics.map((topic) => historyItem(topic)), [{ title: "记录", detail: `${topics.length}` }]);
|
|
841
|
+
return;
|
|
842
|
+
}
|
|
843
|
+
case "favorite-topics":
|
|
844
|
+
case "favorite-updates": {
|
|
845
|
+
const order = action === "favorite-updates" ? 1 : 0;
|
|
846
|
+
const topics = asArray(await this.client.getFavoriteTopics(0, 11, order, 0, false, signal));
|
|
847
|
+
this.openReadOnlyList(action === "favorite-updates" ? "收藏更新" : "收藏主题", topics.map((topic) => topicItem(topic)), [{ title: "主题", detail: `${topics.length}` }]);
|
|
848
|
+
return;
|
|
849
|
+
}
|
|
850
|
+
case "favorite-groups": {
|
|
851
|
+
const groups = asArray(await this.client.getFavoriteGroups(false, signal));
|
|
852
|
+
this.openReadOnlyList("收藏分组", groups.map((group) => genericItem(group, "收藏分组")), [{ title: "分组", detail: `${groups.length}` }]);
|
|
853
|
+
return;
|
|
854
|
+
}
|
|
855
|
+
case "followers":
|
|
856
|
+
case "followees": {
|
|
857
|
+
await this.openFriendUsers(action === "followers" ? "follower" : "followee", signal);
|
|
858
|
+
return;
|
|
859
|
+
}
|
|
860
|
+
case "card-stat": {
|
|
861
|
+
const stat = await this.client.getCardStat(false, signal);
|
|
862
|
+
this.state.modal = "info";
|
|
863
|
+
this.state.infoTitle = "全站统计";
|
|
864
|
+
this.state.infoLines = jsonPreviewLines(stat);
|
|
865
|
+
this.render();
|
|
866
|
+
return;
|
|
867
|
+
}
|
|
868
|
+
case "rate-reasons:0":
|
|
869
|
+
case "rate-reasons:1": {
|
|
870
|
+
const type = action.endsWith(":1") ? 1 : 0;
|
|
871
|
+
const reasons = asArray(await this.client.getPostRateReasons(type, false, signal));
|
|
872
|
+
this.openReadOnlyList(type === 1 ? "评分原因: 管理" : "评分原因: 普通", reasons.map((reason) => genericItem(reason, "评分原因")), [{ title: "原因", detail: `${reasons.length}` }]);
|
|
873
|
+
return;
|
|
874
|
+
}
|
|
875
|
+
case "signin": {
|
|
876
|
+
await this.signin();
|
|
877
|
+
return;
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
this.state.status = "暂不支持该入口";
|
|
881
|
+
this.render();
|
|
882
|
+
}
|
|
883
|
+
async openNoticeList(type, signal) {
|
|
884
|
+
const notices = asArray(await this.client.getNotices(type, 0, 10, false, signal));
|
|
885
|
+
const titleMap = { system: "系统通知", at: "@ 通知", reply: "回复通知" };
|
|
886
|
+
this.openReadOnlyList(titleMap[type], notices.map((notice) => noticeItem(notice)), [{ title: "通知", detail: `${notices.length}` }]);
|
|
887
|
+
}
|
|
888
|
+
async performSearch(signal) {
|
|
889
|
+
const query = this.state.searchQuery.trim();
|
|
890
|
+
if (!query)
|
|
891
|
+
return;
|
|
892
|
+
this.state.loading = true;
|
|
893
|
+
this.render();
|
|
894
|
+
try {
|
|
895
|
+
const results = this.state.searchMode === "topics"
|
|
896
|
+
? asArray(await this.client.searchTopics(query, 0, 20, true, signal)).map((topic) => topicItem(topic))
|
|
897
|
+
: asArray(await this.client.searchUsers(query, true, signal)).map((user) => userItem(user));
|
|
898
|
+
this.state.searchResults = results;
|
|
899
|
+
this.state.itemIndex = 0;
|
|
900
|
+
}
|
|
901
|
+
catch (error) {
|
|
902
|
+
if (!isAbortError(error))
|
|
903
|
+
this.state.status = error instanceof Error ? error.message : "搜索失败";
|
|
904
|
+
}
|
|
905
|
+
finally {
|
|
906
|
+
this.state.loading = false;
|
|
907
|
+
this.render();
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
async toggleFavorite() {
|
|
911
|
+
const topic = this.state.topic;
|
|
912
|
+
if (!topic)
|
|
913
|
+
return;
|
|
914
|
+
try {
|
|
915
|
+
const isFavorite = await this.client.isTopicFavorite(topic.topicId, true);
|
|
916
|
+
if (isFavorite) {
|
|
917
|
+
await this.client.removeFavorite(topic.topicId);
|
|
918
|
+
this.state.status = "已取消收藏";
|
|
919
|
+
}
|
|
920
|
+
else {
|
|
921
|
+
await this.client.addFavorite(topic.topicId);
|
|
922
|
+
this.state.status = "已收藏";
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
catch (error) {
|
|
926
|
+
this.state.status = error instanceof Error ? error.message : "收藏操作失败";
|
|
927
|
+
}
|
|
928
|
+
this.render();
|
|
929
|
+
}
|
|
930
|
+
async reactToCurrentPost(isLike) {
|
|
931
|
+
const topic = this.state.topic;
|
|
932
|
+
if (!topic)
|
|
933
|
+
return;
|
|
934
|
+
const post = currentTopicPost(topic, this.state.scroll);
|
|
935
|
+
if (!post?.id) {
|
|
936
|
+
this.state.status = "当前楼层没有可操作的帖子 ID";
|
|
937
|
+
this.render();
|
|
938
|
+
return;
|
|
939
|
+
}
|
|
940
|
+
try {
|
|
941
|
+
await this.client.reactToPost(post.id, isLike);
|
|
942
|
+
this.state.status = isLike ? "已点赞" : "已踩";
|
|
943
|
+
}
|
|
944
|
+
catch (error) {
|
|
945
|
+
this.state.status = error instanceof Error ? error.message : "操作失败";
|
|
946
|
+
}
|
|
947
|
+
this.render();
|
|
948
|
+
}
|
|
949
|
+
async showCurrentUser(signal) {
|
|
950
|
+
const topic = this.state.topic;
|
|
951
|
+
if (!topic)
|
|
952
|
+
return;
|
|
953
|
+
const post = currentTopicPost(topic, this.state.scroll);
|
|
954
|
+
if (!post?.userId) {
|
|
955
|
+
this.state.status = "当前楼层没有用户 ID";
|
|
956
|
+
this.render();
|
|
957
|
+
return;
|
|
958
|
+
}
|
|
959
|
+
await this.showUserDetailById(post.userId, signal);
|
|
960
|
+
}
|
|
961
|
+
async showUserDetailById(userId, signal) {
|
|
962
|
+
this.state.status = "正在读取用户信息...";
|
|
963
|
+
this.render();
|
|
964
|
+
try {
|
|
965
|
+
const [profileRaw, recentRaw] = await Promise.all([
|
|
966
|
+
this.client.getUserProfile(userId, false, signal),
|
|
967
|
+
this.client.getRecentTopics(userId, 0, 5, false, signal)
|
|
968
|
+
]);
|
|
969
|
+
const profile = asObject(profileRaw);
|
|
970
|
+
this.state.userDetail = {
|
|
971
|
+
userId,
|
|
972
|
+
name: String(profile.name ?? `#${userId}`),
|
|
973
|
+
level: String(profile.levelTitle ?? profile.groupName ?? ""),
|
|
974
|
+
postCount: asNumber(profile.postCount),
|
|
975
|
+
fanCount: asNumber(profile.fanCount),
|
|
976
|
+
followCount: asNumber(profile.followCount),
|
|
977
|
+
isFollowing: Boolean(profile.isFollowing),
|
|
978
|
+
recentTopics: asArray(recentRaw).map((topic) => topicItem(topic))
|
|
979
|
+
};
|
|
980
|
+
this.state.modal = "user";
|
|
981
|
+
this.state.status = getStatus(this.state);
|
|
982
|
+
}
|
|
983
|
+
catch (error) {
|
|
984
|
+
if (!isAbortError(error))
|
|
985
|
+
this.state.status = error instanceof Error ? error.message : "读取用户失败";
|
|
986
|
+
}
|
|
987
|
+
this.render();
|
|
988
|
+
}
|
|
989
|
+
async showTopicVote(signal) {
|
|
990
|
+
const topic = this.state.topic;
|
|
991
|
+
if (!topic)
|
|
992
|
+
return;
|
|
993
|
+
try {
|
|
994
|
+
const vote = await this.client.getTopicVote(topic.topicId, false, signal);
|
|
995
|
+
this.state.modal = "info";
|
|
996
|
+
this.state.infoTitle = "投票信息";
|
|
997
|
+
this.state.infoLines = jsonPreviewLines(vote);
|
|
998
|
+
}
|
|
999
|
+
catch (error) {
|
|
1000
|
+
if (!isAbortError(error))
|
|
1001
|
+
this.state.status = error instanceof Error ? error.message : "读取投票失败";
|
|
1002
|
+
}
|
|
1003
|
+
this.render();
|
|
1004
|
+
}
|
|
1005
|
+
async showPostReactionState(signal) {
|
|
1006
|
+
const topic = this.state.topic;
|
|
1007
|
+
if (!topic)
|
|
1008
|
+
return;
|
|
1009
|
+
const post = currentTopicPost(topic, this.state.scroll);
|
|
1010
|
+
if (!post?.id)
|
|
1011
|
+
return;
|
|
1012
|
+
try {
|
|
1013
|
+
const state = await this.client.getPostReactionState(post.id, true, signal);
|
|
1014
|
+
this.state.modal = "info";
|
|
1015
|
+
this.state.infoTitle = "楼层评价";
|
|
1016
|
+
this.state.infoLines = jsonPreviewLines(state);
|
|
1017
|
+
}
|
|
1018
|
+
catch (error) {
|
|
1019
|
+
if (!isAbortError(error))
|
|
1020
|
+
this.state.status = error instanceof Error ? error.message : "读取评价失败";
|
|
1021
|
+
}
|
|
1022
|
+
this.render();
|
|
1023
|
+
}
|
|
1024
|
+
async toggleFollow() {
|
|
1025
|
+
const user = this.state.userDetail;
|
|
1026
|
+
if (!user)
|
|
1027
|
+
return;
|
|
1028
|
+
try {
|
|
1029
|
+
if (user.isFollowing) {
|
|
1030
|
+
await this.client.unfollowUser(user.userId);
|
|
1031
|
+
user.isFollowing = false;
|
|
1032
|
+
this.state.status = "已取消关注";
|
|
1033
|
+
}
|
|
1034
|
+
else {
|
|
1035
|
+
await this.client.followUser(user.userId);
|
|
1036
|
+
user.isFollowing = true;
|
|
1037
|
+
this.state.status = "已关注";
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
catch (error) {
|
|
1041
|
+
this.state.status = error instanceof Error ? error.message : "关注操作失败";
|
|
1042
|
+
}
|
|
1043
|
+
this.render();
|
|
1044
|
+
}
|
|
1045
|
+
async sendPrivateMessage(userId, content) {
|
|
1046
|
+
try {
|
|
1047
|
+
await this.client.sendMessage(userId, content);
|
|
1048
|
+
this.state.status = "私信已发送";
|
|
1049
|
+
}
|
|
1050
|
+
catch (error) {
|
|
1051
|
+
this.state.status = error instanceof Error ? error.message : "发送失败";
|
|
1052
|
+
}
|
|
1053
|
+
this.render();
|
|
1054
|
+
}
|
|
1055
|
+
async signin() {
|
|
1056
|
+
this.state.status = "正在签到...";
|
|
1057
|
+
this.render();
|
|
1058
|
+
try {
|
|
1059
|
+
const result = await this.client.signin();
|
|
1060
|
+
this.state.modal = "info";
|
|
1061
|
+
this.state.infoTitle = "签到结果";
|
|
1062
|
+
this.state.infoLines = jsonPreviewLines(result);
|
|
1063
|
+
}
|
|
1064
|
+
catch (error) {
|
|
1065
|
+
this.state.status = error instanceof Error ? error.message : "签到失败";
|
|
1066
|
+
}
|
|
1067
|
+
this.render();
|
|
1068
|
+
}
|
|
1069
|
+
async openFriendUsers(type, signal) {
|
|
1070
|
+
const ids = asArray(await this.client.getFriendIds(type, 0, 20, false, signal)).filter((id) => typeof id === "number");
|
|
1071
|
+
const users = asArray(await this.client.getUsers(ids, false, signal));
|
|
1072
|
+
this.openReadOnlyList(type === "follower" ? "粉丝列表" : "关注列表", users.map((user) => userItem(user)), [{ title: "用户", detail: `${users.length}` }]);
|
|
1073
|
+
}
|
|
1074
|
+
openReadOnlyList(title, items, stats) {
|
|
1075
|
+
this.snapshotParent();
|
|
1076
|
+
this.state.viewTitle = title;
|
|
1077
|
+
this.state.items = items;
|
|
1078
|
+
this.state.stats = stats;
|
|
1079
|
+
this.state.itemIndex = 0;
|
|
1080
|
+
this.state.scroll = 0;
|
|
1081
|
+
this.state.focus = "content";
|
|
1082
|
+
this.state.currentBoard = undefined;
|
|
1083
|
+
this.state.currentChat = undefined;
|
|
1084
|
+
this.state.topic = undefined;
|
|
1085
|
+
this.state.mode = "list";
|
|
1086
|
+
this.state.status = `${title}: j/k 选择 l 打开 h 返回`;
|
|
1087
|
+
this.render();
|
|
1088
|
+
}
|
|
1089
|
+
snapshotParent() {
|
|
1090
|
+
if (!this.state.parentList) {
|
|
1091
|
+
this.state.parentList = {
|
|
1092
|
+
title: this.state.viewTitle,
|
|
1093
|
+
items: [...this.state.items],
|
|
1094
|
+
stats: [...this.state.stats],
|
|
1095
|
+
itemIndex: this.state.itemIndex,
|
|
1096
|
+
status: this.state.status
|
|
1097
|
+
};
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
restoreParentList() {
|
|
1101
|
+
const parent = this.state.parentList;
|
|
1102
|
+
if (!parent)
|
|
1103
|
+
return;
|
|
1104
|
+
this.state.viewTitle = parent.title;
|
|
1105
|
+
this.state.items = parent.items;
|
|
1106
|
+
this.state.stats = parent.stats;
|
|
1107
|
+
this.state.itemIndex = parent.itemIndex;
|
|
1108
|
+
this.state.status = parent.status;
|
|
1109
|
+
this.state.parentList = undefined;
|
|
1110
|
+
this.state.currentBoard = undefined;
|
|
1111
|
+
this.state.currentChat = undefined;
|
|
1112
|
+
this.state.topic = undefined;
|
|
1113
|
+
this.state.mode = "list";
|
|
1114
|
+
this.state.focus = "content";
|
|
1115
|
+
this.state.scroll = 0;
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
//# sourceMappingURL=controller.js.map
|