add rejection of messages
This commit is contained in:
parent
619e735fd5
commit
02e35eac48
@ -62,6 +62,16 @@ public class ChatRoomLogic
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool GetAndUpdateBlockedState(Client client)
|
||||||
|
{
|
||||||
|
if (client.blockedUntil != null && DateTime.UtcNow >= client.blockedUntil)
|
||||||
|
{
|
||||||
|
client.blockedUntil = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.blockedUntil != null;
|
||||||
|
}
|
||||||
|
|
||||||
public bool SendMessage(int clientId, string contents, bool needsToBeCensored)
|
public bool SendMessage(int clientId, string contents, bool needsToBeCensored)
|
||||||
{
|
{
|
||||||
lock (state.accessLock)
|
lock (state.accessLock)
|
||||||
@ -72,17 +82,10 @@ public class ChatRoomLogic
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (client.blockedUntil != null)
|
if (GetAndUpdateBlockedState(client))
|
||||||
{
|
|
||||||
if (DateTime.UtcNow < client.blockedUntil)
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
client.blockedUntil = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var message = new Message
|
var message = new Message
|
||||||
{
|
{
|
||||||
@ -107,6 +110,7 @@ public class ChatRoomLogic
|
|||||||
{
|
{
|
||||||
if (message.status != MessageStatus.WaitingForModerator) continue;
|
if (message.status != MessageStatus.WaitingForModerator) continue;
|
||||||
|
|
||||||
|
log.Info($"Message '{message.id}' given to moderator");
|
||||||
message.status = MessageStatus.GivenToModerator;
|
message.status = MessageStatus.GivenToModerator;
|
||||||
return new ChatRoomContract.Message{
|
return new ChatRoomContract.Message{
|
||||||
id = message.id,
|
id = message.id,
|
||||||
@ -139,6 +143,54 @@ public class ChatRoomLogic
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void RejectMessage(int messageId)
|
||||||
|
{
|
||||||
|
lock (state.accessLock)
|
||||||
|
{
|
||||||
|
var message = FindMessageById(messageId);
|
||||||
|
if (message == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (message.status != MessageStatus.GivenToModerator)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
message.status = MessageStatus.Rejected;
|
||||||
|
log.Info($"Message {message.id} was rejected");
|
||||||
|
|
||||||
|
var client = FindClientById(message.clientId);
|
||||||
|
if (client != null && !GetAndUpdateBlockedState(client))
|
||||||
|
{
|
||||||
|
client.strikes++;
|
||||||
|
|
||||||
|
var rnd = new Random();
|
||||||
|
if (client.strikes > rnd.Next(0, 10))
|
||||||
|
{
|
||||||
|
log.Info($"Client '{client.name}' ({client.id}) was blocked for {client.strikes}s");
|
||||||
|
client.blockedUntil = DateTime.UtcNow.AddSeconds(client.strikes);
|
||||||
|
client.strikes = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetStrikes(int clientId)
|
||||||
|
{
|
||||||
|
lock (state.accessLock)
|
||||||
|
{
|
||||||
|
var client = FindClientById(clientId);
|
||||||
|
if (client == null)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.strikes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void BackgroundTask()
|
public void BackgroundTask()
|
||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
|
@ -22,12 +22,12 @@ public class ChatRoomService : ChatRoomContract.IChatRoomService
|
|||||||
|
|
||||||
public int GetStrikes(int clientId)
|
public int GetStrikes(int clientId)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
return logic.GetStrikes(clientId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RejectMessage(int messageId)
|
public void RejectMessage(int messageId)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
logic.RejectMessage(messageId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool SendMessage(int clientId, string contents, bool needsToBeCensored)
|
public bool SendMessage(int clientId, string contents, bool needsToBeCensored)
|
||||||
|
@ -64,9 +64,17 @@ internal class Moderator
|
|||||||
if (message != null)
|
if (message != null)
|
||||||
{
|
{
|
||||||
log.Info($"Checking message ({message.id}): {message.contents}");
|
log.Info($"Checking message ({message.id}): {message.contents}");
|
||||||
|
Thread.Sleep(500);
|
||||||
|
|
||||||
|
if (message.needsToBeCensored)
|
||||||
|
{
|
||||||
|
chatRoom.RejectMessage(message.id);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
chatRoom.ApproveMessage(message.id);
|
chatRoom.ApproveMessage(message.id);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Thread.Sleep(1 * 1000);
|
Thread.Sleep(1 * 1000);
|
||||||
}
|
}
|
||||||
|
@ -43,9 +43,11 @@ internal class Participant
|
|||||||
int clientId = chatRoom.RegisterClient(name);
|
int clientId = chatRoom.RegisterClient(name);
|
||||||
log.Info($"Registered with client id {clientId}");
|
log.Info($"Registered with client id {clientId}");
|
||||||
|
|
||||||
Console.Title = $"Participant | {name} | {clientId}";
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
int strikes = chatRoom.GetStrikes(clientId);
|
||||||
|
Console.Title = $"Participant | {name} | {clientId} | {strikes} Strikes";
|
||||||
|
|
||||||
var message = string.Join(" ", faker.Lorem.Words(5));
|
var message = string.Join(" ", faker.Lorem.Words(5));
|
||||||
bool needsToBeCensored = rnd.Next(0, 100) > 50;
|
bool needsToBeCensored = rnd.Next(0, 100) > 50;
|
||||||
if (chatRoom.SendMessage(clientId, message, needsToBeCensored)) {
|
if (chatRoom.SendMessage(clientId, message, needsToBeCensored)) {
|
||||||
|
Loading…
Reference in New Issue
Block a user