forked from SolidOS/solid-logic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofileLogic.js
More file actions
128 lines (128 loc) · 4.43 KB
/
Copy pathprofileLogic.js
File metadata and controls
128 lines (128 loc) · 4.43 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
import { CrossOriginForbiddenError, FetchError, NotEditableError, SameOriginForbiddenError, UnauthorizedError, WebOperationError } from '../logic/CustomError';
import * as debug from '../util/debug';
import { ns as namespace } from '../util/ns';
import { differentOrigin, suggestPreferencesFile } from '../util/utils';
export function createProfileLogic(store, authn, utilityLogic) {
const ns = namespace;
/**
* loads the preference without throwing errors - if it can create it it does so.
* remark: it still throws error if it cannot load profile.
* @param user
* @returns undefined if preferenceFile cannot be returned or NamedNode if it can find it or create it
*/
async function silencedLoadPreferences(user) {
try {
return await loadPreferences(user);
}
catch (err) {
return undefined;
}
}
/**
* loads the preference without returning different errors if it cannot create or load it.
* remark: it also throws error if it cannot load profile.
* @param user
* @returns undefined if preferenceFile cannot be an Error or NamedNode if it can find it or create it
*/
async function loadPreferences(user) {
await loadProfile(user);
const possiblePreferencesFile = suggestPreferencesFile(user);
let preferencesFile;
try {
preferencesFile = await utilityLogic.followOrCreateLink(user, ns.space('preferencesFile'), possiblePreferencesFile, user.doc());
}
catch (err) {
const message = `User ${user} has no pointer in profile to preferences file.`;
debug.warn(message);
// we are listing the possible errors
if (err instanceof NotEditableError) {
throw err;
}
if (err instanceof WebOperationError) {
throw err;
}
if (err instanceof UnauthorizedError) {
throw err;
}
if (err instanceof CrossOriginForbiddenError) {
throw err;
}
if (err instanceof SameOriginForbiddenError) {
throw err;
}
if (err instanceof FetchError) {
throw err;
}
throw err;
}
try {
await store.fetcher.load(preferencesFile);
}
catch (err) { // Maybe a permission problem or origin problem
const msg = `Unable to load preference of user ${user}: ${err}`;
debug.warn(msg);
if (err.response.status === 401) {
throw new UnauthorizedError();
}
if (err.response.status === 403) {
if (differentOrigin(preferencesFile)) {
throw new CrossOriginForbiddenError();
}
throw new SameOriginForbiddenError();
}
/*if (err.response.status === 404) {
throw new NotFoundError();
}*/
throw new Error(msg);
}
return preferencesFile;
}
async function loadProfile(user) {
if (!user) {
throw new Error('loadProfile: no user given.');
}
try {
await store.fetcher.load(user.doc());
}
catch (err) {
throw new Error(`Unable to load profile of user ${user}: ${err}`);
}
return user.doc();
}
async function loadMe() {
const me = authn.currentUser();
if (me === null) {
throw new Error('Current user not found! Not logged in?');
}
await store.fetcher.load(me.doc());
return me;
}
function getPodRoot(user) {
const podRoot = findStorage(user);
if (!podRoot) {
throw new Error('User pod root not found!');
}
return podRoot;
}
async function getMainInbox(user) {
await store.fetcher.load(user);
const mainInbox = store.any(user, ns.ldp('inbox'), undefined, user.doc());
if (!mainInbox) {
throw new Error('User main inbox not found!');
}
return mainInbox;
}
function findStorage(me) {
return store.any(me, ns.space('storage'), undefined, me.doc());
}
return {
loadMe,
getPodRoot,
getMainInbox,
findStorage,
loadPreferences,
loadProfile,
silencedLoadPreferences
};
}
//# sourceMappingURL=profileLogic.js.map