-
Notifications
You must be signed in to change notification settings - Fork 1
/
PlaylistsStore.js
169 lines (147 loc) · 4.45 KB
/
PlaylistsStore.js
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
import { observable } from 'mobx'
import Identities from 'orbit-db-identity-provider'
import OrbitDB from 'orbit-db'
class PlaylistsStore {
@observable isOnline = false
@observable playlists = []
@observable currentPlaylist = {}
constructor() {
this._ipfs = null
this.odb = null
this.feed = null
}
async connect(ipfs, options = {}) {
this._ipfs = ipfs
const identity = options.identity || await Identities.createIdentity({ id: 'user' })
this.odb = await OrbitDB.createInstance(ipfs, { identity, directory: './odb'})
await this.loadPlaylists()
console.log(this.feed.all)
this.isOnline = true
}
async loadPlaylists() {
this.feed = await this.odb.feed(this.odb.identity.id + '/playlists')
this.feed.events.on('write', (address, entry, heads) => {
console.log("added", entry)
switch (entry.payload.op) {
case 'ADD':
this.playlists.push({
hash: entry.hash,
name: entry.payload.value.name,
address: entry.payload.value.address
})
break
case 'DEL':
const filtered = this.playlists.filter(x => x.hash !== entry.payload.value)
this.playlists = filtered
break
default:
console.log("could not recognize op")
}
})
await this.feed.load()
this.feed.all.map(entry => {
this.playlists.push({
hash: entry.hash,
name: entry.payload.value.name,
address: entry.payload.value.address
})
})
}
async createNewPlaylist(name) {
const playlist = await this.odb.feed(name, { accessController: { type: 'orbitdb', write: [this.odb.identity.id]}})
const p = {
name,
address: playlist.address.toString()
}
const hash = await this.feed.add(p)
return hash
}
async joinPlaylist (address) {
if (this.odb) {
const playlist = this.odb.stores[address] || await this.odb.open(address)
await playlist.load()
this.currentPlaylist = playlist
}
}
async deletePlaylist(hash) {
await this.feed.del(hash)
}
async addToPlaylist (address, data) {
const feed = this.odb.stores[address] || await this.odb.open(address)
if (feed) {
const hash = await feed.add(data)
return feed.get(hash)
}
return
}
async addFile (address, source) {
if (!source || !source.filename) {
throw new Error('Filename not specified')
}
const isBuffer = source.buffer && source.filename
const name = source.filename.split('/').pop()
const size = source.meta && source.meta.size ? source.meta.size : 0
const result = await this._ipfs.add(Buffer.from(source.buffer))
const hash = result[0].hash
console.log("upload", hash)
// Create a post
const data = {
content: hash,
meta: Object.assign(
{
from: this.odb.identity.id,
type: 'file',
ts: new Date().getTime()
},
{ size, name },
source.meta || {}
)
}
return this.addToPlaylist(address, data)
}
async _sendFile (file, address) {
return new Promise(resolve => {
const reader = new FileReader()
reader.onload = async event => {
const f = await this.addFile(address,
{
filename: file.name,
buffer: event.target.result,
meta: { mimeType: file.type, size: file.size }
})
resolve(f)
}
reader.readAsArrayBuffer(file)
})
}
sendFiles (files, address) {
const promises = []
for (let i = 0; i < files.length; i++) {
promises.push(this._sendFile(files[i], address))
}
return Promise.all(promises)
}
async sharePlaylist (address, peerId) {
// add peer to ac
// send message to peer
if (!this.odb) return false
const feed = this.odb.stores[address] || await this.odb.open(address)
await feed.access.grant('write', peerId)
console.log(address, peer)
return true
// const channel = await Channel.open(this._ipfs, peer)
// // Explicitly wait for peers to connect
// console.log("Channel", channel)
// await channel.connect()
// // Send message on the channel
// const thing = await channel.send('Hello World!')
// console.log(thing)
// // Process messages from the other peer
// channel.on('message', (message) => {
// console.log('Message from', message.from, message)
// })
}
}
const store = window.store = new PlaylistsStore
// store.load()
export default store