164 lines
4.8 KiB
C#
164 lines
4.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace SharpChat.Events {
|
|
public class ChatEventDispatcher {
|
|
private readonly object HandlersAccess = new();
|
|
private readonly List<IChatEventHandler> Handlers = new();
|
|
|
|
public void Subscribe(IChatEventHandler handler) {
|
|
lock(HandlersAccess)
|
|
if(!Handlers.Contains(handler))
|
|
Handlers.Add(handler);
|
|
}
|
|
|
|
public void Unsubscribe(IChatEventHandler handler) {
|
|
lock(HandlersAccess)
|
|
Handlers.Remove(handler);
|
|
}
|
|
|
|
public ChatEventInfo Dispatch(ChatEventInfo info) {
|
|
lock(HandlersAccess)
|
|
foreach(IChatEventHandler handler in Handlers)
|
|
handler.HandleEvent(info);
|
|
|
|
return info;
|
|
}
|
|
|
|
public ChatEventInfo Dispatch(
|
|
string eventType,
|
|
long senderId,
|
|
string senderName,
|
|
Colour senderColour,
|
|
int senderRank,
|
|
string senderNickName,
|
|
UserPermissions senderPerms,
|
|
ChatEventData? eventData = null
|
|
) {
|
|
return Dispatch(new ChatEventInfo(
|
|
SharpId.Next(),
|
|
eventType,
|
|
DateTimeOffset.UtcNow,
|
|
string.Empty,
|
|
senderId,
|
|
senderName,
|
|
senderColour,
|
|
senderRank,
|
|
senderNickName,
|
|
senderPerms,
|
|
eventData
|
|
));
|
|
}
|
|
|
|
public ChatEventInfo Dispatch(
|
|
string eventType,
|
|
DateTimeOffset eventCreated,
|
|
string channelName,
|
|
UserInfo userInfo,
|
|
ChatEventData? eventData = null
|
|
) {
|
|
return Dispatch(new ChatEventInfo(
|
|
SharpId.Next(),
|
|
eventType,
|
|
eventCreated,
|
|
channelName,
|
|
userInfo.UserId,
|
|
userInfo.UserName,
|
|
userInfo.Colour,
|
|
userInfo.Rank,
|
|
userInfo.NickName,
|
|
userInfo.Permissions,
|
|
eventData
|
|
));
|
|
}
|
|
|
|
public ChatEventInfo Dispatch(
|
|
string eventType,
|
|
DateTimeOffset eventCreated,
|
|
ChannelInfo channelInfo,
|
|
UserInfo userInfo,
|
|
ChatEventData? eventData = null
|
|
) {
|
|
return Dispatch(new ChatEventInfo(
|
|
SharpId.Next(),
|
|
eventType,
|
|
eventCreated,
|
|
channelInfo.Name,
|
|
userInfo.UserId,
|
|
userInfo.UserName,
|
|
userInfo.Colour,
|
|
userInfo.Rank,
|
|
userInfo.NickName,
|
|
userInfo.Permissions,
|
|
eventData
|
|
));
|
|
}
|
|
|
|
public ChatEventInfo Dispatch(
|
|
string eventType,
|
|
string channelName,
|
|
ChatEventInfo userInfo,
|
|
ChatEventData? eventData = null
|
|
) {
|
|
return Dispatch(new ChatEventInfo(
|
|
SharpId.Next(),
|
|
eventType,
|
|
DateTimeOffset.UtcNow,
|
|
channelName,
|
|
userInfo.SenderId,
|
|
userInfo.SenderName,
|
|
userInfo.SenderColour,
|
|
userInfo.SenderRank,
|
|
userInfo.SenderNickName,
|
|
userInfo.SenderPerms,
|
|
eventData
|
|
));
|
|
}
|
|
|
|
public ChatEventInfo Dispatch(
|
|
string eventType,
|
|
string channelName,
|
|
UserInfo userInfo,
|
|
ChatEventData? eventData = null
|
|
) {
|
|
return Dispatch(new ChatEventInfo(
|
|
SharpId.Next(),
|
|
eventType,
|
|
DateTimeOffset.UtcNow,
|
|
channelName,
|
|
userInfo.UserId,
|
|
userInfo.UserName,
|
|
userInfo.Colour,
|
|
userInfo.Rank,
|
|
userInfo.NickName,
|
|
userInfo.Permissions,
|
|
eventData
|
|
));
|
|
}
|
|
|
|
public ChatEventInfo Dispatch(
|
|
string eventType,
|
|
ChannelInfo channelInfo,
|
|
UserInfo userInfo,
|
|
ChatEventData? eventData = null
|
|
) {
|
|
return Dispatch(eventType, channelInfo.Name, userInfo, eventData);
|
|
}
|
|
|
|
public ChatEventInfo Dispatch(
|
|
string eventType,
|
|
UserInfo userInfo,
|
|
ChatEventData? eventData = null
|
|
) {
|
|
return Dispatch(eventType, string.Empty, userInfo, eventData);
|
|
}
|
|
|
|
public ChatEventInfo Dispatch(
|
|
string eventType,
|
|
ChatEventInfo userInfo,
|
|
ChatEventData? eventData = null
|
|
) {
|
|
return Dispatch(eventType, string.Empty, userInfo, eventData);
|
|
}
|
|
}
|
|
}
|