-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon_graph.js
More file actions
157 lines (137 loc) · 5.12 KB
/
common_graph.js
File metadata and controls
157 lines (137 loc) · 5.12 KB
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
import { finalizeEvent, generateSecretKey, getPublicKey } from '../lib/esm/pure.js';
import { Relay, useWebSocketImplementation } from '../lib/esm/relay.js';
import {
NewSubspaceCreateEvent,
ValidateSubspaceCreateEvent,
NewSubspaceJoinEvent,
ValidateSubspaceJoinEvent,
toNostrEvent,
setParents
} from '../lib/esm/cip/subspace.js';
import { KindSubspaceCreate, DefaultCommonPrjOps, DefaultCommonGraphOps } from '../lib/esm/cip/constants.js'
import {
newProjectEvent,
newTaskEvent,
newEntityEvent,
newRelationEvent,
newObservationEvent,
toNostrEvent as toNostrEventCommon
} from '../lib/esm/cip/cip02/common_graph.js'
import WebSocket from 'ws';
useWebSocketImplementation(WebSocket);
// 0. Connect to the relay for test
const relayURL = 'ws://161.97.129.166:10547'
const relay = await Relay.connect(relayURL);
console.log(`connected to ${relay.url}`);
// Generate keys
let sk = generateSecretKey();
let pk = getPublicKey(sk);
// Subscribe to events
relay.subscribe([
{
kinds: [KindSubspaceCreate],
limit: 1,
},
], {
onevent(event) {
console.log('====================================');
console.log('Subscribe got event:', event);
}
})
// 1. Create a new subspace
const subspaceName = 'TestCommonGraph';
const rules = 'rule1';
const description = 'This is a test subspace for common graph';
const imageURL = 'http://example.com/image.png';
const subspaceEvent = NewSubspaceCreateEvent(subspaceName, DefaultCommonPrjOps + "," + DefaultCommonGraphOps, rules, description, imageURL);
ValidateSubspaceCreateEvent(subspaceEvent);
// Sign and publish the subspace creation event
const signedSubspaceEvent = finalizeEvent(toNostrEvent(subspaceEvent), sk);
await relay.publish(signedSubspaceEvent);
console.log('====================================');
console.log('Subspace creation event published:', signedSubspaceEvent);
// 2. Join the subspace
const joinEvent = NewSubspaceJoinEvent(subspaceEvent.subspaceID, "Join");
ValidateSubspaceJoinEvent(joinEvent);
// Sign and publish the subspace join event
const signedJoinEvent = finalizeEvent(toNostrEvent(joinEvent), sk);
await relay.publish(signedJoinEvent);
console.log('====================================');
console.log('Subspace join event published:', signedJoinEvent);
// 3. Create a project
const projectEvent = await newProjectEvent(subspaceEvent.subspaceID, "Create a new project")
if (!projectEvent) {
throw new Error('Failed to create project event')
}
projectEvent.setProjectInfo(
'proj-001',
'Test Project',
'This is a test project',
['user1', 'user2'],
'active'
)
// Sign and publish the project event
const signedProjectEvent = finalizeEvent(toNostrEventCommon(projectEvent), sk);
await relay.publish(signedProjectEvent);
console.log('====================================');
console.log('Project event published:', signedProjectEvent);
// 4. Create a task
const taskEvent = await newTaskEvent(subspaceEvent.subspaceID, "Create a new task")
if (!taskEvent) {
throw new Error('Failed to create task event')
}
taskEvent.setTaskInfo(
'proj-001',
'task-001',
'Implement feature X',
'user1',
'in-progress',
'2024-12-31',
'high'
)
// Sign and publish the task event
const signedTaskEvent = finalizeEvent(toNostrEventCommon(taskEvent), sk);
await relay.publish(signedTaskEvent);
console.log('====================================');
console.log('Task event published:', signedTaskEvent);
// 5. Create an entity
const entityEvent = await newEntityEvent(subspaceEvent.subspaceID, "Create a new entity")
if (!entityEvent) {
throw new Error('Failed to create entity event')
}
entityEvent.setEntityInfo('User', 'Person')
// Sign and publish the entity event
const signedEntityEvent = finalizeEvent(toNostrEventCommon(entityEvent), sk);
await relay.publish(signedEntityEvent);
console.log('====================================');
console.log('Entity event published:', signedEntityEvent);
// 6. Create a relation
const relationEvent = await newRelationEvent(subspaceEvent.subspaceID, "Create a new relation")
if (!relationEvent) {
throw new Error('Failed to create relation event')
}
relationEvent.setRelationInfo(
'user1',
'user2',
'follows',
'social',
0.85,
'Strong professional relationship with regular interactions'
)
// Sign and publish the relation event
const signedRelationEvent = finalizeEvent(toNostrEventCommon(relationEvent), sk);
await relay.publish(signedRelationEvent);
console.log('====================================');
console.log('Relation event published:', signedRelationEvent);
// 7. Create an observation
const observationEvent = await newObservationEvent(subspaceEvent.subspaceID, "Create a new observation")
if (!observationEvent) {
throw new Error('Failed to create observation event')
}
observationEvent.setObservationInfo('user1', 'User is active')
// Sign and publish the observation event
const signedObservationEvent = finalizeEvent(toNostrEventCommon(observationEvent), sk);
await relay.publish(signedObservationEvent);
console.log('====================================');
console.log('Observation event published:', signedObservationEvent);
relay.close();