mirror of
https://github.com/roleypoly/roleypoly-v1.git
synced 2025-06-15 01:49:10 +00:00
chore: remove semi
This commit is contained in:
parent
4b75eaa0ab
commit
8fcc0dcbf9
64 changed files with 1073 additions and 1073 deletions
|
@ -1,76 +1,76 @@
|
|||
module.exports = (R, $) => {
|
||||
R.post('/api/auth/token', async ctx => {
|
||||
const { token } = ctx.request.body;
|
||||
const { token } = ctx.request.body
|
||||
|
||||
if (token == null || token === '') {
|
||||
ctx.body = { err: 'token_missing' };
|
||||
ctx.status = 400;
|
||||
return;
|
||||
ctx.body = { err: 'token_missing' }
|
||||
ctx.status = 400
|
||||
return
|
||||
}
|
||||
|
||||
if (ctx.session.accessToken === undefined || ctx.session.expiresAt < Date.now()) {
|
||||
const data = await $.discord.getAuthToken(token);
|
||||
ctx.session.accessToken = data.access_token;
|
||||
ctx.session.refreshToken = data.refresh_token;
|
||||
ctx.session.expiresAt = Date.now() + (ctx.expires_in || 1000 * 60 * 60 * 24);
|
||||
const data = await $.discord.getAuthToken(token)
|
||||
ctx.session.accessToken = data.access_token
|
||||
ctx.session.refreshToken = data.refresh_token
|
||||
ctx.session.expiresAt = Date.now() + (ctx.expires_in || 1000 * 60 * 60 * 24)
|
||||
}
|
||||
|
||||
const user = await $.discord.getUser(ctx.session.accessToken);
|
||||
ctx.session.userId = user.id;
|
||||
ctx.session.avatarHash = user.avatar;
|
||||
const user = await $.discord.getUser(ctx.session.accessToken)
|
||||
ctx.session.userId = user.id
|
||||
ctx.session.avatarHash = user.avatar
|
||||
|
||||
ctx.body = {
|
||||
id: user.id,
|
||||
avatar: user.avatar,
|
||||
username: user.username,
|
||||
discriminator: user.discriminator,
|
||||
};
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
R.get('/api/auth/user', async ctx => {
|
||||
if (ctx.session.accessToken === undefined) {
|
||||
ctx.body = { err: 'not_logged_in' };
|
||||
ctx.status = 401;
|
||||
return;
|
||||
ctx.body = { err: 'not_logged_in' }
|
||||
ctx.status = 401
|
||||
return
|
||||
}
|
||||
|
||||
const user = await $.discord.getUser(ctx.session.accessToken);
|
||||
ctx.session.userId = user.id;
|
||||
ctx.session.avatarHash = user.avatar;
|
||||
const user = await $.discord.getUser(ctx.session.accessToken)
|
||||
ctx.session.userId = user.id
|
||||
ctx.session.avatarHash = user.avatar
|
||||
|
||||
ctx.body = {
|
||||
id: user.id,
|
||||
avatar: user.avatar,
|
||||
username: user.username,
|
||||
discriminator: user.discriminator,
|
||||
};
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
R.get('/api/auth/redirect', ctx => {
|
||||
const url = $.discord.getAuthUrl();
|
||||
const url = $.discord.getAuthUrl()
|
||||
if (ctx.query.url === '✔️') {
|
||||
ctx.body = { url };
|
||||
return;
|
||||
ctx.body = { url }
|
||||
return
|
||||
}
|
||||
|
||||
ctx.redirect(url);
|
||||
});
|
||||
ctx.redirect(url)
|
||||
})
|
||||
|
||||
R.post('/api/auth/logout', ctx => {
|
||||
ctx.session = null;
|
||||
});
|
||||
ctx.session = null
|
||||
})
|
||||
|
||||
R.get('/api/oauth/bot', ctx => {
|
||||
const url = $.discord.getBotJoinUrl();
|
||||
const url = $.discord.getBotJoinUrl()
|
||||
if (ctx.query.url === '✔️') {
|
||||
ctx.body = { url };
|
||||
return;
|
||||
ctx.body = { url }
|
||||
return
|
||||
}
|
||||
|
||||
ctx.redirect(url);
|
||||
});
|
||||
ctx.redirect(url)
|
||||
})
|
||||
|
||||
R.get('/api/oauth/bot/callback', ctx => {
|
||||
console.log(ctx.request);
|
||||
});
|
||||
};
|
||||
console.log(ctx.request)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,22 +1,22 @@
|
|||
const log = new (require('../logger'))('api/index');
|
||||
const glob = require('glob');
|
||||
const log = new (require('../logger'))('api/index')
|
||||
const glob = require('glob')
|
||||
|
||||
const PROD = process.env.NODE_ENV === 'production';
|
||||
const PROD = process.env.NODE_ENV === 'production'
|
||||
|
||||
module.exports = async (router, ctx) => {
|
||||
const apis = glob.sync(`./api/**/!(index).js`);
|
||||
log.debug('found apis', apis);
|
||||
const apis = glob.sync(`./api/**/!(index).js`)
|
||||
log.debug('found apis', apis)
|
||||
|
||||
for (let a of apis) {
|
||||
if (a.endsWith('_test.js') && PROD) {
|
||||
log.debug(`skipping ${a}`);
|
||||
continue;
|
||||
log.debug(`skipping ${a}`)
|
||||
continue
|
||||
}
|
||||
log.debug(`mounting ${a}`);
|
||||
log.debug(`mounting ${a}`)
|
||||
try {
|
||||
require(a.replace('api/', ''))(router, ctx);
|
||||
require(a.replace('api/', ''))(router, ctx)
|
||||
} catch (e) {
|
||||
log.error(`couldn't mount ${a}`, e);
|
||||
log.error(`couldn't mount ${a}`, e)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,91 +1,91 @@
|
|||
module.exports = (R, $) => {
|
||||
R.get('/api/servers', async ctx => {
|
||||
try {
|
||||
const { userId } = ctx.session;
|
||||
const srv = $.discord.getRelevantServers(userId);
|
||||
const presentable = await $.P.presentableServers(srv, userId);
|
||||
const { userId } = ctx.session
|
||||
const srv = $.discord.getRelevantServers(userId)
|
||||
const presentable = await $.P.presentableServers(srv, userId)
|
||||
|
||||
ctx.body = presentable;
|
||||
ctx.body = presentable
|
||||
} catch (e) {
|
||||
console.error(e.trace || e.stack);
|
||||
console.error(e.trace || e.stack)
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
R.get('/api/server/:id', async ctx => {
|
||||
const { userId } = ctx.session;
|
||||
const { id } = ctx.params;
|
||||
const { userId } = ctx.session
|
||||
const { id } = ctx.params
|
||||
|
||||
const srv = $.discord.client.guilds.get(id);
|
||||
const srv = $.discord.client.guilds.get(id)
|
||||
|
||||
if (srv == null) {
|
||||
ctx.body = { err: 'not found' };
|
||||
ctx.status = 404;
|
||||
return;
|
||||
ctx.body = { err: 'not found' }
|
||||
ctx.status = 404
|
||||
return
|
||||
}
|
||||
|
||||
let gm;
|
||||
let gm
|
||||
if (srv.members.has(userId)) {
|
||||
gm = $.discord.gm(id, userId);
|
||||
gm = $.discord.gm(id, userId)
|
||||
} else if ($.discord.isRoot(userId)) {
|
||||
gm = $.discord.fakeGm({ id: userId });
|
||||
gm = $.discord.fakeGm({ id: userId })
|
||||
} else {
|
||||
ctx.body = { err: 'not_a_member' };
|
||||
ctx.status = 400;
|
||||
return;
|
||||
ctx.body = { err: 'not_a_member' }
|
||||
ctx.status = 400
|
||||
return
|
||||
}
|
||||
const server = await $.P.presentableServer(srv, gm);
|
||||
const server = await $.P.presentableServer(srv, gm)
|
||||
|
||||
ctx.body = server;
|
||||
});
|
||||
ctx.body = server
|
||||
})
|
||||
|
||||
R.get('/api/server/:id/slug', async ctx => {
|
||||
const { userId } = ctx.session;
|
||||
const { id } = ctx.params;
|
||||
const { userId } = ctx.session
|
||||
const { id } = ctx.params
|
||||
|
||||
const srv = $.discord.client.guilds.get(id);
|
||||
const srv = $.discord.client.guilds.get(id)
|
||||
|
||||
console.log(srv);
|
||||
console.log(srv)
|
||||
|
||||
if (srv == null) {
|
||||
ctx.body = { err: 'not found' };
|
||||
ctx.status = 404;
|
||||
return;
|
||||
ctx.body = { err: 'not found' }
|
||||
ctx.status = 404
|
||||
return
|
||||
}
|
||||
|
||||
ctx.body = await $.P.serverSlug(srv);
|
||||
});
|
||||
ctx.body = await $.P.serverSlug(srv)
|
||||
})
|
||||
|
||||
R.patch('/api/server/:id', async ctx => {
|
||||
const { userId } = ctx.session;
|
||||
const { id } = ctx.params;
|
||||
const { userId } = ctx.session
|
||||
const { id } = ctx.params
|
||||
|
||||
let gm = $.discord.gm(id, userId);
|
||||
let gm = $.discord.gm(id, userId)
|
||||
if (gm == null && $.discord.isRoot(userId)) {
|
||||
gm = $.discord.fakeGm({ id: userId });
|
||||
gm = $.discord.fakeGm({ id: userId })
|
||||
}
|
||||
|
||||
// check perms
|
||||
if (!$.discord.getPermissions(gm).canManageRoles) {
|
||||
ctx.status = 403;
|
||||
ctx.body = { err: 'cannot_manage_roles' };
|
||||
return;
|
||||
ctx.status = 403
|
||||
ctx.body = { err: 'cannot_manage_roles' }
|
||||
return
|
||||
}
|
||||
|
||||
const { message = null, categories = null } = ctx.request.body;
|
||||
const { message = null, categories = null } = ctx.request.body
|
||||
|
||||
// todo make less nasty
|
||||
await $.server.update(id, {
|
||||
...(message != null ? { message } : {}),
|
||||
...(categories != null ? { categories } : {}),
|
||||
});
|
||||
})
|
||||
|
||||
ctx.body = { ok: true };
|
||||
});
|
||||
ctx.body = { ok: true }
|
||||
})
|
||||
|
||||
R.get('/api/admin/servers', async ctx => {
|
||||
const { userId } = ctx.session;
|
||||
const { userId } = ctx.session
|
||||
if (!$.discord.isRoot(userId)) {
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
ctx.body = $.discord.client.guilds.map(g => ({
|
||||
|
@ -93,16 +93,16 @@ module.exports = (R, $) => {
|
|||
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;
|
||||
const { userId } = ctx.session
|
||||
const { server } = ctx.params
|
||||
|
||||
let gm = $.discord.gm(server, userId);
|
||||
let gm = $.discord.gm(server, userId)
|
||||
if (gm == null && $.discord.isRoot(userId)) {
|
||||
gm = $.discord.fakeGm({ id: userId });
|
||||
gm = $.discord.fakeGm({ id: userId })
|
||||
}
|
||||
|
||||
// check perms
|
||||
|
@ -112,24 +112,24 @@ module.exports = (R, $) => {
|
|||
// return
|
||||
// }
|
||||
|
||||
const { added, removed } = ctx.request.body;
|
||||
const { added, removed } = ctx.request.body
|
||||
|
||||
const allowedRoles = await $.server.getAllowedRoles(server);
|
||||
const allowedRoles = await $.server.getAllowedRoles(server)
|
||||
|
||||
const pred = r => $.discord.safeRole(server, r) && allowedRoles.indexOf(r) !== -1;
|
||||
const pred = r => $.discord.safeRole(server, r) && allowedRoles.indexOf(r) !== -1
|
||||
|
||||
if (added.length > 0) {
|
||||
gm = await gm.addRoles(added.filter(pred));
|
||||
gm = await gm.addRoles(added.filter(pred))
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
if (removed.length > 0) {
|
||||
gm.removeRoles(removed.filter(pred));
|
||||
gm.removeRoles(removed.filter(pred))
|
||||
}
|
||||
}, 1000);
|
||||
}, 1000)
|
||||
|
||||
// console.log('role patch', { added, removed, allowedRoles, addedFiltered: added.filterNot(pred), removedFiltered: removed.filterNot(pred) })
|
||||
|
||||
ctx.body = { ok: true };
|
||||
});
|
||||
};
|
||||
ctx.body = { ok: true }
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,26 +1,26 @@
|
|||
module.exports = (R, $) => {
|
||||
R.get('/api/~/relevant-servers/:user', (ctx, next) => {
|
||||
// ctx.body = 'ok'
|
||||
const srv = $.discord.getRelevantServers(ctx.params.user);
|
||||
ctx.body = $.discord.presentableServers(srv, ctx.params.user);
|
||||
return;
|
||||
});
|
||||
const srv = $.discord.getRelevantServers(ctx.params.user)
|
||||
ctx.body = $.discord.presentableServers(srv, ctx.params.user)
|
||||
return
|
||||
})
|
||||
|
||||
R.get('/api/~/roles/:id/:userId', (ctx, next) => {
|
||||
// ctx.body = 'ok'
|
||||
const { id, userId } = ctx.params;
|
||||
const { id, userId } = ctx.params
|
||||
|
||||
const srv = $.discord.client.guilds.get(id);
|
||||
const srv = $.discord.client.guilds.get(id)
|
||||
|
||||
if (srv === undefined) {
|
||||
ctx.body = { err: 'not found' };
|
||||
ctx.status = 404;
|
||||
return;
|
||||
ctx.body = { err: 'not found' }
|
||||
ctx.status = 404
|
||||
return
|
||||
}
|
||||
|
||||
const gm = srv.members.get(userId);
|
||||
const roles = $.discord.presentableRoles(id, gm);
|
||||
const gm = srv.members.get(userId)
|
||||
const roles = $.discord.presentableRoles(id, gm)
|
||||
|
||||
ctx.boy = roles;
|
||||
});
|
||||
};
|
||||
ctx.boy = roles
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue