From bdef1a9ec1f44aa1585b1facfe8734d37eeac691 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Wed, 5 Jun 2019 01:36:50 +0100 Subject: [PATCH] Implements folowing at chatting --- .../Relationship/ChatApiController.cs | 33 +++++++++++++-- src/Yavsc/Hubs/ChatHub.cs | 41 +++++++++++-------- 2 files changed, 55 insertions(+), 19 deletions(-) diff --git a/src/Yavsc/ApiControllers/Relationship/ChatApiController.cs b/src/Yavsc/ApiControllers/Relationship/ChatApiController.cs index f66dd86c..fe10c2b8 100644 --- a/src/Yavsc/ApiControllers/Relationship/ChatApiController.cs +++ b/src/Yavsc/ApiControllers/Relationship/ChatApiController.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using Microsoft.Data.Entity; -using System.Threading.Tasks; using System.Security.Claims; using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Identity; @@ -12,7 +11,6 @@ using Yavsc.ViewModels.Chat; namespace Yavsc.Controllers { - using System.Threading.Tasks; [Route("api/chat")] public class ChatApiController : Controller @@ -65,7 +63,7 @@ namespace Yavsc.Controllers } // GET: api/chat/userName - [HttpGet("{userName}", Name = "uinfo")] + [HttpGet("user/{userName}", Name = "uinfo")] public IActionResult GetUserInfo([FromRoute] string userName) { if (!ModelState.IsValid) @@ -88,5 +86,34 @@ namespace Yavsc.Controllers }); } + public class ChannelShortInfo { + public string RoomName {get; set;} + public string Topic { get; set; } + } + + /// + /// Get firsts 10 biggest channels having + /// a name starting with given prefix. + /// + /// chan Name Prefix + /// + [HttpGet("chanlist/{chanNamePrefix}")] + public IActionResult GetChanList([FromRoute] string chanNamePrefix) + { + var list = ChatHub.Channels.Where(c => c.Key.StartsWith(chanNamePrefix)).OrderByDescending(c=>c.Value.Users.Count).Select(c=>new ChannelShortInfo { RoomName= c.Key, Topic = c.Value.Topic }).Take(10); + return Ok(list); + } + + /// + /// Get firsts 10 biggest channels + /// + /// + [HttpGet("chanlist")] + public IActionResult GetChanList() + { + return Ok(ChatHub.Channels.OrderByDescending(c=>c.Value.Users.Count).Select(c=> new ChannelShortInfo { RoomName= c.Key, Topic = c.Value.Topic }) + .Take(10)); + } + } } diff --git a/src/Yavsc/Hubs/ChatHub.cs b/src/Yavsc/Hubs/ChatHub.cs index dd5292ed..56af864d 100644 --- a/src/Yavsc/Hubs/ChatHub.cs +++ b/src/Yavsc/Hubs/ChatHub.cs @@ -19,15 +19,16 @@ // You should have received a copy of the GNU Lesser General Public License // along with this program. If not, see . -using Microsoft.AspNet.SignalR; -using System.Threading.Tasks; -using System.Collections.Generic; -using System.Linq; using System; using System.Collections.Concurrent; +using System.Collections.Generic; +using Microsoft.AspNet.SignalR; using Microsoft.Data.Entity; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.Extensions.Localization; namespace Yavsc { @@ -37,8 +38,10 @@ namespace Yavsc public class ChatHub : Hub, IDisposable { ApplicationDbContext _dbContext; + private IStringLocalizer _localizer; ILogger _logger; public static ConcurrentDictionary ChatUserNames = new ConcurrentDictionary(); + public static ConcurrentDictionary Channels = new ConcurrentDictionary(); public ChatHub() { @@ -46,6 +49,9 @@ namespace Yavsc _dbContext = scope.ServiceProvider.GetService(); var loggerFactory = scope.ServiceProvider.GetService(); + + var stringLocFactory = scope.ServiceProvider.GetService(); + _localizer = stringLocFactory.Create(typeof(ChatHub)); _logger = loggerFactory.CreateLogger(); } @@ -63,7 +69,7 @@ namespace Yavsc await Groups.Add(Context.ConnectionId, group); if (isAuth) { - _logger.LogInformation("Authenticated chat user"); + _logger.LogInformation(_localizer.GetString(Constants.LabAuthChatUser)); var userId = _dbContext.Users.First(u => u.UserName == userName).Id; @@ -83,6 +89,12 @@ namespace Yavsc Connected = true }); _dbContext.SaveChanges(); + Clients.Group(Constants.HubGroupFollowingPrefix+userId).notifyuser(NotificationTypes.Connected, userName, null); + + foreach (var uid in _dbContext.CircleMembers.Select(m => m.MemberId)) + { + await Groups.Add(Context.ConnectionId, Constants.HubGroupFollowingPrefix+uid); + } } else { @@ -95,9 +107,7 @@ namespace Yavsc { await Groups.Add(Context.ConnectionId, Constants.HubGroupAnonymous); } - // TODO only notify followers - Clients.Group(Constants.HubGroupAuthenticated).notifyuser(NotificationTypes.Connected, userName, ""); - await base.OnConnected(); + await base.OnConnected(); } string setUserName() { @@ -123,7 +133,6 @@ namespace Yavsc public override Task OnDisconnected(bool stopCalled) { string userName = Context.User?.Identity.Name; - Clients.Group("authenticated").notifyUser(NotificationTypes.DisConnected, userName); if (userName != null) { var cx = _dbContext.ChatConnection.SingleOrDefault(c => c.ConnectionId == Context.ConnectionId); @@ -142,6 +151,7 @@ namespace Yavsc _dbContext.SaveChanges(); } } + Clients.Group("authenticated").notifyUser(NotificationTypes.DisConnected, userName, "disconnected"); return base.OnDisconnected(stopCalled); } @@ -170,13 +180,12 @@ namespace Yavsc Connected = true }); _dbContext.SaveChanges(); - Clients.Group("authenticated").notifyUser(NotificationTypes.Reconnected, userName); + Clients.Group("authenticated").notifyUser(NotificationTypes.Reconnected, userName, "reconnected"); } return base.OnReconnected(); } - static ConcurrentDictionary Channels = new ConcurrentDictionary(); - + public class ChatRoomInfo { public string Name; @@ -206,7 +215,7 @@ namespace Yavsc _logger.LogInformation("a client for " + roomName); var userName = ChatUserNames[Context.ConnectionId]; _logger.LogInformation($" chat user : {userName}"); - var roomGroupName = "room_" + roomName; + var roomGroupName = Constants.HubGroupRomsPrefix + roomName; ChatRoomInfo chanInfo; if (Channels.ContainsKey(roomName)) @@ -221,7 +230,7 @@ namespace Yavsc chanInfo.Users.Add(Context.ConnectionId, userName); Groups.Add(Context.ConnectionId, roomGroupName); Clients.Caller.joint(chanInfo); - Clients.Group("room_" + roomName).notifyRoom(NotificationTypes.UserJoin, roomName, userName); + Clients.Group(Constants.HubGroupRomsPrefix + roomName).notifyRoom(NotificationTypes.UserJoin, roomName, userName); return chanInfo; } return null; @@ -301,7 +310,7 @@ namespace Yavsc ChatRoomInfo chanInfo; if (Channels.TryGetValue(roomName, out chanInfo)) { - var roomGroupName = "room_" + roomName; + var roomGroupName = Constants.HubGroupRomsPrefix + roomName; if (!chanInfo.Users.ContainsKey(Context.ConnectionId)) { NotifyRoomError(roomName, "you didn't join."); @@ -336,7 +345,7 @@ namespace Yavsc public void Send(string roomName, string message) { - var groupname = "room_" + roomName; + var groupname = Constants.HubGroupRomsPrefix + roomName; ChatRoomInfo chanInfo; if (Channels.TryGetValue(roomName, out chanInfo)) {