DiscordService: swap to eris and refactor the entire service while we're at it.

This commit is contained in:
41666 2019-03-22 07:01:08 -05:00
parent 27fb06a197
commit 03ad4202b8
No known key found for this signature in database
GPG key ID: BC51D07640DC10AF
14 changed files with 4075 additions and 410 deletions

View file

@ -18,6 +18,9 @@ export default (ctx: AppContext, forceClear: ?boolean = false): {
const filename = path.basename(a)
const dirname = path.dirname(a)
const pathname = a.replace('rpc/', '')
delete require.cache[require.resolve(pathname)]
// internal stuff
if (filename.startsWith('_')) {
log.debug(`skipping ${a}`)
@ -37,10 +40,6 @@ export default (ctx: AppContext, forceClear: ?boolean = false): {
log.debug(`mounting ${a}`)
try {
const pathname = a.replace('rpc/', '')
delete require.cache[require.resolve(pathname)]
const r = require(pathname)
let o = r
if (o.default) {

42
rpc/_security.js Normal file
View file

@ -0,0 +1,42 @@
// @flow
import { type AppContext } from '../Roleypoly'
import { type Context } from 'koa'
import RPCError from './_error'
// import logger from '../logger'
// const log = logger(__filename)
const PermissionError = new RPCError('User does not have permission to call this RPC.', 403)
// export const bot = (fn: Function) => (secret: string, ...args: any[]) => {
// if (secret !== process.env.SHARED_SECRET) {
// log.error('unauthenticated bot request', { secret })
// return { err: 'unauthenticated' }
// }
// return fn(...args)
// }
export const root = ($: AppContext, fn: Function) => (ctx: Context, ...args: any[]) => {
if ($.discord.isRoot(ctx.session.userId)) {
return fn(ctx, ...args)
}
throw PermissionError
}
export const manager = ($: AppContext, fn: Function) => (ctx: Context, server: string, ...args: any[]) => {
if ($.discord.canManageRoles(server, ctx.session.userId)) {
return fn(ctx, server, ...args)
}
throw PermissionError
}
export const member = ($: AppContext, fn: Function) => (ctx: Context, server: string, ...args: any[]) => {
if ($.discord.isMember(server, ctx.session.userId)) {
return fn(ctx, server, ...args)
}
throw PermissionError
}

View file

@ -86,6 +86,18 @@ export default class RPCServer {
}
}
/**
* For internally called stuff, such as from a bot shard.
*/
async call (fn: string, ...args: any[]) {
if (!(fn in this.rpcMap)) {
throw new RPCError(`RPC call ${fn}(...) not found.`, 404)
}
const call = this.rpcMap[fn]
return call(...args)
}
rpcError (ctx: Context & {body: any}, msg: ?string, err: ?Error = null, code: ?number = null) {
log.error('rpc error', { msg, err })

View file

@ -1,17 +1,19 @@
// @flow
import { type AppContext } from '../Roleypoly'
import { type Context } from 'koa'
import { type Guild } from 'discord.js'
import { type Guild } from 'eris'
import { root } from './_security'
export default ($: AppContext) => ({
listRelevantServers (ctx: Context) {
rootGetAllServers: root($, (ctx: Context) => {
return $.discord.client.guilds.map<{
url: string,
name: string,
members: number,
roles: number
}>((g: Guild) => ({ url: `${$.config.appUrl}/s/${g.id}`, name: g.name, members: g.members.array().length, roles: g.roles.array().length }))
},
}>((g: Guild) => ({ url: `${$.config.appUrl}/s/${g.id}`, name: g.name, members: g.memberCount, roles: g.roles.size }))
}),
getServerSlug (ctx: Context, id: string) {
const srv = $.discord.client.guilds.get(id)