Skip to content

Commit 7b8c254

Browse files
authored
Merge pull request #45 from ewingson/try_node_logic
change parameter type from String to NamedNode
2 parents c4f865c + 36488eb commit 7b8c254

9 files changed

Lines changed: 584 additions & 170 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: 530 additions & 120 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
},
2626
"homepage": "https://github.com/solidos/solid-logic#readme",
2727
"devDependencies": {
28+
"@babel/core": "^7.17.10",
2829
"@babel/plugin-proposal-class-properties": "~7.16.7",
2930
"@babel/plugin-proposal-optional-chaining": "~7.16.7",
3031
"@babel/plugin-transform-async-to-generator": "~7.16.8",
@@ -34,6 +35,7 @@
3435
"@types/jest": "^27.4.1",
3536
"@typescript-eslint/eslint-plugin": "^5.19.0",
3637
"@typescript-eslint/parser": "^5.19.0",
38+
"babel-jest": "^28.0.3",
3739
"eslint": "^8.13.0",
3840
"jest": "^27.5.1",
3941
"jest-environment-node-debug": "^2.0.0",

src/inbox/inboxLogic.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function createInboxLogic(store, profileLogic, utilityLogic, containerLog
99
const podRoot: NamedNode = await profileLogic.getPodRoot(myWebId);
1010
const ourInbox = `${podRoot.value}p2p-inboxes/${encodeURIComponent(nick)}/`;
1111
await containerLogic.createContainer(ourInbox);
12-
const aclDocUrl = await aclLogic.findAclDocUrl(sym(ourInbox));
12+
const aclDocUrl = await aclLogic.findAclDocUrl(sym(ourInbox))
1313
await utilityLogic.setSinglePeerAccess({
1414
ownerWebId: myWebId.value,
1515
peerWebId,
@@ -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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ 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(sym(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(sym(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,15 +21,16 @@ 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("getContainerMembers- When container is empty - Resolves to an empty array", async () => {
3132
containerIsEmpty();
32-
const result = await containerLogic.getContainerMembers('https://container.com/');
33+
const result = await containerLogic.getContainerMembers(sym('https://container.com/'));
3334
expect(result).toEqual([]);
3435
});
3536

@@ -45,7 +46,7 @@ describe("Container", () => {
4546

4647
function containerHasSomeContainmentTriples() {
4748
fetchMock.mockOnceIf(
48-
"https://com/",
49+
"https://container.com/",
4950
"<.> <http://www.w3.org/ns/ldp#contains> <./foo.txt>, <./bar/> .",
5051
{
5152
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)