Skip to content

Commit

Permalink
Merge branch 'mod-key-v2' of github.com:cabal-club/cabal-core into mo…
Browse files Browse the repository at this point in the history
…d-key
  • Loading branch information
substack committed May 20, 2020
2 parents 541d2e1 + f02352a commit 7118a5c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
21 changes: 12 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ var thunky = require('thunky')
var timestamp = require('monotonic-timestamp')
var sublevel = require('subleveldown')
var crypto = require('hypercore-crypto')
var url = require('url')
var querystring = require('query-string')
var createChannelView = require('./views/channels')
var createMembershipsView = require('./views/channel-membership')
var createMessagesView = require('./views/messages')
Expand All @@ -18,7 +16,7 @@ var swarm = require('./swarm')

var DATABASE_VERSION = 1
var CHANNELS = 'c'
var MEMBERSHIPS = 'j' // j for joined memberships..? :3
var MEMBERSHIPS = 'j' // j for joined memberships..? :3
var MESSAGES = 'm'
var TOPICS = 't'
var USERS = 'u'
Expand All @@ -33,7 +31,6 @@ module.exports.databaseVersion = DATABASE_VERSION
* @constructor
* @param {string|function} storage - A hyperdb compatible storage function, or a string representing the local data path.
* @param {string} key - a protocol string, optionally with url parameters
* @param {Object} opts - { modKey }
*/
function Cabal (storage, key, opts) {
if (!(this instanceof Cabal)) return new Cabal(storage, key, opts)
Expand All @@ -54,13 +51,20 @@ function Cabal (storage, key, opts) {
}

this.maxFeeds = opts.maxFeeds
this.modKeys = []
this.adminKeys = []

if (!key) this.key = generateKeyHex()
else this.key = sanitizeKey(key)
else {
if (Buffer.isBuffer(key)) key = key.toString('hex')
if (!key.startsWith('cabal://')) key = 'cabal://' + key
const uri = new URL(key)
this.key = sanitizeKey(uri.host)
this.modKeys = uri.searchParams.getAll('mod')
this.adminKeys = uri.searchParams.getAll('admin')
}
if (!isHypercoreKey(this.key)) throw new Error('invalid cabal key')

this.modKey = opts.modKey

this.db = opts.db || level()
this.kcore = kappa(storage, {
valueEncoding: json,
Expand Down Expand Up @@ -89,8 +93,7 @@ function Cabal (storage, key, opts) {
this.kcore.use('users', createUsersView(
sublevel(this.db, USERS, { valueEncoding: json })))
this.kcore.use('moderation', createModerationView(
this, this.modKey,
sublevel(this.db, MODERATION, { valueEncoding: json }))
this, sublevel(this.db, MODERATION, { valueEncoding: json }))
)

this.messages = this.kcore.api.messages
Expand Down
18 changes: 9 additions & 9 deletions test/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ test('block a user by key', function (t) {
t.plan(7)

var addr = randomBytes(32).toString('hex')
var cabal0 = Cabal(ram, 'cabal://' + addr)
var cabal0 = Cabal(ram, addr)
cabal0.ready(function () {
cabal0.getLocalKey(function (err, key) {
t.error(err)
var cabal1 = Cabal(ram, 'cabal://' + addr, { modKey: key })
var cabal2 = Cabal(ram, 'cabal://' + addr, { modKey: key })
var cabal1 = Cabal(ram, addr + "?mod=" + key)
var cabal2 = Cabal(ram, addr + "?mod=" + key)
var pending = 3
cabal1.ready(function () {
if (--pending === 0) ready(cabal0, cabal1, cabal2)
Expand Down Expand Up @@ -91,12 +91,12 @@ test('delegated moderator ban a user by key', function (t) {
t.plan(15)

var addr = randomBytes(32).toString('hex')
var cabal0 = Cabal(ram, 'cabal://' + addr)
var cabal0 = Cabal(ram, addr)
cabal0.ready(function () {
cabal0.getLocalKey(function (err, key) {
t.error(err)
var cabal1 = Cabal(ram, addr, { modKey: key })
var cabal2 = Cabal(ram, addr, { modKey: key })
var cabal1 = Cabal(ram, addr + "?mod=" + key)
var cabal2 = Cabal(ram, addr + "?mod=" + key)
var pending = 3
cabal1.ready(function () {
if (--pending === 0) ready(cabal0, cabal1, cabal2)
Expand Down Expand Up @@ -150,15 +150,15 @@ test('delegated moderator ban a user by key', function (t) {
test('different mod keys have different views', function (t) {
t.plan(11)

var addr = randomBytes(32).toString('hex')
var addr = randomBytes(32).toString('hex')

var cabal0 = Cabal(ram, addr)
cabal0.ready(function () {
cabal0.getLocalKey(function (err, key) {
t.error(err)
var cabal1 = Cabal(ram, addr, { modKey: key })
var cabal1 = Cabal(ram, addr + "?mod=" + key)
var cabal2 = Cabal(ram, addr)
var cabal3 = Cabal(ram, addr, { modKey: key })
var cabal3 = Cabal(ram, addr + "?mod=" + key)
var pending = 4
cabal1.ready(function () {
if (--pending === 0) ready(cabal0, cabal1, cabal2, cabal3)
Expand Down
3 changes: 2 additions & 1 deletion views/moderation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ var through = require('through2')
var collect = require('collect-stream')
var { nextTick } = process

module.exports = function (cabal, modKey, db) {
module.exports = function (cabal, db) {
var events = new EventEmitter()
var modKey = cabal.modKeys[0]
var auth = mauth(db)
auth.on('update', function (update) {
events.emit('update', update)
Expand Down

0 comments on commit 7118a5c

Please sign in to comment.