1 | const ssbKeys = require('ssb-keys')
|
2 | const cont = require('cont')
|
3 | const tape = require('tape')
|
4 | const u = require('./util')
|
5 | const pull = require('pull-stream')
|
6 | const validate = require('ssb-validate')
|
7 | const rimraf = require('rimraf')
|
8 | const mkdirp = require('mkdirp')
|
9 |
|
10 | const os = require('os')
|
11 | const path = require('path')
|
12 |
|
13 | const SecretStack = require('secret-stack')
|
14 | const caps = require('ssb-caps')
|
15 |
|
16 | function liveFriends (ssbServer) {
|
17 | const live = {}
|
18 | pull(
|
19 | ssbServer.friends.createFriendStream({ live: true, meta: true }),
|
20 | pull.drain(function (friend) {
|
21 | if (friend.sync) return
|
22 | live[friend.id] = friend.hops
|
23 | })
|
24 | )
|
25 | return live
|
26 | }
|
27 |
|
28 | const dir = path.join(os.tmpdir(), "friends-db2")
|
29 |
|
30 | rimraf.sync(dir)
|
31 | mkdirp.sync(dir)
|
32 |
|
33 | function Server(opts = {}) {
|
34 | const stack = SecretStack({ caps })
|
35 | .use(require('ssb-db2'))
|
36 | .use(require('..'))
|
37 |
|
38 | return stack(opts)
|
39 | }
|
40 |
|
41 | let state = validate.initial()
|
42 |
|
43 | function addMsg(db, keys, content) {
|
44 | state = validate.appendNew(
|
45 | state,
|
46 | null,
|
47 | keys,
|
48 | content,
|
49 | Date.now()
|
50 | )
|
51 |
|
52 | return (cb) => {
|
53 | value = state.queue.shift().value
|
54 | db.add(value, cb)
|
55 | }
|
56 | }
|
57 |
|
58 | tape('db2 friends test', function (t) {
|
59 | const alice = ssbKeys.generate()
|
60 | const bob = ssbKeys.generate()
|
61 | const carol = ssbKeys.generate()
|
62 |
|
63 | let sbot = Server({
|
64 | keys: alice,
|
65 | db2: true,
|
66 | path: dir
|
67 | })
|
68 | let live = liveFriends(sbot)
|
69 |
|
70 | cont.para([
|
71 | addMsg(sbot.db, alice, u.follow(bob.id)),
|
72 | addMsg(sbot.db, alice, u.follow(carol.id)),
|
73 | addMsg(sbot.db, alice, u.follow(alice.id)),
|
74 | addMsg(sbot.db, bob, u.follow(alice.id)),
|
75 | addMsg(sbot.db, bob, {
|
76 | type: 'contact',
|
77 | contact: carol.id,
|
78 | following: false,
|
79 | flagged: true
|
80 | }),
|
81 | addMsg(sbot.db, carol, u.follow(alice.id))
|
82 | ])(function (err, results) {
|
83 | sbot.friends.hops(function (err, hops) {
|
84 | if (err) throw err
|
85 | t.deepEqual(live, hops)
|
86 |
|
87 | sbot.close(() => {
|
88 | sbot = Server({
|
89 | keys: alice,
|
90 | db2: true,
|
91 | path: dir
|
92 | })
|
93 | live = liveFriends(sbot)
|
94 |
|
95 | addMsg(sbot.db, bob, {
|
96 | type: 'contact',
|
97 | contact: carol.id,
|
98 | following: true
|
99 | })((err) => {
|
100 | if (err) throw err
|
101 |
|
102 | sbot.db.onDrain('contacts', () => {
|
103 | t.deepEqual(live, hops)
|
104 |
|
105 | sbot.close(t.end)
|
106 | })
|
107 | })
|
108 | })
|
109 | })
|
110 | })
|
111 | })
|
112 |
|
113 | tape('db2 unfollow', function (t) {
|
114 | const alice = ssbKeys.generate()
|
115 | const bob = ssbKeys.generate()
|
116 | const carol = ssbKeys.generate()
|
117 |
|
118 | rimraf.sync(dir)
|
119 | mkdirp.sync(dir)
|
120 |
|
121 | let sbot = Server({
|
122 | keys: alice,
|
123 | db2: true,
|
124 | path: dir
|
125 | })
|
126 | let live = liveFriends(sbot)
|
127 |
|
128 | cont.para([
|
129 | addMsg(sbot.db, alice, u.follow(bob.id)),
|
130 | addMsg(sbot.db, alice, u.follow(carol.id)),
|
131 | addMsg(sbot.db, bob, u.follow(alice.id)),
|
132 | addMsg(sbot.db, carol, u.follow(alice.id))
|
133 | ])(function (err, results) {
|
134 | sbot.friends.hops(function (err, hops) {
|
135 | if (err) throw err
|
136 | t.deepEqual(live, hops)
|
137 |
|
138 | sbot.close(() => {
|
139 | sbot = Server({
|
140 | keys: alice,
|
141 | db2: true,
|
142 | path: dir
|
143 | })
|
144 | live = liveFriends(sbot)
|
145 |
|
146 | addMsg(sbot.db, alice, u.unfollow(bob.id))((err) => {
|
147 | if (err) throw err
|
148 |
|
149 | sbot.friends.hops(function (err, hops) {
|
150 | t.deepEqual(live, hops)
|
151 |
|
152 | sbot.close(() => {
|
153 | sbot = Server({
|
154 | keys: alice,
|
155 | db2: true,
|
156 | path: dir
|
157 | })
|
158 |
|
159 | sbot.friends.hops(function (err, hopsAfter) {
|
160 | t.deepEqual(hopsAfter, hops)
|
161 | sbot.close(t.end)
|
162 | })
|
163 | })
|
164 | })
|
165 | })
|
166 | })
|
167 | })
|
168 | })
|
169 | })
|