~james/core

core/web/src/lib/api/notifications.ts -rw-r--r-- 6.2 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
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
import { authedGet, authedPost, type DeliberiContext } from "#lib/api/deliberi.ts";
import type { BobbinContext, QueryValue, XrpcRequestInit } from "#lib/api/client.ts";
import { INVALID_HANDLE, profileIdentifier, resolveMiniDoc } from "#lib/api/identity.ts";
import { getReposByRepoDids, getTickets } from "#lib/api/records.ts";
import { TICKET_COLLECTION } from "#lib/api/ticket.ts";
import { didOf, nsidOf } from "#lib/api/syntax.ts";
import { collectionFromUri, didFromUri, rkeyFromUri } from "#lib/api/uri.ts";
import type {
	NotificationActor,
	NotificationRepo,
	NotificationSubject,
	NotificationSummary,
	NotificationType
} from "#lib/components/notifications/types.ts";

/** mirrors org.tangled.temp.notification.getPreferences#preferences */
export interface NotificationPreferences {
	repoStarred: boolean;
	issueCreated: boolean;
	issueCommented: boolean;
	issueClosed: boolean;
	pullCreated: boolean;
	pullCommented: boolean;
	pullMerged: boolean;
	followed: boolean;
	userMentioned: boolean;
	emailNotifications: boolean;
}

const GET_PREFERENCES = nsidOf("org.tangled.temp.notification.getPreferences");
const UPDATE_PREFERENCES = nsidOf("org.tangled.temp.notification.updatePreferences");

export const getNotificationPreferences = (
	ctx: DeliberiContext,
	init?: XrpcRequestInit
): Promise<NotificationPreferences> =>
	authedGet<NotificationPreferences>(ctx, GET_PREFERENCES, undefined, init);

/** only the provided fields are updated server-side. */
export const updateNotificationPreferences = (
	ctx: DeliberiContext,
	patch: Partial<NotificationPreferences>,
	init?: XrpcRequestInit
): Promise<void> => authedPost(ctx, UPDATE_PREFERENCES, patch, init).then(() => undefined);

/** raw notification row returned by deliberi */
export interface NotificationItem {
	uri: string;
	type: string;
	category: string;
	actorDid: string;
	read: boolean;
	createdAt: string;
	repoDid?: string;
	issueAt?: string;
	pullAt?: string;
}

export interface NotificationListOutput {
	notifications: NotificationItem[];
	socialUnreadCount: number;
	workUnreadCount: number;
}

const LIST_NOTIFICATIONS = nsidOf("org.tangled.temp.notification.listNotifications");

export const listNotifications = (
	ctx: DeliberiContext,
	params?: { read?: string; category?: string; limit?: number },
	init?: XrpcRequestInit
): Promise<NotificationListOutput> =>
	authedGet<NotificationListOutput>(
		ctx,
		LIST_NOTIFICATIONS,
		params as Record<string, QueryValue>,
		init
	);

const UPDATE_SEEN = nsidOf("org.tangled.temp.notification.updateSeen");
const MARK_ALL_READ = nsidOf("org.tangled.temp.notification.markAllRead");
const MARK_ENTITY_READ = nsidOf("org.tangled.temp.notification.markEntityRead");
const GET_UNREAD = nsidOf("org.tangled.temp.notification.getUnreadCount");

export const updateNotificationSeen = (
	ctx: DeliberiContext,
	input: { uri: string; read: boolean }
): Promise<void> => authedPost(ctx, UPDATE_SEEN, input).then(() => undefined);

export const markAllNotificationsRead = (ctx: DeliberiContext): Promise<void> =>
	authedPost(ctx, MARK_ALL_READ, {}).then(() => undefined);

export const markEntityNotificationsRead = (
	ctx: DeliberiContext,
	entityAt: string
): Promise<void> => authedPost(ctx, MARK_ENTITY_READ, { entityAt }).then(() => undefined);

export const getUnreadCount = (ctx: DeliberiContext): Promise<{ count: number }> =>
	authedGet(ctx, GET_UNREAD);

const subjectFromUri = (
	uri: string,
	kind: "issue" | "pull",
	title?: string
): NotificationSubject => ({
	kind,
	uri,
	number: rkeyFromUri(uri),
	title
});

const isTicketUri = (uri: string) => collectionFromUri(uri) === TICKET_COLLECTION;

/** resolve rows best-effort: missing actor, repo, or entity falls back to the raw value */
export const hydrateNotifications = async (
	ctx: BobbinContext,
	items: NotificationItem[]
): Promise<NotificationSummary[]> => {
	if (items.length === 0) return [];

	const actorDids = [...new Set(items.map((n) => n.actorDid))];

	const repoDids = [...new Set(items.map((n) => n.repoDid).filter((d): d is string => !!d))];

	const ticketUris = [
		...new Set(items.flatMap((n) => [n.issueAt, n.pullAt]).filter((u): u is string => !!u))
	].filter(isTicketUri);

	const [actorDocs, repoList, ticketList] = await Promise.all([
		Promise.all(actorDids.map((did) => resolveMiniDoc(ctx, did).catch(() => null))),
		repoDids.length > 0
			? getReposByRepoDids(ctx, repoDids).catch(() => ({ items: [] }))
			: Promise.resolve({ items: [] }),
		ticketUris.length > 0
			? getTickets(ctx, ticketUris).catch(() => ({ items: [] }))
			: Promise.resolve({ items: [] })
	]);
	const ownerDids = [...new Set(repoList.items.map((r) => r.value.declaration.owner))];
	const ownerDocs = await Promise.all(
		ownerDids.map((did) => resolveMiniDoc(ctx, did).catch(() => null))
	);
	const ownerMap = new Map<string, string>();
	for (const doc of ownerDocs) {
		if (doc) ownerMap.set(doc.did, profileIdentifier(doc.handle, doc.did));
	}

	const actorMap = new Map<string, NotificationActor>();
	for (const doc of actorDocs) {
		if (doc) actorMap.set(doc.did, { did: doc.did, handle: doc.handle });
	}

	const repoMap = new Map<string, NotificationRepo>();
	for (const record of repoList.items) {
		const repoDid = didFromUri(record.uri);
		const ownerDid = record.value.declaration.owner;
		const ownerHandle = ownerMap.get(ownerDid) ?? ownerDid;
		repoMap.set(repoDid, {
			ownerHandle,
			name: record.value.declaration.name
		});
	}
	for (const repoDid of repoDids) {
		if (!repoMap.has(repoDid)) {
			repoMap.set(repoDid, { ownerHandle: repoDid, name: repoDid });
		}
	}

	const titleMap = new Map<string, string>();
	for (const record of ticketList.items) {
		titleMap.set(record.uri, record.value.title.text);
	}

	return items.map((item): NotificationSummary => {
		const actorDid = didOf(item.actorDid);
		const actor = actorMap.get(item.actorDid) ?? { did: actorDid, handle: INVALID_HANDLE };
		const repo = item.repoDid ? repoMap.get(item.repoDid) : undefined;
		let subject: NotificationSubject | undefined;
		if (item.issueAt) {
			subject = subjectFromUri(item.issueAt, "issue", titleMap.get(item.issueAt));
		} else if (item.pullAt) {
			subject = subjectFromUri(item.pullAt, "pull", titleMap.get(item.pullAt));
		}

		return {
			uri: item.uri,
			type: item.type as NotificationType,
			read: item.read,
			createdAt: item.createdAt,
			actor,
			repo,
			subject
		};
	});
};