yavsc/src/Yavsc/Hubs/ChatHub.cs

413 lines
16 KiB
C#

//
// ChatHub.cs
//
// Author:
// Paul Schneider <paul@pschneider.fr>
//
// Copyright (c) 2016-2019 GNU GPL
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// 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/>.
7 years ago
using Microsoft.AspNet.SignalR;
using System.Threading.Tasks;
9 years ago
using System.Collections.Generic;
using System.Linq;
using System;
using System.Collections.Concurrent;
using Microsoft.Data.Entity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Yavsc
{
using Models;
using Models.Chat;
public class ChatHub : Hub, IDisposable
{
ApplicationDbContext _dbContext;
ILogger _logger;
7 years ago
public static ConcurrentDictionary<string, string> ChatUserNames = new ConcurrentDictionary<string, string>();
7 years ago
public ChatHub()
7 years ago
{
var scope = Startup.Services.GetRequiredService<IServiceScopeFactory>().CreateScope();
7 years ago
_dbContext = scope.ServiceProvider.GetService<ApplicationDbContext>();
var loggerFactory = scope.ServiceProvider.GetService<ILoggerFactory>();
_logger = loggerFactory.CreateLogger<ChatHub>();
}
7 years ago
public override async Task OnConnected()
9 years ago
{
bool isAuth = false;
7 years ago
string userName = setUserName();
9 years ago
if (Context.User != null)
{
isAuth = Context.User.Identity.IsAuthenticated;
7 years ago
9 years ago
var group = isAuth ?
Constants.HubGroupAuthenticated : Constants.HubGroupAnonymous;
9 years ago
// Log ("Cx: " + group);
7 years ago
await Groups.Add(Context.ConnectionId, group);
if (isAuth)
{
7 years ago
_logger.LogInformation("Authenticated chat user");
7 years ago
var userId = _dbContext.Users.First(u => u.UserName == userName).Id;
var userHadConnections = _dbContext.ChatConnection.Any(accx => accx.ConnectionId == Context.ConnectionId);
7 years ago
if (userHadConnections)
{
var ccx = _dbContext.ChatConnection.First(c => c.ConnectionId == Context.ConnectionId);
ccx.Connected = true;
}
else
_dbContext.ChatConnection.Add(new ChatConnection
7 years ago
{
7 years ago
ApplicationUserId = userId,
7 years ago
ConnectionId = Context.ConnectionId,
UserAgent = Context.Request.Headers["User-Agent"],
Connected = true
});
7 years ago
_dbContext.SaveChanges();
}
else
7 years ago
{
// this line isn't reached: Context.User != null <=> Context.User.Identity.IsAuthenticated
7 years ago
_logger.LogInformation("Anonymous chat user (first use case)");
throw new NotSupportedException();
}
}
7 years ago
else
{
7 years ago
await Groups.Add(Context.ConnectionId, Constants.HubGroupAnonymous);
9 years ago
}
// TODO only notify followers
Clients.Group(Constants.HubGroupAuthenticated).notifyuser(NotificationTypes.Connected, userName, "");
await base.OnConnected();
9 years ago
}
7 years ago
string setUserName()
{
7 years ago
if (Context.User != null)
7 years ago
if (Context.User.Identity.IsAuthenticated)
{
7 years ago
ChatUserNames[Context.ConnectionId] = Context.User.Identity.Name;
7 years ago
_logger.LogInformation($"chat user name set to : {Context.User.Identity.Name}");
return Context.User.Identity.Name;
}
anonymousSequence++;
7 years ago
var queryUname = Context.Request.QueryString[Constants.KeyParamChatUserName];
var aname = $"{Constants.AnonymousUserNamePrefix}{queryUname}{anonymousSequence}";
7 years ago
ChatUserNames[Context.ConnectionId] = aname;
_logger.LogInformation($"Anonymous chat user name set to : {aname}");
return aname;
7 years ago
}
7 years ago
static long anonymousSequence = 0;
9 years ago
public override Task OnDisconnected(bool stopCalled)
{
string userName = Context.User?.Identity.Name;
Clients.Group("authenticated").notifyUser(NotificationTypes.DisConnected, userName);
if (userName != null)
{
7 years ago
var cx = _dbContext.ChatConnection.SingleOrDefault(c => c.ConnectionId == Context.ConnectionId);
if (cx != null)
{
if (stopCalled)
{
7 years ago
var user = _dbContext.Users.Single(u => u.UserName == userName);
user.Connections.Remove(cx);
7 years ago
ChatUserNames[Context.ConnectionId] = null;
}
7 years ago
else
{
cx.Connected = false;
}
_dbContext.SaveChanges();
}
}
9 years ago
return base.OnDisconnected(stopCalled);
}
public override Task OnReconnected()
{
7 years ago
if (Context.User != null) if (Context.User.Identity.IsAuthenticated)
7 years ago
{
var userName = Context.User.Identity.Name;
var user = _dbContext.Users.FirstOrDefault(u => u.UserName == userName);
if (user == null)
_logger.LogWarning($"null user with <{userName}> & Context.User.Identity.IsAuthenticated");
var userId = user.Id;
var userHadConnections = _dbContext.ChatConnection.Any(accx => accx.ConnectionId == Context.ConnectionId);
if (userHadConnections)
7 years ago
{
7 years ago
var ccx = _dbContext.ChatConnection.First(c => c.ConnectionId == Context.ConnectionId);
ccx.Connected = true;
}
else
_dbContext.ChatConnection.Add(new ChatConnection
{
ApplicationUserId = userId,
ConnectionId = Context.ConnectionId,
UserAgent = Context.Request.Headers["User-Agent"],
Connected = true
});
_dbContext.SaveChanges();
Clients.Group("authenticated").notifyUser(NotificationTypes.Reconnected, userName);
7 years ago
}
7 years ago
return base.OnReconnected();
}
7 years ago
static ConcurrentDictionary<string, ChatRoomInfo> Channels = new ConcurrentDictionary<string, ChatRoomInfo>();
7 years ago
public class ChatRoomInfo
{
public string Name;
public Dictionary<string, string> Users = new Dictionary<string, string>();
7 years ago
public string Topic;
}
public void Nick(string nickName)
{
7 years ago
var candidate = "?" + nickName;
if (ChatUserNames.Any(u => u.Value == candidate))
{
Clients.Caller.notifyUser(NotificationTypes.ExistingUserName, nickName, "aborting");
7 years ago
return;
}
7 years ago
ChatUserNames[Context.ConnectionId] = "?" + nickName;
}
public void JoinAsync(string roomName)
{
7 years ago
var info = Join(roomName);
Clients.Caller.joint(info);
}
public ChatRoomInfo Join(string roomName)
7 years ago
{
7 years ago
_logger.LogInformation("a client for " + roomName);
7 years ago
var userName = ChatUserNames[Context.ConnectionId];
_logger.LogInformation($" chat user : {userName}");
7 years ago
var roomGroupName = "room_" + roomName;
7 years ago
ChatRoomInfo chanInfo;
if (Channels.ContainsKey(roomName))
{
7 years ago
if (Channels.TryGetValue(roomName, out chanInfo))
{
7 years ago
_logger.LogInformation("room is avaible.");
if (chanInfo.Users.ContainsKey(Context.ConnectionId))
7 years ago
_logger.LogWarning("user already joint.");
else
{
chanInfo.Users.Add(Context.ConnectionId, userName);
Groups.Add(Context.ConnectionId, roomGroupName);
Clients.Caller.joint(chanInfo);
Clients.Group("room_" + roomName).notifyRoom(NotificationTypes.UserJoin, roomName, userName);
return chanInfo;
}
return null;
7 years ago
}
7 years ago
else
{
7 years ago
_logger.LogInformation("room seemd to be avaible ... but we could get no info on it.");
Clients.Caller.notifyRoom(NotificationTypes.Error, roomName, "join get chan failed ...");
return null;
7 years ago
}
}
7 years ago
// chan was almost empty
_logger.LogInformation("joining empty chan.");
7 years ago
var room = _dbContext.ChatRoom.FirstOrDefault(r => r.Name == roomName);
7 years ago
7 years ago
chanInfo = new ChatRoomInfo();
chanInfo.Users.Add(Context.ConnectionId, userName);
7 years ago
if (room != null)
7 years ago
{
_logger.LogInformation("existent room.");
chanInfo.Topic = room.Topic;
chanInfo.Name = room.Name;
}
7 years ago
else
{ // a first join, we create it.
7 years ago
_logger.LogInformation("room creation.");
chanInfo.Name = roomName;
chanInfo.Topic = "<just created>";
}
if (Channels.TryAdd(roomName, chanInfo))
{
Groups.Add(Context.ConnectionId, roomGroupName);
7 years ago
return (chanInfo);
7 years ago
}
else _logger.LogError("Chan create failed unexpectly...");
return null;
9 years ago
}
7 years ago
[Authorize]
7 years ago
public void Register(string room)
9 years ago
{
7 years ago
var existent = _dbContext.ChatRoom.Any(r => r.Name == room);
7 years ago
if (existent)
{
Clients.Caller.notifyUser(NotificationTypes.Error, room, "already registered.");
7 years ago
return;
}
string userName = Context.User.Identity.Name;
7 years ago
var user = _dbContext.Users.FirstOrDefault(u => u.UserName == userName);
7 years ago
var newroom = new ChatRoom { Name = room, OwnerId = user.Id };
ChatRoomInfo chanInfo;
if (Channels.TryGetValue(room, out chanInfo))
{
// TODO get and require some admin status for current user on this chan
newroom.Topic = chanInfo.Topic;
}
newroom.LatestJoinPart = DateTime.Now;
_dbContext.ChatRoom.Add(newroom);
_dbContext.SaveChanges(user.Id);
9 years ago
}
7 years ago
/** TODO chan register on server command
room = new ChatRoom { Name = roomName, OwnerId = uid };
_dbContext.ChatRoom.Add(room);
_dbContext.SaveChanges(uid);
room.LatestJoinPart = DateTime.Now;
chanInfo.Topic = room.Topic;
*/
9 years ago
7 years ago
public void Part(string roomName, string reason)
7 years ago
{
ChatRoomInfo chanInfo;
if (Channels.TryGetValue(roomName, out chanInfo))
{
7 years ago
var roomGroupName = "room_" + roomName;
if (!chanInfo.Users.ContainsKey(Context.ConnectionId))
{
NotifyRoomError(roomName, "you didn't join.");
return;
}
7 years ago
Groups.Remove(Context.ConnectionId, roomGroupName);
var group = Clients.Group(roomGroupName);
var username = ChatUserNames[Context.ConnectionId];
group.notifyRoom(NotificationTypes.UserPart, roomName, $"{username}: {reason}");
7 years ago
chanInfo.Users.Remove(Context.ConnectionId);
ChatRoomInfo deadchanInfo;
7 years ago
if (chanInfo.Users.Count == 0)
7 years ago
if (Channels.TryRemove(roomName, out deadchanInfo))
{
var room = _dbContext.ChatRoom.FirstOrDefault(r => r.Name == roomName);
room.LatestJoinPart = DateTime.Now;
_dbContext.SaveChanges();
}
}
7 years ago
else
{
NotifyRoomError(roomName, $"could not join: no such room");
7 years ago
}
}
void NotifyRoomError(string room, string reason)
{
Clients.Caller.notifyUser(NotificationTypes.Error, room, reason);
}
7 years ago
public void Send(string roomName, string message)
{
7 years ago
var groupname = "room_" + roomName;
7 years ago
ChatRoomInfo chanInfo;
if (Channels.TryGetValue(roomName, out chanInfo))
{
7 years ago
if (!chanInfo.Users.ContainsKey(Context.ConnectionId))
{
7 years ago
var notSentMsg = $"could not send to channel ({roomName}) (not joint)";
Clients.Caller.notifyUser(NotificationTypes.Error, roomName, notSentMsg);
7 years ago
return;
}
7 years ago
string uname = ChatUserNames[Context.ConnectionId];
7 years ago
Clients.Group(groupname).addMessage(uname, roomName, message);
_logger.LogInformation($"{uname} sent message {message} to {roomName}");
7 years ago
}
else
{
var noChanMsg = $"could not send to channel ({roomName}) (no such chan)";
Clients.Caller.notifyUser(NotificationTypes.Error, roomName, noChanMsg);
_logger.LogWarning(noChanMsg);
7 years ago
return;
}
7 years ago
7 years ago
}
9 years ago
[Authorize]
public void SendPV(string userName, string message)
9 years ago
{
7 years ago
if (string.IsNullOrWhiteSpace(userName))
return;
if (userName[0] != '?')
if (!Context.User.IsInRole(Constants.AdminGroupName))
{
var bl = _dbContext.BlackListed
.Include(r => r.User)
.Include(r => r.Owner)
.Where(r => r.User.UserName == Context.User.Identity.Name && r.Owner.UserName == userName)
.Select(r => r.OwnerId);
if (bl.Count() > 0)
{
Clients.Caller.notifyUser(NotificationTypes.PrivateMessageDenied, userName, "you are black listed.");
7 years ago
return;
}
}
var cxIds = ChatUserNames.Where(name => name.Value == userName).Select(name => name.Key);
foreach (var connectionId in cxIds)
{
var cli = Clients.Client(connectionId);
cli.addPV(Context.User.Identity.Name, message);
}
9 years ago
}
7 years ago
[Authorize]
public void SendStream(string connectionId, long streamId, string message)
{
var sender = Context.User.Identity.Name;
var cli = Clients.Client(connectionId);
cli.addStreamInfo(sender, streamId, message);
}
public void Abort()
9 years ago
{
7 years ago
var cx = _dbContext.ChatConnection.SingleOrDefault(c => c.ConnectionId == Context.ConnectionId);
if (cx != null)
{
7 years ago
_dbContext.ChatConnection.Remove(cx);
7 years ago
_dbContext.SaveChanges();
}
}
9 years ago
}
}