This repository has been archived on 2025-03-10. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
2024-04-07 20:08:20 +01:00

125 lines
3.5 KiB
JavaScript

/**
* Represents a chat room.
*/
class Room {
/**
* Create a new room instance.
* @param {string} id - The unique identifier of the room.
* @param {Object} [config={}] - The configuration options for the room.
*/
constructor(id, config = {}) {
this.id = id;
this.members = new Map();
this.config = {
notifyMuteUnmute: config.notifyMuteUnmute || false,
notifyKick: config.notifyKick || false,
inactivityTimeout: config.inactivityTimeout || 60_000,
};
}
/**
* Add a member to the room.
* @param {string} memberId - The unique identifier of the member.
* @param {Object} memberData - Additional data associated with the member.
*/
add(memberId, memberData) {
this.members.set(memberId, {
...memberData,
lastActivity: Date.now(),
});
}
/**
* Remove a member from the room.
* @param {string} memberId - The unique identifier of the member.
*/
remove(memberId) {
this.members.delete(memberId);
}
/**
* Get the data of a member in the room.
* @param {string} memberId - The unique identifier of the member.
* @returns {Object|undefined} The member data or undefined if the member is not found.
*/
get(memberId) {
return this.members.get(memberId);
}
/**
* Check if a member is muted in the room.
* @param {string} memberId - The unique identifier of the member.
* @returns {boolean} True if the member is muted, false otherwise.
*/
isMuted(memberId) {
const memberData = this.get(memberId);
return memberData ? memberData.muted : false;
}
/**
* Check if a member is kicked from the room.
* @param {string} memberId - The unique identifier of the member.
* @returns {boolean} True if the member is kicked, false otherwise.
*/
isKicked(memberId) {
const memberData = this.get(memberId);
return memberData ? memberData.kicked : false;
}
/**
* Mute a member in the room.
* @param {string} memberId - The unique identifier of the member.
*/
mute(memberId) {
const memberData = this.get(memberId);
if (memberData) {
memberData.muted = true;
}
}
/**
* Unmute a member in the room.
* @param {string} memberId - The unique identifier of the member.
*/
unmute(memberId) {
const memberData = this.get(memberId);
if (memberData) {
memberData.muted = false;
}
}
/**
* Kick a member from the room.
* @param {string} memberId - The unique identifier of the member.
*/
kick(memberId) {
const memberData = this.get(memberId);
if (memberData) {
memberData.kicked = Date.now();
}
}
/**
* Update the last activity timestamp of a member in the room.
* @param {string} memberId - The unique identifier of the member.
*/
update(memberId) {
const memberData = this.get(memberId);
if (memberData) {
memberData.lastActivity = Date.now();
}
}
/**
* Purge inactive members from the room based on the inactivity timeout.
*/
purge() {
const now = Date.now();
for (const [memberId, memberData] of this.members.entries()) {
if (now - memberData.lastActivity > this.config.inactivityTimeout) {
this.kick(memberId);
}
}
}
}