Implements folowing at chatting
This commit is contained in:
parent
bafc68dae0
commit
a592fe3dcf
2 changed files with 55 additions and 19 deletions
|
|
@ -2,7 +2,6 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.Data.Entity;
|
using Microsoft.Data.Entity;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using Microsoft.AspNet.Mvc;
|
using Microsoft.AspNet.Mvc;
|
||||||
using Microsoft.AspNet.Identity;
|
using Microsoft.AspNet.Identity;
|
||||||
|
|
@ -12,7 +11,6 @@ using Yavsc.ViewModels.Chat;
|
||||||
|
|
||||||
namespace Yavsc.Controllers
|
namespace Yavsc.Controllers
|
||||||
{
|
{
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
[Route("api/chat")]
|
[Route("api/chat")]
|
||||||
public class ChatApiController : Controller
|
public class ChatApiController : Controller
|
||||||
|
|
@ -65,7 +63,7 @@ namespace Yavsc.Controllers
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET: api/chat/userName
|
// GET: api/chat/userName
|
||||||
[HttpGet("{userName}", Name = "uinfo")]
|
[HttpGet("user/{userName}", Name = "uinfo")]
|
||||||
public IActionResult GetUserInfo([FromRoute] string userName)
|
public IActionResult GetUserInfo([FromRoute] string userName)
|
||||||
{
|
{
|
||||||
if (!ModelState.IsValid)
|
if (!ModelState.IsValid)
|
||||||
|
|
@ -88,5 +86,34 @@ namespace Yavsc.Controllers
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class ChannelShortInfo {
|
||||||
|
public string RoomName {get; set;}
|
||||||
|
public string Topic { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get firsts 10 biggest channels having
|
||||||
|
/// a name starting with given prefix.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="chanNamePrefix">chan Name Prefix</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get firsts 10 biggest channels
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
[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));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,15 +19,16 @@
|
||||||
// You should have received a copy of the GNU Lesser General Public License
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
using Microsoft.AspNet.SignalR;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Microsoft.AspNet.SignalR;
|
||||||
using Microsoft.Data.Entity;
|
using Microsoft.Data.Entity;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.Extensions.Localization;
|
||||||
|
|
||||||
namespace Yavsc
|
namespace Yavsc
|
||||||
{
|
{
|
||||||
|
|
@ -37,8 +38,10 @@ namespace Yavsc
|
||||||
public class ChatHub : Hub, IDisposable
|
public class ChatHub : Hub, IDisposable
|
||||||
{
|
{
|
||||||
ApplicationDbContext _dbContext;
|
ApplicationDbContext _dbContext;
|
||||||
|
private IStringLocalizer _localizer;
|
||||||
ILogger _logger;
|
ILogger _logger;
|
||||||
public static ConcurrentDictionary<string, string> ChatUserNames = new ConcurrentDictionary<string, string>();
|
public static ConcurrentDictionary<string, string> ChatUserNames = new ConcurrentDictionary<string, string>();
|
||||||
|
public static ConcurrentDictionary<string, ChatRoomInfo> Channels = new ConcurrentDictionary<string, ChatRoomInfo>();
|
||||||
|
|
||||||
public ChatHub()
|
public ChatHub()
|
||||||
{
|
{
|
||||||
|
|
@ -46,6 +49,9 @@ namespace Yavsc
|
||||||
|
|
||||||
_dbContext = scope.ServiceProvider.GetService<ApplicationDbContext>();
|
_dbContext = scope.ServiceProvider.GetService<ApplicationDbContext>();
|
||||||
var loggerFactory = scope.ServiceProvider.GetService<ILoggerFactory>();
|
var loggerFactory = scope.ServiceProvider.GetService<ILoggerFactory>();
|
||||||
|
|
||||||
|
var stringLocFactory = scope.ServiceProvider.GetService<IStringLocalizerFactory>();
|
||||||
|
_localizer = stringLocFactory.Create(typeof(ChatHub));
|
||||||
_logger = loggerFactory.CreateLogger<ChatHub>();
|
_logger = loggerFactory.CreateLogger<ChatHub>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -63,7 +69,7 @@ namespace Yavsc
|
||||||
await Groups.Add(Context.ConnectionId, group);
|
await Groups.Add(Context.ConnectionId, group);
|
||||||
if (isAuth)
|
if (isAuth)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Authenticated chat user");
|
_logger.LogInformation(_localizer.GetString(Constants.LabAuthChatUser));
|
||||||
|
|
||||||
var userId = _dbContext.Users.First(u => u.UserName == userName).Id;
|
var userId = _dbContext.Users.First(u => u.UserName == userName).Id;
|
||||||
|
|
||||||
|
|
@ -83,6 +89,12 @@ namespace Yavsc
|
||||||
Connected = true
|
Connected = true
|
||||||
});
|
});
|
||||||
_dbContext.SaveChanges();
|
_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
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -95,9 +107,7 @@ namespace Yavsc
|
||||||
{
|
{
|
||||||
await Groups.Add(Context.ConnectionId, Constants.HubGroupAnonymous);
|
await Groups.Add(Context.ConnectionId, Constants.HubGroupAnonymous);
|
||||||
}
|
}
|
||||||
// TODO only notify followers
|
await base.OnConnected();
|
||||||
Clients.Group(Constants.HubGroupAuthenticated).notifyuser(NotificationTypes.Connected, userName, "");
|
|
||||||
await base.OnConnected();
|
|
||||||
}
|
}
|
||||||
string setUserName()
|
string setUserName()
|
||||||
{
|
{
|
||||||
|
|
@ -123,7 +133,6 @@ namespace Yavsc
|
||||||
public override Task OnDisconnected(bool stopCalled)
|
public override Task OnDisconnected(bool stopCalled)
|
||||||
{
|
{
|
||||||
string userName = Context.User?.Identity.Name;
|
string userName = Context.User?.Identity.Name;
|
||||||
Clients.Group("authenticated").notifyUser(NotificationTypes.DisConnected, userName);
|
|
||||||
if (userName != null)
|
if (userName != null)
|
||||||
{
|
{
|
||||||
var cx = _dbContext.ChatConnection.SingleOrDefault(c => c.ConnectionId == Context.ConnectionId);
|
var cx = _dbContext.ChatConnection.SingleOrDefault(c => c.ConnectionId == Context.ConnectionId);
|
||||||
|
|
@ -142,6 +151,7 @@ namespace Yavsc
|
||||||
_dbContext.SaveChanges();
|
_dbContext.SaveChanges();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Clients.Group("authenticated").notifyUser(NotificationTypes.DisConnected, userName, "disconnected");
|
||||||
return base.OnDisconnected(stopCalled);
|
return base.OnDisconnected(stopCalled);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -170,12 +180,11 @@ namespace Yavsc
|
||||||
Connected = true
|
Connected = true
|
||||||
});
|
});
|
||||||
_dbContext.SaveChanges();
|
_dbContext.SaveChanges();
|
||||||
Clients.Group("authenticated").notifyUser(NotificationTypes.Reconnected, userName);
|
Clients.Group("authenticated").notifyUser(NotificationTypes.Reconnected, userName, "reconnected");
|
||||||
}
|
}
|
||||||
return base.OnReconnected();
|
return base.OnReconnected();
|
||||||
}
|
}
|
||||||
|
|
||||||
static ConcurrentDictionary<string, ChatRoomInfo> Channels = new ConcurrentDictionary<string, ChatRoomInfo>();
|
|
||||||
|
|
||||||
public class ChatRoomInfo
|
public class ChatRoomInfo
|
||||||
{
|
{
|
||||||
|
|
@ -206,7 +215,7 @@ namespace Yavsc
|
||||||
_logger.LogInformation("a client for " + roomName);
|
_logger.LogInformation("a client for " + roomName);
|
||||||
var userName = ChatUserNames[Context.ConnectionId];
|
var userName = ChatUserNames[Context.ConnectionId];
|
||||||
_logger.LogInformation($" chat user : {userName}");
|
_logger.LogInformation($" chat user : {userName}");
|
||||||
var roomGroupName = "room_" + roomName;
|
var roomGroupName = Constants.HubGroupRomsPrefix + roomName;
|
||||||
|
|
||||||
ChatRoomInfo chanInfo;
|
ChatRoomInfo chanInfo;
|
||||||
if (Channels.ContainsKey(roomName))
|
if (Channels.ContainsKey(roomName))
|
||||||
|
|
@ -221,7 +230,7 @@ namespace Yavsc
|
||||||
chanInfo.Users.Add(Context.ConnectionId, userName);
|
chanInfo.Users.Add(Context.ConnectionId, userName);
|
||||||
Groups.Add(Context.ConnectionId, roomGroupName);
|
Groups.Add(Context.ConnectionId, roomGroupName);
|
||||||
Clients.Caller.joint(chanInfo);
|
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 chanInfo;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|
@ -301,7 +310,7 @@ namespace Yavsc
|
||||||
ChatRoomInfo chanInfo;
|
ChatRoomInfo chanInfo;
|
||||||
if (Channels.TryGetValue(roomName, out chanInfo))
|
if (Channels.TryGetValue(roomName, out chanInfo))
|
||||||
{
|
{
|
||||||
var roomGroupName = "room_" + roomName;
|
var roomGroupName = Constants.HubGroupRomsPrefix + roomName;
|
||||||
if (!chanInfo.Users.ContainsKey(Context.ConnectionId))
|
if (!chanInfo.Users.ContainsKey(Context.ConnectionId))
|
||||||
{
|
{
|
||||||
NotifyRoomError(roomName, "you didn't join.");
|
NotifyRoomError(roomName, "you didn't join.");
|
||||||
|
|
@ -336,7 +345,7 @@ namespace Yavsc
|
||||||
|
|
||||||
public void Send(string roomName, string message)
|
public void Send(string roomName, string message)
|
||||||
{
|
{
|
||||||
var groupname = "room_" + roomName;
|
var groupname = Constants.HubGroupRomsPrefix + roomName;
|
||||||
ChatRoomInfo chanInfo;
|
ChatRoomInfo chanInfo;
|
||||||
if (Channels.TryGetValue(roomName, out chanInfo))
|
if (Channels.TryGetValue(roomName, out chanInfo))
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue