-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathldflexService.js
159 lines (140 loc) · 4.45 KB
/
ldflexService.js
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
import { namedNode } from "@rdfjs/data-model";
import data from "@solid/query-ldflex";
import auth from "solid-auth-client";
import Cache from "./Cache";
const cache = new Cache();
const getSession = async () => {
let session = await auth.currentSession(localStorage);
return session;
};
export const getProfileName = async () => {
const webId = (await getSession()).webId;
let me = data[webId];
return `${await me.vcard_fn}`; // Fullname
};
export const getWebId = async () => {
let session = await getSession();
let webId = session.webId;
return webId;
};
export const getProfileImage = async () => {
let session = await getSession();
let webId = session.webId;
let me = await data[webId];
let pic = `${await me["vcard:hasPhoto"]}`;
let img = `${pic}`;
return img;
};
export const getFriends = async (webId) => {
const me = data[webId];
let returnFriends = [];
for await (const name of me.friends) {
if (cache.contains(`${name}`)) {
returnFriends.push(cache.get(`${name}`));
} else {
returnFriends.push(cache.add(`${name}`, await getFriendData(name)));
}
}
return returnFriends;
};
export const getProfileData = async () => {
// TODO this could be refactor to do not have to await for each of the individual promises
const webId = (await getSession()).webId;
let userData = {};
let me = data[webId];
userData.fn = `${await me.vcard_fn}`; // Fullname
userData.url = `${await me["solid:account"]}`.concat("profile/card#");
userData.image = `${await me["vcard:hasPhoto"]}`;
userData.emails = [];
userData.phones = [];
for await (const email of me["vcard:hasEmail"]) {
let mail = data[email];
let value = await mail["vcard:value"];
value = `${value}`;
if (value !== undefined) {
userData.emails.push(value.split(":")[1]);
}
}
for await (const phone of me["vcard:hasTelephone"]) {
let pho = data[phone];
let value = await pho["vcard:value"];
value = `${value}`;
if (value !== undefined) {
userData.phones.push(value.split(":")[1]);
}
}
userData.company = `${await me["vcard:organization-name"]}`;
userData.role = `${await me["vcard:role"]}`;
userData.note = `${await me["vcard:note"]}`;
return userData;
};
export const updateProfileData = async (userData) => {
const webId = (await getSession()).webId;
let me = await data[webId];
if (userData.fn) {
await me.vcard_fn.set(userData.fn);
}
let emailIndex = 0;
for await (const emailId of me["vcard:hasEmail"]) {
await data[`${emailId}`].vcard$value.set(
namedNode(`mailto:${userData.emails[emailIndex]}`)
);
emailIndex++;
}
let phoneIndex = 0;
for await (const phoneId of me["vcard:hasTelephone"]) {
await data[`${phoneId}`].vcard$value.set(
namedNode(`tel:${userData.phones[phoneIndex]}`)
);
phoneIndex++;
}
if (userData.company)
await me["vcard:organization-name"].set(userData.company);
if (userData.role) await me["vcard:role"].set(userData.role);
};
export const saveNote = async (noteValue) => {
const webId = (await getSession()).webId;
let me = await data[webId];
await me["vcard:note"].set(noteValue);
};
export const getFriendData = async (webId) => {
let friendData = {};
let friend = data[webId];
friendData.fn = await friend.vcard_fn;
friendData.url = `${await friend["solid:account"]}`.concat("profile/card#");
friendData.image = `${await friend["vcard:hasPhoto"]}`;
for await (const email of friend["vcard:hasEmail"]) {
let mail = data[email];
let value = await mail["vcard:value"];
value = `${value}`;
if (value !== undefined) {
friendData.email = value.split(":")[1];
break;
}
}
for await (const phone of friend["vcard:hasTelephone"]) {
let pho = data[phone];
let value = await pho["vcard:value"];
value = `${value}`;
if (value !== undefined) {
friendData.phone = value.split(":")[1];
break;
}
}
friendData.company = `${await friend["vcard:organization-name"]}`;
friendData.role = `${await friend["vcard:role"]}`;
return friendData;
};
export const addFriend = async (friendId) => {
let me = data[await getWebId()];
let friend = data[friendId];
await me.friends.add(friend);
cache.add(friendId, await getFriendData(friendId));
};
export const removeFriend = async (friendId) => {
let me = data[await getWebId()];
let friend = data[friendId.concat("me")];
let friends = me["foaf:knows"];
await friends.delete(friend);
cache.remove(friendId);
};