123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- using Microsoft.AspNetCore.Identity;
- using Microsoft.AspNetCore.SignalR;
- using MTWorkHR.Application.Filters;
- using MTWorkHR.Application.Identity;
- using MTWorkHR.Application.Mapper;
- using MTWorkHR.Application.Models;
- using MTWorkHR.Application.Services;
- using MTWorkHR.Core.Entities;
- using MTWorkHR.Core.Entities.Base;
- using MTWorkHR.Core.Global;
- using MTWorkHR.Core.UnitOfWork;
- using MTWorkHR.Infrastructure.Entities;
- using System;
- using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
- namespace MTWorkHR.API.Chat
- {
- [AppAuthorize]
- public class ChatHub : Hub
- {
- private readonly IUnitOfWork _unitOfWork;
- private readonly GlobalInfo _globalInfo;
- private readonly UserService _userService;
- public ChatHub(IUnitOfWork unitOfWork, GlobalInfo globalInfo, UserService userService)
- {
- _unitOfWork = unitOfWork;
- _globalInfo = globalInfo;
- _userService = userService;
- }
- public async Task authMe()
- {
- string currSignalrID = Context.ConnectionId;
- //Person tempPerson = ctx.Person.Where(p => p.Username == personInfo.userName && p.Password == personInfo.password)
- // .SingleOrDefault();
- if (_globalInfo.UserId != null) //if credentials are correct
- {
- Console.WriteLine("\n" + _globalInfo.UserName + " logged in" + "\nSignalrID: " + currSignalrID);
- HubConnection currUser = new HubConnection
- {
- UserId = _globalInfo.UserId,
- SignalrId = currSignalrID,
- TimeStamp = DateTime.Now
- };
- var newConnection = await _unitOfWork.HubConnection.AddAsync(currUser);
- await _unitOfWork.CompleteAsync();
- ChatUserDto newUser = new ChatUserDto(_globalInfo.UserId, _globalInfo.UserName, currSignalrID);
- await Clients.Caller.SendAsync("authMeResponseSuccess", newUser);//4Tutorial
- await Clients.Others.SendAsync("userOn", newUser);//4Tutorial
- }
- else //if credentials are incorrect
- {
- await Clients.Caller.SendAsync("authMeResponseFail");
- }
- }
-
-
- public async Task getCompanyUsersList()
- {
- var companyUsers = await _userService.GetAllCompanyEmployees();
- var response = MapperObject.Mapper.Map<List<UserAllDto>>(companyUsers);
- await Clients.Caller.SendAsync("getCompanyUsersList", response);
- }
-
- //Simple Test Method
- public async Task SendMessageAll(string user, string message)
- {
- await Clients.All.SendAsync("ReceiveMessage", user, message);
- }
- public async Task sendMsg(string userId, string msg)
- {
- var allConnections = await _unitOfWork.HubConnection.GetAllAsync();
- var receiverUser = allConnections.Item1.FirstOrDefault(c => c.UserId == userId);
- var connId = receiverUser != null ? receiverUser.SignalrId : "0";
- ChatMessage messag = new ChatMessage { Content = msg, ReceiverId = connId, SenderId = _globalInfo.UserId, SenderName = _globalInfo.UserName, IsSeen = false };
- await Clients.Client(connId).SendAsync("sendMsgResponse", Context.ConnectionId, msg);
- }
- public async Task getAllMessagesByUser(string senderId)
- {
- var allmessages = await _unitOfWork.ChatMessage.GetAllWithChildrenAsync(senderId, _globalInfo.UserId);
- await Clients.Caller.SendAsync("allMessagesResponse", allmessages.Item1);
- }
- public async override Task<Task> OnDisconnectedAsync(Exception exception)
- {
- var connections = await _unitOfWork.HubConnection.GetAllAsync(Context.ConnectionId);
- var currUserId = connections.Item1.Select(c => c.UserId).SingleOrDefault();
- _unitOfWork.HubConnection.DeleteRangeAsync(connections.Item1.ToList());
- _unitOfWork.CompleteAsync();
- await Clients.Others.SendAsync("userOff", currUserId);
- return base.OnDisconnectedAsync(exception);
- }
-
- public void logOut(Guid personId)
- {
- var connections = _unitOfWork.HubConnection.GetAllAsync(Context.ConnectionId).Result;
- // var currUserId = connections.Item1.Select(c => c.UserId).SingleOrDefault();
- _unitOfWork.HubConnection.DeleteRangeAsync(connections.Item1.ToList());
- _unitOfWork.CompleteAsync();
- Clients.Caller.SendAsync("logoutResponse");
- Clients.Others.SendAsync("userOff", personId);
- }
-
- public async Task getOnlineUsers()
- {
- var allConnections = await _unitOfWork.HubConnection.GetAllAsync();
- var currUserId = allConnections.Item1.Where(c => c.SignalrId == Context.ConnectionId).Select(c => c.UserId).SingleOrDefault();
- List<ChatUserDto> onlineUsers = allConnections.Item1
- .Where(c => c.UserId != currUserId)
- .Select(c =>
- new ChatUserDto(c.UserId, c.UserName, c.SignalrId)
- ).ToList();
- await Clients.Caller.SendAsync("getOnlineUsersResponse", onlineUsers);
- }
- //public async Task reauthMe(string userId)
- //{
- // string currSignalrID = Context.ConnectionId;
- // //ApplicationUser tempPerson = ctx.Person.Where(p => p.Id == personId)
- // // .SingleOrDefault();
- // if (userId == _globalInfo.UserId) //if credentials are correct
- // {
- // Console.WriteLine("\n" + _globalInfo.UserName + " logged in" + "\nSignalrID: " + currSignalrID);
- // HubConnection currUser = new HubConnection
- // {
- // UserId = _globalInfo.UserId,
- // SignalrId = currSignalrID,
- // TimeStamp = DateTime.Now
- // };
- // var newConnection = await _unitOfWork.HubConnection.AddAsync(currUser);
- // await _unitOfWork.CompleteAsync();
- // ChatUserDto newUser = new ChatUserDto(_globalInfo.UserId, _globalInfo.UserName, currSignalrID);
- // await Clients.Caller.SendAsync("reauthMeResponse", newUser);//4Tutorial
- // await Clients.Others.SendAsync("userOn", newUser);//4Tutorial
- // }
- //} //end of reauthMe
- }
- }
|