Repository navigation
Expand file tree
/
Copy pathldn.js
More file actions
61 lines (53 loc) · 1.7 KB
/
Copy pathldn.js
File metadata and controls
61 lines (53 loc) · 1.7 KB
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
/*
* Linked Data Notifications inbox support.
*
* The inbox list is user-configurable and persisted; Rerum Inbox is the
* default. Results are opt-in (persisted checkbox) and previewed before
* inclusion, similar to the Mirador LDN plugin.
*/
export const DEFAULT_INBOX = 'https://inbox.rerum.io/messages'
const INBOXES_KEY = 'read.ldn.inboxes'
const OPTIN_KEY = 'read.ldn.optin'
export function getInboxes() {
try {
const list = JSON.parse(localStorage.getItem(INBOXES_KEY))
if (Array.isArray(list) && list.length) return list
} catch {
/* fall through to default */
}
return [DEFAULT_INBOX]
}
// The viewer is served over https, so any other inbox is blocked as mixed
// content or is not a URL at all.
export function isInboxUri(uri) {
try {
return new URL(uri).protocol === 'https:'
} catch {
return false
}
}
export function saveInboxes(list) {
localStorage.setItem(INBOXES_KEY, JSON.stringify(list))
}
export function getOptIn() {
return localStorage.getItem(OPTIN_KEY) === 'true'
}
export function setOptIn(value) {
localStorage.setItem(OPTIN_KEY, value ? 'true' : 'false')
}
export function extractMessages(data) {
if (Array.isArray(data)) return data
// Rerum Inbox answers with an LDP container: { "@type": "ldp:Container", "contains": [...] }
for (const key of ['contains', 'ldp:contains', 'messages', 'items']) {
if (Array.isArray(data?.[key])) return data[key]
}
return []
}
export async function queryInbox(inbox, target) {
const url = new URL(inbox)
url.searchParams.set('target', target)
const res = await fetch(url)
if (!res.ok) throw new Error(`Inbox ${inbox} responded ${res.status}`)
const data = await res.json()
return extractMessages(data)
}