Skip to content

Commit 5d7883b

Browse files
committed
contacts: query group members
1 parent 6601707 commit 5d7883b

2 files changed

Lines changed: 109 additions & 1 deletion

File tree

contacts/src/rdflib/queries/GroupQuery.spec.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { graph, lit, sym } from "rdflib";
22
import { GroupQuery } from "./GroupQuery";
33
import { rdf, vcard } from "../namespaces";
4+
import { AddressBookQuery } from "./AddressBookQuery";
5+
import * as querystring from "querystring";
46

57
describe(GroupQuery.name, () => {
68
describe("query name", () => {
@@ -63,4 +65,100 @@ describe(GroupQuery.name, () => {
6365
expect(result).toEqual("");
6466
});
6567
});
68+
describe("query members", () => {
69+
it("returns an empty array if nothing found in store", () => {
70+
const query = new GroupQuery(
71+
graph(),
72+
sym("http://pod.test/alice/contacts/1/group/1/index.ttl#this"),
73+
);
74+
const result = query.queryMembers();
75+
expect(result).toEqual([]);
76+
});
77+
it("returns a single contact with name and URI", () => {
78+
const store = graph();
79+
const groupNode = sym(
80+
"http://pod.test/alice/contacts/1/group/1/index.ttl#this",
81+
);
82+
store.add(
83+
groupNode,
84+
vcard("hasMember"),
85+
sym("http://pod.test/alice/contacts/Person/1#this"),
86+
groupNode.doc(),
87+
);
88+
store.add(
89+
sym("http://pod.test/alice/contacts/Person/1#this"),
90+
vcard("fn"),
91+
lit("Bob"),
92+
groupNode.doc(),
93+
);
94+
const query = new GroupQuery(store, groupNode);
95+
const result = query.queryMembers();
96+
expect(result).toEqual([
97+
{
98+
uri: "http://pod.test/alice/contacts/Person/1#this",
99+
name: "Bob",
100+
},
101+
]);
102+
});
103+
104+
it("returns all contacts with name and URI", () => {
105+
const store = graph();
106+
const groupNode = sym(
107+
"http://pod.test/alice/contacts/1/group/1/index.ttl#this",
108+
);
109+
110+
store.add(
111+
groupNode,
112+
vcard("hasMember"),
113+
sym("http://pod.test/alice/contacts/Person/1#this"),
114+
groupNode.doc(),
115+
);
116+
store.add(
117+
sym("http://pod.test/alice/contacts/Person/1#this"),
118+
vcard("fn"),
119+
lit("Alice"),
120+
groupNode.doc(),
121+
);
122+
store.add(
123+
groupNode,
124+
vcard("hasMember"),
125+
sym("http://pod.test/alice/contacts/Person/2#this"),
126+
groupNode.doc(),
127+
);
128+
store.add(
129+
sym("http://pod.test/alice/contacts/Person/2#this"),
130+
vcard("fn"),
131+
lit("Bob"),
132+
groupNode.doc(),
133+
);
134+
135+
const query = new GroupQuery(
136+
store,
137+
sym("http://pod.test/alice/contacts/1/group/1/index.ttl#this"),
138+
);
139+
const result = query.queryMembers();
140+
expect(result).toEqual([
141+
{
142+
uri: "http://pod.test/alice/contacts/Person/1#this",
143+
name: "Alice",
144+
},
145+
{
146+
uri: "http://pod.test/alice/contacts/Person/2#this",
147+
name: "Bob",
148+
},
149+
]);
150+
});
151+
152+
it("ignores members that are not named nodes", () => {
153+
const store = graph();
154+
const groupNode = sym(
155+
"http://pod.test/alice/contacts/1/group/1/index.ttl#this",
156+
);
157+
store.add(groupNode, vcard("hasMember"), lit("Bob"), groupNode.doc());
158+
159+
const query = new GroupQuery(store, groupNode);
160+
const result = query.queryMembers();
161+
expect(result).toEqual([]);
162+
});
163+
});
66164
});

contacts/src/rdflib/queries/GroupQuery.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IndexedFormula, NamedNode } from "rdflib";
1+
import { IndexedFormula, isNamedNode, NamedNode } from "rdflib";
22
import { vcard } from "../namespaces";
33

44
export class GroupQuery {
@@ -20,4 +20,14 @@ export class GroupQuery {
2020
) ?? ""
2121
);
2222
}
23+
24+
queryMembers() {
25+
return this.store
26+
.each(this.groupNode, vcard("hasMember"))
27+
.filter((it): it is NamedNode => isNamedNode(it))
28+
.map((node) => ({
29+
uri: node.value,
30+
name: this.store.anyValue(node, vcard("fn")),
31+
}));
32+
}
2333
}

0 commit comments

Comments
 (0)