Binato/server/PrivateChatManager.ts

41 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-09-10 12:59:22 +01:00
import Channel from "./objects/Channel";
2023-08-20 13:03:01 +01:00
import { ConsoleHelper } from "../ConsoleHelper";
2023-09-10 12:59:22 +01:00
import FunkyArray from "./objects/FunkyArray";
import User from "./objects/User";
import Shared from "./objects/Shared";
import osu from "../osuTyping";
import PrivateChannel from "./objects/PrivateChannel";
2023-08-20 13:03:01 +01:00
2023-09-10 12:59:22 +01:00
export default class PrivateChatManager {
2023-08-20 13:03:01 +01:00
public chatChannels:FunkyArray<PrivateChannel> = new FunkyArray<PrivateChannel>();
private readonly shared:Shared;
public constructor(shared:Shared) {
this.shared = shared;
}
public AddChannel(user0:User, user1:User) : PrivateChannel {
const stream = this.shared.streams.CreateStream(`private_channel:${user0.username},${user1.username}`, true);
const channel = new PrivateChannel(user0, user1, stream);
this.chatChannels.add(channel.name, channel);
ConsoleHelper.printChat(`Created private chat channel [${channel.name}]`);
return channel;
}
public RemoveChannel(channel:PrivateChannel | string) {
if (channel instanceof Channel) {
channel.stream.Delete();
this.chatChannels.remove(channel.stream.name);
} else {
const chatChannel = this.GetChannelByName(channel);
if (chatChannel instanceof Channel) {
chatChannel.stream.Delete();
this.chatChannels.remove(chatChannel.stream.name);
}
}
}
public GetChannelByName(channelName:string) : PrivateChannel | undefined {
return this.chatChannels.getByKey(channelName);
}
}