Created
May 14, 2018 23:25
-
-
Save hackergrrl/973f0a643a3f23def9259b06210d91ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var hyperdb = require('hyperdb') | |
var hindex = require('hyperdb-index-level') | |
var level = require('level') | |
var ram = require('random-access-memory') | |
var db = hyperdb(ram, { valueEncoding: 'json' }) | |
var lvl = level('./index') | |
var idx = hindex(db, lvl, processor) | |
var alice = hyperdb(ram, { valueEncoding: 'json' }) | |
var bob = hyperdb(ram, { valueEncoding: 'json' }) | |
// imagine alice/bob/db have all been replicted + authorized to each other | |
// by default, let's say 'db' (creator of the first db) is the only one with | |
// admin perms | |
// db.put('role/' + db.local.key.toString('hex'), 'admin') // <- implicit | |
alice.put('role/' + db.local.key.toString('hex'), 'admin') | |
// then db decides to auth alice and mute bob | |
db.put('role/' + alice.local.key.toString('hex'), 'admin') | |
db.put('role/' + bob.local.key.toString('hex'), 'mute') | |
// the processor function below will process these in causal order | |
function processor (node, next) { | |
if (node.key.startsWith('role/')) { | |
var key = node.key.split('/')[1] | |
var role = node.value | |
var authorKey = db.local.key // TODO: lookup how to derive this from the hyperdb node | |
// check if the author is allowed to make permission edits | |
lvl.get(authorKey, function (err, authorRole) { | |
if ((err && err.notFound) || authorRole !== 'admin') return // ignore the node | |
lvl.put(key, role, next) | |
}) | |
} | |
} | |
function getRole (key, cb) { | |
idx.ready(function () { | |
lvl.get(key, cb) | |
}) | |
} | |
// getRole(alice.local.key, function (err, role) { ... } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment