Undid the IPacketTarget system.

This commit is contained in:
flash 2023-02-16 23:47:30 +01:00
parent 13ae843c8d
commit a5089f14b8
16 changed files with 29 additions and 62 deletions

View file

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
namespace SharpChat {
public class ChatChannel : IPacketTarget {
public class ChatChannel {
public string Name { get; set; }
public string Password { get; set; } = string.Empty;
public bool IsTemporary { get; set; } = false;
@ -16,8 +16,6 @@ namespace SharpChat {
public bool HasPassword
=> !string.IsNullOrWhiteSpace(Password);
public string TargetName => Name;
public ChatChannel() { }
public ChatChannel(string name) {

View file

@ -74,7 +74,7 @@ namespace SharpChat {
conn.Send(new AuthSuccessPacket(user, chan, conn, maxMsgLength));
conn.Send(new ContextUsersPacket(chan.GetUsers(new[] { user })));
foreach(IChatEvent msg in Events.GetTargetEventLog(chan.Name))
foreach(IChatEvent msg in Events.GetChannelEventLog(chan.Name))
conn.Send(new ContextMessagePacket(msg));
lock(ChannelsAccess)
@ -148,7 +148,7 @@ namespace SharpChat {
SendTo(user, new ContextClearPacket(chan, ContextClearMode.MessagesUsers));
SendTo(user, new ContextUsersPacket(chan.GetUsers(new[] { user })));
foreach(IChatEvent msg in Events.GetTargetEventLog(chan.Name))
foreach(IChatEvent msg in Events.GetChannelEventLog(chan.Name))
SendTo(user, new ContextMessagePacket(msg));
ForceChannel(user, chan);

View file

@ -78,15 +78,13 @@ namespace SharpChat {
}
}
public class ChatUser : BasicUser, IPacketTarget {
public class ChatUser : BasicUser {
public DateTimeOffset SilencedUntil { get; set; }
private readonly List<ChatChannel> Channels = new();
public readonly ChatRateLimiter RateLimiter = new();
public string TargetName => "@log";
public ChatChannel Channel => Channels.FirstOrDefault();
// This needs to be a session thing

View file

@ -18,8 +18,7 @@ namespace SharpChat.Commands {
return null;
return new ChatMessage {
Target = ctx.Channel,
TargetName = ctx.Channel.TargetName,
ChannelName = ctx.Channel.Name,
DateTime = DateTimeOffset.UtcNow,
Sender = ctx.User,
Text = string.Join(' ', ctx.Args),

View file

@ -32,16 +32,14 @@ namespace SharpChat.Commands {
ctx.Chat.SendTo(whisperUser, new ChatMessageAddPacket(new ChatMessage {
DateTime = DateTimeOffset.Now,
Target = whisperUser,
TargetName = whisperUser.TargetName,
ChannelName = string.Empty,
Sender = ctx.User,
Text = whisperStr,
Flags = ChatMessageFlags.Private,
}));
ctx.Chat.SendTo(ctx.User, new ChatMessageAddPacket(new ChatMessage {
DateTime = DateTimeOffset.Now,
Target = whisperUser,
TargetName = whisperUser.TargetName,
ChannelName = string.Empty,
Sender = ctx.User,
Text = $"{whisperUser.DisplayName} {whisperStr}",
Flags = ChatMessageFlags.Private,

View file

@ -7,6 +7,6 @@ namespace SharpChat.EventStorage {
void AddEvent(IChatEvent evt);
void RemoveEvent(IChatEvent evt);
IChatEvent GetEvent(long seqId);
IEnumerable<IChatEvent> GetTargetEventLog(string target, int amount = 20, int offset = 0);
IEnumerable<IChatEvent> GetChannelEventLog(string channelName, int amount = 20, int offset = 0);
}
}

View file

@ -28,7 +28,7 @@ namespace SharpChat.EventStorage {
new MySqlParameter("id", evt.SequenceId),
new MySqlParameter("created", evt.DateTime.ToUnixTimeSeconds()),
new MySqlParameter("type", evt.GetType().FullName),
new MySqlParameter("target", evt.Target.TargetName),
new MySqlParameter("target", evt.ChannelName),
new MySqlParameter("flags", (byte)evt.Flags),
new MySqlParameter("data", JsonSerializer.SerializeToUtf8Bytes(evt, evt.GetType())),
new MySqlParameter("sender", evt.Sender?.UserId < 1 ? null : (long?)evt.Sender.UserId),
@ -67,7 +67,7 @@ namespace SharpChat.EventStorage {
Type evtType = Type.GetType(Encoding.ASCII.GetString((byte[])reader["event_type"]));
IChatEvent evt = JsonSerializer.Deserialize(Encoding.ASCII.GetString((byte[])reader["event_data"]), evtType) as IChatEvent;
evt.SequenceId = reader.GetInt64("event_id");
evt.TargetName = Encoding.ASCII.GetString((byte[])reader["event_target"]);
evt.ChannelName = Encoding.ASCII.GetString((byte[])reader["event_target"]);
evt.Flags = (ChatMessageFlags)reader.GetByte("event_flags");
evt.DateTime = DateTimeOffset.FromUnixTimeSeconds(reader.GetInt32("event_created"));
@ -85,7 +85,7 @@ namespace SharpChat.EventStorage {
return evt;
}
public IEnumerable<IChatEvent> GetTargetEventLog(string target, int amount = 20, int offset = 0) {
public IEnumerable<IChatEvent> GetChannelEventLog(string channelName, int amount = 20, int offset = 0) {
List<IChatEvent> events = new();
try {
@ -98,7 +98,7 @@ namespace SharpChat.EventStorage {
+ " AND `event_id` > @offset"
+ " ORDER BY `event_id` DESC"
+ " LIMIT @amount",
new MySqlParameter("target", target),
new MySqlParameter("target", channelName),
new MySqlParameter("amount", amount),
new MySqlParameter("offset", offset)
);

View file

@ -23,8 +23,8 @@ namespace SharpChat.EventStorage {
Events.Remove(evt.SequenceId);
}
public IEnumerable<IChatEvent> GetTargetEventLog(string target, int amount = 20, int offset = 0) {
IEnumerable<IChatEvent> subset = Events.Values.Where(ev => ev.TargetName == target);
public IEnumerable<IChatEvent> GetChannelEventLog(string channelName, int amount = 20, int offset = 0) {
IEnumerable<IChatEvent> subset = Events.Values.Where(ev => ev.ChannelName == channelName);
int start = subset.Count() - offset - amount;
if(start < 0) {

View file

@ -7,10 +7,7 @@ namespace SharpChat.Events {
public BasicUser Sender { get; set; }
[JsonIgnore]
public IPacketTarget Target { get; set; }
[JsonIgnore]
public string TargetName { get; set; }
public string ChannelName { get; set; }
[JsonIgnore]
public DateTimeOffset DateTime { get; set; }

View file

@ -13,8 +13,7 @@ namespace SharpChat.Events {
public interface IChatEvent {
DateTimeOffset DateTime { get; set; }
BasicUser Sender { get; set; }
IPacketTarget Target { get; set; }
string TargetName { get; set; }
string ChannelName { get; set; }
ChatMessageFlags Flags { get; set; }
long SequenceId { get; set; }
}

View file

@ -10,10 +10,7 @@ namespace SharpChat.Events {
public BasicUser Sender { get; set; }
[JsonIgnore]
public IPacketTarget Target { get; set; }
[JsonIgnore]
public string TargetName { get; set; }
public string ChannelName { get; set; }
[JsonIgnore]
public ChatMessageFlags Flags { get; set; } = ChatMessageFlags.Log;
@ -22,11 +19,10 @@ namespace SharpChat.Events {
public long SequenceId { get; set; }
public UserChannelJoinEvent() { }
public UserChannelJoinEvent(DateTimeOffset joined, BasicUser user, IPacketTarget target) {
public UserChannelJoinEvent(DateTimeOffset joined, BasicUser user, ChatChannel channel) {
DateTime = joined;
Sender = user;
Target = target;
TargetName = target?.TargetName;
ChannelName = channel.Name;
}
}
}

View file

@ -10,10 +10,7 @@ namespace SharpChat.Events {
public BasicUser Sender { get; set; }
[JsonIgnore]
public IPacketTarget Target { get; set; }
[JsonIgnore]
public string TargetName { get; set; }
public string ChannelName { get; set; }
[JsonIgnore]
public ChatMessageFlags Flags { get; set; } = ChatMessageFlags.Log;
@ -22,11 +19,10 @@ namespace SharpChat.Events {
public long SequenceId { get; set; }
public UserChannelLeaveEvent() { }
public UserChannelLeaveEvent(DateTimeOffset parted, BasicUser user, IPacketTarget target) {
public UserChannelLeaveEvent(DateTimeOffset parted, BasicUser user, ChatChannel channel) {
DateTime = parted;
Sender = user;
Target = target;
TargetName = target?.TargetName;
ChannelName = channel.Name;
}
}
}

View file

@ -10,10 +10,7 @@ namespace SharpChat.Events {
public BasicUser Sender { get; set; }
[JsonIgnore]
public IPacketTarget Target { get; set; }
[JsonIgnore]
public string TargetName { get; set; }
public string ChannelName { get; set; }
[JsonIgnore]
public ChatMessageFlags Flags { get; set; } = ChatMessageFlags.Log;
@ -22,11 +19,10 @@ namespace SharpChat.Events {
public long SequenceId { get; set; }
public UserConnectEvent() { }
public UserConnectEvent(DateTimeOffset joined, BasicUser user, IPacketTarget target) {
public UserConnectEvent(DateTimeOffset joined, BasicUser user, ChatChannel channel) {
DateTime = joined;
Sender = user;
Target = target;
TargetName = target?.TargetName;
ChannelName = channel.Name;
}
}
}

View file

@ -12,10 +12,7 @@ namespace SharpChat.Events {
public BasicUser Sender { get; set; }
[JsonIgnore]
public IPacketTarget Target { get; set; }
[JsonIgnore]
public string TargetName { get; set; }
public string ChannelName { get; set; }
[JsonIgnore]
public ChatMessageFlags Flags { get; set; } = ChatMessageFlags.Log;
@ -27,11 +24,10 @@ namespace SharpChat.Events {
public UserDisconnectReason Reason { get; set; }
public UserDisconnectEvent() { }
public UserDisconnectEvent(DateTimeOffset parted, BasicUser user, IPacketTarget target, UserDisconnectReason reason) {
public UserDisconnectEvent(DateTimeOffset parted, BasicUser user, ChatChannel channel, UserDisconnectReason reason) {
DateTime = parted;
Sender = user;
Target = target;
TargetName = target?.TargetName;
ChannelName = channel.Name;
Reason = reason;
}
}

View file

@ -1,5 +0,0 @@
namespace SharpChat {
public interface IPacketTarget {
string TargetName { get; }
}
}

View file

@ -88,8 +88,7 @@ namespace SharpChat.PacketHandlers {
}
message ??= new ChatMessage {
Target = channel,
TargetName = channel.TargetName,
ChannelName = channel.Name,
DateTime = DateTimeOffset.UtcNow,
Sender = user,
Text = messageText,