@coasys/ad4m 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. package/README.md +229 -0
  2. package/jest-setup.ts +1 -0
  3. package/jest.config.js +6 -0
  4. package/jsdoc.json +28 -0
  5. package/lib/index.cjs +8144 -0
  6. package/lib/shims/reflect-metadata.d.ts +0 -0
  7. package/lib/shims/type-graphql.d.ts +32 -0
  8. package/lib/src/Ad4mClient.d.ts +26 -0
  9. package/lib/src/Address.d.ts +1 -0
  10. package/lib/src/DID.d.ts +1 -0
  11. package/lib/src/Exception.d.ts +6 -0
  12. package/lib/src/Literal.d.ts +7 -0
  13. package/lib/src/PubSub.d.ts +14 -0
  14. package/lib/src/SmartLiteral.d.ts +12 -0
  15. package/lib/src/agent/Agent.d.ts +104 -0
  16. package/lib/src/agent/AgentClient.d.ts +57 -0
  17. package/lib/src/agent/AgentResolver.d.ts +30 -0
  18. package/lib/src/agent/AgentStatus.d.ts +8 -0
  19. package/lib/src/buildSchema.d.ts +1 -0
  20. package/lib/src/expression/Expression.d.ts +28 -0
  21. package/lib/src/expression/ExpressionClient.d.ts +13 -0
  22. package/lib/src/expression/ExpressionRef.d.ts +9 -0
  23. package/lib/src/expression/ExpressionResolver.d.ts +10 -0
  24. package/lib/src/index.d.ts +30 -0
  25. package/lib/src/jestSetup.d.ts +1 -0
  26. package/lib/src/language/Icon.d.ts +4 -0
  27. package/lib/src/language/Language.d.ts +195 -0
  28. package/lib/src/language/LanguageClient.d.ts +17 -0
  29. package/lib/src/language/LanguageContext.d.ts +37 -0
  30. package/lib/src/language/LanguageHandle.d.ts +9 -0
  31. package/lib/src/language/LanguageMeta.d.ts +35 -0
  32. package/lib/src/language/LanguageRef.d.ts +6 -0
  33. package/lib/src/language/LanguageResolver.d.ts +19 -0
  34. package/lib/src/links/Links.d.ts +39 -0
  35. package/lib/src/links/Links.test.d.ts +1 -0
  36. package/lib/src/neighbourhood/Neighbourhood.d.ts +10 -0
  37. package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +25 -0
  38. package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +19 -0
  39. package/lib/src/neighbourhood/NeighbourhoodResolver.d.ts +24 -0
  40. package/lib/src/perspectives/LinkQuery.d.ts +11 -0
  41. package/lib/src/perspectives/Perspective.d.ts +38 -0
  42. package/lib/src/perspectives/Perspective.test.d.ts +1 -0
  43. package/lib/src/perspectives/PerspectiveClient.d.ts +54 -0
  44. package/lib/src/perspectives/PerspectiveDiff.d.ts +9 -0
  45. package/lib/src/perspectives/PerspectiveHandle.d.ts +16 -0
  46. package/lib/src/perspectives/PerspectiveProxy.d.ts +178 -0
  47. package/lib/src/perspectives/PerspectiveResolver.d.ts +37 -0
  48. package/lib/src/runtime/RuntimeClient.d.ts +33 -0
  49. package/lib/src/runtime/RuntimeResolver.d.ts +47 -0
  50. package/lib/src/schema.gql +382 -0
  51. package/lib/src/subject/SDNADecorators.d.ts +45 -0
  52. package/lib/src/subject/Subject.d.ts +7 -0
  53. package/lib/src/subject/SubjectEntity.d.ts +31 -0
  54. package/lib/src/subject/util.d.ts +12 -0
  55. package/lib/src/typeDefs.d.ts +1 -0
  56. package/lib/src/unwrapApolloResult.d.ts +2 -0
  57. package/lib/src/utils.d.ts +2 -0
  58. package/package.json +60 -0
  59. package/rollup.config.js +27 -0
  60. package/shims/reflect-metadata.ts +0 -0
  61. package/shims/type-graphql.ts +75 -0
  62. package/src/Ad4mClient.test.ts +1085 -0
  63. package/src/Ad4mClient.ts +63 -0
  64. package/src/Address.ts +1 -0
  65. package/src/DID.ts +1 -0
  66. package/src/Exception.ts +6 -0
  67. package/src/Literal.test.ts +31 -0
  68. package/src/Literal.ts +81 -0
  69. package/src/PubSub.ts +14 -0
  70. package/src/SmartLiteral.ts +75 -0
  71. package/src/agent/Agent.ts +293 -0
  72. package/src/agent/AgentClient.ts +531 -0
  73. package/src/agent/AgentResolver.ts +263 -0
  74. package/src/agent/AgentStatus.ts +44 -0
  75. package/src/buildSchema.ts +41 -0
  76. package/src/expression/Expression.ts +99 -0
  77. package/src/expression/ExpressionClient.ts +99 -0
  78. package/src/expression/ExpressionRef.ts +63 -0
  79. package/src/expression/ExpressionResolver.ts +64 -0
  80. package/src/index.ts +30 -0
  81. package/src/jestSetup.ts +1 -0
  82. package/src/language/Icon.ts +11 -0
  83. package/src/language/Language.ts +266 -0
  84. package/src/language/LanguageClient.ts +160 -0
  85. package/src/language/LanguageContext.ts +37 -0
  86. package/src/language/LanguageHandle.ts +23 -0
  87. package/src/language/LanguageMeta.ts +70 -0
  88. package/src/language/LanguageRef.ts +18 -0
  89. package/src/language/LanguageResolver.ts +98 -0
  90. package/src/links/Links.test.ts +40 -0
  91. package/src/links/Links.ts +107 -0
  92. package/src/neighbourhood/Neighbourhood.ts +22 -0
  93. package/src/neighbourhood/NeighbourhoodClient.ts +276 -0
  94. package/src/neighbourhood/NeighbourhoodProxy.ts +58 -0
  95. package/src/neighbourhood/NeighbourhoodResolver.ts +100 -0
  96. package/src/perspectives/LinkQuery.ts +66 -0
  97. package/src/perspectives/Perspective.test.ts +23 -0
  98. package/src/perspectives/Perspective.ts +118 -0
  99. package/src/perspectives/PerspectiveClient.ts +444 -0
  100. package/src/perspectives/PerspectiveDiff.ts +15 -0
  101. package/src/perspectives/PerspectiveHandle.ts +39 -0
  102. package/src/perspectives/PerspectiveProxy.ts +611 -0
  103. package/src/perspectives/PerspectiveResolver.ts +220 -0
  104. package/src/runtime/RuntimeClient.ts +283 -0
  105. package/src/runtime/RuntimeResolver.ts +193 -0
  106. package/src/subject/SDNADecorators.ts +334 -0
  107. package/src/subject/Subject.ts +153 -0
  108. package/src/subject/SubjectEntity.ts +292 -0
  109. package/src/subject/util.ts +85 -0
  110. package/src/typeDefs.ts +6 -0
  111. package/src/unwrapApolloResult.ts +14 -0
  112. package/src/utils.ts +25 -0
  113. package/tsconfig-browser.json +82 -0
  114. package/tsconfig.json +80 -0
@@ -0,0 +1,40 @@
1
+ import { LinkQuery } from '../perspectives/LinkQuery'
2
+
3
+ const query = new LinkQuery({
4
+ source: 'a'
5
+ })
6
+
7
+ describe('LinkQuery', () => {
8
+ it('can match positively', () => {
9
+ expect(query.isMatch({
10
+ source: 'a',
11
+ predicate: 'b',
12
+ target: 'c'
13
+ })).toBe(true)
14
+
15
+ expect(query.isMatch({
16
+ source: 'a',
17
+ target: 'c'
18
+ })).toBe(true)
19
+
20
+ expect(query.isMatch({
21
+ source: 'a',
22
+ predicate: 'b',
23
+ target: 'c'
24
+ })).toBe(true)
25
+ })
26
+
27
+ it('can match negatively', () => {
28
+ expect(query.isMatch({
29
+ source: 'c',
30
+ predicate: 'b',
31
+ target: 'c'
32
+ })).toBe(false)
33
+
34
+ expect(query.isMatch({
35
+ source: 'c',
36
+ target: 'b'
37
+ })).toBe(false)
38
+
39
+ })
40
+ })
@@ -0,0 +1,107 @@
1
+ import { Field, InputType, ObjectType } from "type-graphql";
2
+ import { ExpressionGeneric, ExpressionGenericInput } from '../expression/Expression';
3
+ import { LinkStatus } from "../perspectives/PerspectiveProxy";
4
+
5
+ @ObjectType()
6
+ export class Link {
7
+ @Field()
8
+ source: string;
9
+
10
+ @Field()
11
+ target: string;
12
+
13
+ @Field({nullable: true})
14
+ predicate?: string;
15
+
16
+ constructor(obj) {
17
+ this.source = obj.source ? obj.source : ''
18
+ this.target = obj.target ? obj.target : ''
19
+ this.predicate = obj.predicate ? obj.predicate : ''
20
+ }
21
+ }
22
+
23
+ @InputType()
24
+ export class LinkMutations {
25
+ @Field(type => [LinkInput])
26
+ additions: LinkInput[];
27
+
28
+ @Field(type => [LinkExpressionInput])
29
+ removals: LinkExpressionInput[];
30
+ }
31
+ @ObjectType()
32
+ export class LinkExpressionMutations {
33
+ @Field(type => [LinkExpression])
34
+ additions: LinkExpression[];
35
+
36
+ @Field(type => [LinkExpression])
37
+ removals: LinkExpression[];
38
+
39
+ constructor(additions: LinkExpression[], removals: LinkExpression[]) {
40
+ this.additions = additions
41
+ this.removals = removals
42
+ }
43
+ }
44
+
45
+ @InputType()
46
+ export class LinkInput {
47
+ @Field()
48
+ source: string;
49
+
50
+ @Field()
51
+ target: string;
52
+
53
+ @Field({nullable: true})
54
+ predicate?: string;
55
+ }
56
+
57
+ @ObjectType()
58
+ export class LinkExpression extends ExpressionGeneric(Link) {
59
+ hash(): number {
60
+ const mash = JSON.stringify(this.data, Object.keys(this.data).sort()) +
61
+ JSON.stringify(this.author) + this.timestamp
62
+ let hash = 0, i, chr;
63
+ for (i = 0; i < mash.length; i++) {
64
+ chr = mash.charCodeAt(i);
65
+ hash = ((hash << 5) - hash) + chr;
66
+ hash |= 0; // Convert to 32bit integer
67
+ }
68
+ return hash;
69
+ }
70
+
71
+ @Field({ nullable: true, defaultValue: 'shared' })
72
+ status?: LinkStatus;
73
+ };
74
+
75
+ @InputType()
76
+ export class LinkExpressionInput extends ExpressionGenericInput(LinkInput) {
77
+ hash: () => number;
78
+
79
+ @Field({ nullable: true, defaultValue: 'shared' })
80
+ status?: LinkStatus;
81
+ };
82
+
83
+ export function linkEqual(l1: LinkExpression, l2: LinkExpression): boolean {
84
+ return l1.author == l2.author &&
85
+ l1.timestamp == l2.timestamp &&
86
+ l1.data.source == l2.data.source &&
87
+ l1.data.predicate == l2.data.predicate &&
88
+ l1.data.target == l2.data.target
89
+ }
90
+
91
+ export function isLink(l: any): boolean {
92
+ return l && l.source && l.target
93
+ }
94
+
95
+ @ObjectType()
96
+ export class LinkExpressionUpdated {
97
+ @Field(type => LinkExpression)
98
+ oldLink: LinkExpression;
99
+
100
+ @Field(type => LinkExpression)
101
+ newLink: LinkExpression;
102
+
103
+ constructor(oldLink: LinkExpression, newLink: LinkExpression) {
104
+ this.oldLink = oldLink
105
+ this.newLink = newLink
106
+ }
107
+ }
@@ -0,0 +1,22 @@
1
+ import { Field, ObjectType } from "type-graphql";
2
+ import { ExpressionGeneric } from "../expression/Expression";
3
+ import { LanguageRef } from "../language/LanguageRef";
4
+ import { Perspective } from "../perspectives/Perspective";
5
+
6
+
7
+ @ObjectType()
8
+ export class Neighbourhood {
9
+ @Field()
10
+ linkLanguage: string
11
+
12
+ @Field()
13
+ meta: Perspective
14
+
15
+ constructor(linkLanguage: string, meta: Perspective) {
16
+ this.linkLanguage = linkLanguage;
17
+ this.meta = meta;
18
+ }
19
+ }
20
+
21
+ @ObjectType()
22
+ export class NeighbourhoodExpression extends ExpressionGeneric(Neighbourhood) {};
@@ -0,0 +1,276 @@
1
+ import { ApolloClient, ApolloQueryResult, gql } from "@apollo/client/core"
2
+ import { Address } from "../Address"
3
+ import { DID } from "../DID"
4
+ import { OnlineAgent, TelepresenceSignalCallback } from "../language/Language"
5
+ import { Perspective, PerspectiveUnsignedInput } from "../perspectives/Perspective"
6
+ import { PerspectiveHandle } from "../perspectives/PerspectiveHandle"
7
+ import unwrapApolloResult from "../unwrapApolloResult"
8
+ import { NeighbourhoodProxy } from "./NeighbourhoodProxy"
9
+
10
+ export class NeighbourhoodClient {
11
+ #apolloClient: ApolloClient<any>
12
+ #signalHandlers: Map<string, TelepresenceSignalCallback[]> = new Map()
13
+
14
+ constructor(client: ApolloClient<any>) {
15
+ this.#apolloClient = client
16
+ }
17
+
18
+ async publishFromPerspective(
19
+ perspectiveUUID: string,
20
+ linkLanguage: Address,
21
+ meta: Perspective
22
+ ): Promise<string> {
23
+ const { neighbourhoodPublishFromPerspective } = unwrapApolloResult(await this.#apolloClient.mutate({
24
+ mutation: gql`mutation neighbourhoodPublishFromPerspective(
25
+ $linkLanguage: String!,
26
+ $meta: PerspectiveInput!,
27
+ $perspectiveUUID: String!
28
+ ) {
29
+ neighbourhoodPublishFromPerspective(
30
+ linkLanguage: $linkLanguage,
31
+ meta: $meta,
32
+ perspectiveUUID: $perspectiveUUID
33
+ )
34
+ }`,
35
+ variables: { perspectiveUUID, linkLanguage, meta: meta}
36
+ }))
37
+ return neighbourhoodPublishFromPerspective
38
+ }
39
+
40
+ async joinFromUrl(url: string): Promise<PerspectiveHandle> {
41
+ const { neighbourhoodJoinFromUrl } = unwrapApolloResult(await this.#apolloClient.mutate({
42
+ mutation: gql`mutation neighbourhoodJoinFromUrl($url: String!) {
43
+ neighbourhoodJoinFromUrl(url: $url) {
44
+ uuid
45
+ name
46
+ sharedUrl
47
+ state
48
+ neighbourhood {
49
+ data {
50
+ linkLanguage
51
+ meta {
52
+ links
53
+ {
54
+ author
55
+ timestamp
56
+ data { source, predicate, target }
57
+ proof { valid, invalid, signature, key }
58
+ }
59
+ }
60
+ }
61
+ author
62
+ }
63
+ }
64
+ }`,
65
+ variables: { url }
66
+ }))
67
+ return neighbourhoodJoinFromUrl
68
+ }
69
+
70
+ async otherAgents(perspectiveUUID: string): Promise<DID[]> {
71
+ const { neighbourhoodOtherAgents } = unwrapApolloResult(await this.#apolloClient.query({
72
+ query: gql`query neighbourhoodOtherAgents($perspectiveUUID: String!) {
73
+ neighbourhoodOtherAgents(perspectiveUUID: $perspectiveUUID)
74
+ }`,
75
+ variables: { perspectiveUUID }
76
+ }))
77
+ return neighbourhoodOtherAgents
78
+ }
79
+
80
+ async hasTelepresenceAdapter(perspectiveUUID: string): Promise<boolean> {
81
+ const { neighbourhoodHasTelepresenceAdapter } = unwrapApolloResult(await this.#apolloClient.query({
82
+ query: gql`query neighbourhoodHasTelepresenceAdapter($perspectiveUUID: String!) {
83
+ neighbourhoodHasTelepresenceAdapter(perspectiveUUID: $perspectiveUUID)
84
+ }`,
85
+ variables: { perspectiveUUID }
86
+ }))
87
+ return neighbourhoodHasTelepresenceAdapter
88
+ }
89
+
90
+ async onlineAgents(perspectiveUUID: string): Promise<OnlineAgent[]> {
91
+ const { neighbourhoodOnlineAgents } = unwrapApolloResult(await this.#apolloClient.query({
92
+ query: gql`query neighbourhoodOnlineAgents($perspectiveUUID: String!) {
93
+ neighbourhoodOnlineAgents(perspectiveUUID: $perspectiveUUID) {
94
+ did
95
+ status {
96
+ author
97
+ timestamp
98
+ data {
99
+ links {
100
+ author
101
+ timestamp
102
+ data { source, predicate, target }
103
+ proof { valid, invalid, signature, key }
104
+ }
105
+ }
106
+ proof { valid, invalid, signature, key }
107
+ }
108
+ }
109
+ }`,
110
+ variables: { perspectiveUUID }
111
+ }))
112
+ return neighbourhoodOnlineAgents
113
+ }
114
+
115
+ async setOnlineStatus(perspectiveUUID: string, status: Perspective): Promise<boolean> {
116
+ const { neighbourhoodSetOnlineStatus } = unwrapApolloResult(await this.#apolloClient.mutate({
117
+ mutation: gql`mutation neighbourhoodSetOnlineStatus(
118
+ $perspectiveUUID: String!,
119
+ $status: PerspectiveInput!
120
+ ) {
121
+ neighbourhoodSetOnlineStatus(
122
+ perspectiveUUID: $perspectiveUUID,
123
+ status: $status
124
+ )
125
+ }`,
126
+ variables: { perspectiveUUID, status }
127
+ }))
128
+
129
+ return neighbourhoodSetOnlineStatus
130
+ }
131
+
132
+ async setOnlineStatusU(perspectiveUUID: string, status: PerspectiveUnsignedInput): Promise<boolean> {
133
+ const { neighbourhoodSetOnlineStatusU } = unwrapApolloResult(await this.#apolloClient.mutate({
134
+ mutation: gql`mutation neighbourhoodSetOnlineStatusU(
135
+ $perspectiveUUID: String!,
136
+ $status: PerspectiveUnsignedInput!
137
+ ) {
138
+ neighbourhoodSetOnlineStatusU(
139
+ perspectiveUUID: $perspectiveUUID,
140
+ status: $status
141
+ )
142
+ }`,
143
+ variables: { perspectiveUUID, status }
144
+ }))
145
+
146
+ return neighbourhoodSetOnlineStatusU
147
+ }
148
+
149
+ async sendSignal(perspectiveUUID: string, remoteAgentDid: string, payload: Perspective): Promise<boolean> {
150
+ const { neighbourhoodSendSignal } = unwrapApolloResult(await this.#apolloClient.mutate({
151
+ mutation: gql`mutation neighbourhoodSendSignal(
152
+ $perspectiveUUID: String!,
153
+ $remoteAgentDid: String!,
154
+ $payload: PerspectiveInput!
155
+ ) {
156
+ neighbourhoodSendSignal(
157
+ perspectiveUUID: $perspectiveUUID,
158
+ remoteAgentDid: $remoteAgentDid,
159
+ payload: $payload
160
+ )
161
+ }`,
162
+ variables: { perspectiveUUID, remoteAgentDid, payload }
163
+ }))
164
+
165
+ return neighbourhoodSendSignal
166
+ }
167
+
168
+ async sendSignalU(perspectiveUUID: string, remoteAgentDid: string, payload: PerspectiveUnsignedInput): Promise<boolean> {
169
+ const { neighbourhoodSendSignalU } = unwrapApolloResult(await this.#apolloClient.mutate({
170
+ mutation: gql`mutation neighbourhoodSendSignalU(
171
+ $perspectiveUUID: String!,
172
+ $remoteAgentDid: String!,
173
+ $payload: PerspectiveUnsignedInput!
174
+ ) {
175
+ neighbourhoodSendSignalU(
176
+ perspectiveUUID: $perspectiveUUID,
177
+ remoteAgentDid: $remoteAgentDid,
178
+ payload: $payload
179
+ )
180
+ }`,
181
+ variables: { perspectiveUUID, remoteAgentDid, payload }
182
+ }))
183
+
184
+ return neighbourhoodSendSignalU
185
+ }
186
+
187
+ async sendBroadcast(perspectiveUUID: string, payload: Perspective): Promise<boolean> {
188
+ const { neighbourhoodSendBroadcast } = unwrapApolloResult(await this.#apolloClient.mutate({
189
+ mutation: gql`mutation neighbourhoodSendBroadcast(
190
+ $perspectiveUUID: String!,
191
+ $payload: PerspectiveInput!
192
+ ) {
193
+ neighbourhoodSendBroadcast(
194
+ perspectiveUUID: $perspectiveUUID,
195
+ payload: $payload
196
+ )
197
+ }`,
198
+ variables: { perspectiveUUID, payload }
199
+ }))
200
+
201
+ return neighbourhoodSendBroadcast
202
+ }
203
+
204
+ async sendBroadcastU(perspectiveUUID: string, payload: PerspectiveUnsignedInput): Promise<boolean> {
205
+ const { neighbourhoodSendBroadcastU } = unwrapApolloResult(await this.#apolloClient.mutate({
206
+ mutation: gql`mutation neighbourhoodSendBroadcastU(
207
+ $perspectiveUUID: String!,
208
+ $payload: PerspectiveUnsignedInput!
209
+ ) {
210
+ neighbourhoodSendBroadcastU(
211
+ perspectiveUUID: $perspectiveUUID,
212
+ payload: $payload
213
+ )
214
+ }`,
215
+ variables: { perspectiveUUID, payload }
216
+ }))
217
+
218
+ return neighbourhoodSendBroadcastU
219
+ }
220
+
221
+ dispatchSignal(perspectiveUUID:string, signal: any) {
222
+ const handlers = this.#signalHandlers.get(perspectiveUUID)
223
+ if (handlers) {
224
+ handlers.forEach(handler => handler(signal))
225
+ }
226
+ }
227
+
228
+ async subscribeToSignals(perspectiveUUID: string): Promise<void> {
229
+ const that = this
230
+ await this.#apolloClient.subscribe({
231
+ query: gql`subscription neighbourhoodSignal($perspectiveUUID: String!) {
232
+ neighbourhoodSignal(perspectiveUUID: $perspectiveUUID) {
233
+ author
234
+ timestamp
235
+ data {
236
+ links
237
+ {
238
+ author
239
+ timestamp
240
+ data { source, predicate, target }
241
+ proof { valid, invalid, signature, key }
242
+ }
243
+ }
244
+ proof { valid, invalid, signature, key }
245
+ }
246
+ }`,
247
+ variables: { perspectiveUUID }
248
+ }).subscribe({
249
+ next: (result: ApolloQueryResult<any>) => {
250
+ const { neighbourhoodSignal } = unwrapApolloResult(result)
251
+ that.dispatchSignal(perspectiveUUID, neighbourhoodSignal)
252
+ }
253
+ })
254
+ }
255
+
256
+ async addSignalHandler(perspectiveUUID: string, handler: TelepresenceSignalCallback): Promise<void> {
257
+ let handlersForPerspective = this.#signalHandlers.get(perspectiveUUID)
258
+ if (!handlersForPerspective) {
259
+ handlersForPerspective = []
260
+ await this.subscribeToSignals(perspectiveUUID)
261
+ }
262
+ handlersForPerspective.push(handler)
263
+ this.#signalHandlers.set(perspectiveUUID, handlersForPerspective)
264
+ }
265
+
266
+ removeSignalHandler(perspectiveUUID: string, handler: TelepresenceSignalCallback): void {
267
+ const handlersForPerspective = this.#signalHandlers.get(perspectiveUUID)
268
+ if (handlersForPerspective) {
269
+ const index = handlersForPerspective.indexOf(handler)
270
+ if (index > -1) {
271
+ handlersForPerspective.splice(index, 1)
272
+ }
273
+ }
274
+ this.#signalHandlers.set(perspectiveUUID, handlersForPerspective)
275
+ }
276
+ }
@@ -0,0 +1,58 @@
1
+ import { DID } from "../DID";
2
+ import { OnlineAgent } from "../language/Language";
3
+ import { Perspective, PerspectiveExpression, PerspectiveUnsignedInput } from "../perspectives/Perspective";
4
+ import { NeighbourhoodClient } from "./NeighbourhoodClient";
5
+
6
+ export class NeighbourhoodProxy {
7
+ #client: NeighbourhoodClient
8
+ #pID: string
9
+
10
+ constructor(client: NeighbourhoodClient, pID: string) {
11
+ this.#client = client
12
+ this.#pID = pID
13
+ }
14
+
15
+ async otherAgents(): Promise<DID[]> {
16
+ return await this.#client.otherAgents(this.#pID)
17
+ }
18
+
19
+ async hasTelepresenceAdapter(): Promise<boolean> {
20
+ return await this.#client.hasTelepresenceAdapter(this.#pID)
21
+ }
22
+
23
+ async onlineAgents(): Promise<OnlineAgent[]> {
24
+ return await this.#client.onlineAgents(this.#pID)
25
+ }
26
+
27
+ async setOnlineStatus(status: Perspective): Promise<boolean> {
28
+ return await this.#client.setOnlineStatus(this.#pID, status)
29
+ }
30
+
31
+ async setOnlineStatusU(status: PerspectiveUnsignedInput): Promise<boolean> {
32
+ return await this.#client.setOnlineStatusU(this.#pID, status)
33
+ }
34
+
35
+ async sendSignal(remoteAgentDid: string, payload: Perspective): Promise<boolean> {
36
+ return await this.#client.sendSignal(this.#pID, remoteAgentDid, payload)
37
+ }
38
+
39
+ async sendSignalU(remoteAgentDid: string, payload: PerspectiveUnsignedInput): Promise<boolean> {
40
+ return await this.#client.sendSignalU(this.#pID, remoteAgentDid, payload)
41
+ }
42
+
43
+ async sendBroadcast(payload: Perspective): Promise<boolean> {
44
+ return await this.#client.sendBroadcast(this.#pID, payload)
45
+ }
46
+
47
+ async sendBroadcastU(payload: PerspectiveUnsignedInput): Promise<boolean> {
48
+ return await this.#client.sendBroadcastU(this.#pID, payload)
49
+ }
50
+
51
+ async addSignalHandler(handler: (payload: PerspectiveExpression) => void): Promise<void> {
52
+ await this.#client.addSignalHandler(this.#pID, handler)
53
+ }
54
+
55
+ removeSignalHandler(handler: (payload: PerspectiveExpression) => void) {
56
+ this.#client.removeSignalHandler(this.#pID, handler)
57
+ }
58
+ }
@@ -0,0 +1,100 @@
1
+ import { Arg, Mutation, Query, Resolver, Subscription, PubSub } from "type-graphql";
2
+ import { PerspectiveHandle, PerspectiveState } from "../perspectives/PerspectiveHandle";
3
+ import { Perspective, PerspectiveExpression, PerspectiveInput, PerspectiveUnsignedInput } from "../perspectives/Perspective";
4
+ import { DID } from "../DID";
5
+ import { TEST_AGENT_DID } from "../agent/AgentResolver";
6
+ import { OnlineAgent } from "../language/Language";
7
+ import { testLink } from "../perspectives/PerspectiveResolver";
8
+ import { NEIGHBOURHOOD_SIGNAL_RECEIVED_TOPIC, PERSPECTIVE_UPDATED_TOPIC } from "../PubSub";
9
+
10
+ const testPerspectiveExpression = new PerspectiveExpression()
11
+ testPerspectiveExpression.author = TEST_AGENT_DID
12
+ testPerspectiveExpression.timestamp = new Date().toISOString()
13
+ testPerspectiveExpression.data = new Perspective([testLink])
14
+ testPerspectiveExpression.proof = {
15
+ signature: '',
16
+ key: '',
17
+ valid: true
18
+ }
19
+
20
+ /**
21
+ * Resolver classes are used here to define the GraphQL schema
22
+ * (through the type-graphql annotations)
23
+ * and are spawned in the client tests in Ad4mClient.test.ts.
24
+ * For the latter, they return test fixtures.
25
+ */
26
+ @Resolver()
27
+ export default class NeighbourhoodResolver {
28
+ @Mutation(returns => String)
29
+ neighbourhoodPublishFromPerspective(
30
+ @Arg('perspectiveUUID') perspectiveUUID: string,
31
+ @Arg('linkLanguage') linkLanguage: string,
32
+ @Arg('meta') meta: PerspectiveInput
33
+ ): string {
34
+ return "neighbourhood://neighbourhoodAddress"
35
+ }
36
+
37
+ @Mutation(returns => PerspectiveHandle)
38
+ neighbourhoodJoinFromUrl(@Arg('url') url: string, @PubSub() pubSub: any): PerspectiveHandle {
39
+ const perspective = new PerspectiveHandle
40
+ perspective.name = "test-perspective"
41
+ perspective.sharedUrl = url
42
+ perspective.uuid = "234234234"
43
+ perspective.state = PerspectiveState.Synced
44
+ pubSub.publish(PERSPECTIVE_UPDATED_TOPIC, { perspective});
45
+ return perspective
46
+ }
47
+
48
+ @Query(returns => [String])
49
+ neighbourhoodOtherAgents(@Arg('perspectiveUUID') perspectiveUUID: string): DID[] {
50
+ return ['did:test:other']
51
+ }
52
+
53
+ @Query()
54
+ neighbourhoodHasTelepresenceAdapter(@Arg('perspectiveUUID') perspectiveUUID: string): boolean {
55
+ return true
56
+ }
57
+
58
+ @Query(returns => [OnlineAgent])
59
+ neighbourhoodOnlineAgents(@Arg('perspectiveUUID') perspectiveUUID: string): OnlineAgent[] {
60
+ const onlineAgent = new OnlineAgent()
61
+ onlineAgent.did = 'did:test:online'
62
+ onlineAgent.status = testPerspectiveExpression
63
+ return [onlineAgent]
64
+ }
65
+
66
+ @Mutation()
67
+ neighbourhoodSetOnlineStatus(@Arg('perspectiveUUID') perspectiveUUID: string, @Arg('status') status: PerspectiveInput): boolean {
68
+ return true
69
+ }
70
+
71
+ @Mutation()
72
+ neighbourhoodSetOnlineStatusU(@Arg('perspectiveUUID') perspectiveUUID: string, @Arg('status') status: PerspectiveUnsignedInput): boolean {
73
+ return true
74
+ }
75
+
76
+ @Mutation()
77
+ neighbourhoodSendSignal(@Arg('perspectiveUUID') perspectiveUUID: string, @Arg('remoteAgentDid') recipient: DID, @Arg('payload') signal: PerspectiveInput): boolean {
78
+ return true
79
+ }
80
+
81
+ @Mutation()
82
+ neighbourhoodSendSignalU(@Arg('perspectiveUUID') perspectiveUUID: string, @Arg('remoteAgentDid') recipient: DID, @Arg('payload') signal: PerspectiveUnsignedInput): boolean {
83
+ return true
84
+ }
85
+
86
+ @Mutation()
87
+ neighbourhoodSendBroadcast(@Arg('perspectiveUUID') perspectiveUUID: string, @Arg('payload') signal: PerspectiveInput): boolean {
88
+ return true
89
+ }
90
+
91
+ @Mutation()
92
+ neighbourhoodSendBroadcastU(@Arg('perspectiveUUID') perspectiveUUID: string, @Arg('payload') signal: PerspectiveUnsignedInput): boolean {
93
+ return true
94
+ }
95
+
96
+ @Subscription({topics: NEIGHBOURHOOD_SIGNAL_RECEIVED_TOPIC, nullable: true})
97
+ neighbourhoodSignal(@Arg('perspectiveUUID') pID: string): PerspectiveExpression {
98
+ return testPerspectiveExpression
99
+ }
100
+ }
@@ -0,0 +1,66 @@
1
+ import { Field, InputType, ObjectType } from "type-graphql";
2
+ import { Link } from "../links/Links"
3
+
4
+ @ObjectType()
5
+ @InputType()
6
+ export class LinkQuery {
7
+ @Field({nullable: true})
8
+ source?: string;
9
+
10
+ @Field({nullable: true})
11
+ target?: string;
12
+
13
+ @Field({nullable: true})
14
+ predicate?: string;
15
+
16
+ @Field({nullable: true})
17
+ fromDate?: Date;
18
+
19
+ @Field({nullable: true})
20
+ untilDate?: Date;
21
+
22
+ @Field({nullable: true})
23
+ limit?: number;
24
+
25
+ constructor(obj: object) {
26
+ if(obj) {
27
+ // @ts-ignore
28
+ this.source = obj.source
29
+ // @ts-ignore
30
+ this.predicate = obj.predicate
31
+ // @ts-ignore
32
+ this.target = obj.target
33
+ // @ts-ignore
34
+ if(obj.fromDate) {
35
+ // @ts-ignore
36
+ this.fromDate = obj.fromDate;
37
+ };
38
+ // @ts-ignore
39
+ if (obj.untilDate) {
40
+ // @ts-ignore
41
+ this.untilDate = obj.untilDate;
42
+ }
43
+ // @ts-ignore
44
+ if (obj.limit) {
45
+ // @ts-ignore
46
+ this.limit = obj.limit;
47
+ }
48
+ }
49
+ }
50
+
51
+ isMatch(l: Link): boolean {
52
+ if(this.source)
53
+ if(this.source !== l.source)
54
+ return false
55
+
56
+ if(this.predicate)
57
+ if(this.predicate !== l.predicate)
58
+ return false
59
+
60
+ if(this.target)
61
+ if(this.target !== l.target)
62
+ return false
63
+
64
+ return true
65
+ }
66
+ }
@@ -0,0 +1,23 @@
1
+ import { Perspective } from "./Perspective"
2
+ import { Link, LinkExpression } from "../links/Links"
3
+ import { LinkQuery } from "./LinkQuery"
4
+
5
+ const link = new LinkExpression()
6
+ link.author = 'did:method:12345'
7
+ link.timestamp = new Date().toString()
8
+ link.data = new Link({source: 'root', target: 'perspective://Qm34589a3ccc0'})
9
+ link.proof = { signature: 'asdfasdf', key: 'asdfasdf' }
10
+
11
+ describe('Perspective', () => {
12
+ it('implements get()', () => {
13
+ let p = new Perspective([link])
14
+ let result = p.get(new LinkQuery({source: 'root'}))
15
+ expect(result).toEqual([link])
16
+ })
17
+
18
+ it('implements getSingleTarget()', () => {
19
+ let p = new Perspective([link])
20
+ let result = p.getSingleTarget(new LinkQuery({source: 'root'}))
21
+ expect(result).toEqual(link.data.target)
22
+ })
23
+ })