flowtyped everything, some functional, safety, and structural changes

This commit is contained in:
41666 2019-03-10 03:18:11 -05:00
parent 6f3eca7a64
commit d2aecb38ca
92 changed files with 17554 additions and 1440 deletions

View file

@ -1,5 +1,10 @@
module.exports = (R, $) => {
R.get('/api/servers', async (ctx) => {
// @flow
import { type Context } from 'koa'
import { type AppContext, type Router } from '../Roleypoly'
import { type ServerModel } from '../models/Server'
export default (R: Router, $: AppContext) => {
R.get('/api/servers', async (ctx: Context) => {
try {
const { userId } = ctx.session
const srv = $.discord.getRelevantServers(userId)
@ -11,7 +16,7 @@ module.exports = (R, $) => {
}
})
R.get('/api/server/:id', async (ctx) => {
R.get('/api/server/:id', async (ctx: Context) => {
const { userId } = ctx.session
const { id } = ctx.params
@ -28,17 +33,21 @@ module.exports = (R, $) => {
gm = $.discord.gm(id, userId)
} else if ($.discord.isRoot(userId)) {
gm = $.discord.fakeGm({ id: userId })
} else {
}
if (gm == null) {
ctx.body = { err: 'not_a_member' }
ctx.status = 400
return
}
const server = await $.P.presentableServer(srv, gm)
// $FlowFixMe bad koa type
ctx.body = server
})
R.get('/api/server/:id/slug', async (ctx) => {
R.get('/api/server/:id/slug', async (ctx: Context) => {
// const { userId } = ctx.session
const { id } = ctx.params
@ -52,16 +61,25 @@ module.exports = (R, $) => {
return
}
// $FlowFixMe bad koa type
ctx.body = await $.P.serverSlug(srv)
})
R.patch('/api/server/:id', async (ctx) => {
const { userId } = ctx.session
const { id } = ctx.params
R.patch('/api/server/:id', async (ctx: Context) => {
const { userId } = (ctx.session: { userId: string })
const { id } = (ctx.params: { id: string })
let gm = $.discord.gm(id, userId)
if (gm == null && $.discord.isRoot(userId)) {
gm = $.discord.fakeGm({ id: userId })
if (gm == null) {
if ($.discord.isRoot(userId)) {
gm = $.discord.fakeGm({ id: userId })
} else {
ctx.status = 403
ctx.body = {
err: 'not permitted'
}
return
}
}
// check perms
@ -71,7 +89,7 @@ module.exports = (R, $) => {
return
}
const { message = null, categories = null } = ctx.request.body
const { message, categories } = ((ctx.request.body: any): $Shape<ServerModel>)
// todo make less nasty
await $.server.update(id, {
@ -82,32 +100,33 @@ module.exports = (R, $) => {
ctx.body = { ok: true }
})
R.get('/api/admin/servers', async ctx => {
const { userId } = ctx.session
R.get('/api/admin/servers', async (ctx: Context) => {
const { userId } = (ctx.session: { userId: string })
if (!$.discord.isRoot(userId)) {
return
}
ctx.body = $.discord.client.guilds.map(g => ({ url: `${process.env.APP_URL}/s/${g.id}`, name: g.name, members: g.members.array().length, roles: g.roles.array().length }))
ctx.body = $.discord.client.guilds.map(g => ({ url: `${$.config.appUrl}/s/${g.id}`, name: g.name, members: g.members.array().length, roles: g.roles.array().length }))
})
R.patch('/api/servers/:server/roles', async ctx => {
const { userId } = ctx.session
const { server } = ctx.params
R.patch('/api/servers/:server/roles', async (ctx: Context) => {
const { userId } = (ctx.session: { userId: string })
const { server } = (ctx.params: { server: string })
let gm = $.discord.gm(server, userId)
if (gm == null && $.discord.isRoot(userId)) {
gm = $.discord.fakeGm({ id: userId })
if (gm == null) {
if ($.discord.isRoot(userId)) {
gm = $.discord.fakeGm({ id: userId })
} else {
ctx.status = 403
ctx.body = {
err: 'not permitted'
}
return
}
}
// check perms
// if (!$.discord.getPermissions(gm).canManageRoles) {
// ctx.status = 403
// ctx.body = { err: 'cannot_manage_roles' }
// return
// }
const { added, removed } = ctx.request.body
const { added, removed } = ((ctx.request.body: any): { added: string[], removed: string[] })
const allowedRoles = await $.server.getAllowedRoles(server)
@ -118,13 +137,20 @@ module.exports = (R, $) => {
}
setTimeout(() => {
if (gm == null) {
ctx.body = {
err: 'guild member disappeared on remove, this should never happen.'
}
ctx.status = 500
return
}
if (removed.length > 0) {
gm.removeRoles(removed.filter(pred))
}
}, 1000)
// console.log('role patch', { added, removed, allowedRoles, addedFiltered: added.filterNot(pred), removedFiltered: removed.filterNot(pred) })
ctx.body = { ok: true }
})
}