forked from SolidOS/solid-logic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatLogic.js
More file actions
157 lines (157 loc) · 6.61 KB
/
chatLogic.js
File metadata and controls
157 lines (157 loc) · 6.61 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
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
import { NamedNode, st, term } from 'rdflib';
import { ns as namespace } from '../util/ns';
import { determineChatContainer, newThing } from '../util/utils';
const CHAT_LOCATION_IN_CONTAINER = 'index.ttl#this';
export function createChatLogic(store, profileLogic) {
const ns = namespace;
async function setAcl(chatContainer, me, invitee) {
// Some servers don't present a Link http response header
// if the container doesn't exist yet, so refetch the container
// now that it has been created:
await store.fetcher.load(chatContainer);
// FIXME: check the Why value on this quad:
const chatAclDoc = store.any(chatContainer, new NamedNode('http://www.iana.org/assignments/link-relations/acl'));
if (!chatAclDoc) {
throw new Error('Chat ACL doc not found!');
}
const aclBody = `
@prefix acl: <http://www.w3.org/ns/auth/acl#>.
<#owner>
a acl:Authorization;
acl:agent <${me.value}>;
acl:accessTo <.>;
acl:default <.>;
acl:mode
acl:Read, acl:Write, acl:Control.
<#invitee>
a acl:Authorization;
acl:agent <${invitee.value}>;
acl:accessTo <.>;
acl:default <.>;
acl:mode
acl:Read, acl:Append.
`;
await store.fetcher.webOperation('PUT', chatAclDoc.value, {
data: aclBody,
contentType: 'text/turtle',
});
}
async function addToPrivateTypeIndex(chatThing, me) {
// Add to private type index
const privateTypeIndex = store.any(me, ns.solid('privateTypeIndex'));
if (!privateTypeIndex) {
throw new Error('Private type index not found!');
}
await store.fetcher.load(privateTypeIndex);
const reg = newThing(privateTypeIndex);
const ins = [
st(reg, ns.rdf('type'), ns.solid('TypeRegistration'), privateTypeIndex.doc()),
st(reg, ns.solid('forClass'), ns.meeting('LongChat'), privateTypeIndex.doc()),
st(reg, ns.solid('instance'), chatThing, privateTypeIndex.doc()),
];
await new Promise((resolve, reject) => {
store.updater.update([], ins, function (_uri, ok, errm) {
if (!ok) {
reject(new Error(errm));
}
else {
resolve(null);
}
});
});
}
async function findChat(invitee) {
const me = await profileLogic.loadMe();
const podRoot = await profileLogic.getPodRoot(me);
const chatContainer = determineChatContainer(invitee, podRoot);
let exists = true;
try {
await store.fetcher.load(new NamedNode(chatContainer.value + 'index.ttl#this'));
}
catch (e) {
exists = false;
}
return { me, chatContainer, exists };
}
async function createChatThing(chatContainer, me) {
const created = await mintNew({
me,
newBase: chatContainer.value,
});
return created.newInstance;
}
function mintNew(newPaneOptions) {
const kb = store;
const updater = kb.updater;
if (newPaneOptions.me && !newPaneOptions.me.uri) {
throw new Error('chat mintNew: Invalid userid ' + newPaneOptions.me);
}
const newInstance = (newPaneOptions.newInstance =
newPaneOptions.newInstance ||
kb.sym(newPaneOptions.newBase + CHAT_LOCATION_IN_CONTAINER));
const newChatDoc = newInstance.doc();
kb.add(newInstance, ns.rdf('type'), ns.meeting('LongChat'), newChatDoc);
kb.add(newInstance, ns.dc('title'), 'Chat channel', newChatDoc);
kb.add(newInstance, ns.dc('created'), term(new Date(Date.now())), newChatDoc);
if (newPaneOptions.me) {
kb.add(newInstance, ns.dc('author'), newPaneOptions.me, newChatDoc);
}
return new Promise(function (resolve, reject) {
updater === null || updater === void 0 ? void 0 : updater.put(newChatDoc, kb.statementsMatching(undefined, undefined, undefined, newChatDoc), 'text/turtle', function (uri2, ok, message) {
if (ok) {
resolve({
...newPaneOptions,
newInstance,
});
}
else {
reject(new Error('FAILED to save new chat channel at: ' + uri2 + ' : ' + message));
}
});
});
}
/**
* Find (and optionally create) an individual chat between the current user and the given invitee
* @param invitee - The person to chat with
* @param createIfMissing - Whether the chat should be created, if missing
* @returns null if missing, or a node referring to an already existing chat, or the newly created chat
*/
async function getChat(invitee, createIfMissing = true) {
const { me, chatContainer, exists } = await findChat(invitee);
if (exists) {
return new NamedNode(chatContainer.value + CHAT_LOCATION_IN_CONTAINER);
}
if (createIfMissing) {
const chatThing = await createChatThing(chatContainer, me);
await sendInvite(invitee, chatThing);
await setAcl(chatContainer, me, invitee);
await addToPrivateTypeIndex(chatThing, me);
return chatThing;
}
return null;
}
async function sendInvite(invitee, chatThing) {
var _a;
await store.fetcher.load(invitee.doc());
const inviteeInbox = store.any(invitee, ns.ldp('inbox'), undefined, invitee.doc());
if (!inviteeInbox) {
throw new Error(`Invitee inbox not found! ${invitee.value}`);
}
const inviteBody = `
<> a <http://www.w3.org/ns/pim/meeting#LongChatInvite> ;
${ns.rdf('seeAlso')} <${chatThing.value}> .
`;
const inviteResponse = await ((_a = store.fetcher) === null || _a === void 0 ? void 0 : _a.webOperation('POST', inviteeInbox.value, {
data: inviteBody,
contentType: 'text/turtle',
}));
const locationStr = inviteResponse === null || inviteResponse === void 0 ? void 0 : inviteResponse.headers.get('location');
if (!locationStr) {
throw new Error(`Invite sending returned a ${inviteResponse === null || inviteResponse === void 0 ? void 0 : inviteResponse.status}`);
}
}
return {
setAcl, addToPrivateTypeIndex, findChat, createChatThing, getChat, sendInvite, mintNew
};
}
//# sourceMappingURL=chatLogic.js.map