sharp-chat/SharpChat.Common/Bans/BanManager.cs

345 lines
12 KiB
C#

using SharpChat.Events;
using SharpChat.Users;
using SharpChat.Users.Remote;
using System;
using System.Collections.Generic;
using System.Net;
namespace SharpChat.Bans {
public class BanManager {
private UserManager Users { get; }
private IBanClient BanClient { get; }
private IRemoteUserClient RemoteUserClient { get; }
private IEventDispatcher Dispatcher { get; }
private readonly object Sync = new();
public BanManager(
UserManager users,
IBanClient banClient,
IRemoteUserClient remoteUserClient,
IEventDispatcher dispatcher
) {
Users = users ?? throw new ArgumentNullException(nameof(users));
BanClient = banClient ?? throw new ArgumentNullException(nameof(banClient));
RemoteUserClient = remoteUserClient ?? throw new ArgumentNullException(nameof(remoteUserClient));
Dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
}
public void GetBanList(
Action<IEnumerable<IBanRecord>> onSuccess,
Action<Exception> onFailure
) {
if(onSuccess == null)
throw new ArgumentNullException(nameof(onSuccess));
if(onFailure == null)
throw new ArgumentNullException(nameof(onFailure));
lock(Sync)
BanClient.GetBanList(onSuccess, onFailure);
}
public void CheckBan(
long userId,
IPAddress ipAddress,
Action<IBanRecord> onSuccess,
Action<Exception> onFailure
) {
if(ipAddress == null)
throw new ArgumentNullException(nameof(ipAddress));
if(onSuccess == null)
throw new ArgumentNullException(nameof(onSuccess));
if(onFailure == null)
throw new ArgumentNullException(nameof(onFailure));
lock(Sync)
RemoteUserClient.ResolveUser(
userId,
rui => {
if(rui == null)
onSuccess(null);
else
CheckBan(rui, ipAddress, onSuccess, onFailure);
},
onFailure
);
}
public void CheckBan(
IUser localUserInfo,
IPAddress ipAddress,
Action<IBanRecord> onSuccess,
Action<Exception> onFailure
) {
if(localUserInfo == null)
throw new ArgumentNullException(nameof(localUserInfo));
if(ipAddress == null)
throw new ArgumentNullException(nameof(ipAddress));
if(onSuccess == null)
throw new ArgumentNullException(nameof(onSuccess));
if(onFailure == null)
throw new ArgumentNullException(nameof(onFailure));
lock(Sync)
RemoteUserClient.ResolveUser(
localUserInfo,
rui => {
if(rui == null)
onSuccess(null);
else
CheckBan(rui, ipAddress, onSuccess, onFailure);
},
onFailure
);
}
public void CheckBan(
IRemoteUser remoteUserInfo,
IPAddress ipAddress,
Action<IBanRecord> onSuccess,
Action<Exception> onFailure
) {
if(remoteUserInfo == null)
throw new ArgumentNullException(nameof(remoteUserInfo));
if(ipAddress == null)
throw new ArgumentNullException(nameof(ipAddress));
if(onSuccess == null)
throw new ArgumentNullException(nameof(onSuccess));
if(onFailure == null)
throw new ArgumentNullException(nameof(onFailure));
lock(Sync)
BanClient.CheckBan(remoteUserInfo, ipAddress, onSuccess, onFailure);
}
public void CreateBan(
string subjectName,
IUser localModerator,
bool permanent,
TimeSpan duration,
string reason,
Action<bool> onSuccess,
Action<Exception> onFailure
) {
if(subjectName == null)
throw new ArgumentNullException(nameof(subjectName));
if(reason == null)
throw new ArgumentNullException(nameof(reason));
if(onSuccess == null)
throw new ArgumentNullException(nameof(onSuccess));
if(onFailure == null)
throw new ArgumentNullException(nameof(onFailure));
lock(Sync)
RemoteUserClient.ResolveUser(
subjectName,
remoteSubject => {
if(remoteSubject == null)
onSuccess(false);
else
CreateBan(remoteSubject, localModerator, permanent, duration, reason, onSuccess, onFailure);
},
onFailure
);
}
public void CreateBan(
IUser localSubject,
IUser localModerator,
bool permanent,
TimeSpan duration,
string reason,
Action<bool> onSuccess,
Action<Exception> onFailure
) {
if(localSubject == null)
throw new ArgumentNullException(nameof(localSubject));
if(reason == null)
throw new ArgumentNullException(nameof(reason));
if(onSuccess == null)
throw new ArgumentNullException(nameof(onSuccess));
if(onFailure == null)
throw new ArgumentNullException(nameof(onFailure));
lock(Sync)
RemoteUserClient.ResolveUser(
localSubject,
remoteSubject => {
if(remoteSubject == null)
onSuccess(false);
else
CreateBan(remoteSubject, localModerator, permanent, duration, reason, onSuccess, onFailure);
},
onFailure
);
}
public void CreateBan(
IRemoteUser remoteSubject,
IUser localModerator,
bool permanent,
TimeSpan duration,
string reason,
Action<bool> onSuccess,
Action<Exception> onFailure
) {
if(remoteSubject == null)
throw new ArgumentNullException(nameof(remoteSubject));
if(reason == null)
throw new ArgumentNullException(nameof(reason));
if(onSuccess == null)
throw new ArgumentNullException(nameof(onSuccess));
if(onFailure == null)
throw new ArgumentNullException(nameof(onFailure));
lock(Sync)
RemoteUserClient.ResolveUser(
localModerator,
remoteModerator => CreateBan(remoteSubject, remoteModerator, permanent, duration, reason, onSuccess, onFailure),
onFailure
);
}
public void CreateBan(
IRemoteUser remoteSubject,
IRemoteUser remoteModerator,
bool permanent,
TimeSpan duration,
string reason,
Action<bool> onSuccess,
Action<Exception> onFailure
) {
if(remoteSubject == null)
throw new ArgumentNullException(nameof(remoteSubject));
if(reason == null)
throw new ArgumentNullException(nameof(reason));
if(onSuccess == null)
throw new ArgumentNullException(nameof(onSuccess));
if(onFailure == null)
throw new ArgumentNullException(nameof(onFailure));
lock(Sync)
BanClient.CreateBan(remoteSubject, remoteModerator, permanent, duration, reason, success => {
Dispatcher.DispatchEvent(this, new UserBanCreatedEvent(remoteSubject, remoteModerator, permanent, duration, reason));
Users.Disconnect(
remoteSubject,
remoteModerator == null
? UserDisconnectReason.Flood
: UserDisconnectReason.Kicked
);
onSuccess.Invoke(success);
}, onFailure);
}
public void RemoveBan(
long userId,
Action<bool> onSuccess,
Action<Exception> onFailure
) {
if(onSuccess == null)
throw new ArgumentNullException(nameof(onSuccess));
if(onFailure == null)
throw new ArgumentNullException(nameof(onFailure));
lock(Sync)
RemoteUserClient.ResolveUser(
userId,
remoteUser => {
if(remoteUser == null)
onSuccess(false);
else
RemoveBan(remoteUser, onSuccess, onFailure);
},
onFailure
);
}
public void RemoveBan(
string userName,
Action<bool> onSuccess,
Action<Exception> onFailure
) {
if(userName == null)
throw new ArgumentNullException(nameof(userName));
if(onSuccess == null)
throw new ArgumentNullException(nameof(onSuccess));
if(onFailure == null)
throw new ArgumentNullException(nameof(onFailure));
lock(Sync)
RemoteUserClient.ResolveUser(
userName,
remoteUser => {
if(remoteUser == null)
onSuccess(false);
else
RemoveBan(remoteUser, onSuccess, onFailure);
},
onFailure
);
}
public void RemoveBan(
IUser localUser,
Action<bool> onSuccess,
Action<Exception> onFailure
) {
if(localUser == null)
throw new ArgumentNullException(nameof(localUser));
if(onSuccess == null)
throw new ArgumentNullException(nameof(onSuccess));
if(onFailure == null)
throw new ArgumentNullException(nameof(onFailure));
lock(Sync)
RemoteUserClient.ResolveUser(
localUser,
remoteUser => {
if(remoteUser == null)
onSuccess(false);
else
RemoveBan(remoteUser, onSuccess, onFailure);
},
onFailure
);
}
public void RemoveBan(
IRemoteUser remoteUser,
Action<bool> onSuccess,
Action<Exception> onFailure
) {
if(remoteUser == null)
throw new ArgumentNullException(nameof(remoteUser));
if(onSuccess == null)
throw new ArgumentNullException(nameof(onSuccess));
if(onFailure == null)
throw new ArgumentNullException(nameof(onFailure));
lock(Sync)
BanClient.RemoveBan(remoteUser, success => {
Dispatcher.DispatchEvent(this, new UserBanRemovedEvent(remoteUser));
onSuccess.Invoke(success);
}, onFailure);
}
public void RemoveBan(
IPAddress ipAddress,
Action<bool> onSuccess,
Action<Exception> onFailure
) {
if(ipAddress == null)
throw new ArgumentNullException(nameof(ipAddress));
if(onSuccess == null)
throw new ArgumentNullException(nameof(onSuccess));
if(onFailure == null)
throw new ArgumentNullException(nameof(onFailure));
lock(Sync)
BanClient.RemoveBan(ipAddress, success => {
Dispatcher.DispatchEvent(this, new IPBanRemovedEvent(ipAddress));
onSuccess.Invoke(success);
}, onFailure);
}
}
}