sharp-chat/SharpChat/Packet/ChatMessageAddPacket.cs
flashwave e17aed7c25
Switched to Index brand random Snowflakes instead of SharpIds.
If you were still handling message ids as integers in an environment that can't handle signed 64-bit integers you're going to be having a fun time after this update!
2025-04-25 20:05:57 +00:00

51 lines
1.2 KiB
C#

using System.Text;
namespace SharpChat.Packet {
public class ChatMessageAddPacket(
long msgId,
DateTimeOffset created,
long userId,
string text,
bool isAction,
bool isPrivate
) : IServerPacket {
public string Text { get; } = text ?? throw new ArgumentNullException(nameof(text));
public IEnumerable<string> Pack() {
StringBuilder sb = new();
sb.Append('2');
sb.Append('\t');
sb.Append(created.ToUnixTimeSeconds());
sb.Append('\t');
sb.Append(userId);
sb.Append('\t');
if(isAction)
sb.Append("<i>");
sb.Append(
Text.Replace("<", "&lt;")
.Replace(">", "&gt;")
.Replace("\n", " <br/> ")
.Replace("\t", " ")
);
if(isAction)
sb.Append("</i>");
sb.Append('\t');
sb.Append(msgId);
sb.AppendFormat(
"\t1{0}0{1}{2}",
isAction ? '1' : '0',
isAction ? '0' : '1',
isPrivate ? '1' : '0'
);
yield return sb.ToString();
}
}
}