@epic-web/workshop-presence 0.0.0-semantically-released
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/README.md +22 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/presence.d.ts +478 -0
- package/dist/esm/presence.d.ts.map +1 -0
- package/dist/esm/presence.js +43 -0
- package/dist/esm/presence.js.map +1 -0
- package/dist/esm/presence.server.d.ts +61 -0
- package/dist/esm/presence.server.d.ts.map +1 -0
- package/dist/esm/presence.server.js +105 -0
- package/dist/esm/presence.server.js.map +1 -0
- package/dist/esm/server.d.ts +53 -0
- package/dist/esm/server.d.ts.map +1 -0
- package/dist/esm/server.js +211 -0
- package/dist/esm/server.js.map +1 -0
- package/package.json +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
## 🎈 epic-web-presence
|
|
2
|
+
|
|
3
|
+
Welcome to the party, pal!
|
|
4
|
+
|
|
5
|
+
This is a [Partykit](https://partykit.io) project, which lets you create
|
|
6
|
+
real-time collaborative applications with minimal coding effort.
|
|
7
|
+
|
|
8
|
+
[`server.ts`](./src/server.ts) is the server-side code, which is responsible for
|
|
9
|
+
handling WebSocket events and HTTP requests.
|
|
10
|
+
[`presence.tsx`](../workshop-app/app/utils/presence.tsx) is the client-side
|
|
11
|
+
code, which connects to the server and listens for events.
|
|
12
|
+
|
|
13
|
+
You can start developing by running `npm run dev` and opening
|
|
14
|
+
[http://localhost:1999](http://localhost:1999) in your browser. When you're
|
|
15
|
+
ready, you can deploy your application on to the PartyKit cloud with
|
|
16
|
+
`npm run deploy`.
|
|
17
|
+
|
|
18
|
+
Refer to our docs for more information:
|
|
19
|
+
https://github.com/partykit/partykit/blob/main/README.md. For more help, reach
|
|
20
|
+
out to us on [Discord](https://discord.gg/g5uqHQJc3z),
|
|
21
|
+
[GitHub](https://github.com/partykit/partykit), or
|
|
22
|
+
[Twitter](https://twitter.com/partykit_io).
|
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export declare const partykitRoom = "epic-web-presence";
|
|
3
|
+
export declare const partykitBaseUrl = "https://epic-web-presence.kentcdodds.partykit.dev/parties/main/epic-web-presence";
|
|
4
|
+
export declare const UserSchema: z.ZodObject<{
|
|
5
|
+
id: z.ZodString;
|
|
6
|
+
hasAccess: z.ZodOptional<z.ZodNullable<z.ZodBoolean>>;
|
|
7
|
+
avatarUrl: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
8
|
+
imageUrlSmall: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
9
|
+
imageUrlLarge: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
10
|
+
name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
11
|
+
location: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
12
|
+
workshopTitle: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
13
|
+
origin: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
14
|
+
exercise: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
15
|
+
type: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodLiteral<"problem">, z.ZodLiteral<"solution">]>>>;
|
|
16
|
+
exerciseNumber: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
17
|
+
stepNumber: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
18
|
+
}, "strip", z.ZodTypeAny, {
|
|
19
|
+
type?: "problem" | "solution" | null | undefined;
|
|
20
|
+
exerciseNumber?: number | null | undefined;
|
|
21
|
+
stepNumber?: number | null | undefined;
|
|
22
|
+
}, {
|
|
23
|
+
type?: "problem" | "solution" | null | undefined;
|
|
24
|
+
exerciseNumber?: number | null | undefined;
|
|
25
|
+
stepNumber?: number | null | undefined;
|
|
26
|
+
}>>>;
|
|
27
|
+
}, "strip", z.ZodTypeAny, {
|
|
28
|
+
workshopTitle?: string | null | undefined;
|
|
29
|
+
origin?: string | null | undefined;
|
|
30
|
+
exercise?: {
|
|
31
|
+
type?: "problem" | "solution" | null | undefined;
|
|
32
|
+
exerciseNumber?: number | null | undefined;
|
|
33
|
+
stepNumber?: number | null | undefined;
|
|
34
|
+
} | null | undefined;
|
|
35
|
+
}, {
|
|
36
|
+
workshopTitle?: string | null | undefined;
|
|
37
|
+
origin?: string | null | undefined;
|
|
38
|
+
exercise?: {
|
|
39
|
+
type?: "problem" | "solution" | null | undefined;
|
|
40
|
+
exerciseNumber?: number | null | undefined;
|
|
41
|
+
stepNumber?: number | null | undefined;
|
|
42
|
+
} | null | undefined;
|
|
43
|
+
}>>>;
|
|
44
|
+
}, "strip", z.ZodTypeAny, {
|
|
45
|
+
id: string;
|
|
46
|
+
hasAccess?: boolean | null | undefined;
|
|
47
|
+
avatarUrl?: string | null | undefined;
|
|
48
|
+
imageUrlSmall?: string | null | undefined;
|
|
49
|
+
imageUrlLarge?: string | null | undefined;
|
|
50
|
+
name?: string | null | undefined;
|
|
51
|
+
location?: {
|
|
52
|
+
workshopTitle?: string | null | undefined;
|
|
53
|
+
origin?: string | null | undefined;
|
|
54
|
+
exercise?: {
|
|
55
|
+
type?: "problem" | "solution" | null | undefined;
|
|
56
|
+
exerciseNumber?: number | null | undefined;
|
|
57
|
+
stepNumber?: number | null | undefined;
|
|
58
|
+
} | null | undefined;
|
|
59
|
+
} | null | undefined;
|
|
60
|
+
}, {
|
|
61
|
+
id: string;
|
|
62
|
+
hasAccess?: boolean | null | undefined;
|
|
63
|
+
avatarUrl?: string | null | undefined;
|
|
64
|
+
imageUrlSmall?: string | null | undefined;
|
|
65
|
+
imageUrlLarge?: string | null | undefined;
|
|
66
|
+
name?: string | null | undefined;
|
|
67
|
+
location?: {
|
|
68
|
+
workshopTitle?: string | null | undefined;
|
|
69
|
+
origin?: string | null | undefined;
|
|
70
|
+
exercise?: {
|
|
71
|
+
type?: "problem" | "solution" | null | undefined;
|
|
72
|
+
exerciseNumber?: number | null | undefined;
|
|
73
|
+
stepNumber?: number | null | undefined;
|
|
74
|
+
} | null | undefined;
|
|
75
|
+
} | null | undefined;
|
|
76
|
+
}>;
|
|
77
|
+
export declare const MessageSchema: z.ZodUnion<[z.ZodUnion<[z.ZodObject<{
|
|
78
|
+
type: z.ZodLiteral<"remove-user">;
|
|
79
|
+
payload: z.ZodObject<{
|
|
80
|
+
id: z.ZodString;
|
|
81
|
+
}, "strip", z.ZodTypeAny, {
|
|
82
|
+
id: string;
|
|
83
|
+
}, {
|
|
84
|
+
id: string;
|
|
85
|
+
}>;
|
|
86
|
+
}, "strip", z.ZodTypeAny, {
|
|
87
|
+
type: "remove-user";
|
|
88
|
+
payload: {
|
|
89
|
+
id: string;
|
|
90
|
+
};
|
|
91
|
+
}, {
|
|
92
|
+
type: "remove-user";
|
|
93
|
+
payload: {
|
|
94
|
+
id: string;
|
|
95
|
+
};
|
|
96
|
+
}>, z.ZodObject<{
|
|
97
|
+
type: z.ZodLiteral<"add-user">;
|
|
98
|
+
payload: z.ZodObject<{
|
|
99
|
+
id: z.ZodString;
|
|
100
|
+
hasAccess: z.ZodOptional<z.ZodNullable<z.ZodBoolean>>;
|
|
101
|
+
avatarUrl: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
102
|
+
imageUrlSmall: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
103
|
+
imageUrlLarge: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
104
|
+
name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
105
|
+
location: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
106
|
+
workshopTitle: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
107
|
+
origin: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
108
|
+
exercise: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
109
|
+
type: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodLiteral<"problem">, z.ZodLiteral<"solution">]>>>;
|
|
110
|
+
exerciseNumber: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
111
|
+
stepNumber: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
112
|
+
}, "strip", z.ZodTypeAny, {
|
|
113
|
+
type?: "problem" | "solution" | null | undefined;
|
|
114
|
+
exerciseNumber?: number | null | undefined;
|
|
115
|
+
stepNumber?: number | null | undefined;
|
|
116
|
+
}, {
|
|
117
|
+
type?: "problem" | "solution" | null | undefined;
|
|
118
|
+
exerciseNumber?: number | null | undefined;
|
|
119
|
+
stepNumber?: number | null | undefined;
|
|
120
|
+
}>>>;
|
|
121
|
+
}, "strip", z.ZodTypeAny, {
|
|
122
|
+
workshopTitle?: string | null | undefined;
|
|
123
|
+
origin?: string | null | undefined;
|
|
124
|
+
exercise?: {
|
|
125
|
+
type?: "problem" | "solution" | null | undefined;
|
|
126
|
+
exerciseNumber?: number | null | undefined;
|
|
127
|
+
stepNumber?: number | null | undefined;
|
|
128
|
+
} | null | undefined;
|
|
129
|
+
}, {
|
|
130
|
+
workshopTitle?: string | null | undefined;
|
|
131
|
+
origin?: string | null | undefined;
|
|
132
|
+
exercise?: {
|
|
133
|
+
type?: "problem" | "solution" | null | undefined;
|
|
134
|
+
exerciseNumber?: number | null | undefined;
|
|
135
|
+
stepNumber?: number | null | undefined;
|
|
136
|
+
} | null | undefined;
|
|
137
|
+
}>>>;
|
|
138
|
+
}, "strip", z.ZodTypeAny, {
|
|
139
|
+
id: string;
|
|
140
|
+
hasAccess?: boolean | null | undefined;
|
|
141
|
+
avatarUrl?: string | null | undefined;
|
|
142
|
+
imageUrlSmall?: string | null | undefined;
|
|
143
|
+
imageUrlLarge?: string | null | undefined;
|
|
144
|
+
name?: string | null | undefined;
|
|
145
|
+
location?: {
|
|
146
|
+
workshopTitle?: string | null | undefined;
|
|
147
|
+
origin?: string | null | undefined;
|
|
148
|
+
exercise?: {
|
|
149
|
+
type?: "problem" | "solution" | null | undefined;
|
|
150
|
+
exerciseNumber?: number | null | undefined;
|
|
151
|
+
stepNumber?: number | null | undefined;
|
|
152
|
+
} | null | undefined;
|
|
153
|
+
} | null | undefined;
|
|
154
|
+
}, {
|
|
155
|
+
id: string;
|
|
156
|
+
hasAccess?: boolean | null | undefined;
|
|
157
|
+
avatarUrl?: string | null | undefined;
|
|
158
|
+
imageUrlSmall?: string | null | undefined;
|
|
159
|
+
imageUrlLarge?: string | null | undefined;
|
|
160
|
+
name?: string | null | undefined;
|
|
161
|
+
location?: {
|
|
162
|
+
workshopTitle?: string | null | undefined;
|
|
163
|
+
origin?: string | null | undefined;
|
|
164
|
+
exercise?: {
|
|
165
|
+
type?: "problem" | "solution" | null | undefined;
|
|
166
|
+
exerciseNumber?: number | null | undefined;
|
|
167
|
+
stepNumber?: number | null | undefined;
|
|
168
|
+
} | null | undefined;
|
|
169
|
+
} | null | undefined;
|
|
170
|
+
}>;
|
|
171
|
+
}, "strip", z.ZodTypeAny, {
|
|
172
|
+
type: "add-user";
|
|
173
|
+
payload: {
|
|
174
|
+
id: string;
|
|
175
|
+
hasAccess?: boolean | null | undefined;
|
|
176
|
+
avatarUrl?: string | null | undefined;
|
|
177
|
+
imageUrlSmall?: string | null | undefined;
|
|
178
|
+
imageUrlLarge?: string | null | undefined;
|
|
179
|
+
name?: string | null | undefined;
|
|
180
|
+
location?: {
|
|
181
|
+
workshopTitle?: string | null | undefined;
|
|
182
|
+
origin?: string | null | undefined;
|
|
183
|
+
exercise?: {
|
|
184
|
+
type?: "problem" | "solution" | null | undefined;
|
|
185
|
+
exerciseNumber?: number | null | undefined;
|
|
186
|
+
stepNumber?: number | null | undefined;
|
|
187
|
+
} | null | undefined;
|
|
188
|
+
} | null | undefined;
|
|
189
|
+
};
|
|
190
|
+
}, {
|
|
191
|
+
type: "add-user";
|
|
192
|
+
payload: {
|
|
193
|
+
id: string;
|
|
194
|
+
hasAccess?: boolean | null | undefined;
|
|
195
|
+
avatarUrl?: string | null | undefined;
|
|
196
|
+
imageUrlSmall?: string | null | undefined;
|
|
197
|
+
imageUrlLarge?: string | null | undefined;
|
|
198
|
+
name?: string | null | undefined;
|
|
199
|
+
location?: {
|
|
200
|
+
workshopTitle?: string | null | undefined;
|
|
201
|
+
origin?: string | null | undefined;
|
|
202
|
+
exercise?: {
|
|
203
|
+
type?: "problem" | "solution" | null | undefined;
|
|
204
|
+
exerciseNumber?: number | null | undefined;
|
|
205
|
+
stepNumber?: number | null | undefined;
|
|
206
|
+
} | null | undefined;
|
|
207
|
+
} | null | undefined;
|
|
208
|
+
};
|
|
209
|
+
}>]>, z.ZodObject<{
|
|
210
|
+
type: z.ZodLiteral<"presence">;
|
|
211
|
+
payload: z.ZodObject<{
|
|
212
|
+
users: z.ZodArray<z.ZodObject<{
|
|
213
|
+
id: z.ZodString;
|
|
214
|
+
hasAccess: z.ZodOptional<z.ZodNullable<z.ZodBoolean>>;
|
|
215
|
+
avatarUrl: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
216
|
+
imageUrlSmall: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
217
|
+
imageUrlLarge: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
218
|
+
name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
219
|
+
location: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
220
|
+
workshopTitle: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
221
|
+
origin: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
222
|
+
exercise: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
223
|
+
type: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodLiteral<"problem">, z.ZodLiteral<"solution">]>>>;
|
|
224
|
+
exerciseNumber: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
225
|
+
stepNumber: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
226
|
+
}, "strip", z.ZodTypeAny, {
|
|
227
|
+
type?: "problem" | "solution" | null | undefined;
|
|
228
|
+
exerciseNumber?: number | null | undefined;
|
|
229
|
+
stepNumber?: number | null | undefined;
|
|
230
|
+
}, {
|
|
231
|
+
type?: "problem" | "solution" | null | undefined;
|
|
232
|
+
exerciseNumber?: number | null | undefined;
|
|
233
|
+
stepNumber?: number | null | undefined;
|
|
234
|
+
}>>>;
|
|
235
|
+
}, "strip", z.ZodTypeAny, {
|
|
236
|
+
workshopTitle?: string | null | undefined;
|
|
237
|
+
origin?: string | null | undefined;
|
|
238
|
+
exercise?: {
|
|
239
|
+
type?: "problem" | "solution" | null | undefined;
|
|
240
|
+
exerciseNumber?: number | null | undefined;
|
|
241
|
+
stepNumber?: number | null | undefined;
|
|
242
|
+
} | null | undefined;
|
|
243
|
+
}, {
|
|
244
|
+
workshopTitle?: string | null | undefined;
|
|
245
|
+
origin?: string | null | undefined;
|
|
246
|
+
exercise?: {
|
|
247
|
+
type?: "problem" | "solution" | null | undefined;
|
|
248
|
+
exerciseNumber?: number | null | undefined;
|
|
249
|
+
stepNumber?: number | null | undefined;
|
|
250
|
+
} | null | undefined;
|
|
251
|
+
}>>>;
|
|
252
|
+
}, "strip", z.ZodTypeAny, {
|
|
253
|
+
id: string;
|
|
254
|
+
hasAccess?: boolean | null | undefined;
|
|
255
|
+
avatarUrl?: string | null | undefined;
|
|
256
|
+
imageUrlSmall?: string | null | undefined;
|
|
257
|
+
imageUrlLarge?: string | null | undefined;
|
|
258
|
+
name?: string | null | undefined;
|
|
259
|
+
location?: {
|
|
260
|
+
workshopTitle?: string | null | undefined;
|
|
261
|
+
origin?: string | null | undefined;
|
|
262
|
+
exercise?: {
|
|
263
|
+
type?: "problem" | "solution" | null | undefined;
|
|
264
|
+
exerciseNumber?: number | null | undefined;
|
|
265
|
+
stepNumber?: number | null | undefined;
|
|
266
|
+
} | null | undefined;
|
|
267
|
+
} | null | undefined;
|
|
268
|
+
}, {
|
|
269
|
+
id: string;
|
|
270
|
+
hasAccess?: boolean | null | undefined;
|
|
271
|
+
avatarUrl?: string | null | undefined;
|
|
272
|
+
imageUrlSmall?: string | null | undefined;
|
|
273
|
+
imageUrlLarge?: string | null | undefined;
|
|
274
|
+
name?: string | null | undefined;
|
|
275
|
+
location?: {
|
|
276
|
+
workshopTitle?: string | null | undefined;
|
|
277
|
+
origin?: string | null | undefined;
|
|
278
|
+
exercise?: {
|
|
279
|
+
type?: "problem" | "solution" | null | undefined;
|
|
280
|
+
exerciseNumber?: number | null | undefined;
|
|
281
|
+
stepNumber?: number | null | undefined;
|
|
282
|
+
} | null | undefined;
|
|
283
|
+
} | null | undefined;
|
|
284
|
+
}>, "many">;
|
|
285
|
+
}, "strip", z.ZodTypeAny, {
|
|
286
|
+
users: {
|
|
287
|
+
id: string;
|
|
288
|
+
hasAccess?: boolean | null | undefined;
|
|
289
|
+
avatarUrl?: string | null | undefined;
|
|
290
|
+
imageUrlSmall?: string | null | undefined;
|
|
291
|
+
imageUrlLarge?: string | null | undefined;
|
|
292
|
+
name?: string | null | undefined;
|
|
293
|
+
location?: {
|
|
294
|
+
workshopTitle?: string | null | undefined;
|
|
295
|
+
origin?: string | null | undefined;
|
|
296
|
+
exercise?: {
|
|
297
|
+
type?: "problem" | "solution" | null | undefined;
|
|
298
|
+
exerciseNumber?: number | null | undefined;
|
|
299
|
+
stepNumber?: number | null | undefined;
|
|
300
|
+
} | null | undefined;
|
|
301
|
+
} | null | undefined;
|
|
302
|
+
}[];
|
|
303
|
+
}, {
|
|
304
|
+
users: {
|
|
305
|
+
id: string;
|
|
306
|
+
hasAccess?: boolean | null | undefined;
|
|
307
|
+
avatarUrl?: string | null | undefined;
|
|
308
|
+
imageUrlSmall?: string | null | undefined;
|
|
309
|
+
imageUrlLarge?: string | null | undefined;
|
|
310
|
+
name?: string | null | undefined;
|
|
311
|
+
location?: {
|
|
312
|
+
workshopTitle?: string | null | undefined;
|
|
313
|
+
origin?: string | null | undefined;
|
|
314
|
+
exercise?: {
|
|
315
|
+
type?: "problem" | "solution" | null | undefined;
|
|
316
|
+
exerciseNumber?: number | null | undefined;
|
|
317
|
+
stepNumber?: number | null | undefined;
|
|
318
|
+
} | null | undefined;
|
|
319
|
+
} | null | undefined;
|
|
320
|
+
}[];
|
|
321
|
+
}>;
|
|
322
|
+
}, "strip", z.ZodTypeAny, {
|
|
323
|
+
type: "presence";
|
|
324
|
+
payload: {
|
|
325
|
+
users: {
|
|
326
|
+
id: string;
|
|
327
|
+
hasAccess?: boolean | null | undefined;
|
|
328
|
+
avatarUrl?: string | null | undefined;
|
|
329
|
+
imageUrlSmall?: string | null | undefined;
|
|
330
|
+
imageUrlLarge?: string | null | undefined;
|
|
331
|
+
name?: string | null | undefined;
|
|
332
|
+
location?: {
|
|
333
|
+
workshopTitle?: string | null | undefined;
|
|
334
|
+
origin?: string | null | undefined;
|
|
335
|
+
exercise?: {
|
|
336
|
+
type?: "problem" | "solution" | null | undefined;
|
|
337
|
+
exerciseNumber?: number | null | undefined;
|
|
338
|
+
stepNumber?: number | null | undefined;
|
|
339
|
+
} | null | undefined;
|
|
340
|
+
} | null | undefined;
|
|
341
|
+
}[];
|
|
342
|
+
};
|
|
343
|
+
}, {
|
|
344
|
+
type: "presence";
|
|
345
|
+
payload: {
|
|
346
|
+
users: {
|
|
347
|
+
id: string;
|
|
348
|
+
hasAccess?: boolean | null | undefined;
|
|
349
|
+
avatarUrl?: string | null | undefined;
|
|
350
|
+
imageUrlSmall?: string | null | undefined;
|
|
351
|
+
imageUrlLarge?: string | null | undefined;
|
|
352
|
+
name?: string | null | undefined;
|
|
353
|
+
location?: {
|
|
354
|
+
workshopTitle?: string | null | undefined;
|
|
355
|
+
origin?: string | null | undefined;
|
|
356
|
+
exercise?: {
|
|
357
|
+
type?: "problem" | "solution" | null | undefined;
|
|
358
|
+
exerciseNumber?: number | null | undefined;
|
|
359
|
+
stepNumber?: number | null | undefined;
|
|
360
|
+
} | null | undefined;
|
|
361
|
+
} | null | undefined;
|
|
362
|
+
}[];
|
|
363
|
+
};
|
|
364
|
+
}>]>;
|
|
365
|
+
export type Message = z.infer<typeof MessageSchema>;
|
|
366
|
+
export type User = z.infer<typeof UserSchema>;
|
|
367
|
+
export declare const PresenceSchema: z.ZodObject<{
|
|
368
|
+
users: z.ZodArray<z.ZodObject<{
|
|
369
|
+
id: z.ZodString;
|
|
370
|
+
hasAccess: z.ZodOptional<z.ZodNullable<z.ZodBoolean>>;
|
|
371
|
+
avatarUrl: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
372
|
+
imageUrlSmall: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
373
|
+
imageUrlLarge: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
374
|
+
name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
375
|
+
location: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
376
|
+
workshopTitle: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
377
|
+
origin: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
378
|
+
exercise: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
379
|
+
type: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodLiteral<"problem">, z.ZodLiteral<"solution">]>>>;
|
|
380
|
+
exerciseNumber: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
381
|
+
stepNumber: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
382
|
+
}, "strip", z.ZodTypeAny, {
|
|
383
|
+
type?: "problem" | "solution" | null | undefined;
|
|
384
|
+
exerciseNumber?: number | null | undefined;
|
|
385
|
+
stepNumber?: number | null | undefined;
|
|
386
|
+
}, {
|
|
387
|
+
type?: "problem" | "solution" | null | undefined;
|
|
388
|
+
exerciseNumber?: number | null | undefined;
|
|
389
|
+
stepNumber?: number | null | undefined;
|
|
390
|
+
}>>>;
|
|
391
|
+
}, "strip", z.ZodTypeAny, {
|
|
392
|
+
workshopTitle?: string | null | undefined;
|
|
393
|
+
origin?: string | null | undefined;
|
|
394
|
+
exercise?: {
|
|
395
|
+
type?: "problem" | "solution" | null | undefined;
|
|
396
|
+
exerciseNumber?: number | null | undefined;
|
|
397
|
+
stepNumber?: number | null | undefined;
|
|
398
|
+
} | null | undefined;
|
|
399
|
+
}, {
|
|
400
|
+
workshopTitle?: string | null | undefined;
|
|
401
|
+
origin?: string | null | undefined;
|
|
402
|
+
exercise?: {
|
|
403
|
+
type?: "problem" | "solution" | null | undefined;
|
|
404
|
+
exerciseNumber?: number | null | undefined;
|
|
405
|
+
stepNumber?: number | null | undefined;
|
|
406
|
+
} | null | undefined;
|
|
407
|
+
}>>>;
|
|
408
|
+
}, "strip", z.ZodTypeAny, {
|
|
409
|
+
id: string;
|
|
410
|
+
hasAccess?: boolean | null | undefined;
|
|
411
|
+
avatarUrl?: string | null | undefined;
|
|
412
|
+
imageUrlSmall?: string | null | undefined;
|
|
413
|
+
imageUrlLarge?: string | null | undefined;
|
|
414
|
+
name?: string | null | undefined;
|
|
415
|
+
location?: {
|
|
416
|
+
workshopTitle?: string | null | undefined;
|
|
417
|
+
origin?: string | null | undefined;
|
|
418
|
+
exercise?: {
|
|
419
|
+
type?: "problem" | "solution" | null | undefined;
|
|
420
|
+
exerciseNumber?: number | null | undefined;
|
|
421
|
+
stepNumber?: number | null | undefined;
|
|
422
|
+
} | null | undefined;
|
|
423
|
+
} | null | undefined;
|
|
424
|
+
}, {
|
|
425
|
+
id: string;
|
|
426
|
+
hasAccess?: boolean | null | undefined;
|
|
427
|
+
avatarUrl?: string | null | undefined;
|
|
428
|
+
imageUrlSmall?: string | null | undefined;
|
|
429
|
+
imageUrlLarge?: string | null | undefined;
|
|
430
|
+
name?: string | null | undefined;
|
|
431
|
+
location?: {
|
|
432
|
+
workshopTitle?: string | null | undefined;
|
|
433
|
+
origin?: string | null | undefined;
|
|
434
|
+
exercise?: {
|
|
435
|
+
type?: "problem" | "solution" | null | undefined;
|
|
436
|
+
exerciseNumber?: number | null | undefined;
|
|
437
|
+
stepNumber?: number | null | undefined;
|
|
438
|
+
} | null | undefined;
|
|
439
|
+
} | null | undefined;
|
|
440
|
+
}>, "many">;
|
|
441
|
+
}, "strip", z.ZodTypeAny, {
|
|
442
|
+
users: {
|
|
443
|
+
id: string;
|
|
444
|
+
hasAccess?: boolean | null | undefined;
|
|
445
|
+
avatarUrl?: string | null | undefined;
|
|
446
|
+
imageUrlSmall?: string | null | undefined;
|
|
447
|
+
imageUrlLarge?: string | null | undefined;
|
|
448
|
+
name?: string | null | undefined;
|
|
449
|
+
location?: {
|
|
450
|
+
workshopTitle?: string | null | undefined;
|
|
451
|
+
origin?: string | null | undefined;
|
|
452
|
+
exercise?: {
|
|
453
|
+
type?: "problem" | "solution" | null | undefined;
|
|
454
|
+
exerciseNumber?: number | null | undefined;
|
|
455
|
+
stepNumber?: number | null | undefined;
|
|
456
|
+
} | null | undefined;
|
|
457
|
+
} | null | undefined;
|
|
458
|
+
}[];
|
|
459
|
+
}, {
|
|
460
|
+
users: {
|
|
461
|
+
id: string;
|
|
462
|
+
hasAccess?: boolean | null | undefined;
|
|
463
|
+
avatarUrl?: string | null | undefined;
|
|
464
|
+
imageUrlSmall?: string | null | undefined;
|
|
465
|
+
imageUrlLarge?: string | null | undefined;
|
|
466
|
+
name?: string | null | undefined;
|
|
467
|
+
location?: {
|
|
468
|
+
workshopTitle?: string | null | undefined;
|
|
469
|
+
origin?: string | null | undefined;
|
|
470
|
+
exercise?: {
|
|
471
|
+
type?: "problem" | "solution" | null | undefined;
|
|
472
|
+
exerciseNumber?: number | null | undefined;
|
|
473
|
+
stepNumber?: number | null | undefined;
|
|
474
|
+
} | null | undefined;
|
|
475
|
+
} | null | undefined;
|
|
476
|
+
}[];
|
|
477
|
+
}>;
|
|
478
|
+
//# sourceMappingURL=presence.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"presence.d.ts","sourceRoot":"","sources":["../../src/presence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,eAAO,MAAM,YAAY,sBAAsB,CAAA;AAE/C,eAAO,MAAM,eAAe,qFAAmF,CAAA;AAE/G,eAAO,MAAM,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA0BrB,CAAA;AAEF,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAWxB,CAAA;AAEF,MAAM,MAAM,OAAO,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,aAAa,CAAC,CAAA;AAEnD,MAAM,MAAM,IAAI,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,UAAU,CAAC,CAAA;AAE7C,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAA2C,CAAA"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export const partykitRoom = 'epic-web-presence';
|
|
3
|
+
// export const partykitBaseUrl = `http://127.0.0.1:1999/parties/main/${partykitRoom}`
|
|
4
|
+
export const partykitBaseUrl = `https://epic-web-presence.kentcdodds.partykit.dev/parties/main/${partykitRoom}`;
|
|
5
|
+
export const UserSchema = z.object({
|
|
6
|
+
id: z.string(),
|
|
7
|
+
hasAccess: z.boolean().nullable().optional(),
|
|
8
|
+
// TODO: remove the avatarUrl field once people have updated their workshops
|
|
9
|
+
avatarUrl: z.string().nullable().optional(),
|
|
10
|
+
imageUrlSmall: z.string().nullable().optional(),
|
|
11
|
+
imageUrlLarge: z.string().nullable().optional(),
|
|
12
|
+
name: z.string().nullable().optional(),
|
|
13
|
+
location: z
|
|
14
|
+
.object({
|
|
15
|
+
workshopTitle: z.string().nullable().optional(),
|
|
16
|
+
origin: z.string().nullable().optional(),
|
|
17
|
+
exercise: z
|
|
18
|
+
.object({
|
|
19
|
+
type: z
|
|
20
|
+
.union([z.literal('problem'), z.literal('solution')])
|
|
21
|
+
.nullable()
|
|
22
|
+
.optional(),
|
|
23
|
+
exerciseNumber: z.number().nullable().optional(),
|
|
24
|
+
stepNumber: z.number().nullable().optional(),
|
|
25
|
+
})
|
|
26
|
+
.nullable()
|
|
27
|
+
.optional(),
|
|
28
|
+
})
|
|
29
|
+
.nullable()
|
|
30
|
+
.optional(),
|
|
31
|
+
});
|
|
32
|
+
export const MessageSchema = z
|
|
33
|
+
.object({
|
|
34
|
+
type: z.literal('remove-user'),
|
|
35
|
+
payload: z.object({ id: z.string() }),
|
|
36
|
+
})
|
|
37
|
+
.or(z.object({ type: z.literal('add-user'), payload: UserSchema }))
|
|
38
|
+
.or(z.object({
|
|
39
|
+
type: z.literal('presence'),
|
|
40
|
+
payload: z.object({ users: z.array(UserSchema) }),
|
|
41
|
+
}));
|
|
42
|
+
export const PresenceSchema = z.object({ users: z.array(UserSchema) });
|
|
43
|
+
//# sourceMappingURL=presence.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"presence.js","sourceRoot":"","sources":["../../src/presence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,MAAM,CAAC,MAAM,YAAY,GAAG,mBAAmB,CAAA;AAC/C,sFAAsF;AACtF,MAAM,CAAC,MAAM,eAAe,GAAG,kEAAkE,YAAY,EAAE,CAAA;AAE/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CAAC,MAAM,aAAa,GAAG,CAAC;KAC5B,MAAM,CAAC;IACP,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,aAAa,CAAC;IAC9B,OAAO,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC;CACrC,CAAC;KACD,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC,CAAC;KAClE,EAAE,CACF,CAAC,CAAC,MAAM,CAAC;IACR,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC;IAC3B,OAAO,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,EAAE,CAAC;CACjD,CAAC,CACF,CAAA;AAMF,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,EAAE,CAAC,CAAA","sourcesContent":["import { z } from 'zod'\n\nexport const partykitRoom = 'epic-web-presence'\n// export const partykitBaseUrl = `http://127.0.0.1:1999/parties/main/${partykitRoom}`\nexport const partykitBaseUrl = `https://epic-web-presence.kentcdodds.partykit.dev/parties/main/${partykitRoom}`\n\nexport const UserSchema = z.object({\n\tid: z.string(),\n\thasAccess: z.boolean().nullable().optional(),\n\t// TODO: remove the avatarUrl field once people have updated their workshops\n\tavatarUrl: z.string().nullable().optional(),\n\timageUrlSmall: z.string().nullable().optional(),\n\timageUrlLarge: z.string().nullable().optional(),\n\tname: z.string().nullable().optional(),\n\tlocation: z\n\t\t.object({\n\t\t\tworkshopTitle: z.string().nullable().optional(),\n\t\t\torigin: z.string().nullable().optional(),\n\t\t\texercise: z\n\t\t\t\t.object({\n\t\t\t\t\ttype: z\n\t\t\t\t\t\t.union([z.literal('problem'), z.literal('solution')])\n\t\t\t\t\t\t.nullable()\n\t\t\t\t\t\t.optional(),\n\t\t\t\t\texerciseNumber: z.number().nullable().optional(),\n\t\t\t\t\tstepNumber: z.number().nullable().optional(),\n\t\t\t\t})\n\t\t\t\t.nullable()\n\t\t\t\t.optional(),\n\t\t})\n\t\t.nullable()\n\t\t.optional(),\n})\n\nexport const MessageSchema = z\n\t.object({\n\t\ttype: z.literal('remove-user'),\n\t\tpayload: z.object({ id: z.string() }),\n\t})\n\t.or(z.object({ type: z.literal('add-user'), payload: UserSchema }))\n\t.or(\n\t\tz.object({\n\t\t\ttype: z.literal('presence'),\n\t\t\tpayload: z.object({ users: z.array(UserSchema) }),\n\t\t}),\n\t)\n\nexport type Message = z.infer<typeof MessageSchema>\n\nexport type User = z.infer<typeof UserSchema>\n\nexport const PresenceSchema = z.object({ users: z.array(UserSchema) })\n"]}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { type Timings } from '@epic-web/workshop-utils/timing.server';
|
|
2
|
+
import { type User } from './presence.js';
|
|
3
|
+
export declare const presenceCache: {
|
|
4
|
+
name: string;
|
|
5
|
+
set: (key: string, value: import("@epic-web/cachified").CacheEntry<{
|
|
6
|
+
id: string;
|
|
7
|
+
hasAccess?: boolean | null | undefined;
|
|
8
|
+
avatarUrl?: string | null | undefined;
|
|
9
|
+
imageUrlSmall?: string | null | undefined;
|
|
10
|
+
imageUrlLarge?: string | null | undefined;
|
|
11
|
+
name?: string | null | undefined;
|
|
12
|
+
location?: {
|
|
13
|
+
workshopTitle?: string | null | undefined;
|
|
14
|
+
origin?: string | null | undefined;
|
|
15
|
+
exercise?: {
|
|
16
|
+
type?: "problem" | "solution" | null | undefined;
|
|
17
|
+
exerciseNumber?: number | null | undefined;
|
|
18
|
+
stepNumber?: number | null | undefined;
|
|
19
|
+
} | null | undefined;
|
|
20
|
+
} | null | undefined;
|
|
21
|
+
}[]>) => import("@epic-web/cachified").CacheEntry<{
|
|
22
|
+
id: string;
|
|
23
|
+
hasAccess?: boolean | null | undefined;
|
|
24
|
+
avatarUrl?: string | null | undefined;
|
|
25
|
+
imageUrlSmall?: string | null | undefined;
|
|
26
|
+
imageUrlLarge?: string | null | undefined;
|
|
27
|
+
name?: string | null | undefined;
|
|
28
|
+
location?: {
|
|
29
|
+
workshopTitle?: string | null | undefined;
|
|
30
|
+
origin?: string | null | undefined;
|
|
31
|
+
exercise?: {
|
|
32
|
+
type?: "problem" | "solution" | null | undefined;
|
|
33
|
+
exerciseNumber?: number | null | undefined;
|
|
34
|
+
stepNumber?: number | null | undefined;
|
|
35
|
+
} | null | undefined;
|
|
36
|
+
} | null | undefined;
|
|
37
|
+
}[]>;
|
|
38
|
+
get: (key: string) => import("@epic-web/cachified").CacheEntry<{
|
|
39
|
+
id: string;
|
|
40
|
+
hasAccess?: boolean | null | undefined;
|
|
41
|
+
avatarUrl?: string | null | undefined;
|
|
42
|
+
imageUrlSmall?: string | null | undefined;
|
|
43
|
+
imageUrlLarge?: string | null | undefined;
|
|
44
|
+
name?: string | null | undefined;
|
|
45
|
+
location?: {
|
|
46
|
+
workshopTitle?: string | null | undefined;
|
|
47
|
+
origin?: string | null | undefined;
|
|
48
|
+
exercise?: {
|
|
49
|
+
type?: "problem" | "solution" | null | undefined;
|
|
50
|
+
exerciseNumber?: number | null | undefined;
|
|
51
|
+
stepNumber?: number | null | undefined;
|
|
52
|
+
} | null | undefined;
|
|
53
|
+
} | null | undefined;
|
|
54
|
+
}[]> | undefined;
|
|
55
|
+
delete: (key: string) => boolean;
|
|
56
|
+
};
|
|
57
|
+
export declare function getPresentUsers({ timings, request, }?: {
|
|
58
|
+
timings?: Timings;
|
|
59
|
+
request?: Request;
|
|
60
|
+
}): Promise<Array<User>>;
|
|
61
|
+
//# sourceMappingURL=presence.server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"presence.server.d.ts","sourceRoot":"","sources":["../../src/presence.server.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,wCAAwC,CAAA;AAErE,OAAO,EAAmC,KAAK,IAAI,EAAE,MAAM,eAAe,CAAA;AAE1E,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAAmD,CAAA;AAE7E,wBAAsB,eAAe,CAAC,EACrC,OAAO,EACP,OAAO,GACP,GAAE;IAAE,OAAO,CAAC,EAAE,OAAO,CAAC;IAAC,OAAO,CAAC,EAAE,OAAO,CAAA;CAAO,GAAG,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CA6FtE"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { cachified, makeSingletonCache, } from '@epic-web/workshop-utils/cache.server';
|
|
2
|
+
import { getWorkshopConfig } from '@epic-web/workshop-utils/config.server';
|
|
3
|
+
import { getAuthInfo, getPreferences } from '@epic-web/workshop-utils/db.server';
|
|
4
|
+
import { getUserInfo, userHasAccessToWorkshop, } from '@epic-web/workshop-utils/epic-api.server';
|
|
5
|
+
import { getUserId } from '@epic-web/workshop-utils/user.server';
|
|
6
|
+
import { PresenceSchema, partykitBaseUrl } from './presence.js';
|
|
7
|
+
export const presenceCache = makeSingletonCache('PresenceCache');
|
|
8
|
+
export async function getPresentUsers({ timings, request, } = {}) {
|
|
9
|
+
const presence = await cachified({
|
|
10
|
+
key: 'presence',
|
|
11
|
+
cache: presenceCache,
|
|
12
|
+
timings,
|
|
13
|
+
request,
|
|
14
|
+
ttl: 1000 * 2,
|
|
15
|
+
swr: 1000 * 60 * 60 * 24,
|
|
16
|
+
offlineFallbackValue: { users: [] },
|
|
17
|
+
checkValue: PresenceSchema,
|
|
18
|
+
async getFreshValue(context) {
|
|
19
|
+
try {
|
|
20
|
+
const response = await Promise.race([
|
|
21
|
+
fetch(`${partykitBaseUrl}/presence`),
|
|
22
|
+
new Promise((resolve) => setTimeout(() => {
|
|
23
|
+
resolve(new Response('Timeout', { status: 500 }));
|
|
24
|
+
}, 500)),
|
|
25
|
+
]);
|
|
26
|
+
if (response.statusText === 'Timeout') {
|
|
27
|
+
throw new Error(`Timeout fetching partykit presence`);
|
|
28
|
+
}
|
|
29
|
+
if (!response.ok) {
|
|
30
|
+
throw new Error(`Unexpected response from partykit: ${response.status} ${response.statusText}`);
|
|
31
|
+
}
|
|
32
|
+
const presence = PresenceSchema.parse(await response.json());
|
|
33
|
+
return presence;
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
// console.error(err)
|
|
37
|
+
context.metadata.ttl = 300;
|
|
38
|
+
return { users: [] };
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
const { users } = presence;
|
|
43
|
+
const authInfo = await getAuthInfo();
|
|
44
|
+
const userId = request
|
|
45
|
+
? (await getUserId({ request })).id
|
|
46
|
+
: (authInfo?.id ?? null);
|
|
47
|
+
const preferences = await getPreferences();
|
|
48
|
+
if (preferences?.presence.optOut || !userId) {
|
|
49
|
+
return uniqueUsers(users.filter((u) => u.id !== userId));
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
const user = { id: userId };
|
|
53
|
+
const config = getWorkshopConfig();
|
|
54
|
+
const url = request ? new URL(request.url) : undefined;
|
|
55
|
+
user.location = {
|
|
56
|
+
workshopTitle: config.title,
|
|
57
|
+
origin: url ? url.origin : undefined,
|
|
58
|
+
};
|
|
59
|
+
if (url) {
|
|
60
|
+
if (url.pathname.startsWith('/exercise/')) {
|
|
61
|
+
const [exerciseNumber, stepNumber, type] = url.pathname
|
|
62
|
+
.split('/')
|
|
63
|
+
.slice(2);
|
|
64
|
+
user.location.exercise = {
|
|
65
|
+
exerciseNumber: isNaN(Number(exerciseNumber))
|
|
66
|
+
? null
|
|
67
|
+
: Number(exerciseNumber),
|
|
68
|
+
stepNumber: isNaN(Number(stepNumber)) ? null : Number(stepNumber),
|
|
69
|
+
type: type === 'problem'
|
|
70
|
+
? 'problem'
|
|
71
|
+
: type === 'solution'
|
|
72
|
+
? 'solution'
|
|
73
|
+
: null,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (authInfo) {
|
|
78
|
+
const [userInfo, hasAccess] = await Promise.all([
|
|
79
|
+
getUserInfo({ request, timings }),
|
|
80
|
+
userHasAccessToWorkshop({ request, timings }),
|
|
81
|
+
]);
|
|
82
|
+
Object.assign(user, {
|
|
83
|
+
name: userInfo?.name,
|
|
84
|
+
avatarUrl: userInfo?.imageUrlLarge,
|
|
85
|
+
imageUrlSmall: userInfo?.imageUrlSmall,
|
|
86
|
+
imageUrlLarge: userInfo?.imageUrlLarge,
|
|
87
|
+
hasAccess,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return uniqueUsers([...users, user]);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
// A user maybe on the same page in multiple tabs
|
|
94
|
+
// so let's make sure we only show them once
|
|
95
|
+
function uniqueUsers(users) {
|
|
96
|
+
const seen = new Set();
|
|
97
|
+
return users.filter(Boolean).filter((user) => {
|
|
98
|
+
if (seen.has(user.id)) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
seen.add(user.id);
|
|
102
|
+
return true;
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=presence.server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"presence.server.js","sourceRoot":"","sources":["../../src/presence.server.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,SAAS,EACT,kBAAkB,GAClB,MAAM,uCAAuC,CAAA;AAC9C,OAAO,EAAE,iBAAiB,EAAE,MAAM,wCAAwC,CAAA;AAC1E,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAA;AAChF,OAAO,EACN,WAAW,EACX,uBAAuB,GACvB,MAAM,0CAA0C,CAAA;AAEjD,OAAO,EAAE,SAAS,EAAE,MAAM,sCAAsC,CAAA;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CAAA;IACrC,CAAC;AACF,CAAC;AAED,iDAAiD;AACjD,4CAA4C;AAC5C,SAAS,WAAW,CAAC,KAAkB;IACtC,MAAM,IAAI,GAAG,IAAI,GAAG,EAAE,CAAA;IACtB,OAAO,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE;QAC5C,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;YACvB,OAAO,KAAK,CAAA;QACb,CAAC;QACD,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;QACjB,OAAO,IAAI,CAAA;IACZ,CAAC,CAAC,CAAA;AACH,CAAC","sourcesContent":["import {\n\tcachified,\n\tmakeSingletonCache,\n} from '@epic-web/workshop-utils/cache.server'\nimport { getWorkshopConfig } from '@epic-web/workshop-utils/config.server'\nimport { getAuthInfo, getPreferences } from '@epic-web/workshop-utils/db.server'\nimport {\n\tgetUserInfo,\n\tuserHasAccessToWorkshop,\n} from '@epic-web/workshop-utils/epic-api.server'\nimport { type Timings } from '@epic-web/workshop-utils/timing.server'\nimport { getUserId } from '@epic-web/workshop-utils/user.server'\nimport { PresenceSchema, partykitBaseUrl, type User } from './presence.js'\n\nexport const presenceCache = makeSingletonCache<Array<User>>('PresenceCache')\n\nexport async function getPresentUsers({\n\ttimings,\n\trequest,\n}: { timings?: Timings; request?: Request } = {}): Promise<Array<User>> {\n\tconst presence = await cachified({\n\t\tkey: 'presence',\n\t\tcache: presenceCache,\n\t\ttimings,\n\t\trequest,\n\t\tttl: 1000 * 2,\n\t\tswr: 1000 * 60 * 60 * 24,\n\t\tofflineFallbackValue: { users: [] },\n\t\tcheckValue: PresenceSchema,\n\t\tasync getFreshValue(context) {\n\t\t\ttry {\n\t\t\t\tconst response = await Promise.race([\n\t\t\t\t\tfetch(`${partykitBaseUrl}/presence`),\n\t\t\t\t\tnew Promise<Response>((resolve) =>\n\t\t\t\t\t\tsetTimeout(() => {\n\t\t\t\t\t\t\tresolve(new Response('Timeout', { status: 500 }))\n\t\t\t\t\t\t}, 500),\n\t\t\t\t\t),\n\t\t\t\t] as const)\n\t\t\t\tif (response.statusText === 'Timeout') {\n\t\t\t\t\tthrow new Error(`Timeout fetching partykit presence`)\n\t\t\t\t}\n\t\t\t\tif (!response.ok) {\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t`Unexpected response from partykit: ${response.status} ${response.statusText}`,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tconst presence = PresenceSchema.parse(await response.json())\n\t\t\t\treturn presence\n\t\t\t} catch {\n\t\t\t\t// console.error(err)\n\t\t\t\tcontext.metadata.ttl = 300\n\t\t\t\treturn { users: [] }\n\t\t\t}\n\t\t},\n\t})\n\tconst { users } = presence\n\n\tconst authInfo = await getAuthInfo()\n\tconst userId = request\n\t\t? (await getUserId({ request })).id\n\t\t: (authInfo?.id ?? null)\n\n\tconst preferences = await getPreferences()\n\n\tif (preferences?.presence.optOut || !userId) {\n\t\treturn uniqueUsers(users.filter((u) => u.id !== userId))\n\t} else {\n\t\tconst user: User = { id: userId }\n\t\tconst config = getWorkshopConfig()\n\t\tconst url = request ? new URL(request.url) : undefined\n\t\tuser.location = {\n\t\t\tworkshopTitle: config.title,\n\t\t\torigin: url ? url.origin : undefined,\n\t\t}\n\t\tif (url) {\n\t\t\tif (url.pathname.startsWith('/exercise/')) {\n\t\t\t\tconst [exerciseNumber, stepNumber, type] = url.pathname\n\t\t\t\t\t.split('/')\n\t\t\t\t\t.slice(2)\n\t\t\t\tuser.location.exercise = {\n\t\t\t\t\texerciseNumber: isNaN(Number(exerciseNumber))\n\t\t\t\t\t\t? null\n\t\t\t\t\t\t: Number(exerciseNumber),\n\t\t\t\t\tstepNumber: isNaN(Number(stepNumber)) ? null : Number(stepNumber),\n\t\t\t\t\ttype:\n\t\t\t\t\t\ttype === 'problem'\n\t\t\t\t\t\t\t? 'problem'\n\t\t\t\t\t\t\t: type === 'solution'\n\t\t\t\t\t\t\t\t? 'solution'\n\t\t\t\t\t\t\t\t: null,\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (authInfo) {\n\t\t\tconst [userInfo, hasAccess] = await Promise.all([\n\t\t\t\tgetUserInfo({ request, timings }),\n\t\t\t\tuserHasAccessToWorkshop({ request, timings }),\n\t\t\t])\n\n\t\t\tObject.assign(user, {\n\t\t\t\tname: userInfo?.name,\n\t\t\t\tavatarUrl: userInfo?.imageUrlLarge,\n\t\t\t\timageUrlSmall: userInfo?.imageUrlSmall,\n\t\t\t\timageUrlLarge: userInfo?.imageUrlLarge,\n\t\t\t\thasAccess,\n\t\t\t})\n\t\t}\n\n\t\treturn uniqueUsers([...users, user])\n\t}\n}\n\n// A user maybe on the same page in multiple tabs\n// so let's make sure we only show them once\nfunction uniqueUsers(users: Array<User>) {\n\tconst seen = new Set()\n\treturn users.filter(Boolean).filter((user) => {\n\t\tif (seen.has(user.id)) {\n\t\t\treturn false\n\t\t}\n\t\tseen.add(user.id)\n\t\treturn true\n\t})\n}\n"]}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type * as Party from 'partykit/server';
|
|
2
|
+
declare const _default: {
|
|
3
|
+
new (party: Party.Room): {
|
|
4
|
+
options: Party.ServerOptions;
|
|
5
|
+
readonly party: Party.Room;
|
|
6
|
+
onClose(): void;
|
|
7
|
+
onError(): void;
|
|
8
|
+
updateUsers(): void;
|
|
9
|
+
getPresenceMessage(): {
|
|
10
|
+
type: "presence";
|
|
11
|
+
payload: {
|
|
12
|
+
users: {
|
|
13
|
+
id: string;
|
|
14
|
+
hasAccess?: boolean | null | undefined;
|
|
15
|
+
avatarUrl?: string | null | undefined;
|
|
16
|
+
imageUrlSmall?: string | null | undefined;
|
|
17
|
+
imageUrlLarge?: string | null | undefined;
|
|
18
|
+
name?: string | null | undefined;
|
|
19
|
+
location?: {
|
|
20
|
+
workshopTitle?: string | null | undefined;
|
|
21
|
+
origin?: string | null | undefined;
|
|
22
|
+
exercise?: {
|
|
23
|
+
type?: "problem" | "solution" | null | undefined;
|
|
24
|
+
exerciseNumber?: number | null | undefined;
|
|
25
|
+
stepNumber?: number | null | undefined;
|
|
26
|
+
} | null | undefined;
|
|
27
|
+
} | null | undefined;
|
|
28
|
+
}[];
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
getUsers(): {
|
|
32
|
+
id: string;
|
|
33
|
+
hasAccess?: boolean | null | undefined;
|
|
34
|
+
avatarUrl?: string | null | undefined;
|
|
35
|
+
imageUrlSmall?: string | null | undefined;
|
|
36
|
+
imageUrlLarge?: string | null | undefined;
|
|
37
|
+
name?: string | null | undefined;
|
|
38
|
+
location?: {
|
|
39
|
+
workshopTitle?: string | null | undefined;
|
|
40
|
+
origin?: string | null | undefined;
|
|
41
|
+
exercise?: {
|
|
42
|
+
type?: "problem" | "solution" | null | undefined;
|
|
43
|
+
exerciseNumber?: number | null | undefined;
|
|
44
|
+
stepNumber?: number | null | undefined;
|
|
45
|
+
} | null | undefined;
|
|
46
|
+
} | null | undefined;
|
|
47
|
+
}[];
|
|
48
|
+
onMessage(message: string, sender: Party.Connection): void;
|
|
49
|
+
onRequest(req: Party.Request): Response | Promise<Response>;
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
export default _default;
|
|
53
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,KAAK,MAAM,iBAAiB,CAAA;;gBAwCzB,KAAK,CAAC,IAAI;iBANpB,KAAK,CAAC,aAAa;wBAIZ,KAAK,CAAC,IAAI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;2BAyCP,MAAM,UAAU,KAAK,CAAC,UAAU;uBAepC,KAAK,CAAC,OAAO,GAAG,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;;;AA7D5D,wBA+GyB"}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { UserSchema } from './presence.js';
|
|
3
|
+
const ConnectionStateSchema = z
|
|
4
|
+
.object({
|
|
5
|
+
user: UserSchema.nullable().optional(),
|
|
6
|
+
})
|
|
7
|
+
.nullable();
|
|
8
|
+
const MessageSchema = z
|
|
9
|
+
.object({
|
|
10
|
+
type: z.literal('remove-user'),
|
|
11
|
+
payload: z.object({ id: z.string() }),
|
|
12
|
+
})
|
|
13
|
+
.or(z.object({ type: z.literal('add-user'), payload: UserSchema }))
|
|
14
|
+
.or(z.object({
|
|
15
|
+
type: z.literal('add-anonymous-user'),
|
|
16
|
+
payload: z.object({ id: z.string() }),
|
|
17
|
+
}))
|
|
18
|
+
.or(z.object({
|
|
19
|
+
type: z.literal('presence'),
|
|
20
|
+
payload: z.object({ users: z.array(UserSchema) }),
|
|
21
|
+
}));
|
|
22
|
+
export default (class Server {
|
|
23
|
+
options = {
|
|
24
|
+
hibernate: true,
|
|
25
|
+
};
|
|
26
|
+
party;
|
|
27
|
+
constructor(party) {
|
|
28
|
+
this.party = party;
|
|
29
|
+
}
|
|
30
|
+
onClose() {
|
|
31
|
+
this.updateUsers();
|
|
32
|
+
}
|
|
33
|
+
onError() {
|
|
34
|
+
this.updateUsers();
|
|
35
|
+
}
|
|
36
|
+
updateUsers() {
|
|
37
|
+
const presenceMessage = JSON.stringify(this.getPresenceMessage());
|
|
38
|
+
for (const connection of this.party.getConnections()) {
|
|
39
|
+
connection.send(presenceMessage);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
getPresenceMessage() {
|
|
43
|
+
return {
|
|
44
|
+
type: 'presence',
|
|
45
|
+
payload: { users: this.getUsers() },
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
getUsers() {
|
|
49
|
+
const users = new Map();
|
|
50
|
+
for (const connection of this.party.getConnections()) {
|
|
51
|
+
const state = getConnectionState(connection);
|
|
52
|
+
if (state?.user) {
|
|
53
|
+
users.set(state.user.id, state.user);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return sortUsers(Array.from(users.values()));
|
|
57
|
+
}
|
|
58
|
+
onMessage(message, sender) {
|
|
59
|
+
const result = MessageSchema.safeParse(JSON.parse(message));
|
|
60
|
+
if (!result.success)
|
|
61
|
+
return;
|
|
62
|
+
if (result.data.type === 'add-user') {
|
|
63
|
+
shallowMergeConnectionState(sender, { user: result.data.payload });
|
|
64
|
+
this.updateUsers();
|
|
65
|
+
}
|
|
66
|
+
else if (result.data.type === 'remove-user') {
|
|
67
|
+
setConnectionState(sender, null);
|
|
68
|
+
this.updateUsers();
|
|
69
|
+
}
|
|
70
|
+
else if (result.data.type === 'add-anonymous-user') {
|
|
71
|
+
setConnectionState(sender, { user: result.data.payload });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
onRequest(req) {
|
|
75
|
+
const url = new URL(req.url);
|
|
76
|
+
if (url.pathname.endsWith('/presence')) {
|
|
77
|
+
return Response.json(this.getPresenceMessage().payload);
|
|
78
|
+
}
|
|
79
|
+
if (url.pathname.endsWith('/show')) {
|
|
80
|
+
const users = this.getUsers();
|
|
81
|
+
const workshopUsers = organizeUsersByWorkshop(users);
|
|
82
|
+
return new Response(`
|
|
83
|
+
<!DOCTYPE html>
|
|
84
|
+
<html lang="en">
|
|
85
|
+
<head>
|
|
86
|
+
<meta charset="UTF-8">
|
|
87
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
88
|
+
<meta http-equiv="refresh" content="5">
|
|
89
|
+
<title>Epic Web Presence</title>
|
|
90
|
+
<style>
|
|
91
|
+
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
|
|
92
|
+
h1, h2 { color: #333; }
|
|
93
|
+
ul { padding: 0; }
|
|
94
|
+
li { list-style: none; margin-bottom: 10px; }
|
|
95
|
+
.user-avatar { width: 64px; height: 64px; border-radius: 50%; vertical-align: middle; margin-right: 10px; }
|
|
96
|
+
</style>
|
|
97
|
+
</head>
|
|
98
|
+
<body>
|
|
99
|
+
<h1>Epic Web Presence</h1>
|
|
100
|
+
<p>Total Users: ${users.length}</p>
|
|
101
|
+
${Object.entries(workshopUsers)
|
|
102
|
+
.map(([workshop, workshopUsers]) => `
|
|
103
|
+
<h2>${workshop} (${workshopUsers.length})</h2>
|
|
104
|
+
<ul>
|
|
105
|
+
${workshopUsers.map(generateUserListItem).join('')}
|
|
106
|
+
</ul>
|
|
107
|
+
`)
|
|
108
|
+
.join('')}
|
|
109
|
+
</body>
|
|
110
|
+
</html>
|
|
111
|
+
`, {
|
|
112
|
+
headers: {
|
|
113
|
+
'Content-Type': 'text/html',
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
return new Response('not found', { status: 404 });
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
function shallowMergeConnectionState(connection, state) {
|
|
121
|
+
setConnectionState(connection, (prev) => ({ ...prev, ...state }));
|
|
122
|
+
}
|
|
123
|
+
function setConnectionState(connection, state) {
|
|
124
|
+
if (typeof state !== 'function') {
|
|
125
|
+
return connection.setState(state);
|
|
126
|
+
}
|
|
127
|
+
connection.setState((prev) => {
|
|
128
|
+
const prevParseResult = ConnectionStateSchema.safeParse(prev);
|
|
129
|
+
if (prevParseResult.success) {
|
|
130
|
+
return state(prevParseResult.data);
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
return state(null);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
function getConnectionState(connection) {
|
|
138
|
+
const result = ConnectionStateSchema.safeParse(connection.state);
|
|
139
|
+
if (result.success) {
|
|
140
|
+
return result.data;
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
setConnectionState(connection, null);
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
function sortUsers(users) {
|
|
148
|
+
return [...users].sort((a, b) => {
|
|
149
|
+
const aScore = getScore(a);
|
|
150
|
+
const bScore = getScore(b);
|
|
151
|
+
if (aScore === bScore)
|
|
152
|
+
return 0;
|
|
153
|
+
return aScore > bScore ? -1 : 1;
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
function getScore(user) {
|
|
157
|
+
let score = 0;
|
|
158
|
+
if (user.imageUrlSmall)
|
|
159
|
+
score += 1;
|
|
160
|
+
if (user.imageUrlSmall?.includes('discordapp'))
|
|
161
|
+
score += 0.5;
|
|
162
|
+
if (user.name)
|
|
163
|
+
score += 1;
|
|
164
|
+
return score;
|
|
165
|
+
}
|
|
166
|
+
function organizeUsersByWorkshop(users) {
|
|
167
|
+
const workshopUsers = {};
|
|
168
|
+
for (const user of users) {
|
|
169
|
+
const workshop = user.location?.workshopTitle ?? 'Unknown Workshop';
|
|
170
|
+
if (!workshopUsers[workshop]) {
|
|
171
|
+
workshopUsers[workshop] = [];
|
|
172
|
+
}
|
|
173
|
+
workshopUsers[workshop]?.push(user);
|
|
174
|
+
}
|
|
175
|
+
// Sort users within each workshop by exercise and step number
|
|
176
|
+
for (const workshop in workshopUsers) {
|
|
177
|
+
workshopUsers[workshop]?.sort((a, b) => {
|
|
178
|
+
const aExercise = a.location?.exercise?.exerciseNumber ?? 0;
|
|
179
|
+
const bExercise = b.location?.exercise?.exerciseNumber ?? 0;
|
|
180
|
+
if (aExercise !== bExercise)
|
|
181
|
+
return aExercise - bExercise;
|
|
182
|
+
const aStep = a.location?.exercise?.stepNumber ?? 0;
|
|
183
|
+
const bStep = b.location?.exercise?.stepNumber ?? 0;
|
|
184
|
+
return aStep - bStep;
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
return workshopUsers;
|
|
188
|
+
}
|
|
189
|
+
function generateUserListItem(user) {
|
|
190
|
+
const imageUrl = user.imageUrlLarge ?? user.avatarUrl ?? '/avatar.png';
|
|
191
|
+
const name = user.name ?? 'Anonymous';
|
|
192
|
+
const location = user.location?.exercise
|
|
193
|
+
? [
|
|
194
|
+
`Exercise ${user.location.exercise.exerciseNumber}`,
|
|
195
|
+
user.location.exercise.stepNumber &&
|
|
196
|
+
`Step ${user.location.exercise.stepNumber}`,
|
|
197
|
+
]
|
|
198
|
+
.filter(Boolean)
|
|
199
|
+
.join(', ')
|
|
200
|
+
: user.location?.origin
|
|
201
|
+
? user.location.origin
|
|
202
|
+
: 'Unknown location';
|
|
203
|
+
const accessLabel = typeof user.hasAccess === 'boolean' ? (user.hasAccess ? '🔑' : '🆓') : '';
|
|
204
|
+
return `
|
|
205
|
+
<li>
|
|
206
|
+
<img class="user-avatar" src="${imageUrl}" alt="${name}" />
|
|
207
|
+
<strong>${name}</strong> - ${location} ${accessLabel}
|
|
208
|
+
</li>
|
|
209
|
+
`;
|
|
210
|
+
}
|
|
211
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../src/server.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAG1C,MAAM,qBAAqB,GAAG,CAAC;KAC7B,MAAM,CAAC;IACP,IAAI,EAAE,UAAU,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE;CACtC,CAAC;KACD,QAAQ,EAAE,CAAA;AAIZ,MAAM,aAAa,GAAG,CAAC;KACrB,MAAM,CAAC;IACP,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,aAAa,CAAC;IAC9B,OAAO,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC;CACrC,CAAC;KACD,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC,CAAC;KACl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AU;IACvC,MAAM,QAAQ,GAAG,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,SAAS,IAAI,aAAa,CAAA;IACtE,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,IAAI,WAAW,CAAA;IACrC,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,EAAE,QAAQ;QACvC,CAAC,CAAC;YACA,YAAY,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,cAAc,EAAE;YACnD,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,UAAU;gBAChC,QAAQ,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,UAAU,EAAE;SAC5C;aACC,MAAM,CAAC,OAAO,CAAC;aACf,IAAI,CAAC,IAAI,CAAC;QACb,CAAC,CAAC,IAAI,CAAC,QAAQ,EAAE,MAAM;YACtB,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM;YACtB,CAAC,CAAC,kBAAkB,CAAA;IAEtB,MAAM,WAAW,GAChB,OAAO,IAAI,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;IAE1E,OAAO;;mCAE2B,QAAQ,UAAU,IAAI;aAC5C,IAAI,eAAe,QAAQ,IAAI,WAAW;;EAErD,CAAA;AACF,CAAC","sourcesContent":["import type * as Party from 'partykit/server'\nimport { z } from 'zod'\nimport { UserSchema } from './presence.js'\n\ntype User = z.infer<typeof UserSchema>\nconst ConnectionStateSchema = z\n\t.object({\n\t\tuser: UserSchema.nullable().optional(),\n\t})\n\t.nullable()\n\ntype ConnectionState = z.infer<typeof ConnectionStateSchema>\n\nconst MessageSchema = z\n\t.object({\n\t\ttype: z.literal('remove-user'),\n\t\tpayload: z.object({ id: z.string() }),\n\t})\n\t.or(z.object({ type: z.literal('add-user'), payload: UserSchema }))\n\t.or(\n\t\tz.object({\n\t\t\ttype: z.literal('add-anonymous-user'),\n\t\t\tpayload: z.object({ id: z.string() }),\n\t\t}),\n\t)\n\t.or(\n\t\tz.object({\n\t\t\ttype: z.literal('presence'),\n\t\t\tpayload: z.object({ users: z.array(UserSchema) }),\n\t\t}),\n\t)\ntype Message = z.infer<typeof MessageSchema>\n\nexport default (class Server implements Party.Server {\n\toptions: Party.ServerOptions = {\n\t\thibernate: true,\n\t}\n\n\treadonly party: Party.Room\n\n\tconstructor(party: Party.Room) {\n\t\tthis.party = party\n\t}\n\n\tonClose() {\n\t\tthis.updateUsers()\n\t}\n\n\tonError() {\n\t\tthis.updateUsers()\n\t}\n\n\tupdateUsers() {\n\t\tconst presenceMessage = JSON.stringify(this.getPresenceMessage())\n\t\tfor (const connection of this.party.getConnections()) {\n\t\t\tconnection.send(presenceMessage)\n\t\t}\n\t}\n\n\tgetPresenceMessage() {\n\t\treturn {\n\t\t\ttype: 'presence',\n\t\t\tpayload: { users: this.getUsers() },\n\t\t} satisfies Message\n\t}\n\n\tgetUsers() {\n\t\tconst users = new Map<string, z.infer<typeof UserSchema>>()\n\n\t\tfor (const connection of this.party.getConnections()) {\n\t\t\tconst state = getConnectionState(connection)\n\t\t\tif (state?.user) {\n\t\t\t\tusers.set(state.user.id, state.user)\n\t\t\t}\n\t\t}\n\n\t\treturn sortUsers(Array.from(users.values()))\n\t}\n\n\tonMessage(message: string, sender: Party.Connection) {\n\t\tconst result = MessageSchema.safeParse(JSON.parse(message))\n\t\tif (!result.success) return\n\n\t\tif (result.data.type === 'add-user') {\n\t\t\tshallowMergeConnectionState(sender, { user: result.data.payload })\n\t\t\tthis.updateUsers()\n\t\t} else if (result.data.type === 'remove-user') {\n\t\t\tsetConnectionState(sender, null)\n\t\t\tthis.updateUsers()\n\t\t} else if (result.data.type === 'add-anonymous-user') {\n\t\t\tsetConnectionState(sender, { user: result.data.payload })\n\t\t}\n\t}\n\n\tonRequest(req: Party.Request): Response | Promise<Response> {\n\t\tconst url = new URL(req.url)\n\t\tif (url.pathname.endsWith('/presence')) {\n\t\t\treturn Response.json(this.getPresenceMessage().payload)\n\t\t}\n\t\tif (url.pathname.endsWith('/show')) {\n\t\t\tconst users = this.getUsers()\n\t\t\tconst workshopUsers = organizeUsersByWorkshop(users)\n\t\t\treturn new Response(\n\t\t\t\t`\n\t\t\t\t<!DOCTYPE html>\n\t\t\t\t<html lang=\"en\">\n\t\t\t\t<head>\n\t\t\t\t\t<meta charset=\"UTF-8\">\n\t\t\t\t\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n\t\t\t\t\t<meta http-equiv=\"refresh\" content=\"5\">\n\t\t\t\t\t<title>Epic Web Presence</title>\n\t\t\t\t\t<style>\n\t\t\t\t\t\tbody { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }\n\t\t\t\t\t\th1, h2 { color: #333; }\n\t\t\t\t\t\tul { padding: 0; }\n\t\t\t\t\t\tli { list-style: none; margin-bottom: 10px; }\n\t\t\t\t\t\t.user-avatar { width: 64px; height: 64px; border-radius: 50%; vertical-align: middle; margin-right: 10px; }\n\t\t\t\t\t</style>\n\t\t\t\t</head>\n\t\t\t\t<body>\n\t\t\t\t\t<h1>Epic Web Presence</h1>\n\t\t\t\t\t<p>Total Users: ${users.length}</p>\n\t\t\t\t\t${Object.entries(workshopUsers)\n\t\t\t\t\t\t.map(\n\t\t\t\t\t\t\t([workshop, workshopUsers]) => `\n\t\t\t\t\t\t\t<h2>${workshop} (${workshopUsers.length})</h2>\n\t\t\t\t\t\t\t<ul>\n\t\t\t\t\t\t\t\t${workshopUsers.map(generateUserListItem).join('')}\n\t\t\t\t\t\t\t</ul>\n\t\t\t\t\t\t`,\n\t\t\t\t\t\t)\n\t\t\t\t\t\t.join('')}\n\t\t\t\t</body>\n\t\t\t\t</html>\n\t\t\t\t`,\n\t\t\t\t{\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t'Content-Type': 'text/html',\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t)\n\t\t}\n\t\treturn new Response('not found', { status: 404 })\n\t}\n} satisfies Party.Worker)\n\nfunction shallowMergeConnectionState(\n\tconnection: Party.Connection,\n\tstate: ConnectionState,\n) {\n\tsetConnectionState(connection, (prev) => ({ ...prev, ...state }))\n}\n\nfunction setConnectionState(\n\tconnection: Party.Connection,\n\tstate:\n\t\t| ConnectionState\n\t\t| ((prev: ConnectionState | null) => ConnectionState | null),\n) {\n\tif (typeof state !== 'function') {\n\t\treturn connection.setState(state)\n\t}\n\tconnection.setState((prev: unknown) => {\n\t\tconst prevParseResult = ConnectionStateSchema.safeParse(prev)\n\t\tif (prevParseResult.success) {\n\t\t\treturn state(prevParseResult.data)\n\t\t} else {\n\t\t\treturn state(null)\n\t\t}\n\t})\n}\n\nfunction getConnectionState(connection: Party.Connection) {\n\tconst result = ConnectionStateSchema.safeParse(connection.state)\n\tif (result.success) {\n\t\treturn result.data\n\t} else {\n\t\tsetConnectionState(connection, null)\n\t\treturn null\n\t}\n}\n\nfunction sortUsers(users: Array<User>) {\n\treturn [...users].sort((a, b) => {\n\t\tconst aScore = getScore(a)\n\t\tconst bScore = getScore(b)\n\t\tif (aScore === bScore) return 0\n\t\treturn aScore > bScore ? -1 : 1\n\t})\n}\n\nfunction getScore(user: User) {\n\tlet score = 0\n\tif (user.imageUrlSmall) score += 1\n\tif (user.imageUrlSmall?.includes('discordapp')) score += 0.5\n\tif (user.name) score += 1\n\treturn score\n}\n\nfunction organizeUsersByWorkshop(users: Array<User>) {\n\tconst workshopUsers: Record<string, Array<User>> = {}\n\n\tfor (const user of users) {\n\t\tconst workshop = user.location?.workshopTitle ?? 'Unknown Workshop'\n\t\tif (!workshopUsers[workshop]) {\n\t\t\tworkshopUsers[workshop] = []\n\t\t}\n\t\tworkshopUsers[workshop]?.push(user)\n\t}\n\n\t// Sort users within each workshop by exercise and step number\n\tfor (const workshop in workshopUsers) {\n\t\tworkshopUsers[workshop]?.sort((a, b) => {\n\t\t\tconst aExercise = a.location?.exercise?.exerciseNumber ?? 0\n\t\t\tconst bExercise = b.location?.exercise?.exerciseNumber ?? 0\n\t\t\tif (aExercise !== bExercise) return aExercise - bExercise\n\n\t\t\tconst aStep = a.location?.exercise?.stepNumber ?? 0\n\t\t\tconst bStep = b.location?.exercise?.stepNumber ?? 0\n\t\t\treturn aStep - bStep\n\t\t})\n\t}\n\n\treturn workshopUsers\n}\n\nfunction generateUserListItem(user: User) {\n\tconst imageUrl = user.imageUrlLarge ?? user.avatarUrl ?? '/avatar.png'\n\tconst name = user.name ?? 'Anonymous'\n\tconst location = user.location?.exercise\n\t\t? [\n\t\t\t\t`Exercise ${user.location.exercise.exerciseNumber}`,\n\t\t\t\tuser.location.exercise.stepNumber &&\n\t\t\t\t\t`Step ${user.location.exercise.stepNumber}`,\n\t\t\t]\n\t\t\t\t.filter(Boolean)\n\t\t\t\t.join(', ')\n\t\t: user.location?.origin\n\t\t\t? user.location.origin\n\t\t\t: 'Unknown location'\n\n\tconst accessLabel =\n\t\ttypeof user.hasAccess === 'boolean' ? (user.hasAccess ? '🔑' : '🆓') : ''\n\n\treturn `\n\t\t<li>\n\t\t\t<img class=\"user-avatar\" src=\"${imageUrl}\" alt=\"${name}\" />\n\t\t\t<strong>${name}</strong> - ${location} ${accessLabel}\n\t\t</li>\n\t`\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name":"@epic-web/workshop-presence","version":"0.0.0-semantically-released","publishConfig":{"access":"public"},"type":"module","scripts":{"typecheck":"tsc --noEmit","build":"tshy","dev":"partykit dev","deploy":"partykit deploy"},"dependencies":{"@epic-web/workshop-utils":"^5.29.2","zod":"^3.25.71"},"devDependencies":{"partykit":"0.0.115","typescript":"^5.8.3","tshy":"^3.0.2"},"files":["dist"],"tshy":{"project":"./tsconfig.build.json","dialects":["esm"],"exports":{"./package.json":"./package.json","./presence.server":"./src/presence.server.ts","./presence":"./src/presence.ts"}},"repository":{"type":"git","url":"https://github.com/epicweb-dev/epicshop.git","directory":"packages/presence"},"exports":{"./package.json":"./package.json","./presence.server":{"import":{"types":"./dist/esm/presence.server.d.ts","default":"./dist/esm/presence.server.js"}},"./presence":{"import":{"types":"./dist/esm/presence.d.ts","default":"./dist/esm/presence.js"}}}}
|