finish redux feature parity

This commit is contained in:
41666 2017-12-16 17:39:53 -06:00
parent f5220aa6dc
commit 5510d5a1c4
29 changed files with 220 additions and 100 deletions

View file

@ -1,4 +1,4 @@
const log = new (require('./logger'))('World')
const log = new (require('./logger'))('Roleypoly')
const Sequelize = require('sequelize')
const fetchModels = require('./models')
const fetchApis = require('./api')
@ -40,9 +40,9 @@ class Roleypoly {
// enableReadyCheck: true,
// enableOfflineQueue: true
// })
this.ctx.discord = new (require('./services/discord'))(this.ctx)
this.ctx.sessions = new (require('./services/sessions'))(this.ctx)
this.ctx.P = new (require('./services/presentation'))(this.ctx)
}
async mountRoutes () {

View file

@ -36,6 +36,7 @@ module.exports = (R, $) => {
if (ctx.session.accessToken === undefined) {
ctx.body = { err: 'not_logged_in' }
ctx.status = 401
return
}
const user = await $.discord.getUser(ctx.session.accessToken)

View file

@ -2,7 +2,7 @@ module.exports = (R, $) => {
R.get('/api/servers', async (ctx) => {
const { userId } = ctx.session
const srv = $.discord.getRelevantServers(userId)
const presentable = $.discord.presentableServers(srv, userId)
const presentable = $.P.oldPresentableServers(srv, userId)
ctx.body = presentable
})

View file

@ -37,9 +37,9 @@ async function start () {
await next()
} catch (e) {
log.error(e)
ctx.status = 500
ctx.status = ctx.status || 500
if (DEVEL) {
ctx.body = e.stack
ctx.body = ctx.body || e.stack
} else {
ctx.body = {
err: 'something terrible happened.'

View file

@ -7,7 +7,7 @@ module.exports = (sql, DataTypes) => {
categories: {
type: DataTypes.JSON
},
note: {
message: {
type: DataTypes.TEXT
}
})

View file

@ -26,6 +26,7 @@
"koa-passport": "^4.0.1",
"koa-session": "^5.5.1",
"ksuid": "^0.4.0",
"lru-cache": "^4.1.1",
"passport-discord": "^0.1.3",
"passport-oauth2-refresh": "^1.0.0",
"pg": "^7.4.0",

View file

@ -15,6 +15,7 @@ class DiscordService extends Service {
this.client = new discord.Client()
this.startBot()
}
async startBot () {
@ -25,42 +26,12 @@ class DiscordService extends Service {
return this.client.guilds.filter((g) => g.members.has(userId))
}
presentableServers (collection, userId) {
return collection.map((server) => {
const gm = server.members.get(userId)
return {
id: server.id,
gm: {
nickname: gm.nickname,
color: gm.displayHexColor
},
server: {
id: server.id,
name: server.name,
ownerID: server.ownerID,
icon: server.icon
},
roles: this.presentableRoles(server.id, gm),
message: 'moe moe kyuuuuuuuuun~',
perms: this.getPermissions(gm)
}
})
gm (serverId, userId) {
return this.client.guilds.get(serverId).members.get(userId)
}
presentableRoles (serverId, gm) {
return this.client.guilds
.get(serverId)
.roles
.filter(r => r.id !== serverId)
.map((role) => ({
color: role.hexColor,
position: role.position,
calculatedPosition: role.calculatedPosition,
id: role.id,
name: role.name,
selected: gm.roles.has(role.id)
}))
getRoles (server) {
return this.client.guilds.get(server).roles
}
getPermissions (gm) {

View file

@ -0,0 +1,49 @@
const Service = require('./Service')
const LRU = require('lru-cache')
class PresentationService extends Service {
constructor (ctx) {
super(ctx)
this.M = ctx.M
this.discord = ctx.discord
this.cache = LRU({ max: 500, maxAge: 100 * 60 * 5 })
}
oldPresentableServers (collection, userId) {
return collection.map((server) => {
const gm = server.members.get(userId)
return {
id: server.id,
gm: {
nickname: gm.nickname,
color: gm.displayHexColor
},
server: {
id: server.id,
name: server.name,
ownerID: server.ownerID,
icon: server.icon
},
roles: server.roles.filter(r => r.id !== server.id).map(r => ({
id: r.id,
color: r.color,
name: r.name,
selected: gm.roles.has(r.id),
position: r.position
})),
message: 'moe moe kyuuuuuuuuun~',
perms: this.discord.getPermissions(gm)
}
})
}
rolesByServer (serverId, userId) {
// get from discord, merge with server categories
}
}
module.exports = PresentationService

42
Server/services/server.js Normal file
View file

@ -0,0 +1,42 @@
const Service = require('./Service')
class ServerService extends Service {
constructor (ctx) {
super(ctx)
this.Server = ctx.M.Server
this.P = ctx.P
}
async ensure (server) {
const srv = await this.get(server.id)
if (srv == null) {
return this.create({
id: server.id,
message: '',
categories: {}
})
}
}
create ({ id, message, categories }) {
const srv = this.Server.build({ id, message, categories })
return srv.save()
}
update (id, newData) {
const srv = this.get(id)
return srv.update(newData)
}
get (id) {
return this.Server.findOne({
where: {
id
}
})
}
}
module.exports = ServerService

View file

@ -2022,6 +2022,13 @@ lru-cache@^4.0.1:
pseudomap "^1.0.1"
yallist "^2.0.0"
lru-cache@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-4.1.1.tgz#622e32e82488b49279114a4f9ecf45e7cd6bba55"
dependencies:
pseudomap "^1.0.2"
yallist "^2.1.2"
media-typer@0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"
@ -2635,7 +2642,7 @@ promptly@2.2.0:
dependencies:
read "^1.0.4"
pseudomap@^1.0.1:
pseudomap@^1.0.1, pseudomap@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3"
@ -3441,7 +3448,7 @@ y18n@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/y18n/-/y18n-3.2.1.tgz#6d15fba884c08679c0d77e88e7759e811e07fa41"
yallist@^2.0.0:
yallist@^2.0.0, yallist@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-2.1.2.tgz#1c11f9218f076089a47dd512f93c6699a6a81d52"