Split name change notification out of UserUpdatePacket.

This commit is contained in:
flash 2024-05-10 15:07:56 +00:00
parent 54af837c82
commit fc7d428f76
3 changed files with 41 additions and 22 deletions

View file

@ -188,8 +188,12 @@ namespace SharpChat {
hasChanged = true; hasChanged = true;
} }
if(hasChanged) if(hasChanged) {
SendToUserChannels(user, new UserUpdatePacket(user, previousName)); if(previousName != null)
SendToUserChannels(user, new UserUpdateNotificationPacket(previousName, user.LegacyNameWithStatus));
SendToUserChannels(user, new UserUpdatePacket(user));
}
} }
public void BanUser(ChatUser user, TimeSpan duration, UserDisconnectReason reason = UserDisconnectReason.Kicked) { public void BanUser(ChatUser user, TimeSpan duration, UserDisconnectReason reason = UserDisconnectReason.Kicked) {

View file

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace SharpChat.Packet {
public class UserUpdateNotificationPacket : ServerPacket {
public string PreviousName { get; private set; }
public string NewName { get; private set; }
public DateTimeOffset Timestamp { get; }
public UserUpdateNotificationPacket(string previousName, string newName) {
PreviousName = previousName ?? throw new ArgumentNullException(nameof(previousName));
NewName = newName ?? throw new ArgumentNullException(nameof(newName));
Timestamp = DateTimeOffset.Now;
}
public override IEnumerable<string> Pack() {
StringBuilder sb = new();
sb.Append('2');
sb.Append('\t');
sb.Append(Timestamp.ToUnixTimeSeconds());
sb.Append("\t-1\t0\fnick\f");
sb.Append(PreviousName);
sb.Append('\f');
sb.Append(NewName);
sb.Append('\t');
sb.Append(SequenceId);
sb.Append("\t10010");
yield return sb.ToString();
}
}
}

View file

@ -5,33 +5,14 @@ using System.Text;
namespace SharpChat.Packet { namespace SharpChat.Packet {
public class UserUpdatePacket : ServerPacket { public class UserUpdatePacket : ServerPacket {
public ChatUser User { get; private set; } public ChatUser User { get; private set; }
public string PreviousName { get; private set; }
public UserUpdatePacket(ChatUser user, string previousName = null) { public UserUpdatePacket(ChatUser user) {
User = user ?? throw new ArgumentNullException(nameof(user)); User = user ?? throw new ArgumentNullException(nameof(user));
PreviousName = previousName;
} }
public override IEnumerable<string> Pack() { public override IEnumerable<string> Pack() {
StringBuilder sb = new(); StringBuilder sb = new();
bool isSilent = string.IsNullOrEmpty(PreviousName);
if(!isSilent) {
sb.Append('2');
sb.Append('\t');
sb.Append(DateTimeOffset.Now.ToUnixTimeSeconds());
sb.Append("\t-1\t0\fnick\f");
sb.Append(PreviousName);
sb.Append('\f');
sb.Append(User.LegacyNameWithStatus);
sb.Append('\t');
sb.Append(SequenceId);
sb.Append("\t10010");
yield return sb.ToString();
sb.Clear();
}
sb.Append("10"); sb.Append("10");
sb.Append('\t'); sb.Append('\t');
sb.Append(User.Pack()); sb.Append(User.Pack());