-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
225 lines (203 loc) · 5.75 KB
/
util.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
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
// SPDX-FileCopyrightText: 2023 the cable-core.js authors
//
// SPDX-License-Identifier: AGPL-3.0-or-later
const b4a = require("b4a")
const cable = require("cable.js")
const constants = require("cable.js/constants.js")
// returns a timestamp in UNIX Time form
function timestamp () {
return +(new Date())
}
// for use in indexes to ensure that we never have any collisions with non-monotonic timestamps.
// takes a timestamp, and if that timestamp has been seen, returns a unique monotonic timestamp
function monotonicTimestamp () {
this.seen = {}
// TODO (2023-03-01): maintain a fixed length on seen dictionary
// list of keys that may be sorted to trim this.seen length once max reached
// this.keys = []
const pad = (n) => {
const str = `${n}`
return `${'0'.repeat(3-str.length)}${str}`
}
return (ts) => {
if (typeof ts === "undefined") {
ts = timestamp()
}
const key = `${ts}`
let ret = key
if (this.seen[key]) {
const prev = this.seen[key]
const str = `${prev[prev.length - 1]}`
const index = str.indexOf(".")
if (index < 0) {
ret = `${ts}.${pad(1)}`
} else {
const i = parseInt(str.slice(index+1))
ret = `${ts}.${pad(i+1)}`
}
}
if (!this.seen[key]) {
this.seen[key] = []
// this.keys.push(key)
}
this.seen[key].push(ret)
return ret
}
}
function humanizeMessageType(msgtype) {
switch (msgtype) {
case 0:
return "hash response"
case 1:
return "post response"
case 2:
return "post request"
case 3:
return "cancel request"
case 4:
return "channel time range request"
case 5:
return "channel state request"
case 6:
return "channel list request"
case 7:
return "channel list response"
case 8:
return "moderation state request"
default:
return "unknown"
}
}
function humanizePostType(posttype) {
switch (posttype) {
case 0:
return "post/text"
case 1:
return "post/delete"
case 2:
return "post/info"
case 3:
return "post/topic"
case 4:
return "post/join"
case 5:
return "post/leave"
case 6:
return "post/role"
case 7:
return "post/moderation"
case 8:
return "post/block"
case 9:
return "post/unblock"
default:
return "unknown"
}
}
function hex (input) {
if (typeof input === "string") { return input }
return b4a.toString(input, "hex")
}
function transformUserRoleMapScheme (activeRoles) {
const userMap = new Map()
for (const [context, pubkeyMap] of activeRoles) {
for (const [pubkey, roleTs] of pubkeyMap) {
if (!userMap.has(pubkey)) { userMap.set(pubkey, new Map()) }
userMap.get(pubkey).set(context, roleTs.role)
}
}
return userMap
}
/* used by moderation tests and functionality */
function getRole(map, role) {
const keys = new Set()
for (let [recipient, roleObj] of map) {
if (roleObj.role === role) { keys.add(recipient); }
}
return keys
}
function getSmallestValidTime (tracker, cabal, author) {
const trackerTime = tracker.getRoleValidSince(author)
const cabalTime = cabal.getRoleValidSince(author)
if (trackerTime === -1 || trackerTime > cabalTime) {
return cabalTime
}
return trackerTime
}
function _getContext (obj) {
// block/unblock don't have a channel, they act on the entire cabal context
if ((obj.postType === constants.BLOCK_POST || obj.postType === constants.UNBLOCK_POST) || obj.channel.length === 0) {
return constants.CABAL_CONTEXT
}
return obj.channel
}
function determineAuthority (localKp, buf, roles, authorityTest) {
const obj = cable.parsePost(buf)
// if the context for the post doesn't exist, for some reason, use the cabal context
let authorityMap
const context = _getContext(obj)
if (roles.has(context)) {
authorityMap = roles.get(context)
} else {
authorityMap = roles.get(constants.CABAL_CONTEXT)
}
if (!authorityMap) {
return b4a.equals(obj.publicKey, localKp.publicKey)
}
const role_ts = authorityMap.get(hex(obj.publicKey))
return (role_ts && authorityTest(obj, role_ts))
}
const testIsModAuthority = (obj, role_ts) => role_ts.role !== constants.USER_FLAG && obj.timestamp >= role_ts.since
const testIsAdmin = (obj, role_ts) => role_ts.role === constants.ADMIN_FLAG && obj.timestamp >= role_ts.since
function isApplicable (localKp, buf, roles) {
return determineAuthority(localKp, buf, roles, testIsModAuthority)
}
function isAdmin (localKp, buf, roles) {
return determineAuthority(localKp, buf, roles, testIsAdmin)
}
class Ready {
// callback processing queue. functions are pushed onto the queue if they are dispatched before the store is ready or
// there are pending transactions in the pipeline
queue = []
// when unprocessedBatches is at 0 our index has finished processing pending transactions => ok to process queue
unprocessedBatches = 0
constructor(viewName) {
this.debug = require("debug")(`core:${viewName}:ready`)
}
increment() {
this.debug("incr")
this.unprocessedBatches++
}
decrement() {
this.debug("decr")
this.unprocessedBatches--
if (this.unprocessedBatches <= 0) { this.call() }
}
call (cb) {
this.debug("ready called")
this.debug("unprocessedBatches %d", this.unprocessedBatches)
if (!cb) cb = function () {}
// we can process the queue
if (this.unprocessedBatches <= 0) {
this.debug("ready firing!")
for (let fn of this.queue) { fn() }
this.queue = []
return cb()
}
this.queue.push(cb)
}
}
module.exports = {
timestamp,
monotonicTimestamp,
noop: function() {},
humanizeMessageType,
humanizePostType,
hex,
getRole,
getSmallestValidTime,
transformUserRoleMapScheme,
Ready,
isAdmin,
isApplicable
}