diff options
Diffstat (limited to 'Backend/Api')
-rw-r--r-- | Backend/Api/Api/Controllers/MessageController.cs | 2 | ||||
-rw-r--r-- | Backend/Api/Api/Services/ChatHub.cs | 12 | ||||
-rw-r--r-- | Backend/Api/Api/Services/MessageService.cs | 21 |
3 files changed, 30 insertions, 5 deletions
diff --git a/Backend/Api/Api/Controllers/MessageController.cs b/Backend/Api/Api/Controllers/MessageController.cs index 63351d0..750fc5f 100644 --- a/Backend/Api/Api/Controllers/MessageController.cs +++ b/Backend/Api/Api/Controllers/MessageController.cs @@ -24,7 +24,7 @@ namespace Api.Controllers } [HttpPost("add")] [Authorize(Roles = "User")] - public async Task<ActionResult<MessageSend>> addMessage([FromBody] MessageReceive msg) + public async Task<ActionResult<Message>> addMessage([FromBody] MessageReceive msg) { var msgTemp = await _messageService.addMessage(msg); if(msgTemp == null) diff --git a/Backend/Api/Api/Services/ChatHub.cs b/Backend/Api/Api/Services/ChatHub.cs index c196065..4092d8f 100644 --- a/Backend/Api/Api/Services/ChatHub.cs +++ b/Backend/Api/Api/Services/ChatHub.cs @@ -19,6 +19,7 @@ namespace Api.Services if (id == null) return; Users.Add(Context.ConnectionId, id); + //await SendDirect(id, "poruka"); //await Send(Context.ConnectionId); await base.OnConnectedAsync(); @@ -38,6 +39,17 @@ namespace Api.Services } return keys; } + public static bool CheckUser(string id) + { + var users = Users.Values; + foreach (var user in users) + { + if (user == id) + return true; + } + return false; + } + } } //private readonly IHubContext<ChatHub> _ichat; diff --git a/Backend/Api/Api/Services/MessageService.cs b/Backend/Api/Api/Services/MessageService.cs index 0e48285..9cc818b 100644 --- a/Backend/Api/Api/Services/MessageService.cs +++ b/Backend/Api/Api/Services/MessageService.cs @@ -1,6 +1,7 @@ using System.Security.Claims; using Api.Interfaces; using Api.Models; +using Microsoft.AspNetCore.SignalR; using MongoDB.Driver; namespace Api.Services @@ -11,14 +12,15 @@ namespace Api.Services private readonly IMongoCollection<Message> _messages; private readonly IJwtService _jwtService; private IConfiguration _configuration; - private readonly IFileService _fileService; - public MessageService(IDatabaseConnection settings, IMongoClient mongoClient, IJwtService jwtService, IHttpContextAccessor httpContextAccessor, IConfiguration configuration) + private readonly IHubContext<ChatHub> _chatHub; + public MessageService(IDatabaseConnection settings, IMongoClient mongoClient, IJwtService jwtService, IHttpContextAccessor httpContextAccessor, IConfiguration configuration,IHubContext<ChatHub> chatHub) { var database = mongoClient.GetDatabase(settings.DatabaseName); _messages = database.GetCollection<Message>(settings.MessageCollectionname); _jwtService = jwtService; _httpContext = httpContextAccessor; _configuration = configuration; + _chatHub = chatHub; } public async Task<Message> addMessage(MessageReceive msg) { @@ -33,10 +35,21 @@ namespace Api.Services tempMsg.senderId = senderId; tempMsg.messagge = msg.messagge; tempMsg.timestamp = DateTime.Now.ToUniversalTime(); + if (ChatHub.CheckUser(msg.receiverId)) + { + //user online + var temp =messageToMessageSend(tempMsg); + foreach (var connection in ChatHub.getAllConnectionsOfUser(msg.receiverId)) + await _chatHub.Clients.Client(connection).SendAsync("Message",temp); + } + else + { + //user offline + await _messages.InsertOneAsync(tempMsg); - //TODO if user online send thru socket - await _messages.InsertOneAsync(tempMsg); + } + return tempMsg; } |