fishpi 0.0.4 → 0.0.8

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.
@@ -0,0 +1,315 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
+ return new (P || (P = Promise))(function (resolve, reject) {
24
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
28
+ });
29
+ };
30
+ var __generator = (this && this.__generator) || function (thisArg, body) {
31
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
32
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
33
+ function verb(n) { return function (v) { return step([n, v]); }; }
34
+ function step(op) {
35
+ if (f) throw new TypeError("Generator is already executing.");
36
+ while (_) try {
37
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
38
+ if (y = 0, t) op = [op[0] & 2, t.value];
39
+ switch (op[0]) {
40
+ case 0: case 1: t = op; break;
41
+ case 4: _.label++; return { value: op[1], done: false };
42
+ case 5: _.label++; y = op[1]; op = [0]; continue;
43
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
44
+ default:
45
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
46
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
47
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
48
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
49
+ if (t[2]) _.ops.pop();
50
+ _.trys.pop(); continue;
51
+ }
52
+ op = body.call(thisArg, _);
53
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
54
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
55
+ }
56
+ };
57
+ var __importDefault = (this && this.__importDefault) || function (mod) {
58
+ return (mod && mod.__esModule) ? mod : { "default": mod };
59
+ };
60
+ Object.defineProperty(exports, "__esModule", { value: true });
61
+ var reconnecting_websocket_1 = __importDefault(require("reconnecting-websocket"));
62
+ var utils_1 = require("./utils");
63
+ var Chat = /** @class */ (function () {
64
+ function Chat(token) {
65
+ if (token === void 0) { token = ''; }
66
+ this._apiKey = '';
67
+ this._rwss = {};
68
+ this._wsCallbacks = {};
69
+ if (!token) {
70
+ return;
71
+ }
72
+ this._apiKey = token;
73
+ }
74
+ /**
75
+ * 重新设置请求 Token
76
+ * @param apiKey 接口 API Key
77
+ */
78
+ Chat.prototype.setToken = function (apiKey) {
79
+ this._apiKey = apiKey;
80
+ };
81
+ /**
82
+ * 获取有私聊用户列表第一条消息
83
+ * @returns 私聊消息列表
84
+ */
85
+ Chat.prototype.list = function () {
86
+ return __awaiter(this, void 0, void 0, function () {
87
+ var rsp, e_1;
88
+ return __generator(this, function (_a) {
89
+ switch (_a.label) {
90
+ case 0:
91
+ _a.trys.push([0, 2, , 3]);
92
+ return [4 /*yield*/, (0, utils_1.request)({
93
+ url: "chat/get-list?apiKey=".concat(this._apiKey)
94
+ })];
95
+ case 1:
96
+ rsp = _a.sent();
97
+ if (rsp.status === 401) {
98
+ return [2 /*return*/, { code: -1, msg: '登录已失效,请重新登录!' }];
99
+ }
100
+ rsp = rsp.data;
101
+ return [2 /*return*/, rsp];
102
+ case 2:
103
+ e_1 = _a.sent();
104
+ throw e_1;
105
+ case 3: return [2 /*return*/];
106
+ }
107
+ });
108
+ });
109
+ };
110
+ /**
111
+ * 获取用户私聊历史消息
112
+ * @param param 消息参数
113
+ * @returns 私聊消息列表
114
+ */
115
+ Chat.prototype.get = function (param) {
116
+ return __awaiter(this, void 0, void 0, function () {
117
+ var rsp, e_2;
118
+ return __generator(this, function (_a) {
119
+ switch (_a.label) {
120
+ case 0:
121
+ _a.trys.push([0, 2, , 3]);
122
+ return [4 /*yield*/, (0, utils_1.request)({
123
+ url: "chat/get-message?apiKey=".concat(this._apiKey, "&toUser=").concat(param.user, "&page=").concat(param.page, "&Size=").concat(param.size)
124
+ })];
125
+ case 1:
126
+ rsp = _a.sent();
127
+ if (rsp.status === 401) {
128
+ return [2 /*return*/, { code: -1, msg: '登录已失效,请重新登录!' }];
129
+ }
130
+ rsp = rsp.data;
131
+ if (param.autoRead)
132
+ this.markRead(param.user);
133
+ return [2 /*return*/, rsp];
134
+ case 2:
135
+ e_2 = _a.sent();
136
+ throw e_2;
137
+ case 3: return [2 /*return*/];
138
+ }
139
+ });
140
+ });
141
+ };
142
+ /**
143
+ * 标记用户消息已读
144
+ * @param user 用户名
145
+ * @returns 执行结果
146
+ */
147
+ Chat.prototype.markRead = function (user) {
148
+ return __awaiter(this, void 0, void 0, function () {
149
+ var rsp, e_3;
150
+ return __generator(this, function (_a) {
151
+ switch (_a.label) {
152
+ case 0:
153
+ _a.trys.push([0, 2, , 3]);
154
+ return [4 /*yield*/, (0, utils_1.request)({
155
+ url: "chat/get-message?apiKey=".concat(this._apiKey, "&fromUser=").concat(user)
156
+ })];
157
+ case 1:
158
+ rsp = _a.sent();
159
+ if (rsp.status === 401) {
160
+ return [2 /*return*/, { code: -1, msg: '登录已失效,请重新登录!' }];
161
+ }
162
+ rsp = rsp.data;
163
+ return [2 /*return*/, rsp];
164
+ case 2:
165
+ e_3 = _a.sent();
166
+ throw e_3;
167
+ case 3: return [2 /*return*/];
168
+ }
169
+ });
170
+ });
171
+ };
172
+ /**
173
+ * 获取未读消息
174
+ * @returns 未读消息列表
175
+ */
176
+ Chat.prototype.unread = function () {
177
+ return __awaiter(this, void 0, void 0, function () {
178
+ var rsp, e_4;
179
+ return __generator(this, function (_a) {
180
+ switch (_a.label) {
181
+ case 0:
182
+ _a.trys.push([0, 2, , 3]);
183
+ return [4 /*yield*/, (0, utils_1.request)({
184
+ url: "chat/has-unread?apiKey=".concat(this._apiKey)
185
+ })];
186
+ case 1:
187
+ rsp = _a.sent();
188
+ if (rsp.status === 401) {
189
+ return [2 /*return*/, { code: -1, msg: '登录已失效,请重新登录!' }];
190
+ }
191
+ rsp = rsp.data;
192
+ return [2 /*return*/, rsp];
193
+ case 2:
194
+ e_4 = _a.sent();
195
+ throw e_4;
196
+ case 3: return [2 /*return*/];
197
+ }
198
+ });
199
+ });
200
+ };
201
+ /**
202
+ * 移除聊天室消息监听函数
203
+ * @param user 指定用户消息监听函数,空为新信息监听
204
+ * @param wsCallback 要移除的函数,若为空,则清空消息监听
205
+ */
206
+ Chat.prototype.removeListener = function (user, wsCallback) {
207
+ if (user === void 0) { user = ''; }
208
+ if (wsCallback == null)
209
+ delete this._wsCallbacks[user];
210
+ if (this._wsCallbacks[user].indexOf(wsCallback) < 0)
211
+ return;
212
+ this._wsCallbacks[user].splice(this._wsCallbacks[user].indexOf(wsCallback), 1);
213
+ };
214
+ /**
215
+ * 添加聊天室消息监听函数
216
+ * @param wsCallback 消息监听函数
217
+ * @param user 指定为用户消息监听函数,空为新信息监听
218
+ */
219
+ Chat.prototype.addListener = function (wsCallback, user) {
220
+ if (user === void 0) { user = ''; }
221
+ return __awaiter(this, void 0, void 0, function () {
222
+ return __generator(this, function (_a) {
223
+ if (this._rwss[user]) {
224
+ if (this._wsCallbacks[user].indexOf(wsCallback) < 0)
225
+ this._wsCallbacks[user].push(wsCallback);
226
+ return [2 /*return*/];
227
+ }
228
+ this._wsCallbacks[user].push(wsCallback);
229
+ this.connect(user);
230
+ return [2 /*return*/];
231
+ });
232
+ });
233
+ };
234
+ /**
235
+ * 连接用户私聊频道
236
+ * @param user 私聊用户名
237
+ * @returns Websocket 连接对象
238
+ */
239
+ Chat.prototype.connect = function (user) {
240
+ var _this = this;
241
+ return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
242
+ var _a, _b, _c, _d, _e;
243
+ var _f;
244
+ var _this = this;
245
+ return __generator(this, function (_g) {
246
+ switch (_g.label) {
247
+ case 0:
248
+ if (this._rwss[user])
249
+ return [2 /*return*/, resolve(this._rwss[user])];
250
+ _a = this._rwss;
251
+ _b = user;
252
+ _c = reconnecting_websocket_1.default.bind;
253
+ _d = [void 0, user ?
254
+ "wss://".concat(utils_1.domain, "chat-channel?apiKey=").concat(this._apiKey, "&toUser=").concat(user)
255
+ : "wss://".concat(utils_1.domain, "user-channel"), []];
256
+ _f = {};
257
+ if (!utils_1.isBrowse) return [3 /*break*/, 1];
258
+ _e = window.WebSocket;
259
+ return [3 /*break*/, 3];
260
+ case 1: return [4 /*yield*/, Promise.resolve().then(function () { return __importStar(require('ws')); })];
261
+ case 2:
262
+ _e = (_g.sent()).WebSocket;
263
+ _g.label = 3;
264
+ case 3:
265
+ _a[_b] = new (_c.apply(reconnecting_websocket_1.default, _d.concat([(
266
+ // eslint-disable-next-line @typescript-eslint/naming-convention
267
+ _f.WebSocket = _e,
268
+ _f.connectionTimeout = 10000,
269
+ _f)])))();
270
+ this._rwss[user].onopen = function (e) {
271
+ resolve(_this._rwss[user]);
272
+ };
273
+ this._rwss[user].onmessage = function (e) { return __awaiter(_this, void 0, void 0, function () {
274
+ return __generator(this, function (_a) {
275
+ this._wsCallbacks[user].forEach(function (call) { return call(e); });
276
+ return [2 /*return*/];
277
+ });
278
+ }); };
279
+ this._rwss[user].onerror = function (e) {
280
+ reject(e);
281
+ };
282
+ this._rwss[user].onclose = function (e) {
283
+ delete _this._rwss[user];
284
+ };
285
+ return [2 /*return*/];
286
+ }
287
+ });
288
+ }); });
289
+ };
290
+ /**
291
+ *
292
+ * @param user 私聊用户名
293
+ * @param content 私聊内容
294
+ * @returns Websocket 连接对象
295
+ */
296
+ Chat.prototype.send = function (user, content) {
297
+ return __awaiter(this, void 0, void 0, function () {
298
+ return __generator(this, function (_a) {
299
+ switch (_a.label) {
300
+ case 0:
301
+ if (!!this._rwss[user]) return [3 /*break*/, 2];
302
+ return [4 /*yield*/, this.connect(user)];
303
+ case 1:
304
+ _a.sent();
305
+ _a.label = 2;
306
+ case 2:
307
+ this._rwss[user].send(content);
308
+ return [2 /*return*/, this._rwss[user]];
309
+ }
310
+ });
311
+ });
312
+ };
313
+ return Chat;
314
+ }());
315
+ exports.default = Chat;
@@ -0,0 +1,80 @@
1
+ import { ApiResponse, ChatContentType, ChatMessageType, ChatRoomMessage, GestureType, RedPacket, RedPacketInfo } from './typing';
2
+ declare class ChatRoom {
3
+ private _apiKey;
4
+ private _discusse;
5
+ private _onlines;
6
+ private _rws;
7
+ private _wsTimer;
8
+ private _wsCallbacks;
9
+ constructor(token?: string);
10
+ /**
11
+ * 当前在线人数列表,需要先调用 addListener 添加聊天室消息监听
12
+ */
13
+ get onlines(): any[];
14
+ /**
15
+ * 当前聊天室话题,需要先调用 addListener 添加聊天室消息监听
16
+ */
17
+ get discusse(): string;
18
+ /**
19
+ * 設置当前聊天室话题
20
+ */
21
+ set discusse(val: string);
22
+ /**
23
+ * 重新设置请求 Token
24
+ * @param apiKey 接口 API Key
25
+ */
26
+ setToken(apiKey: string): void;
27
+ /**
28
+ * 查询聊天室历史消息
29
+ * @param page 消息页码
30
+ */
31
+ more(page?: number, type?: ChatContentType): Promise<ApiResponse<Array<ChatRoomMessage>>>;
32
+ get(data: {
33
+ oId: string;
34
+ mode: ChatMessageType.Context;
35
+ size: 25;
36
+ type: ChatContentType.HTML;
37
+ }): Promise<ApiResponse<Array<ChatRoomMessage>>>;
38
+ /**
39
+ * 撤回消息,普通成员 24 小时内可撤回一条自己的消息,纪律委员/OP/管理员角色可以撤回任意人消息
40
+ * @param oId 消息 Id
41
+ */
42
+ revoke(oId: string): Promise<ApiResponse<undefined>>;
43
+ /**
44
+ * 发送一条消息
45
+ * @param msg 消息内容,支持 Markdown
46
+ */
47
+ send(msg: string): Promise<ApiResponse<undefined>>;
48
+ /**
49
+ * 获取消息原文(比如 Markdown)
50
+ * @param oId 消息 Id
51
+ */
52
+ raw(oId: string): Promise<string>;
53
+ /**
54
+ * 红包接口对象
55
+ */
56
+ get redpacket(): {
57
+ /**
58
+ * 打开一个红包
59
+ * @param oId 红包消息 Id
60
+ * @param gesture 猜拳类型
61
+ */
62
+ open(oId: string, gesture?: GestureType | undefined): Promise<ApiResponse<RedPacketInfo>>;
63
+ /**
64
+ * 发送一个红包
65
+ * @param redpacket 红包对象
66
+ */
67
+ send(redpacket: RedPacket): Promise<ApiResponse<undefined>>;
68
+ };
69
+ /**
70
+ * 移除聊天室消息监听函数
71
+ * @param wsCallback 消息监听函数
72
+ */
73
+ removeListener(wsCallback: Function): void;
74
+ /**
75
+ * 添加聊天室消息监听函数
76
+ * @param wsCallback 消息监听函数
77
+ */
78
+ addListener(wsCallback: Function): Promise<void>;
79
+ }
80
+ export default ChatRoom;