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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
/**
* The environment is a place where services and shared dependencies between
* models live. They are made available to every model via dependency injection.
*/
import {getEnv, IStateTreeNode} from 'mobx-state-tree'
// import {ReactNativeStore} from './auth'
import {AdxClient, blueskywebSchemas, AdxRepoClient} from '@adxp/mock-api'
import * as storage from './lib/storage'
export const adx = new AdxClient({
pds: 'http://localhost',
schemas: blueskywebSchemas,
})
export class Environment {
adx = adx
// authStore?: ReactNativeStore
constructor() {}
async setup() {
await adx.setupMock(
() => storage.load('mock-root'),
async root => {
await storage.save('mock-root', root)
},
generateMockData,
)
// this.authStore = await ReactNativeStore.load()
}
}
/**
* Extension to the MST models that adds the env property.
* Usage:
*
* .extend(withEnvironment)
*
*/
export const withEnvironment = (self: IStateTreeNode) => ({
views: {
get env() {
return getEnv<Environment>(self)
},
},
})
// TEMPORARY
// mock api config
// =======
function* dateGen() {
let start = 1657846031914
while (true) {
yield new Date(start).toISOString()
start += 1e3
}
}
const date = dateGen()
function repo(didOrName: string) {
const userDb = adx.mockDb.getUser(didOrName)
if (!userDb) throw new Error(`User not found: ${didOrName}`)
return adx.mainPds.repo(userDb.did, userDb.writable)
}
export async function generateMockData() {
await adx.mockDb.addUser({name: 'alice.com', writable: true})
await adx.mockDb.addUser({name: 'bob.com', writable: true})
await adx.mockDb.addUser({name: 'carla.com', writable: true})
const alice = repo('alice.com')
const bob = repo('bob.com')
const carla = repo('carla.com')
await alice.collection('blueskyweb.xyz:Profiles').put('Profile', 'profile', {
$type: 'blueskyweb.xyz:Profile',
displayName: 'Alice',
description: 'Test user 1',
})
await bob.collection('blueskyweb.xyz:Profiles').put('Profile', 'profile', {
$type: 'blueskyweb.xyz:Profile',
displayName: 'Bob',
description: 'Test user 2',
})
await carla.collection('blueskyweb.xyz:Profiles').put('Profile', 'profile', {
$type: 'blueskyweb.xyz:Profile',
displayName: 'Carla',
description: 'Test user 3',
})
// everybody follows everybody
const follow = async (who: AdxRepoClient, subjectName: string) => {
const subjectDb = adx.mockDb.getUser(subjectName)
return who.collection('blueskyweb.xyz:Follows').create('Follow', {
$type: 'blueskyweb.xyz:Follow',
subject: {
did: subjectDb?.did,
name: subjectDb?.name,
},
createdAt: date.next().value,
})
}
await follow(alice, 'bob.com')
await follow(alice, 'carla.com')
await follow(bob, 'alice.com')
await follow(bob, 'carla.com')
await follow(carla, 'alice.com')
await follow(carla, 'bob.com')
// 2 posts on each user
const alicePosts: {uri: string}[] = []
for (let i = 0; i < 2; i++) {
alicePosts.push(
await alice.collection('blueskyweb.xyz:Posts').create('Post', {
$type: 'blueskyweb.xyz:Post',
text: `Alice post ${i + 1}`,
createdAt: date.next().value,
}),
)
await bob.collection('blueskyweb.xyz:Posts').create('Post', {
$type: 'blueskyweb.xyz:Post',
text: `Bob post ${i + 1}`,
createdAt: date.next().value,
})
await carla.collection('blueskyweb.xyz:Posts').create('Post', {
$type: 'blueskyweb.xyz:Post',
text: `Carla post ${i + 1}`,
createdAt: date.next().value,
})
}
// small thread of replies on alice's first post
const bobReply1 = await bob
.collection('blueskyweb.xyz:Posts')
.create('Post', {
$type: 'blueskyweb.xyz:Post',
text: 'Bob reply',
reply: {root: alicePosts[0].uri, parent: alicePosts[0].uri},
createdAt: date.next().value,
})
const carlaReply1 = await carla
.collection('blueskyweb.xyz:Posts')
.create('Post', {
$type: 'blueskyweb.xyz:Post',
text: 'Carla reply',
reply: {root: alicePosts[0].uri, parent: alicePosts[0].uri},
createdAt: date.next().value,
})
const aliceReply1 = await alice
.collection('blueskyweb.xyz:Posts')
.create('Post', {
$type: 'blueskyweb.xyz:Post',
text: 'Alice reply',
reply: {root: alicePosts[0].uri, parent: bobReply1.uri},
createdAt: date.next().value,
})
// bob and carla repost alice's first post
await bob.collection('blueskyweb.xyz:Posts').create('Repost', {
$type: 'blueskyweb.xyz:Repost',
subject: alicePosts[0].uri,
createdAt: date.next().value,
})
await carla.collection('blueskyweb.xyz:Posts').create('Repost', {
$type: 'blueskyweb.xyz:Repost',
subject: alicePosts[0].uri,
createdAt: date.next().value,
})
// bob likes all of alice's posts
for (let i = 0; i < 2; i++) {
await bob.collection('blueskyweb.xyz:Likes').create('Like', {
$type: 'blueskyweb.xyz:Like',
subject: alicePosts[i].uri,
createdAt: date.next().value,
})
}
// carla likes all of alice's posts and everybody's replies
for (let i = 0; i < 2; i++) {
await carla.collection('blueskyweb.xyz:Likes').create('Like', {
$type: 'blueskyweb.xyz:Like',
subject: alicePosts[i].uri,
createdAt: date.next().value,
})
}
await carla.collection('blueskyweb.xyz:Likes').create('Like', {
$type: 'blueskyweb.xyz:Like',
subject: aliceReply1.uri,
createdAt: date.next().value,
})
await carla.collection('blueskyweb.xyz:Likes').create('Like', {
$type: 'blueskyweb.xyz:Like',
subject: bobReply1.uri,
createdAt: date.next().value,
})
// give alice 3 badges, 2 from bob and 2 from carla, with one ignored
const inviteBadge = await bob
.collection('blueskyweb.xyz:Badges')
.create('Badge', {
$type: 'blueskyweb.xyz:Badge',
subject: {did: alice.did, name: 'alice.com'},
assertion: {type: 'invite'},
createdAt: date.next().value,
})
const techTagBadge1 = await bob
.collection('blueskyweb.xyz:Badges')
.create('Badge', {
$type: 'blueskyweb.xyz:Badge',
subject: {did: alice.did, name: 'alice.com'},
assertion: {type: 'tag', tag: 'tech'},
createdAt: date.next().value,
})
const techTagBadge2 = await carla
.collection('blueskyweb.xyz:Badges')
.create('Badge', {
$type: 'blueskyweb.xyz:Badge',
subject: {did: alice.did, name: 'alice.com'},
assertion: {type: 'tag', tag: 'tech'},
createdAt: date.next().value,
})
const employeeBadge = await bob
.collection('blueskyweb.xyz:Badges')
.create('Badge', {
$type: 'blueskyweb.xyz:Badge',
subject: {did: alice.did, name: 'alice.com'},
assertion: {type: 'employee'},
createdAt: date.next().value,
})
await alice.collection('blueskyweb.xyz:Profiles').put('Profile', 'profile', {
$type: 'blueskyweb.xyz:Profile',
displayName: 'Alice',
description: 'Test user 1',
badges: [
{uri: inviteBadge.uri},
{uri: techTagBadge1.uri},
{uri: techTagBadge2.uri},
],
})
}
|