sharp-chat/SharpChat/User.cs

74 lines
2.8 KiB
C#
Raw Normal View History

using SharpChat.ClientCommands;
using System.Globalization;
2022-08-30 17:00:58 +02:00
using System.Text;
namespace SharpChat {
public class User(
2025-04-25 15:49:46 +00:00
long userId,
string userName,
2025-04-26 13:15:27 +00:00
ColourInheritable colour,
2025-04-25 15:49:46 +00:00
int rank,
UserPermissions perms,
2025-04-25 15:49:46 +00:00
string nickName = "",
UserStatus status = UserStatus.Online,
2025-04-25 15:49:46 +00:00
string statusText = ""
) {
public const int DEFAULT_SIZE = 30;
public const int DEFAULT_MINIMUM_DELAY = 10000;
public const int DEFAULT_RISKY_OFFSET = 5;
2025-04-25 15:49:46 +00:00
public long UserId { get; } = userId;
public string UserName { get; set; } = userName ?? throw new ArgumentNullException(nameof(userName));
2025-04-26 13:15:27 +00:00
public ColourInheritable Colour { get; set; } = colour;
2025-04-25 15:49:46 +00:00
public int Rank { get; set; } = rank;
public UserPermissions Permissions { get; set; } = perms;
2025-04-25 18:18:13 +00:00
public string NickName { get; set; } = nickName;
public UserStatus Status { get; set; } = status;
2025-04-25 18:18:13 +00:00
public string StatusText { get; set; } = statusText;
public string LegacyName => string.IsNullOrWhiteSpace(NickName) ? UserName : $"~{NickName}";
public string LegacyNameWithStatus {
2022-08-30 17:00:58 +02:00
get {
2023-02-07 16:01:56 +01:00
StringBuilder sb = new();
2022-08-30 17:00:58 +02:00
if(Status == UserStatus.Away) {
string statusText = StatusText.Trim();
2025-04-25 15:49:46 +00:00
StringInfo sti = new(statusText);
if(Encoding.UTF8.GetByteCount(statusText) > AFKClientCommand.MAX_BYTES
|| sti.LengthInTextElements > AFKClientCommand.MAX_GRAPHEMES)
statusText = sti.SubstringByTextElements(0, Math.Min(sti.LengthInTextElements, AFKClientCommand.MAX_GRAPHEMES)).Trim();
sb.AppendFormat("<{0}>_", statusText.ToUpperInvariant());
}
2022-08-30 17:00:58 +02:00
sb.Append(LegacyName);
2022-08-30 17:00:58 +02:00
return sb.ToString();
}
}
public bool Can(UserPermissions perm, bool strict = false) {
UserPermissions perms = Permissions & perm;
2022-08-30 17:00:58 +02:00
return strict ? perms == perm : perms > 0;
}
public bool NameEquals(string name) {
return string.Equals(name, UserName, StringComparison.InvariantCultureIgnoreCase)
|| string.Equals(name, NickName, StringComparison.InvariantCultureIgnoreCase)
|| string.Equals(name, LegacyName, StringComparison.InvariantCultureIgnoreCase)
|| string.Equals(name, LegacyNameWithStatus, StringComparison.InvariantCultureIgnoreCase);
}
public override int GetHashCode() {
return UserId.GetHashCode();
}
public static string GetDMChannelName(User user1, User user2) {
2023-02-16 23:56:50 +01:00
return user1.UserId < user2.UserId
? $"@{user1.UserId}-{user2.UserId}"
: $"@{user2.UserId}-{user1.UserId}";
}
2022-08-30 17:00:58 +02:00
}
}