-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtual.js
executable file
·144 lines (121 loc) · 4.02 KB
/
virtual.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
/* dBrowser Global Variables and Configuration */
const assert = require('assert')
const {VFSystemContainer} = require('./base')
const {VFSystemVault} = require('./vault')
const {diffUpdate, sortCompare} = require('./util')
class VFSystemFolder extends VFSystemContainer {
constructor (parent) {
super()
this.parent = parent
this._children = []
}
get type () { return 'folder' }
get isEmpty () { return this._children.length === 0 }
get children () { return this._children }
async readData () {
// fetch new children and update via diff
var newChildren = await this.readChildren()
this._children = diffUpdate(this._children, newChildren)
}
// should be overridden by subclass
async readChildren () {
return this._children
}
sort (column, dir) {
this._children.forEach(child => child.sort(column, dir))
this._children.sort((a, b) => {
// by current setting
return sortCompare(a, b, column, dir)
})
}
}
class VFSystemRoot extends VFSystemFolder {
get type () { return 'root folder' }
get url () { return 'virtual://root' }
get name () { return 'Root' }
async readChildren () {
// read user profile
const profile = await dbrowser.profiles.getCurrentUserProfile()
profile.isCurrentUser = true
// do not add additional child node when following self
const followUrls = profile.followUrls ? profile.followUrls.filter((url) => url !== profile._origin) : []
// read followed profiles
const followedProfiles = await Promise.all(followUrls.map(dbrowser.profiles.getUserProfile))
const followedFolders = followedProfiles.map(p => new VFSystemFolder_User(this, p))
// generate children
return [
new VFSystemFolder_User(this, profile),
new VFSystemFolder_Network(this),
...followedFolders,
new VFSystemFolder_Trash(this)
]
}
sort () {
// dont sort
}
}
class VFSystemFolder_User extends VFSystemFolder {
constructor (parent, profile) {
super()
this.parent = parent
this._profile = profile
}
get name () { return this._profile.name || 'Anonymous' }
get url () { return 'virtual://user-' + this._profile._origin }
copyDataFrom (node) {
this._profile = node._profile
}
async readChildren () {
// read source set of vaults
var vaults
if (this._profile.isCurrentUser) {
vaults = await dbrowser.vaults.list({isSaved: true, isOwner: true})
} else {
// fetch their published vaults
vaults = await dbrowser.vaults.listPublished({author: this._profile._origin})
// remove their profile vault if its in there (we want the direct source)
vaults = vaults.filter(a => a.url !== this._profile._origin)
// now add their profile vault to the front
let profileVault = new DWebVault(this._profile._origin)
let profileVaultInfo = await profileVault.getInfo()
vaults.unshift(profileVaultInfo)
}
return vaults.map(a => new VFSystemVault(this, a))
}
}
class VFSystemFolder_Network extends VFSystemFolder {
get name () { return 'Network' }
get url () { return 'virtual://network' }
async readChildren () {
const vaults = await dbrowser.vaults.list({isSaved: true, isOwner: false})
return vaults.map(a => new VFSystemVault(this, a))
}
// special helper
// this folder has vaults added arbitrarily on user access
// so we need this method to add the vault
addVault (vaultInfo) {
const alreadyExists = !!this._children.find(item => item._vaultInfo.url === vaultInfo.url)
if (!alreadyExists) {
const vault = new VFSystemVault(this, vaultInfo)
this._children.push(vault)
}
}
sort () {
// dont sort
}
}
class VFSystemFolder_Trash extends VFSystemFolder {
get name () { return 'Trash' }
get url () { return 'virtual://trash' }
async readChildren () {
const vaults = await dbrowser.vaults.list({isSaved: false})
return vaults.map(a => new VFSystemVault(this, a))
}
}
module.exports = {
VFSystemRoot,
VFSystemFolder,
VFSystemFolder_User,
VFSystemFolder_Network,
VFSystemFolder_Trash
}