using NswagGenerated;
namespace ChatRoomContract;
///
/// Wrapper around generated NSwag client
///
public class ChatRoomClient : IChatRoomService
{
NswagGenerated.NswagChatRoomClient nswag;
///
/// Chat room client constructor
///
/// Server base url
/// HTTP Client
public ChatRoomClient(string baseUrl, HttpClient httpClient)
{
nswag = new NswagGenerated.NswagChatRoomClient(baseUrl, httpClient);
}
///
/// Approve a message
///
/// Message ID
public void ApproveMessage(int messageId)
{
nswag.ApproveMessage(messageId);
}
///
/// Get timestamp until when the client is blocked
///
/// Client ID
/// Optional datetime object
public DateTime? GetBlockedUntil(int clientId)
{
try {
var offset = nswag.GetBlockedUntil(clientId);
return offset.DateTime;
} catch (ApiException e) {
if (e.StatusCode == 204) {
return null;
}
throw;
}
}
///
/// Get the next message which hasn't been approved or rejected
///
/// Message object. Returns null if there is no message
public Message? GetNewMessage()
{
NswagGenerated.Message message;
try {
message = nswag.GetNewMessage();
} catch (ApiException e) {
if (e.StatusCode == 204) {
return null;
}
throw;
}
return new Message{
id = message.Id,
contents = message.Contents,
needsToBeCensored = message.NeedsToBeCensored
};
}
///
/// Get number of strikes a participant has
///
/// Client ID
/// Number of strikes
public int GetStrikes(int clientId)
{
return nswag.GetStrikes(clientId);
}
///
/// Register client with a name
///
/// Name of client, can be duplicate between clients
/// Client ID
public int RegisterClient(string name)
{
return nswag.RegisterClient(name);
}
///
/// Reject a message
///
/// Message ID
public void RejectMessage(int messageId)
{
nswag.RejectMessage(messageId);
}
///
/// Send a message, will be given to a moderator to be approved
///
/// Client ID
/// Message contents
/// Does this message need to be censored?
/// Was sending successful, can fail if user is blocked
public bool SendMessage(int clientId, string contents, bool needsToBeCensored)
{
return nswag.SendMessage(clientId, contents, needsToBeCensored);
}
}