code format

This commit is contained in:
2019-05-24 20:15:48 +01:00
parent 8dcfe28816
commit 09b95fef54

View File

@ -18,6 +18,7 @@
// //
// 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 Microsoft.AspNet.SignalR;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Collections.Generic; using System.Collections.Generic;
@ -37,10 +38,10 @@ namespace Yavsc
{ {
ApplicationDbContext _dbContext; ApplicationDbContext _dbContext;
ILogger _logger; ILogger _logger;
public static ConcurrentDictionary<string, string> ChatUserNames = new ConcurrentDictionary<string, string>();
public ChatHub() public ChatHub()
{ {
var scope = Startup.Services.GetRequiredService<IServiceScopeFactory>().CreateScope(); var scope = Startup.Services.GetRequiredService<IServiceScopeFactory>().CreateScope();
_dbContext = scope.ServiceProvider.GetService<ApplicationDbContext>(); _dbContext = scope.ServiceProvider.GetService<ApplicationDbContext>();
@ -93,7 +94,6 @@ namespace Yavsc
} }
else else
{ {
// TODO var uname = Context.Request.QueryString[Constants.KeyParamChatUserName] ?? "anon";
await Groups.Add(Context.ConnectionId, Constants.HubGroupAnonymous); await Groups.Add(Context.ConnectionId, Constants.HubGroupAnonymous);
} }
@ -101,8 +101,6 @@ namespace Yavsc
Clients.Group(Constants.HubGroupAuthenticated).notify(NotificationTypes.Connected, userName); Clients.Group(Constants.HubGroupAuthenticated).notify(NotificationTypes.Connected, userName);
await base.OnConnected(); await base.OnConnected();
} }
static ConcurrentDictionary<string, string> ChatUserNames
= new ConcurrentDictionary<string, string>();
string setUserName() string setUserName()
{ {
if (Context.User != null) if (Context.User != null)
@ -137,7 +135,7 @@ namespace Yavsc
{ {
var user = _dbContext.Users.Single(u => u.UserName == userName); var user = _dbContext.Users.Single(u => u.UserName == userName);
user.Connections.Remove(cx); user.Connections.Remove(cx);
ChatUserNames[Context.ConnectionId]=null; ChatUserNames[Context.ConnectionId] = null;
} }
else else
{ {
@ -190,13 +188,13 @@ namespace Yavsc
public void Nick(string nickName) public void Nick(string nickName)
{ {
var candidate = "?"+nickName; var candidate = "?" + nickName;
if (ChatUserNames.Any(u=> u.Value == candidate )) if (ChatUserNames.Any(u => u.Value == candidate))
{ {
Clients.Caller.notify(NotificationTypes.ExistingUserName, nickName); Clients.Caller.notify(NotificationTypes.ExistingUserName, nickName);
return ; return;
} }
ChatUserNames[ Context.ConnectionId ] = "?"+nickName; ChatUserNames[Context.ConnectionId] = "?" + nickName;
} }
public void JoinAsync(string roomName) public void JoinAsync(string roomName)
@ -263,7 +261,7 @@ namespace Yavsc
if (Channels.TryAdd(roomName, chanInfo)) if (Channels.TryAdd(roomName, chanInfo))
{ {
Groups.Add(Context.ConnectionId, roomGroupName); Groups.Add(Context.ConnectionId, roomGroupName);
return(chanInfo); return (chanInfo);
} }
else _logger.LogError("Chan create failed unexpectly..."); else _logger.LogError("Chan create failed unexpectly...");
return null; return null;
@ -311,7 +309,7 @@ namespace Yavsc
Groups.Remove(Context.ConnectionId, roomGroupName); Groups.Remove(Context.ConnectionId, roomGroupName);
var group = Clients.Group(roomGroupName); var group = Clients.Group(roomGroupName);
var username = ChatUserNames[Context.ConnectionId]; var username = ChatUserNames[Context.ConnectionId];
group.notify( NotificationTypes.UserPart, $"{roomName} {username} ({reason})"); group.notify(NotificationTypes.UserPart, $"{roomName} {username} ({reason})");
chanInfo.Users.Remove(Context.ConnectionId); chanInfo.Users.Remove(Context.ConnectionId);
ChatRoomInfo deadchanInfo; ChatRoomInfo deadchanInfo;
@ -343,7 +341,7 @@ namespace Yavsc
{ {
if (!chanInfo.Users.ContainsKey(Context.ConnectionId)) if (!chanInfo.Users.ContainsKey(Context.ConnectionId))
{ {
var notSentMsg =$"could not send to channel ({roomName}) (not joint)"; var notSentMsg = $"could not send to channel ({roomName}) (not joint)";
Clients.Caller.notify(NotificationTypes.Error, notSentMsg); Clients.Caller.notify(NotificationTypes.Error, notSentMsg);
return; return;
} }
@ -367,7 +365,7 @@ namespace Yavsc
if (string.IsNullOrWhiteSpace(userName)) if (string.IsNullOrWhiteSpace(userName))
return; return;
if (userName[0]!='?') if (userName[0] != '?')
if (!Context.User.IsInRole(Constants.AdminGroupName)) if (!Context.User.IsInRole(Constants.AdminGroupName))
{ {
var bl = _dbContext.BlackListed var bl = _dbContext.BlackListed
@ -376,13 +374,13 @@ namespace Yavsc
.Where(r => r.User.UserName == Context.User.Identity.Name && r.Owner.UserName == userName) .Where(r => r.User.UserName == Context.User.Identity.Name && r.Owner.UserName == userName)
.Select(r => r.OwnerId); .Select(r => r.OwnerId);
if (bl.Count()>0) if (bl.Count() > 0)
{ {
Clients.Caller.notify(NotificationTypes.PrivateMessageDenied, userName); Clients.Caller.notify(NotificationTypes.PrivateMessageDenied, userName);
return; return;
} }
} }
var cxIds = ChatUserNames.Where(name => name.Value == userName ).Select( name => name.Key ); var cxIds = ChatUserNames.Where(name => name.Value == userName).Select(name => name.Key);
foreach (var connectionId in cxIds) foreach (var connectionId in cxIds)
{ {