diff --git a/libs/actions.js b/libs/actions.js index 49c6517..4739e87 100644 --- a/libs/actions.js +++ b/libs/actions.js @@ -11,6 +11,7 @@ module.exports = ({ traders, users, tokens, + providers, }) => { return { async echo(payload) { @@ -80,17 +81,45 @@ module.exports = ({ const { userid } = await tokens.get(token) return tokens.listUserSorted(userid) }, - async listEventProviders() { - return events.listGroups('provider') + async listProviders() { + return providers.listDone() }, - async listUserEventProviders() { - return events.listGroups('provider_userid') + async createProvider({ + name, + token, + description = 'User Generated Provider Key.', + }) { + assert(name, 'name required') + assert(token, 'token required') + const { userid } = await tokens.get(token) + + const provider = await providers.create(name, userid, { + description, + }) + + // create an api token for the provider. + let token = await tokens.generate('user', 'provider') + token = await tokens.validate(token.id, provider.id) + + return { provider, token } }, + // async disableProvider({ providerid, token }) { + // assert(providerid, 'providerid required') + // assert(token, 'token required') + // const { userid } = await tokens.get(token) + + // return providers.update(providerid, { + // done: true, + // }) + // }, async createSubscription({ providerid, token }) { assert(providerid, 'providerid required') assert(token, 'token required') const { userid } = await tokens.get(token) + const exists = await providers.has(providerid) + assert(exists, 'Provider does not exist!') + const subbed = await subscriptions.isSubscribed(providerid, userid) assert(!subbed, 'You have already subscribed to this provider.') diff --git a/models/index.js b/models/index.js index ececade..d189f1a 100644 --- a/models/index.js +++ b/models/index.js @@ -8,6 +8,7 @@ module.exports = config => { require('./stats'), require('./tokens'), require('./users'), - require('./subscriptions') + require('./subscriptions'), + require('./providers') ]) } diff --git a/models/list.js b/models/list.js new file mode 100644 index 0000000..8cda6fc --- /dev/null +++ b/models/list.js @@ -0,0 +1,47 @@ +const { Table } = require('utils').Rethink +const assert = require('assert') + +module.exports = name => async con => { + assert(name, 'requires table name') + const schema = { + table: name, + } + + const table = await Table(con, schema) + + table.create = props => { + assert(props.id, 'requires id') + props.list = props.list || [] + return table.upsert(props) + } + + table.push = async (id, data) => { + assert(id, 'requires id') + assert(data, 'requires data to push') + const item = await table.get(id) + item.list.push(data) + return table.upsert(item) + } + + table.pop = async id => { + const item = await table.get(id) + const data = item.list.pop() + await table.upsert(item) + return data + } + + table.dequeue = async id => { + const item = await table.get(id) + const data = item.list.shift() + await table.upsert(item) + return data + } + + table.splice = async (id, ...args) => { + const item = await table.get(id) + const data = item.list.splice(...args) + return table.upsert(item) + } + + return table +} diff --git a/models/providers.js b/models/providers.js new file mode 100644 index 0000000..a77b918 --- /dev/null +++ b/models/providers.js @@ -0,0 +1,65 @@ +const { Table } = require('rethink-table') + +module.exports = async con => { + const schema = { + table: 'providers', + indices: ['created', 'type', 'userid', 'name'], + } + + const table = await Table(con, schema) + + return { + ...table, + changes() { + const query = table.table().changes() + return table.streamify(query) + }, + streamSorted() { + const query = table.table().orderBy({ index: 'created' }) + return table.streamify(query) + }, + listSorted() { + const q = table + .table() + .orderBy({ index: table.r.desc('created') }) + .limit(100) + .coerceTo('array') + return table.run(q) + }, + listUserSorted(userid) { + const query = table + .table() + .orderBy({ index: 'created' }) + .filter({ userid }) + .limit(100) + .coerceTo('array') + return table.run(query) + }, + create(name, userid, meta ={}) { + assert(name, 'name required') + assert(userid, 'userid required') + + return table.upsert({ + done: false, + verified: false, + id: uuid(), + name, + userid, + expires: null, + created: Date.now(), + updated: null, + ...meta + }) + }, + listGroups(index) { + const q = table + .table() + .group({ index }) + .count() + return table.run(q) + }, + listDone(done=false) { + return table.getBy('done', done) + } + } +} diff --git a/models/tokens.js b/models/tokens.js index 4f031a0..948512b 100644 --- a/models/tokens.js +++ b/models/tokens.js @@ -21,7 +21,7 @@ module.exports = async con => { const query = table.table().orderBy({ index: 'created' }) return table.streamify(query) }, - generate(provider, type) { + generate(provider, type, meta = {}) { return table.upsert({ provider, type, @@ -30,6 +30,7 @@ module.exports = async con => { expires: ONE_DAY_MS, created: Date.now(), updated: null, + ...meta, }) }, validate(id, userid) {