Skip to content

Commit

Permalink
Merge branch 'release/1.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Sep 23, 2023
2 parents e7b708c + 2229e97 commit ede4b87
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "adaptto-live-server",
"version": "1.4.1",
"version": "1.5.0",
"private": true,
"scripts": {
"dev": "nodemon src/index.ts | pino-pretty",
Expand Down
2 changes: 2 additions & 0 deletions src/repository/mongodb.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface User {
code: string
username: string
admin: boolean
qaadmin: boolean
blocked: boolean
created: Date
updated: Date
Expand All @@ -27,6 +28,7 @@ const UserSchema = new mongoose.Schema<User>({
code: { type: String, required:true, index:true },
username: { type: String, required:true, index:true },
admin: { type: Boolean, required:false },
qaadmin: { type: Boolean, required:false },
blocked: { type: Boolean, required:false },
created: { type: Date, required:true },
updated: { type: Date, required:false }
Expand Down
1 change: 1 addition & 0 deletions src/repository/validation.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ export const userObject = Joi.object({
id: uuidString.required(),
username: usernameString.required(),
admin: Joi.bool(),
qaadmin: Joi.bool(),
blocked: Joi.bool()
})
6 changes: 4 additions & 2 deletions src/socket/admin/userManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export async function handleAdminUserManagement(socket : Socket<ClientToServerEv
log.debug('Admin: get users')
const users = await UserModel.find().sort({username:1}).exec()
socket.emit('adminUsers', users.map(user =>
({id: user.id, code: user.code, username: user.username, admin: user.admin, blocked: user.blocked,
({id: user.id, code: user.code, username: user.username,
admin: user.admin, qaadmin: user.qaadmin, blocked: user.blocked,
created: user.created, updated: user.updated})))
})

Expand All @@ -28,13 +29,14 @@ export async function handleAdminUserManagement(socket : Socket<ClientToServerEv
return
}

const {id, username, admin, blocked} = userData
const {id, username, admin, qaadmin, blocked} = userData
log.debug(`Admin: update user ${username}`)
const user = await UserModel.findOne({_id:id}).sort({username:1}).exec()
if (user) {
const userNameChanged = user.username != username
user.username = username
user.admin = admin
user.qaadmin = qaadmin
user.blocked = blocked
user.updated = new Date()
await user.save()
Expand Down
4 changes: 2 additions & 2 deletions src/socket/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { UserModel } from '../repository/mongodb.schema'
import changeUsernameInAllDocuments from '../util/changeUsernameInAllDocuments'

export async function handleConnection(socket : Socket<ClientToServerEvents,ServerToClientEvents,InterServerEvents,SocketData>) {
const { userid, username, admin, usernameChanged } = socket.data
const { userid, username, admin, qaadmin, usernameChanged } = socket.data
if (!userid || !username) {
return
}
Expand All @@ -24,7 +24,7 @@ export async function handleConnection(socket : Socket<ClientToServerEvents,Serv
}

log.debug(`User connected: ${username}`)
socket.emit('login', userid, admin ?? false)
socket.emit('login', userid, admin ?? false, qaadmin ?? false)

// user disconnects
socket.on('disconnect', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/socket/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export async function middleware(
socket.data.userid = user._id
socket.data.username = username
socket.data.admin = user.admin
socket.data.qaadmin = user.qaadmin
socket.data.usernameChanged = (user.username != username)
next()
return
Expand All @@ -55,6 +56,7 @@ export async function middleware(
socket.data.userid = userid
socket.data.username = username
socket.data.admin = false
socket.data.qaadmin = false
socket.data.usernameChanged = false
next()
}
Expand Down
1 change: 1 addition & 0 deletions src/socket/socket.server.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export interface SocketData {
userid: string
username: string
admin: boolean
qaadmin: boolean
usernameChanged: boolean
}
4 changes: 3 additions & 1 deletion src/socket/socket.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface ServerToClientEvents {
login: (userid: string, admin: boolean) => void
login: (userid: string, admin: boolean, qaadmin?: boolean) => void
currentTalk: (talkId: string) => void
talkRatings: (talkRatings: TalkRating[]) => void
roomUsers: (usernames : string[]) => void
Expand Down Expand Up @@ -100,6 +100,7 @@ export interface User {
code: string
username: string
admin: boolean
qaadmin: boolean
blocked: boolean
created: Date
updated: Date
Expand All @@ -109,6 +110,7 @@ export interface UserUpdate {
id: string
username: string
admin: boolean
qaadmin: boolean
blocked: boolean
}

Expand Down
12 changes: 8 additions & 4 deletions src/socket/talkRoomQAEntries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { qaEntryAnsweredToServerObject, qaEntryToServerObject, uuidString } from
import isInputValid from '../util/isInputValid'

export async function handleTalkRoomQAEntries(socket : Socket<ClientToServerEvents,ServerToClientEvents,InterServerEvents,SocketData>) {
const { userid, username, admin } = socket.data
const { userid, username, admin, qaadmin } = socket.data
if (!userid || !username) {
return
}
Expand Down Expand Up @@ -69,15 +69,15 @@ export async function handleTalkRoomQAEntries(socket : Socket<ClientToServerEven
const { id, answered } = updatedQaEntryAnswered
log.debug(`User ${username} updated Q&A entry answered ${id}: ${answered}`)
const message = await QAEntryModel.findById(id).exec()
if (message != null) {
if (message != null && isUpdateAnsweredAllowed(message)) {
message.answered = answered
await message.save()
callback({success: true})
socket.in(message.talkId).emit('qaEntryUpdate',
{id, date: message.date, userid: message.userid, username: message.username, text: message.text, highlight: message.highlight, answered: message.answered})
}
else {
callback({success: false, error: `QA entry ${id} not found.`})
callback({success: false, error: `QA entry ${id} not found or not allowed to update.`})
}
}

Expand All @@ -100,7 +100,11 @@ export async function handleTalkRoomQAEntries(socket : Socket<ClientToServerEven
}

function isEditAllowed(message: QAEntry) : boolean {
return message != null && ((message.userid == userid) || admin)
return message != null && ((message.userid == userid) || admin)
}

function isUpdateAnsweredAllowed(message: QAEntry) : boolean {
return message != null && ((message.userid == userid) || admin || qaadmin)
}

}
Expand Down

0 comments on commit ede4b87

Please sign in to comment.