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,8 +1,49 @@
const Service = require('./Service')
const LRU = require('lru-cache')
// @flow
import Service from './Service'
import LRU from 'lru-cache'
import { type AppContext } from '../Roleypoly'
import { type Models } from '../models'
import { type ServerModel } from '../models/Server'
import type DiscordService, { Permissions } from './discord'
import {
type Guild,
type GuildMember,
type Collection
} from 'discord.js'
import areduce from '../util/areduce'
export type ServerSlug = {
id: string,
name: string,
ownerID: string,
icon: string
}
export type PresentableRole = {
id: string,
color: number,
name: string,
position: number,
safe: boolean
}
export type PresentableServer = ServerModel & {
id: string,
gm: {
nickname: string,
color: string
},
server: ServerSlug,
roles: ?PresentableRole[],
perms: Permissions
}
class PresentationService extends Service {
constructor (ctx) {
cache: LRU
M: Models
discord: DiscordService
constructor (ctx: AppContext) {
super(ctx)
this.M = ctx.M
this.discord = ctx.discord
@ -10,7 +51,7 @@ class PresentationService extends Service {
this.cache = new LRU({ max: 500, maxAge: 100 * 60 * 5 })
}
serverSlug (server) {
serverSlug (server: Guild): ServerSlug {
return {
id: server.id,
name: server.name,
@ -19,27 +60,34 @@ class PresentationService extends Service {
}
}
async oldPresentableServers (collection, userId) {
async oldPresentableServers (collection: Collection<string, Guild>, userId: string) {
this.log.deprecated('use presentableServers instead of oldPresentableServers')
let servers = []
for (let server of collection.array()) {
const gm = server.members.get(userId)
// $FlowFixMe this is deprecated, forget adding more check code.
servers.push(await this.presentableServer(server, gm))
}
return servers
}
async presentableServers (collection, userId) {
return collection.array().areduce(async (acc, server) => {
presentableServers (collection: Collection<string, Guild>, userId: string) {
return areduce(collection.array(), async (acc, server) => {
const gm = server.members.get(userId)
if (gm == null) {
throw new Error(`somehow this guildmember ${userId} of ${server.id} didn't exist.`)
}
acc.push(await this.presentableServer(server, gm, { incRoles: false }))
return acc
})
}, [])
}
async presentableServer (server, gm, { incRoles = true } = {}) {
async presentableServer (server: Guild, gm: GuildMember, { incRoles = true }: { incRoles: boolean } = {}): Promise<PresentableServer> {
const sd = await this.ctx.server.get(server.id)
return {
@ -56,7 +104,7 @@ class PresentationService extends Service {
}
}
async rolesByServer (server) {
async rolesByServer (server: Guild, sd: ServerModel): Promise<PresentableRole[]> {
return server.roles
.filter(r => r.id !== server.id) // get rid of @everyone
.map(r => ({