-
Notifications
You must be signed in to change notification settings - Fork 0
/
contribution.ts
438 lines (389 loc) · 13.8 KB
/
contribution.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/**
* User Generated Content (UGC) Contributions Management System. Can be used for
* a variety of use-cases such as commenting system (e.g. blog comments), forum
* messages, chats, etc.
*
* Try and keep this file runtime-dependencies free (meaning no Deno, NodeJS, or
* Bun-specific functionality). Put Deno, NodeJS, and Bun deps into separate
* files.
*/
import { z } from "zod";
export const partyIDSchema = z.string();
export type PartyID = z.infer<typeof partyIDSchema>;
export const contributionSessionIDSchema = z.string();
export type ContributionSessionID = z.infer<typeof contributionSessionIDSchema>;
export const contributionIDSchema = z.string();
export type ContributionID = z.infer<typeof contributionIDSchema>;
export const contributionTargetIDSchema = z.string();
export type ContributionTargetID = z.infer<typeof contributionTargetIDSchema>;
export const partySchema = z.object({
partyID: partyIDSchema,
});
export type Party = z.infer<typeof partySchema>;
export const personSchema = partySchema.extend({
firstName: z.string().optional(),
lastName: z.string().optional(),
email: z.string().optional(),
profilePicture: z.string().optional(),
});
export type Person = z.infer<typeof personSchema>;
export const organizationSchema = partySchema.extend({
name: z.string(),
});
export type Organization = z.infer<typeof organizationSchema>;
export const userAgentSchema = z.object({
ipAddress: z.string(),
host: z.string().optional(),
});
export type UserAgent = z.infer<typeof userAgentSchema>;
export const contributionSessionAuthSchema = z.union([
z.object({
type: z.literal("apiTokenAuth"),
apiToken: z.string(),
}),
z.object({
type: z.literal("oAuthDelegatedAuth"),
oAuthSessionId: z.string(),
}),
z.object({
type: z.literal("emailAuth"),
email: z.string(),
}),
]);
export type ContributionSessionAuth = z.infer<
typeof contributionSessionAuthSchema
>;
export const contributionSessionSchema = z.object({
id: contributionSessionIDSchema,
auth: contributionSessionAuthSchema,
userAgent: userAgentSchema,
person: personSchema.optional(),
organization: organizationSchema.optional(),
});
export type ContributionSession = z.infer<typeof contributionSessionSchema>;
export const contributionSchema = z.object({
// TODO: add discriminated union to support all kinds of UGC like blog post
// comments, audio/video, chat messages, and other Disqus- and discord-like
// capabilities (also design Slack, Discord, Teams, etc. integrations so
// that we can use this library as a sink/source for sync'ing with Discord);
// also figure out how to store lineage data (e.g. manual entry, sync from
// Discord, etc.);
// determine how we can store LinkedIn shares sync, Tweets sync, etc.
id: contributionIDSchema,
content: z.string(),
session: contributionSessionSchema,
parentId: contributionIDSchema.optional(),
created_at: z.date(),
});
export type Contribution = z.infer<typeof contributionSchema>;
export const contributionReactionSchema = z.object({
reactionId: contributionIDSchema,
contributionId: contributionIDSchema,
reaction: z.string(),
session: contributionSessionSchema,
created_at: z.date(),
});
export type ContributionReaction = z.infer<typeof contributionReactionSchema>;
export type ContributionReactions = ContributionReaction[];
export type HierarchicalContribution = Contribution & {
subContributions: ContributionThread;
reactions?: ContributionReaction[];
};
export type ContributionThread = HierarchicalContribution[];
export const contributionTargetSchema = z.object({
// TODO: create a discriminated union that would allow different
// target types like URL for blog post comments, URL for
// Wiki comments, channel name for chats, Discord sync'ing,
// etc. and need to figure out how to mimic hierarchical
// channels or channels that belong to categories, etc.
// (could contribution target just be hierarchical?)
targetID: contributionTargetIDSchema,
});
export type ContributionTarget = z.infer<typeof contributionTargetSchema>;
export const contributionTargetRelationshipSchema = z.object({
contribution: contributionSchema,
target: contributionTargetSchema,
});
export type ContributionTargetRelationship = z.infer<
typeof contributionTargetRelationshipSchema
>;
export interface ContributionSessionPersistenceStrategy {
readonly createSession: (
auth: ContributionSessionAuth,
userAgent: UserAgent,
) => Promise<ContributionSession>;
readonly getSession: (
sessionId: ContributionSessionID,
) => Promise<ContributionSession | null>;
readonly timeoutSession: (
session: ContributionSession,
) => Promise<ContributionSession>;
}
export interface ContributionPersistenceStrategy {
readonly saveContribution: (
contribution: Contribution,
) => Promise<Contribution>;
readonly saveReaction: (
...reaction: ContributionReaction[]
) => Promise<ContributionReaction[]>;
readonly getContribution: (
id: ContributionID,
options?: { includeReactions?: boolean },
) => Promise<
Contribution | (Contribution & { reactions: ContributionReactions }) | null
>;
readonly getThread: (
rootContributionId: ContributionID,
options?: { includeReactions?: boolean },
) => Promise<HierarchicalContribution | null>;
// TODO: soft delete and hard delete contributions
// TODO: delete all reactions for a contribution
// TODO: toggle reactions for a contribution (e.g. like/unlike)
}
export interface ContributionTargetRelationshipPersistenceStrategy {
readonly saveRelationship: (
relationship: ContributionTargetRelationship,
) => Promise<ContributionTargetRelationship>;
readonly getRelationshipsByTargetID: (
targetID: ContributionTargetID,
) => Promise<ContributionTargetRelationship[]>;
readonly getRelationshipsByContributionID: (
contributionID: ContributionID,
) => Promise<ContributionTargetRelationship[]>;
// TODO: soft delete and hard delete relationships
}
export interface ContributionInteractionStrategy {
readonly postContribution: (
contribution: Contribution | ContributionTargetRelationship,
) => Promise<Contribution | ContributionTargetRelationship>;
readonly postReaction: (
...reaction: ContributionReaction[]
) => Promise<ContributionReaction[]>;
readonly getContribution: (
id: ContributionID,
options?: { includeReactions?: boolean },
) => Promise<
Contribution | (Contribution & { reactions: ContributionReactions }) | null
>;
readonly getThread: (
rootContributionId: ContributionID,
options?: { includeReactions?: boolean },
) => Promise<HierarchicalContribution | null>;
// TODO: soft delete and hard delete contributions
// TODO: delete all reactions for a contribution
// TODO: toggle reactions for a contribution (e.g. like/unlike)
// TODO: soft delete and hard delete relationships
}
export interface ContributionPresentationStrategy {
readonly renderContribution: (contribution: Contribution) => string;
readonly renderThread: (contribution: HierarchicalContribution) => string;
}
export class ContributionSessionMemoryPersistence
implements ContributionSessionPersistenceStrategy {
protected sessions: Record<string, ContributionSession> = {};
getRandomString(s: number) {
if (s % 2 == 1) {
throw new Deno.errors.InvalidData("Only even sizes are supported");
}
const buf = new Uint8Array(s / 2);
crypto.getRandomValues(buf);
let ret = "";
for (let i = 0; i < buf.length; ++i) {
ret += ("0" + buf[i].toString(16)).slice(-2);
}
return ret;
}
// deno-lint-ignore require-await
async createSession(
auth: ContributionSessionAuth,
userAgent: UserAgent,
): Promise<ContributionSession> {
return {
id: this.getRandomString(10),
auth,
userAgent,
};
}
// deno-lint-ignore require-await
async getSession(
sessionId: ContributionSessionID,
): Promise<ContributionSession | null> {
return this.sessions[sessionId];
}
// deno-lint-ignore require-await
async timeoutSession(
session: ContributionSession,
): Promise<ContributionSession> {
delete this.sessions[session.id];
return session;
}
}
export class ContributionMemoryPersistence
implements ContributionPersistenceStrategy {
protected contributions: Record<ContributionID, Contribution> = {};
protected reactions: Record<ContributionID, ContributionReaction[]> = {};
// deno-lint-ignore require-await
async saveContribution(contribution: Contribution): Promise<Contribution> {
this.contributions[contribution.id] = contribution;
return contribution;
}
// deno-lint-ignore require-await
async saveReaction(
...reaction: ContributionReaction[]
): Promise<ContributionReaction[]> {
for (const r of reaction) {
let reactions = this.reactions[r.contributionId];
if (!reactions) {
reactions = [];
this.reactions[r.contributionId] = reactions;
}
reactions.push(r);
}
return reaction;
}
// deno-lint-ignore require-await
async getContribution(
id: ContributionID,
options?: { includeReactions?: boolean },
): Promise<
Contribution | (Contribution & { reactions: ContributionReactions }) | null
> {
const contribution = this.contributions[id];
if (contribution && options?.includeReactions) {
return {
...contribution,
reactions: this.reactions[id],
};
}
return contribution || null;
}
// deno-lint-ignore require-await
async getThread(
rootContributionId: ContributionID,
options?: { includeReactions?: boolean },
): Promise<HierarchicalContribution | null> {
const recursiveFetch = (
find: ContributionID,
parent?: HierarchicalContribution,
) => {
const found = this.contributions[find];
if (found) {
const foundHC: HierarchicalContribution = {
...found,
reactions: options?.includeReactions
? this.reactions[find]
: undefined,
subContributions: [],
};
if (parent) parent.subContributions.push(foundHC);
for (
const [childId, childContrib] of Object.entries(
this.contributions,
)
) {
if (childContrib.parentId == find) {
recursiveFetch(childId, foundHC);
}
}
return foundHC;
} else {
return null;
}
};
return recursiveFetch(rootContributionId);
}
}
export class ContributionTargetRelationshipMemoryPersistence
implements ContributionTargetRelationshipPersistenceStrategy {
protected relationships: ContributionTargetRelationship[] = [];
// deno-lint-ignore require-await
async saveRelationship(
relationship: ContributionTargetRelationship,
): Promise<ContributionTargetRelationship> {
this.relationships.push(relationship);
return relationship;
}
// deno-lint-ignore require-await
async getRelationshipsByTargetID(
targetID: ContributionTargetID,
): Promise<ContributionTargetRelationship[]> {
return this.relationships.filter((rel) => rel.target.targetID === targetID);
}
// deno-lint-ignore require-await
async getRelationshipsByContributionID(
contributionID: ContributionID,
): Promise<ContributionTargetRelationship[]> {
return this.relationships.filter(
(rel) => rel.contribution.id === contributionID,
);
}
}
export class TypicalContributionInteraction
implements ContributionInteractionStrategy, ContributionPresentationStrategy {
constructor(
readonly csps: ContributionSessionPersistenceStrategy =
new ContributionSessionMemoryPersistence(),
readonly cps: ContributionPersistenceStrategy =
new ContributionMemoryPersistence(),
readonly ctrps: ContributionTargetRelationshipPersistenceStrategy =
new ContributionTargetRelationshipMemoryPersistence(),
) {}
async postContribution(
contribution: Contribution | ContributionTargetRelationship,
): Promise<Contribution | ContributionTargetRelationship> {
if ("contribution" in contribution) {
return await this.ctrps.saveRelationship(contribution);
} else {
return await this.cps.saveContribution(contribution);
}
}
async postReaction(
...reaction: ContributionReaction[]
): Promise<ContributionReaction[]> {
await this.cps.saveReaction(...reaction);
return reaction;
}
// deno-lint-ignore require-await
async getContribution(
id: ContributionID,
options?: { includeReactions?: boolean },
): Promise<
Contribution | (Contribution & { reactions: ContributionReactions }) | null
> {
return this.cps.getContribution(id, options);
}
// deno-lint-ignore require-await
async getThread(
rootContributionId: ContributionID,
options?: { includeReactions?: boolean },
): Promise<HierarchicalContribution | null> {
return this.cps.getThread(rootContributionId, options);
}
renderContribution(contribution: Contribution): string {
let renderedText = `Contribution ID: ${contribution.id}\n`;
if (contribution.content) {
renderedText += `Content: ${contribution.content}\n`;
}
return renderedText;
}
renderThread(contribution: HierarchicalContribution): string {
const renderSingle = (
contribution: HierarchicalContribution,
level = 0,
) => {
const levelText = level ? " ".repeat(level) : "";
let renderedText = `${levelText}Contribution ID: ${contribution.id}\n`;
if (contribution.content) {
renderedText += `${levelText}Content: ${contribution.content}`;
}
if (contribution.subContributions?.length) {
renderedText += `\n${levelText}${contribution.id} Subcontributions:\n`;
for (const sub of contribution.subContributions) {
renderedText += renderSingle(sub, level + 1);
}
}
return renderedText;
};
return renderSingle(contribution);
}
}