This repository has been archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatch.ts
119 lines (111 loc) · 3.03 KB
/
dispatch.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { ObjectEntries } from "./common.js";
import { Message } from "./message.js";
import { Member } from "./node.js";
import { Comment, Thought } from "./thought.js";
import { User } from "./user.js";
export enum DispatchEvent {
UserTyping = 'user_typing',
MessageCreate = 'message_create',
MessageDelete = 'message_delete',
MessageEngagement = 'message_engagement',
ThoughtCreate = 'thought_create',
ThoughtUpdate = 'thought_update',
ThoughtDelete = 'thought_delete',
ThoughtEngagement = 'thought_engagement',
CommentCreate = 'comment_create',
CommentEngagement = 'comment_engagement',
NodeMemberAdd = 'node_member_add',
NodeMemberRemove = 'node_member_remove',
NodeMemberBan = 'node_member_ban',
NodeMemberActivityChange = 'node_member_activity_change',
}
/**
* 总分发事件
*/
export interface Dispatch {
/**
* 事件类型
*/
event: DispatchEvent;
/**
* 数据
*/
data: UserTyping | Message | Thought | Comment | Engagement | Member | NodeMemberActivityChange;
}
export interface UserTyping {
node_id: string; // 据点id
topic_id: string; // 话题/节点id
user_id: string; // 用户id
user: User; // 用户信息
member: Member; // 用户在据点中的成员信息
}
/**
* 提取并赋值额外字段
* @deprecated 即将删除,请使用{@link refineMessage}代替
* @param data 消息
*/
export function refine(data: Message) {
return refineMessage(data);
}
/**
* 提取并赋值额外字段
* 大部分情况下无需手动调用此方法
* @param data 消息
*/
export function refineMessage(data: Message) {
// 没有node_id则为私聊消息
data._isDirect = !data.node_id;
if (!data.objects) return;
// 分离作者与其他用户
if (data.objects.users) {
data._otherUsers = [];
for (const key in data.objects.users) {
if (key == data.author_id)
data._author = data.objects.users[data.author_id];
else
data._otherUsers.push(data.objects.users[key]);
}
}
// 获取消息相关媒体
if (data.objects.media) {
data._media = new Array<Media>();
for (const key in data.objects.media) {
const media = data.objects.media[key];
media._id = key;
if (data.media.indexOf(key) != -1)
data._media.push(media);
}
}
}
export interface Media {
type: string;
url: string;
_id?: string;
}
/**
* 消息、想法、评论情绪变化
*/
export interface Engagement {
node_id?: string;
topic_id?: string;
thought_id?: string;
comment_id?: string;
comment_count?: string;
message_id?: string;
user_id?: string;
/**
* 情绪id
*/
stamp_id?: string;
/**
* 数量
*/
count?: number;
/**
* `1`增加 `-1`减少
*/
type?: number;
}
export type NodeMemberActivityChange = ObjectEntries<User> & {
memeber_info: Record<string, Partial<Member>>;
}