mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-11 19:44:15 +00:00
support for moderation
This commit is contained in:
parent
7dc84241f2
commit
ab171c150a
@ -605,6 +605,8 @@ export default class Player extends Core {
|
|||||||
//
|
//
|
||||||
|
|
||||||
async play(instance, params = {}) {
|
async play(instance, params = {}) {
|
||||||
|
this.state.startingNew = true
|
||||||
|
|
||||||
if (typeof instance === "number") {
|
if (typeof instance === "number") {
|
||||||
instance = this.audioQueue[instance]
|
instance = this.audioQueue[instance]
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,10 @@ class MusicSyncSubCore {
|
|||||||
return this.currentRoomData
|
return this.currentRoomData
|
||||||
}.bind(this),
|
}.bind(this),
|
||||||
eventBus: this.eventBus,
|
eventBus: this.eventBus,
|
||||||
|
moderation: {
|
||||||
|
kickUser: this.kickUser.bind(this),
|
||||||
|
transferOwner: this.transferOwner.bind(this),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
hubEvents = {
|
hubEvents = {
|
||||||
@ -110,6 +114,8 @@ class MusicSyncSubCore {
|
|||||||
|
|
||||||
app.cores.player.toogleSyncMode(true, !isSelf)
|
app.cores.player.toogleSyncMode(true, !isSelf)
|
||||||
|
|
||||||
|
app.cores.player.playback.stop()
|
||||||
|
|
||||||
this.eventBus.emit("room:owner:changed", data)
|
this.eventBus.emit("room:owner:changed", data)
|
||||||
},
|
},
|
||||||
// Room Control
|
// Room Control
|
||||||
@ -155,7 +161,25 @@ class MusicSyncSubCore {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
"room:moderation:kicked": (data) => {
|
||||||
|
console.log("room:moderation:kicked", data)
|
||||||
|
|
||||||
|
this.dettachCard()
|
||||||
|
|
||||||
|
this.currentRoomData = null
|
||||||
|
|
||||||
|
app.cores.player.toogleSyncMode(false, false)
|
||||||
|
|
||||||
|
app.notification.new({
|
||||||
|
title: "Kicked",
|
||||||
|
description: "You have been kicked from the sync room"
|
||||||
|
}, {
|
||||||
|
type: "error",
|
||||||
|
})
|
||||||
|
|
||||||
|
this.eventBus.emit("room:moderation:kicked", data)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
async onInitialize() {
|
async onInitialize() {
|
||||||
@ -270,6 +294,32 @@ class MusicSyncSubCore {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
kickUser(userId) {
|
||||||
|
if (!this.currentRoomData) {
|
||||||
|
console.warn(`Not joined any room`)
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
this.musicWs.emit("room:moderation:kick", {
|
||||||
|
room_id: this.currentRoomData.roomId,
|
||||||
|
user_id: userId,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
transferOwner(userId) {
|
||||||
|
if (!this.currentRoomData) {
|
||||||
|
console.warn(`Not joined any room`)
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
this.musicWs.emit("room:moderation:transfer_ownership", {
|
||||||
|
room_id: this.currentRoomData.roomId,
|
||||||
|
user_id: userId,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class SyncCore extends Core {
|
export default class SyncCore extends Core {
|
||||||
|
@ -88,12 +88,72 @@ class Room {
|
|||||||
|
|
||||||
this.io.to(this.roomId).emit("music:player:status", composePayloadData(socket, data))
|
this.io.to(this.roomId).emit("music:player:status", composePayloadData(socket, data))
|
||||||
},
|
},
|
||||||
"music:owner:state_update": (socket, data) => {
|
// ROOM CONTROL
|
||||||
|
"room:moderation:kick": (socket, data) => {
|
||||||
if (socket.userData._id !== this.ownerUserId) {
|
if (socket.userData._id !== this.ownerUserId) {
|
||||||
return false
|
return socket.emit("error", {
|
||||||
|
message: "You are not the owner of this room, cannot kick this user",
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
this.currentState = data
|
const { room_id, user_id } = data
|
||||||
|
|
||||||
|
if (this.roomId !== room_id) {
|
||||||
|
console.warn(`[${socket.id}][@${socket.userData.username}] not connected to room ${room_id}, cannot kick`)
|
||||||
|
|
||||||
|
return socket.emit("error", {
|
||||||
|
message: "You are not connected to requested room, cannot kick this user",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const socket_conn = this.connections.find((socket_conn) => {
|
||||||
|
return socket_conn.userData._id === user_id
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!socket_conn) {
|
||||||
|
console.warn(`[${socket.id}][@${socket.userData.username}] not found user ${user_id} in room ${room_id}, cannot kick`)
|
||||||
|
|
||||||
|
return socket.emit("error", {
|
||||||
|
message: "User not found in room, cannot kick",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
socket_conn.emit("room:moderation:kicked", {
|
||||||
|
room_id,
|
||||||
|
})
|
||||||
|
|
||||||
|
this.leave(socket_conn)
|
||||||
|
},
|
||||||
|
"room:moderation:transfer_ownership": (socket, data) => {
|
||||||
|
if (socket.userData._id !== this.ownerUserId) {
|
||||||
|
return socket.emit("error", {
|
||||||
|
message: "You are not the owner of this room, cannot transfer ownership",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const { room_id, user_id } = data
|
||||||
|
|
||||||
|
if (this.roomId !== room_id) {
|
||||||
|
console.warn(`[${socket.id}][@${socket.userData.username}] not connected to room ${room_id}, cannot transfer ownership`)
|
||||||
|
|
||||||
|
return socket.emit("error", {
|
||||||
|
message: "You are not connected to requested room, cannot transfer ownership",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const socket_conn = this.connections.find((socket_conn) => {
|
||||||
|
return socket_conn.userData._id === user_id
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!socket_conn) {
|
||||||
|
console.warn(`[${socket.id}][@${socket.userData.username}] not found user ${user_id} in room ${room_id}, cannot transfer ownership`)
|
||||||
|
|
||||||
|
return socket.emit("error", {
|
||||||
|
message: "User not found in room, cannot transfer ownership",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
this.transferOwner(socket_conn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,6 +284,8 @@ class Room {
|
|||||||
this.io.to(this.roomId).emit("room:owner:changed", {
|
this.io.to(this.roomId).emit("room:owner:changed", {
|
||||||
ownerUserId: this.ownerUserId,
|
ownerUserId: this.ownerUserId,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
this.sendRoomData()
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy = () => {
|
destroy = () => {
|
||||||
@ -359,34 +421,7 @@ export default class RoomsServer {
|
|||||||
connectionPool = []
|
connectionPool = []
|
||||||
|
|
||||||
events = {
|
events = {
|
||||||
"connection": (socket) => {
|
|
||||||
console.log(`[${socket.id}][${socket.userData.username}] connected to hub.`)
|
|
||||||
|
|
||||||
this.connectionPool.push(socket)
|
|
||||||
|
|
||||||
socket.on("disconnect", () => this.events.disconnect)
|
|
||||||
|
|
||||||
// Rooms
|
|
||||||
socket.on("join:room", (data) => this.RoomsController.connectSocketToRoom(socket, data.room, data.options))
|
|
||||||
socket.on("leave:room", (data) => this.RoomsController.disconnectSocketFromRoom(socket, data?.room ?? socket.connectedRoomId, data?.options ?? {}))
|
|
||||||
socket.on("invite:user", generateFnHandler(this.inviteUserToRoom, socket))
|
|
||||||
|
|
||||||
socket.on("ping", (callback) => {
|
|
||||||
callback()
|
|
||||||
})
|
|
||||||
|
|
||||||
socket.on("disconnect", (_socket) => {
|
|
||||||
console.log(`[${socket.id}][@${socket.userData.username}] disconnected to hub.`)
|
|
||||||
|
|
||||||
if (socket.connectedRoomId) {
|
|
||||||
console.log(`[${socket.id}][@${socket.userData.username}] was connected to room [${socket.connectedRoomId}], leaving...`)
|
|
||||||
this.RoomsController.disconnectSocketFromRoom(socket)
|
|
||||||
}
|
|
||||||
|
|
||||||
// remove from connection pool
|
|
||||||
this.connectionPool = this.connectionPool.filter((client) => client.id !== socket.id)
|
|
||||||
})
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inviteUserToRoom = async (socket, data) => {
|
inviteUserToRoom = async (socket, data) => {
|
||||||
@ -430,14 +465,47 @@ export default class RoomsServer {
|
|||||||
initialize = async () => {
|
initialize = async () => {
|
||||||
this.io.use(withWsAuth)
|
this.io.use(withWsAuth)
|
||||||
|
|
||||||
Object.entries(this.events).forEach(([event, handler]) => {
|
this.io.on("connection", (socket) => {
|
||||||
this.io.on(event, (socket) => {
|
try {
|
||||||
try {
|
console.log(`[${socket.id}][${socket.userData.username}] connected to hub.`)
|
||||||
handler(socket)
|
|
||||||
} catch (error) {
|
this.connectionPool.push(socket)
|
||||||
console.error(error)
|
|
||||||
}
|
socket.on("disconnect", () => this.events.disconnect)
|
||||||
})
|
|
||||||
|
// Rooms
|
||||||
|
socket.on("join:room", (data) => this.RoomsController.connectSocketToRoom(socket, data.room, data.options))
|
||||||
|
socket.on("leave:room", (data) => this.RoomsController.disconnectSocketFromRoom(socket, data?.room ?? socket.connectedRoomId, data?.options ?? {}))
|
||||||
|
socket.on("invite:user", generateFnHandler(this.inviteUserToRoom, socket))
|
||||||
|
|
||||||
|
socket.on("ping", (callback) => {
|
||||||
|
callback()
|
||||||
|
})
|
||||||
|
|
||||||
|
socket.on("disconnect", (_socket) => {
|
||||||
|
console.log(`[${socket.id}][@${socket.userData.username}] disconnected to hub.`)
|
||||||
|
|
||||||
|
if (socket.connectedRoomId) {
|
||||||
|
console.log(`[${socket.id}][@${socket.userData.username}] was connected to room [${socket.connectedRoomId}], leaving...`)
|
||||||
|
this.RoomsController.disconnectSocketFromRoom(socket)
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove from connection pool
|
||||||
|
this.connectionPool = this.connectionPool.filter((client) => client.id !== socket.id)
|
||||||
|
})
|
||||||
|
|
||||||
|
Object.entries(this.events).forEach(([event, handler]) => {
|
||||||
|
socket.on(event, (data) => {
|
||||||
|
try {
|
||||||
|
handler(socket, data)
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user