You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
3.0 KiB
125 lines
3.0 KiB
// chat.js
|
|
// Copyright (C) 2024 DTP Technologies, LLC
|
|
// All Rights Reserved
|
|
|
|
'use strict';
|
|
|
|
import mongoose from 'mongoose';
|
|
// const AuthToken = mongoose.model('AuthToken');
|
|
const ChatRoom = mongoose.model('ChatRoom');
|
|
const ChatMessage = mongoose.model('ChatMessage');
|
|
// const ChatRoomInvite = mongoose.model('ChatRoomInvite');
|
|
|
|
import { SiteService, SiteError } from '../../lib/site-lib.js';
|
|
import { MAX_ROOM_CAPACITY } from '../models/lib/constants.js';
|
|
|
|
export default class ChatService extends SiteService {
|
|
|
|
static get name ( ) { return 'ChatService'; }
|
|
static get slug () { return 'chat'; }
|
|
|
|
constructor (dtp) {
|
|
super(dtp, ChatService);
|
|
}
|
|
|
|
async start ( ) {
|
|
const { user: userService } = this.dtp.services;
|
|
this.populateChatRoom = [
|
|
{
|
|
path: 'owner',
|
|
select: userService.USER_SELECT,
|
|
},
|
|
];
|
|
}
|
|
|
|
async createRoom (owner, roomDefinition) {
|
|
const { text: textService } = this.dtp.services;
|
|
const NOW = new Date();
|
|
|
|
const room = new ChatRoom();
|
|
room.created = NOW;
|
|
room.owner = owner._id;
|
|
room.name = textService.filter(roomDefinition.name);
|
|
if (roomDefinition.topic) {
|
|
room.topic = textService.filter(roomDefinition.topic);
|
|
}
|
|
room.capacity = MAX_ROOM_CAPACITY;
|
|
room.members = [owner._id];
|
|
|
|
await room.save();
|
|
|
|
return room.toObject();
|
|
}
|
|
|
|
async destroyRoom (user, room) {
|
|
if (user._id.equals(room.owner._id)) {
|
|
throw new SiteError(401, 'This is not your chat room');
|
|
}
|
|
await this.removeMessagesForChannel(room);
|
|
await ChatRoom.deleteOne({ _id: room._id });
|
|
}
|
|
|
|
async joinRoom (room, user) {
|
|
await ChatRoom.updateOne(
|
|
{ _id: room._id },
|
|
{
|
|
$push: { members: user._id },
|
|
},
|
|
);
|
|
}
|
|
|
|
async leaveRoom (room, user) {
|
|
await ChatRoom.updateOne(
|
|
{ _id: room._id },
|
|
{
|
|
$pull: { members: user._id },
|
|
},
|
|
);
|
|
}
|
|
|
|
async getRoomMemberList (room) {
|
|
const roomData = await ChatRoom.findOne({ _id: room._id }).select('members');
|
|
if (!roomData) {
|
|
throw new SiteError(404, 'Room not found');
|
|
}
|
|
return roomData.members;
|
|
}
|
|
|
|
async getRoomBlockList (room) {
|
|
const roomData = await ChatRoom.findOne({ _id: room._id }).select('members');
|
|
if (!roomData) {
|
|
throw new SiteError(404, 'Room not found');
|
|
}
|
|
return roomData.banned;
|
|
}
|
|
|
|
async getRoomById (roomId) {
|
|
const room = await ChatRoom
|
|
.findOne({ _id: roomId })
|
|
.populate(this.populateChatRoom)
|
|
.lean();
|
|
return room;
|
|
}
|
|
|
|
async getRoomsForOwner (owner) {
|
|
const rooms = await ChatRoom
|
|
.find({ owner: owner._id })
|
|
.populate(this.populateChatRoom)
|
|
.lean();
|
|
return rooms;
|
|
}
|
|
|
|
async getRoomsForMember (member, pagination) {
|
|
const rooms = await ChatRoom
|
|
.find({ members: member._id })
|
|
.populate(this.populateChatRoom)
|
|
.skip(pagination.skip)
|
|
.limit(pagination.cpp)
|
|
.lean();
|
|
return rooms;
|
|
}
|
|
|
|
async removeMessagesForChannel (channel) {
|
|
await ChatMessage.deleteMany({ channel: channel._id });
|
|
}
|
|
}
|