2023-02-22 00:28:53 +00:00
|
|
|
|
using System;
|
2022-08-30 15:00:58 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace SharpChat {
|
2023-02-17 21:47:44 +00:00
|
|
|
|
public class ChatUser : IEquatable<ChatUser> {
|
2023-02-17 22:17:24 +00:00
|
|
|
|
public const int DEFAULT_SIZE = 30;
|
|
|
|
|
public const int DEFAULT_MINIMUM_DELAY = 10000;
|
|
|
|
|
public const int DEFAULT_RISKY_OFFSET = 5;
|
|
|
|
|
|
2023-02-22 00:28:53 +00:00
|
|
|
|
public long UserId { get; }
|
|
|
|
|
public string UserName { get; set; }
|
2022-08-30 15:00:58 +00:00
|
|
|
|
public ChatColour Colour { get; set; }
|
|
|
|
|
public int Rank { get; set; }
|
|
|
|
|
public ChatUserPermissions Permissions { get; set; }
|
2023-02-22 00:28:53 +00:00
|
|
|
|
public string NickName { get; set; }
|
|
|
|
|
public ChatUserStatus Status { get; set; }
|
|
|
|
|
public string StatusText { get; set; }
|
2023-02-17 21:47:44 +00:00
|
|
|
|
|
2023-02-22 00:28:53 +00:00
|
|
|
|
public string LegacyName {
|
2022-08-30 15:00:58 +00:00
|
|
|
|
get {
|
2023-02-07 15:01:56 +00:00
|
|
|
|
StringBuilder sb = new();
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
|
|
|
|
if(Status == ChatUserStatus.Away)
|
2023-02-22 00:28:53 +00:00
|
|
|
|
sb.AppendFormat("<{0}>_", StatusText[..Math.Min(StatusText.Length, 5)].ToUpperInvariant());
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
2023-02-22 00:28:53 +00:00
|
|
|
|
if(string.IsNullOrWhiteSpace(NickName))
|
|
|
|
|
sb.Append(UserName);
|
2022-08-30 15:00:58 +00:00
|
|
|
|
else {
|
|
|
|
|
sb.Append('~');
|
2023-02-22 00:28:53 +00:00
|
|
|
|
sb.Append(NickName);
|
2022-08-30 15:00:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 00:28:53 +00:00
|
|
|
|
public ChatUser(
|
|
|
|
|
long userId,
|
|
|
|
|
string userName,
|
|
|
|
|
ChatColour colour,
|
|
|
|
|
int rank,
|
|
|
|
|
ChatUserPermissions perms,
|
|
|
|
|
string nickName = null,
|
|
|
|
|
ChatUserStatus status = ChatUserStatus.Online,
|
|
|
|
|
string statusText = null
|
|
|
|
|
) {
|
|
|
|
|
UserId = userId;
|
|
|
|
|
UserName = userName ?? throw new ArgumentNullException(nameof(userName));
|
|
|
|
|
Colour = colour;
|
|
|
|
|
Rank = rank;
|
|
|
|
|
Permissions = perms;
|
|
|
|
|
NickName = nickName ?? string.Empty;
|
|
|
|
|
Status = status;
|
|
|
|
|
StatusText = statusText ?? string.Empty;
|
2023-02-17 21:47:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 15:00:58 +00:00
|
|
|
|
public bool Can(ChatUserPermissions perm, bool strict = false) {
|
|
|
|
|
ChatUserPermissions perms = Permissions & perm;
|
|
|
|
|
return strict ? perms == perm : perms > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Pack() {
|
2023-02-07 15:01:56 +00:00
|
|
|
|
StringBuilder sb = new();
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
|
|
|
|
sb.Append(UserId);
|
|
|
|
|
sb.Append('\t');
|
2023-02-22 00:28:53 +00:00
|
|
|
|
sb.Append(LegacyName);
|
2022-08-30 15:00:58 +00:00
|
|
|
|
sb.Append('\t');
|
|
|
|
|
sb.Append(Colour);
|
|
|
|
|
sb.Append('\t');
|
|
|
|
|
sb.Append(Rank);
|
|
|
|
|
sb.Append(' ');
|
|
|
|
|
sb.Append(Can(ChatUserPermissions.KickUser) ? '1' : '0');
|
2023-02-22 00:28:53 +00:00
|
|
|
|
sb.Append(' ');
|
|
|
|
|
sb.Append(Can(ChatUserPermissions.ViewLogs) ? '1' : '0');
|
|
|
|
|
sb.Append(' ');
|
2022-08-30 15:00:58 +00:00
|
|
|
|
sb.Append(Can(ChatUserPermissions.SetOwnNickname) ? '1' : '0');
|
|
|
|
|
sb.Append(' ');
|
2023-02-22 00:28:53 +00:00
|
|
|
|
sb.Append(Can(ChatUserPermissions.CreateChannel | ChatUserPermissions.SetChannelPermanent, true) ? '2' : (
|
|
|
|
|
Can(ChatUserPermissions.CreateChannel) ? '1' : '0'
|
2022-08-30 15:00:58 +00:00
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
2023-02-10 06:07:59 +00:00
|
|
|
|
|
2023-02-17 21:47:44 +00:00
|
|
|
|
public bool NameEquals(string name) {
|
2023-02-22 00:28:53 +00:00
|
|
|
|
return string.Equals(name, UserName, StringComparison.InvariantCultureIgnoreCase)
|
|
|
|
|
|| string.Equals(name, NickName, StringComparison.InvariantCultureIgnoreCase)
|
|
|
|
|
|| string.Equals(name, LegacyName, StringComparison.InvariantCultureIgnoreCase);
|
2023-02-17 21:47:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-10 06:07:59 +00:00
|
|
|
|
public override int GetHashCode() {
|
|
|
|
|
return UserId.GetHashCode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object obj) {
|
2023-02-17 21:47:44 +00:00
|
|
|
|
return Equals(obj as ChatUser);
|
2023-02-10 06:07:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-17 21:47:44 +00:00
|
|
|
|
public bool Equals(ChatUser other) {
|
2023-02-10 06:07:59 +00:00
|
|
|
|
return UserId == other?.UserId;
|
|
|
|
|
}
|
2023-02-16 22:56:50 +00:00
|
|
|
|
|
|
|
|
|
public static string GetDMChannelName(ChatUser user1, ChatUser user2) {
|
|
|
|
|
return user1.UserId < user2.UserId
|
|
|
|
|
? $"@{user1.UserId}-{user2.UserId}"
|
|
|
|
|
: $"@{user2.UserId}-{user1.UserId}";
|
|
|
|
|
}
|
2022-08-30 15:00:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|