Skip to content

Commit d1f8803

Browse files
authored
Merge pull request #58 from SolidOS/stringUrl
Refactoring String url into NamedNode
2 parents f20ef74 + ab00e7b commit d1f8803

9 files changed

Lines changed: 793 additions & 1023 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ Core business logic of SolidOS
33

44
# Adendum
55

6-
Solid-logic was a move to sparate business logic from UI functionality so that people using different UI frameworks could use logic code.
6+
Solid-logic was a move to separate business logic from UI functionality so that people using different UI frameworks could use logic code.
77

8-
It was created when the "chat with me" feature was built. We needed shared logic between chat-pane and profile-pane (which was part of solid-panes back then I think) due to that feature. The whole idea of it is to separate core solid logic from UI components, to make logic reusable, e.g. by other UI libraries or even non web apps like CLI tools etc.
8+
It was created when the "chat with me" feature was built. We needed to share logic between chat-pane and profile-pane (which was part of solid-panes back then I think) due to that feature. The whole idea of it is to separate core solid logic from UI components, to make logic reusable, e.g. by other UI libraries or even non web apps like CLI tools etc.

package-lock.json

Lines changed: 736 additions & 967 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/acl/aclLogic.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ export function createAclLogic(store): AclLogic {
1111

1212
const ns = namespace
1313

14-
async function findAclDocUrl(url: string) {
15-
const doc = store.sym(url);
16-
await store.fetcher.load(doc);
17-
const docNode = store.any(doc, ACL_LINK);
14+
async function findAclDocUrl(url: NamedNode) {
15+
await store.fetcher.load(url);
16+
const docNode = store.any(url, ACL_LINK);
1817
if (!docNode) {
1918
throw new Error(`No ACL link discovered for ${url}`);
2019
}

src/inbox/inboxLogic.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NamedNode } from "rdflib";
1+
import { NamedNode, sym } from "rdflib";
22
import { InboxLogic } from "../types";
33
import { getArchiveUrl } from "../util/utils";
44

@@ -21,12 +21,12 @@ export function createInboxLogic(store, profileLogic, utilityLogic, containerLog
2121

2222
async function getNewMessages(
2323
user?: NamedNode
24-
): Promise<string[]> {
24+
): Promise<NamedNode[]> {
2525
if (!user) {
2626
user = await profileLogic.loadMe();
2727
}
2828
const inbox = await profileLogic.getMainInbox(user);
29-
const urls = await containerLogic.getContainerMembers(inbox.value);
29+
const urls = await containerLogic.getContainerMembers(inbox);
3030
return urls.filter(url => !containerLogic.isContainer(url));
3131
}
3232

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export interface ProfileLogic {
7272
}
7373

7474
export interface AclLogic {
75-
findAclDocUrl: (url: string) => Promise<any>,
75+
findAclDocUrl: (url: NamedNode) => Promise<any>,
7676
setACLUserPublic: (docURI: string, me: NamedNode,
7777
options: {
7878
defaultForNew?: boolean,
@@ -89,7 +89,7 @@ export interface AclLogic {
8989

9090
export interface InboxLogic {
9191
createInboxFor: (peerWebId: string, nick: string) => Promise<string>,
92-
getNewMessages: (user?: NamedNode) => Promise<string[]>,
92+
getNewMessages: (user?: NamedNode) => Promise<NamedNode[]>,
9393
markAsRead: (url: string, date: Date) => void
9494
}
9595

src/util/containerLogic.ts

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,43 @@ import { NamedNode, Statement, sym } from "rdflib";
66
export function createContainerLogic(store) {
77

88
function getContainerElements(containerNode: NamedNode): NamedNode[] {
9-
return store
10-
.statementsMatching(
11-
containerNode,
12-
sym("http://www.w3.org/ns/ldp#contains"),
13-
undefined,
14-
containerNode.doc()
15-
)
16-
.map((st: Statement) => st.object as NamedNode);
9+
return store
10+
.statementsMatching(
11+
containerNode,
12+
sym("http://www.w3.org/ns/ldp#contains"),
13+
undefined
14+
)
15+
.map((st: Statement) => st.object as NamedNode);
1716
}
1817

19-
function isContainer(url: string) {
20-
return url.charAt(url.length - 1) === "/";
18+
function isContainer(url: NamedNode) {
19+
const nodeToString = url.value;
20+
return nodeToString.charAt(nodeToString.length - 1) === "/";
2121
}
2222

2323
async function createContainer(url: string) {
24-
if (!isContainer(url)) {
25-
throw new Error(`Not a container URL ${url}`);
26-
}
27-
// Copied from https://github.com/solidos/solid-crud-tests/blob/v3.1.0/test/surface/create-container.test.ts#L56-L64
28-
const result = await store.fetcher._fetch(url, {
29-
method: "PUT",
30-
headers: {
31-
"Content-Type": "text/turtle",
32-
"If-None-Match": "*",
33-
Link: '<http://www.w3.org/ns/ldp#BasicContainer>; rel="type"', // See https://github.com/solidos/node-solid-server/issues/1465
34-
},
35-
body: " ", // work around https://github.com/michielbdejong/community-server/issues/4#issuecomment-776222863
36-
});
37-
if (result.status.toString()[0] !== '2') {
38-
throw new Error(`Not OK: got ${result.status} response while creating container at ${url}`);
39-
}
24+
const stringToNode = sym(url);
25+
if (!isContainer(stringToNode)) {
26+
throw new Error(`Not a container URL ${url}`);
27+
}
28+
// Copied from https://github.com/solidos/solid-crud-tests/blob/v3.1.0/test/surface/create-container.test.ts#L56-L64
29+
const result = await store.fetcher._fetch(url, {
30+
method: "PUT",
31+
headers: {
32+
"Content-Type": "text/turtle",
33+
"If-None-Match": "*",
34+
Link: '<http://www.w3.org/ns/ldp#BasicContainer>; rel="type"', // See https://github.com/solidos/node-solid-server/issues/1465
35+
},
36+
body: " ", // work around https://github.com/michielbdejong/community-server/issues/4#issuecomment-776222863
37+
});
38+
if (result.status.toString()[0] !== '2') {
39+
throw new Error(`Not OK: got ${result.status} response while creating container at ${url}`);
40+
}
4041
}
4142

42-
async function getContainerMembers(containerUrl: string): Promise<string[]> {
43-
const containerNode = store.sym(containerUrl);
44-
await store.fetcher.load(containerNode);
45-
const nodes = getContainerElements(containerNode);
46-
return nodes.map(node => node.value);
43+
async function getContainerMembers(containerUrl: NamedNode): Promise<NamedNode[]> {
44+
await store.fetcher.load(containerUrl);
45+
return getContainerElements(containerUrl);
4746
}
4847
return {
4948
isContainer,

src/util/utilityLogic.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
import { NamedNode, st } from "rdflib";
1+
import { NamedNode, st, sym } from "rdflib";
22
import { CrossOriginForbiddenError, FetchError, NotEditableError, SameOriginForbiddenError, UnauthorizedError, WebOperationError } from "../logic/CustomError";
33
import * as debug from '../util/debug';
44
import { differentOrigin } from "./utils";
55

66
export function createUtilityLogic(store, aclLogic, containerLogic) {
77

8-
async function recursiveDelete(url: string) {
8+
async function recursiveDelete(containerNode: NamedNode) {
99
try {
10-
if (containerLogic.isContainer(url)) {
11-
const aclDocUrl = await aclLogic.findAclDocUrl(url);
10+
if (containerLogic.isContainer(containerNode)) {
11+
const aclDocUrl = await aclLogic.findAclDocUrl(containerNode)
1212
await store.fetcher._fetch(aclDocUrl, { method: "DELETE" });
13-
const containerMembers = await containerLogic.getContainerMembers(url);
13+
const containerMembers = await containerLogic.getContainerMembers(containerNode);
1414
await Promise.all(
1515
containerMembers.map((url) => recursiveDelete(url))
1616
);
1717
}
18-
return store.fetcher._fetch(url, { method: "DELETE" });
18+
const nodeToStringHere = containerNode.value;
19+
return store.fetcher._fetch(nodeToStringHere, { method: "DELETE" });
1920
} catch (e) {
2021
// debug.log(`Please manually remove ${url} from your system under test.`, e);
2122
}
@@ -126,7 +127,7 @@ export function createUtilityLogic(store, aclLogic, containerLogic) {
126127
''
127128
].join('\n')
128129
}
129-
const aclDocUrl = await aclLogic.findAclDocUrl(options.target);
130+
const aclDocUrl = await aclLogic.findAclDocUrl(sym(options.target))
130131
return store.fetcher._fetch(aclDocUrl, {
131132
method: 'PUT',
132133
body: str,

test/container.test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @jest-environment jsdom
33
*
44
*/
5-
import { UpdateManager, Store, Fetcher } from "rdflib";
5+
import { UpdateManager, Store, Fetcher, sym } from "rdflib";
66
import { createContainerLogic } from "../src/util/containerLogic";
77
import { alice } from "./helpers/dataSetup";
88

@@ -21,16 +21,17 @@ describe("Container", () => {
2121

2222
it("getContainerMembers - When container has some containment triples", async () => {
2323
containerHasSomeContainmentTriples()
24-
const result = await containerLogic.getContainerMembers('https://com/');
24+
const containerMembers = await containerLogic.getContainerMembers(sym('https://container.com/'));
25+
const result = containerMembers.map(oneResult => oneResult.value)
2526
expect(result.sort()).toEqual([
26-
'https://com/foo.txt',
27-
'https://com/bar/'
27+
'https://container.com/foo.txt',
28+
'https://container.com/bar/'
2829
].sort());
2930
});
3031
it.skip("getContainerMembers- When container is empty - Resolves to an empty array", async () => {
3132
jest.setTimeout(2000)
3233
containerIsEmpty();
33-
const result = await containerLogic.getContainerMembers('https://container.com/');
34+
const result = await containerLogic.getContainerMembers(sym('https://container.com/'));
3435
expect(result).toEqual([]);
3536
});
3637

@@ -46,7 +47,7 @@ describe("Container", () => {
4647

4748
function containerHasSomeContainmentTriples() {
4849
fetchMock.mockOnceIf(
49-
"https://com/",
50+
"https://container.com/",
5051
"<.> <http://www.w3.org/ns/ldp#contains> <./foo.txt>, <./bar/> .",
5152
{
5253
headers: { "Content-Type": "text/turtle" },

test/inboxLogic.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ describe("Inbox logic", () => {
5252
beforeEach(async () => {
5353
bobHasAnInbox();
5454
inboxHasSomeContainmentTriples();
55-
result = await inboxLogic.getNewMessages(bob);
55+
const messages = await inboxLogic.getNewMessages(bob);
56+
result = messages.map(oneMessage => oneMessage.value)
5657
});
5758
it("Resolves to an array with URLs of non-container resources in inbox", () => {
5859
expect(result.sort()).toEqual([

0 commit comments

Comments
 (0)