-
Notifications
You must be signed in to change notification settings - Fork 29
/
sidebar-source-message-spec.js
191 lines (168 loc) · 5.57 KB
/
sidebar-source-message-spec.js
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// @flow
import invariant from 'invariant';
import {
pushTypes,
type CreateMessageInfoParams,
type MessageSpec,
type RawMessageInfoFromServerDBRowParams,
} from './message-spec.js';
import { joinResult } from './utils.js';
import { messageTypes } from '../../types/message-types-enum.js';
import {
type RawSidebarSourceMessageInfo,
type SidebarSourceMessageData,
type SidebarSourceMessageInfo,
type ClientDBMessageInfo,
rawSidebarSourceMessageInfoValidator,
} from '../../types/message-types.js';
import type { RawUnsupportedMessageInfo } from '../../types/messages/unsupported.js';
import type { NotifTexts } from '../../types/notif-types.js';
import type { RelativeUserInfo } from '../../types/user-types.js';
import { isInvalidSidebarSource } from '../message-utils.js';
export const sidebarSourceMessageSpec: MessageSpec<
SidebarSourceMessageData,
RawSidebarSourceMessageInfo,
SidebarSourceMessageInfo,
> = Object.freeze({
messageContentForServerDB(
data: SidebarSourceMessageData | RawSidebarSourceMessageInfo,
): string {
const sourceMessageID = data.sourceMessage?.id;
invariant(sourceMessageID, 'Source message id should be set');
return JSON.stringify({
sourceMessageID,
});
},
messageContentForClientDB(data: RawSidebarSourceMessageInfo): string {
invariant(
data.sourceMessage && data.sourceMessage.id,
'sourceMessage and sourceMessage.id should be defined',
);
return JSON.stringify(data.sourceMessage);
},
rawMessageInfoFromClientDB(
clientDBMessageInfo: ClientDBMessageInfo,
): RawSidebarSourceMessageInfo {
invariant(
clientDBMessageInfo.content !== undefined &&
clientDBMessageInfo.content !== null,
'content must be defined for SidebarSource',
);
const sourceMessage = JSON.parse(clientDBMessageInfo.content);
const rawSidebarSourceMessageInfo: RawSidebarSourceMessageInfo = {
type: messageTypes.SIDEBAR_SOURCE,
id: clientDBMessageInfo.id,
threadID: clientDBMessageInfo.thread,
time: parseInt(clientDBMessageInfo.time),
creatorID: clientDBMessageInfo.user,
sourceMessage,
};
return rawSidebarSourceMessageInfo;
},
messageTitle() {
invariant(false, 'Cannot call messageTitle on sidebarSourceMessageSpec');
},
rawMessageInfoFromServerDBRow(
row: Object,
params: RawMessageInfoFromServerDBRowParams,
): RawSidebarSourceMessageInfo {
const { derivedMessages } = params;
invariant(derivedMessages, 'Derived messages should be provided');
const content = JSON.parse(row.content);
let sourceMessage = derivedMessages.get(content.sourceMessageID);
if (!sourceMessage || isInvalidSidebarSource(sourceMessage)) {
console.warn(
`Message with id ${row.id} has a derived message ` +
`${content.sourceMessageID} which is not present in the database`,
);
sourceMessage = undefined;
}
return {
type: messageTypes.SIDEBAR_SOURCE,
id: row.id.toString(),
threadID: row.threadID.toString(),
time: row.time,
creatorID: row.creatorID.toString(),
sourceMessage,
};
},
createMessageInfo(
rawMessageInfo: RawSidebarSourceMessageInfo,
creator: RelativeUserInfo,
params: CreateMessageInfoParams,
): ?SidebarSourceMessageInfo {
if (!rawMessageInfo.sourceMessage) {
return null;
}
const sourceMessage = params.createMessageInfoFromRaw(
rawMessageInfo.sourceMessage,
);
invariant(
sourceMessage && !isInvalidSidebarSource(sourceMessage),
'Sidebars can not be created from SIDEBAR SOURCE, REACTION, EDIT OR PIN MESSAGE',
);
return {
type: messageTypes.SIDEBAR_SOURCE,
id: rawMessageInfo.id,
threadID: rawMessageInfo.threadID,
creator,
time: rawMessageInfo.time,
sourceMessage,
};
},
rawMessageInfoFromMessageData(
messageData: SidebarSourceMessageData,
id: ?string,
): RawSidebarSourceMessageInfo {
invariant(id, 'RawSidebarSourceMessageInfo needs id');
return { ...messageData, id };
},
shimUnsupportedMessageInfo(
rawMessageInfo: RawSidebarSourceMessageInfo,
): RawSidebarSourceMessageInfo | RawUnsupportedMessageInfo {
if (rawMessageInfo.sourceMessage) {
return rawMessageInfo;
}
const { id } = rawMessageInfo;
invariant(id !== null && id !== undefined, 'id should be set on server');
return {
type: messageTypes.UNSUPPORTED,
id,
threadID: rawMessageInfo.threadID,
creatorID: rawMessageInfo.creatorID,
time: rawMessageInfo.time,
robotext: 'first message in thread',
dontPrefixCreator: true,
unsupportedMessageInfo: rawMessageInfo,
};
},
unshimMessageInfo(
unwrapped: RawSidebarSourceMessageInfo,
): RawSidebarSourceMessageInfo {
return unwrapped;
},
notificationTexts(): Promise<NotifTexts> {
invariant(
false,
'SIDEBAR_SOURCE notificationTexts should never be called directly!',
);
},
notificationCollapseKey(rawMessageInfo: RawSidebarSourceMessageInfo): string {
return joinResult(messageTypes.CREATE_SIDEBAR, rawMessageInfo.threadID);
},
generatesNotifs: async () => pushTypes.NOTIF,
startsThread: true,
canBeSidebarSource: false,
canBePinned: false,
parseDerivedMessages(row: Object, requiredIDs: Set<string>): void {
try {
const content = JSON.parse(row.content);
requiredIDs.add(content.sourceMessageID);
} catch (e) {
console.error(
`Error parsing content of message with id ${row.id}: ${e.message}`,
);
}
},
validator: rawSidebarSourceMessageInfoValidator,
});