Skip to content

Commit

Permalink
providers
Browse files Browse the repository at this point in the history
  • Loading branch information
tacyarg committed Oct 5, 2019
1 parent e7b527a commit 1b68c6e
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 6 deletions.
37 changes: 33 additions & 4 deletions libs/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = ({
traders,
users,
tokens,
providers,
}) => {
return {
async echo(payload) {
Expand Down Expand Up @@ -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.')

Expand Down
3 changes: 2 additions & 1 deletion models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = config => {
require('./stats'),
require('./tokens'),
require('./users'),
require('./subscriptions')
require('./subscriptions'),
require('./providers')
])
}
47 changes: 47 additions & 0 deletions models/list.js
Original file line number Diff line number Diff line change
@@ -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
}
65 changes: 65 additions & 0 deletions models/providers.js
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
3 changes: 2 additions & 1 deletion models/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -30,6 +30,7 @@ module.exports = async con => {
expires: ONE_DAY_MS,
created: Date.now(),
updated: null,
...meta,
})
},
validate(id, userid) {
Expand Down

0 comments on commit 1b68c6e

Please sign in to comment.