2023-02-10 06:07:59 +00:00
|
|
|
|
using System;
|
2022-08-30 15:00:58 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace SharpChat {
|
2023-02-16 22:47:30 +00:00
|
|
|
|
public class ChatChannel {
|
2023-02-22 00:28:53 +00:00
|
|
|
|
public string Name { get; }
|
|
|
|
|
public string Password { get; set; }
|
|
|
|
|
public bool IsTemporary { get; set; }
|
|
|
|
|
public int Rank { get; set; }
|
|
|
|
|
public long OwnerId { get; set; }
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
|
|
|
|
public bool HasPassword
|
|
|
|
|
=> !string.IsNullOrWhiteSpace(Password);
|
|
|
|
|
|
2023-02-22 00:28:53 +00:00
|
|
|
|
public ChatChannel(
|
|
|
|
|
ChatUser owner,
|
|
|
|
|
string name,
|
|
|
|
|
string password = null,
|
|
|
|
|
bool isTemporary = false,
|
|
|
|
|
int rank = 0
|
|
|
|
|
) : this(name, password, isTemporary, rank, owner?.UserId ?? 0) {}
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
2023-02-22 00:28:53 +00:00
|
|
|
|
public ChatChannel(
|
|
|
|
|
string name,
|
|
|
|
|
string password = null,
|
|
|
|
|
bool isTemporary = false,
|
|
|
|
|
int rank = 0,
|
|
|
|
|
long ownerId = 0
|
|
|
|
|
) {
|
2022-08-30 15:00:58 +00:00
|
|
|
|
Name = name;
|
2023-02-22 00:28:53 +00:00
|
|
|
|
Password = password ?? string.Empty;
|
|
|
|
|
IsTemporary = isTemporary;
|
|
|
|
|
Rank = rank;
|
|
|
|
|
OwnerId = ownerId;
|
2022-08-30 15:00:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Pack() {
|
2023-02-07 15:01:56 +00:00
|
|
|
|
StringBuilder sb = new();
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
|
|
|
|
sb.Append(Name);
|
|
|
|
|
sb.Append('\t');
|
|
|
|
|
sb.Append(string.IsNullOrEmpty(Password) ? '0' : '1');
|
|
|
|
|
sb.Append('\t');
|
|
|
|
|
sb.Append(IsTemporary ? '1' : '0');
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
2023-02-10 06:07:59 +00:00
|
|
|
|
|
|
|
|
|
public bool NameEquals(string name) {
|
|
|
|
|
return string.Equals(name, Name, StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 00:28:53 +00:00
|
|
|
|
public bool IsOwner(ChatUser user) {
|
|
|
|
|
return OwnerId > 0
|
|
|
|
|
&& user != null
|
|
|
|
|
&& OwnerId == user.UserId;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-10 06:07:59 +00:00
|
|
|
|
public override int GetHashCode() {
|
|
|
|
|
return Name.GetHashCode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool CheckName(string name) {
|
|
|
|
|
return !string.IsNullOrWhiteSpace(name) && name.All(CheckNameChar);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool CheckNameChar(char c) {
|
2023-02-10 06:28:36 +00:00
|
|
|
|
return char.IsLetter(c) || char.IsNumber(c) || c == '-' || c == '_';
|
2023-02-10 06:07:59 +00:00
|
|
|
|
}
|
2022-08-30 15:00:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|