Skip to content

Commit

Permalink
refactor clan management (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
fcaps authored Dec 5, 2023
1 parent db94e34 commit e954663
Show file tree
Hide file tree
Showing 28 changed files with 487 additions and 543 deletions.
9 changes: 0 additions & 9 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,3 @@ services:
build:
dockerfile: Dockerfile-dev
context: .
networks:
- faf-stack
logging:
driver: "json-file"

networks:
faf-stack:
name: faf-stack_faf
external: true
30 changes: 30 additions & 0 deletions public/styles/site/clans.sass
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,33 @@
z-index: 2
position: relative


.faf-button
appearance: button
text-decoration: none
margin: 0.2em
padding: 0.8em
color: #000000
border: 0px solid #262626
background-color: #ec9d36
border-radius: 20px
justify-self: center
font-weight: bold
letter-spacing: 0.1em
cursor: pointer
&:link
color: #000000
&:visited
color: #000000
&:hover
color: #ffffff
background-color: #731d00

.action-link
color: #ec9d36
&:link
color: #ec9d36
&:visited
color: #ec9d36
&:hover
color: #731d00
23 changes: 12 additions & 11 deletions src/backend/routes/views/clanRouter.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
const create = require('../views/clans/create')
const invite = require('./clans/invite')
const leave = require('./clans/leave')
const express = require('../../ExpressApp')
const router = express.Router()
const middlewares = require('../middleware')

// This will be replaced soon, therefor I did not spend time on it
router.get('/', (req, res) => res.render('clans/clans'))
router.get('/view/:id', async (req, res) => {
const clanId = parseInt(req.params.id || null)
if (!clanId) {
return res.redirect('/clans')
}
router.get('/view/:id', require('./clans/view'))

return res.render('clans/clan', { clan: await req.appContainer.get('ClanService').getClan(clanId) })
})

router.get('/create', create)
router.post('/create', create)
router.get('/create', middlewares.isAuthenticated(), create)
router.post('/create', middlewares.isAuthenticated(), create)
router.get('/manage', middlewares.isAuthenticated(), require('./clans/get/manage'))
router.post('/update', middlewares.isAuthenticated(), require('./clans/post/update'))
router.post('/destroy', middlewares.isAuthenticated(), require('./clans/post/destroy'))

router.get('/invite', middlewares.isAuthenticated(), invite)
router.post('/invite', middlewares.isAuthenticated(), invite)
router.get('/kick/:memberId', middlewares.isAuthenticated(), require('./clans/kick'))
router.get('/leave', middlewares.isAuthenticated(), leave)
router.post('/leave', middlewares.isAuthenticated(), leave)
router.get('/join', middlewares.isAuthenticated(), require('./clans/join'))
router.get('/invite-accept', middlewares.isAuthenticated(), require('./clans/inviteAccept'))
router.get('*', (req, res) => res.status(503).render('errors/503-known-issue'))

module.exports = router
92 changes: 0 additions & 92 deletions src/backend/routes/views/clans/get/accept_invite.js

This file was deleted.

7 changes: 6 additions & 1 deletion src/backend/routes/views/clans/get/manage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const { JavaApiError } = require('../../../../services/ApiErrors')
exports = module.exports = async (req, res) => {
// if something changed in another session we should refresh the user first
await req.requestContainer.get('UserService').refreshUser()

const clanMembershipId = req.requestContainer.get('UserService').getUser()?.clan?.membershipId || null
if (!clanMembershipId) {
await req.asyncFlash('error', 'You don\'t belong to a clan')

return res.redirect('/clans')
}

Expand All @@ -17,6 +22,6 @@ exports = module.exports = async (req, res) => {

await req.asyncFlash('error', message)

return res.redirect('/')
return res.redirect('/clans')
}
}
46 changes: 46 additions & 0 deletions src/backend/routes/views/clans/invite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const { JavaApiError } = require('../../../services/ApiErrors')
const { body } = require('express-validator')
const url = require('url')

exports = module.exports = [
body('invited_player', 'Please select a player').notEmpty().isLength({ max: 20 }),
async (req, res) => {
if (req.method === 'POST') {
const user = await req.appContainer.get('DataRepository').fetchUserByName(req.body.invited_player)

if (!user) {
await req.asyncFlash('error', 'User not found')
return res.render('clans/invite', {
invited_player: req.body.invited_player
})
}

try {
const invitation = await req.requestContainer.get('ClanManagementService').createInvite(user.id)

return res.render('clans/invite', {
invited_player: req.body.invited_player,
link: url.format({
pathname: '/clans/invite-accept',
query: {
token: encodeURIComponent(invitation)
}
})
})
} catch (e) {
let message = e.toString()
if (e instanceof JavaApiError && e.error?.errors) {
message = e.error.errors[0].detail
}

await req.asyncFlash('error', message)

return res.redirect('/clans/manage')
}
}

return res.render('clans/invite', {
invited_player: ''
})
}
]
36 changes: 36 additions & 0 deletions src/backend/routes/views/clans/inviteAccept.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const decodingJWT = (token) => {
if (token !== null) {
const base64String = token.split('.')[1]
return JSON.parse(Buffer.from(base64String,
'base64').toString('ascii'))
}
return null
}

exports = module.exports = async function (req, res) {
if (!req.query.token) {
await req.asyncFlash('error', 'The invitation link is invalid!')

return res.redirect('/clans')
}
const token = req.query.token

const decodedToken = decodingJWT(req.query.token)

if (decodedToken === null || decodedToken.clan?.name === undefined) {
await req.asyncFlash('error', 'The invitation link is invalid!')

return res.redirect('/clans')
}

if (req.requestContainer.get('UserService').getUser()?.clan) {
await req.asyncFlash('error', 'You are already in a clan')

return res.redirect('/clans/view/' + req.requestContainer.get('UserService').getUser().clan.id)
}

res.render('clans/accept_invite', {
acceptURL: `/clans/join?token=${token}`,
clanName: decodedToken.clan.name
})
}
33 changes: 33 additions & 0 deletions src/backend/routes/views/clans/join.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { JavaApiError } = require('../../../services/ApiErrors')

exports = module.exports = async function (req, res) {
if (!req.query.token) {
await req.asyncFlash('error', 'The invitation link is invalid!')

return res.redirect('/clans')
}
const token = req.query.token

try {
await req.requestContainer.get('ClanManagementService').acceptInvitation(token)
await req.asyncFlash('info', 'Clan joined!')

return res.redirect('/clans/view/' + req.requestContainer.get('UserService').getUser().clan.id)
} catch (e) {
console.log(e.stack)
let message = e.toString()

if (e instanceof JavaApiError && e.error?.errors) {
message = e.error.errors[0].detail

if (e.error.errors[0].code === '152') {
await req.asyncFlash('error', message)
return res.redirect('/clans')
}
}

await req.asyncFlash('error', message)

return res.redirect('/clans')
}
}
33 changes: 33 additions & 0 deletions src/backend/routes/views/clans/kick.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { JavaApiError } = require('../../../services/ApiErrors')

exports = module.exports = async function (req, res) {
const memberId = parseInt(req.params.memberId || null)
if (!memberId) {
await req.asyncFlash('error', 'memberId is required')

return res.redirect('/clans')
}

const user = req.requestContainer.get('UserService').getUser()

if (!user.clan) {
await req.asyncFlash('info', 'You are not in a clan')

return res.redirect('/clans')
}

try {
await req.requestContainer.get('ClanManagementService').kickMember(memberId)

return res.redirect('/clans/view/' + req.requestContainer.get('UserService').getUser().clan.id)
} catch (e) {
let message = e.toString()
if (e instanceof JavaApiError && e.error?.errors) {
message = e.error.errors[0].detail
}

await req.asyncFlash('error', message)

return res.redirect('/clans/view/' + req.requestContainer.get('UserService').getUser().clan.id)
}
}
32 changes: 32 additions & 0 deletions src/backend/routes/views/clans/leave.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { JavaApiError } = require('../../../services/ApiErrors')

exports = module.exports = async function (req, res) {
const user = req.requestContainer.get('UserService').getUser()

if (!user.clan.membershipId) {
await req.asyncFlash('info', 'You are not in a clan')

return res.redirect('/clans')
}

if (req.method === 'POST') {
try {
await req.requestContainer.get('ClanManagementService').leaveClan()
await req.asyncFlash('info', 'Clan left')

return res.redirect('/clans')
} catch (e) {
console.error(e.stack)
let message = e.toString()
if (e instanceof JavaApiError && e.error?.errors) {
message = e.error.errors[0].detail
}

await req.asyncFlash('error', message)

return res.redirect('/clans')
}
}

return res.render('clans/leave')
}
Loading

0 comments on commit e954663

Please sign in to comment.