/
Chat.cs
1246 lines (1000 loc) · 42.4 KB
/
Chat.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using JabbR.Commands;
using JabbR.ContentProviders.Core;
using JabbR.Infrastructure;
using JabbR.Models;
using JabbR.Services;
using JabbR.ViewModels;
using Microsoft.AspNet.SignalR;
using Newtonsoft.Json;
namespace JabbR
{
[AuthorizeClaim(JabbRClaimTypes.Identifier)]
public class Chat : Hub, INotificationService
{
private static readonly TimeSpan _disconnectThreshold = TimeSpan.FromSeconds(10);
private readonly IJabbrRepository _repository;
private readonly IChatService _service;
private readonly IRecentMessageCache _recentMessageCache;
private readonly ICache _cache;
private readonly ContentProviderProcessor _resourceProcessor;
private readonly ILogger _logger;
private readonly ApplicationSettings _settings;
public Chat(ContentProviderProcessor resourceProcessor,
IChatService service,
IRecentMessageCache recentMessageCache,
IJabbrRepository repository,
ICache cache,
ILogger logger,
ApplicationSettings settings)
{
_resourceProcessor = resourceProcessor;
_service = service;
_recentMessageCache = recentMessageCache;
_repository = repository;
_cache = cache;
_logger = logger;
_settings = settings;
}
private string UserAgent
{
get
{
if (Context.Headers != null)
{
return Context.Headers["User-Agent"];
}
return null;
}
}
private bool OutOfSync
{
get
{
string version = Context.QueryString["version"];
if (String.IsNullOrEmpty(version))
{
return true;
}
return new Version(version) != Constants.JabbRVersion;
}
}
public override Task OnConnected()
{
_logger.Log("OnConnected({0})", Context.ConnectionId);
CheckStatus();
return base.OnConnected();
}
public void Join()
{
Join(reconnecting: false);
}
public void Join(bool reconnecting)
{
// Get the client state
var userId = Context.User.GetUserId();
// Try to get the user from the client state
ChatUser user = _repository.GetUserById(userId);
if (reconnecting)
{
_logger.Log("{0}:{1} connected after dropping connection.", user.Name, Context.ConnectionId);
// If the user was marked as offline then mark them inactive
if (user.Status == (int)UserStatus.Offline)
{
user.Status = (int)UserStatus.Inactive;
_repository.CommitChanges();
}
// Ensure the client is re-added
_service.AddClient(user, Context.ConnectionId, UserAgent);
}
else
{
_logger.Log("{0}:{1} connected.", user.Name, Context.ConnectionId);
// Update some user values
_service.UpdateActivity(user, Context.ConnectionId, UserAgent);
_repository.CommitChanges();
}
ClientState clientState = GetClientState();
OnUserInitialize(clientState, user, reconnecting);
}
private void CheckStatus()
{
if (OutOfSync)
{
Clients.Caller.outOfSync();
}
}
private void OnUserInitialize(ClientState clientState, ChatUser user, bool reconnecting)
{
// Update the active room on the client (only if it's still a valid room)
if (user.Rooms.Any(room => room.Name.Equals(clientState.ActiveRoom, StringComparison.OrdinalIgnoreCase)))
{
// Update the active room on the client (only if it's still a valid room)
Clients.Caller.activeRoom = clientState.ActiveRoom;
}
LogOn(user, Context.ConnectionId, reconnecting);
}
public bool Send(string content, string roomName)
{
var message = new ClientMessage
{
Content = content,
Room = roomName
};
return Send(message);
}
public bool Send(ClientMessage clientMessage)
{
CheckStatus();
// reject it if it's too long
if (_settings.MaxMessageLength > 0 && clientMessage.Content.Length > _settings.MaxMessageLength)
{
throw new HubException(String.Format(LanguageResources.SendMessageTooLong, _settings.MaxMessageLength));
}
// See if this is a valid command (starts with /)
if (TryHandleCommand(clientMessage.Content, clientMessage.Room))
{
return true;
}
var userId = Context.User.GetUserId();
ChatUser user = _repository.VerifyUserId(userId);
ChatRoom room = _repository.VerifyUserRoom(_cache, user, clientMessage.Room);
if (room == null || (room.Private && !user.AllowedRooms.Contains(room)))
{
return false;
}
// REVIEW: Is it better to use the extension method room.EnsureOpen here?
if (room.Closed)
{
throw new HubException(String.Format(LanguageResources.SendMessageRoomClosed, clientMessage.Room));
}
// Update activity *after* ensuring the user, this forces them to be active
UpdateActivity(user, room);
// Create a true unique id and save the message to the db
string id = Guid.NewGuid().ToString("d");
ChatMessage chatMessage = _service.AddMessage(user, room, id, clientMessage.Content);
_repository.CommitChanges();
var messageViewModel = new MessageViewModel(chatMessage);
if (clientMessage.Id == null)
{
// If the client didn't generate an id for the message then just
// send it to everyone. The assumption is that the client has some ui
// that it wanted to update immediately showing the message and
// then when the actual message is roundtripped it would "solidify it".
Clients.Group(room.Name).addMessage(messageViewModel, room.Name);
}
else
{
// If the client did set an id then we need to give everyone the real id first
Clients.OthersInGroup(room.Name).addMessage(messageViewModel, room.Name);
// Now tell the caller to replace the message
Clients.Caller.replaceMessage(clientMessage.Id, messageViewModel, room.Name);
}
// Add mentions
AddMentions(chatMessage);
var urls = UrlExtractor.ExtractUrls(chatMessage.Content);
if (urls.Count > 0)
{
_resourceProcessor.ProcessUrls(urls, Clients, room.Name, chatMessage.Id);
}
return true;
}
private void AddMentions(ChatMessage message)
{
var mentionedUsers = new List<ChatUser>();
foreach (var userName in MentionExtractor.ExtractMentions(message.Content))
{
ChatUser mentionedUser = _repository.GetUserByName(userName);
// Don't create a mention if
// 1. If the mentioned user doesn't exist.
// 2. If you mention yourself
// 3. If you're mentioned in a private room that you don't have access to
// 4. You've already been mentioned in this message
if (mentionedUser == null ||
mentionedUser == message.User ||
(message.Room.Private && !mentionedUser.AllowedRooms.Contains(message.Room)) ||
mentionedUsers.Contains(mentionedUser))
{
continue;
}
// mark as read if ALL of the following
// 1. user is not offline
// 2. user is not AFK
// 3. user has been active within the last 10 minutes
// 4. user is currently in the room
bool markAsRead = mentionedUser.Status != (int)UserStatus.Offline
&& !mentionedUser.IsAfk
&& (DateTimeOffset.UtcNow - mentionedUser.LastActivity) < TimeSpan.FromMinutes(10)
&& _repository.IsUserInRoom(_cache, mentionedUser, message.Room);
_service.AddNotification(mentionedUser, message, message.Room, markAsRead);
mentionedUsers.Add(mentionedUser);
}
if (mentionedUsers.Count > 0)
{
_repository.CommitChanges();
}
foreach (var user in mentionedUsers)
{
UpdateUnreadMentions(user);
}
}
private void UpdateUnreadMentions(ChatUser mentionedUser)
{
var unread = _repository.GetUnreadNotificationsCount(mentionedUser);
Clients.User(mentionedUser.Id)
.updateUnreadNotifications(unread);
}
public UserViewModel GetUserInfo()
{
var userId = Context.User.GetUserId();
ChatUser user = _repository.VerifyUserId(userId);
return new UserViewModel(user);
}
public override Task OnReconnected()
{
_logger.Log("OnReconnected({0})", Context.ConnectionId);
var userId = Context.User.GetUserId();
ChatUser user = _repository.VerifyUserId(userId);
if (user == null)
{
_logger.Log("Reconnect failed user {0}:{1} doesn't exist.", userId, Context.ConnectionId);
return TaskAsyncHelper.Empty;
}
// Make sure this client is being tracked
_service.AddClient(user, Context.ConnectionId, UserAgent);
var currentStatus = (UserStatus)user.Status;
if (currentStatus == UserStatus.Offline)
{
_logger.Log("{0}:{1} reconnected after temporary network problem and marked offline.", user.Name, Context.ConnectionId);
// Mark the user as inactive
user.Status = (int)UserStatus.Inactive;
_repository.CommitChanges();
// If the user was offline that means they are not in the user list so we need to tell
// everyone the user is really in the room
var userViewModel = new UserViewModel(user);
foreach (var room in user.Rooms)
{
var isOwner = user.OwnedRooms.Contains(room);
// Tell the people in this room that you've joined
Clients.Group(room.Name).addUser(userViewModel, room.Name, isOwner);
}
}
else
{
_logger.Log("{0}:{1} reconnected after temporary network problem.", user.Name, Context.ConnectionId);
}
CheckStatus();
return base.OnReconnected();
}
public override Task OnDisconnected()
{
_logger.Log("OnDisconnected({0})", Context.ConnectionId);
DisconnectClient(Context.ConnectionId, useThreshold: true);
return base.OnDisconnected();
}
public object GetCommands()
{
return CommandManager.GetCommandsMetaData();
}
public object GetShortcuts()
{
return new[] {
new { Name = "Tab or Shift + Tab", Group = "shortcut", IsKeyCombination = true, Description = LanguageResources.Client_ShortcutTabs },
new { Name = "Alt + L", Group = "shortcut", IsKeyCombination = true, Description = LanguageResources.Client_ShortcutLobby },
new { Name = "Alt + Number", Group = "shortcut", IsKeyCombination = true, Description = LanguageResources.Client_ShortcutSpecificTab }
};
}
public Task<List<LobbyRoomViewModel>> GetRooms()
{
string userId = Context.User.GetUserId();
ChatUser user = _repository.VerifyUserId(userId);
return _repository.GetAllowedRooms(user).Select(r => new LobbyRoomViewModel
{
Name = r.Name,
Count = r.Users.Count(u => u.Status != (int)UserStatus.Offline),
Private = r.Private,
Closed = r.Closed,
Topic = r.Topic
}).ToListAsync();
}
public async Task<IEnumerable<MessageViewModel>> GetPreviousMessages(string messageId)
{
var previousMessages = await (from m in _repository.GetPreviousMessages(messageId)
orderby m.When descending
select m).Take(100).ToListAsync();
return previousMessages.AsEnumerable()
.Reverse()
.Select(m => new MessageViewModel(m));
}
public async Task LoadRooms(string[] roomNames)
{
string userId = Context.User.GetUserId();
ChatUser user = _repository.VerifyUserId(userId);
var rooms = await _repository.Rooms.Where(r => roomNames.Contains(r.Name))
.ToListAsync();
// Can't async whenall because we'd be hitting a single
// EF context with multiple concurrent queries.
foreach (var room in rooms)
{
if (room == null || (room.Private && !user.AllowedRooms.Contains(room)))
{
continue;
}
RoomViewModel roomInfo = null;
while (true)
{
try
{
// If invoking roomLoaded fails don't get the roomInfo again
roomInfo = roomInfo ?? await GetRoomInfoCore(room);
Clients.Caller.roomLoaded(roomInfo);
break;
}
catch (Exception ex)
{
_logger.Log(ex);
}
}
}
}
public Task<RoomViewModel> GetRoomInfo(string roomName)
{
if (String.IsNullOrEmpty(roomName))
{
return null;
}
string userId = Context.User.GetUserId();
ChatUser user = _repository.VerifyUserId(userId);
ChatRoom room = _repository.GetRoomByName(roomName);
if (room == null || (room.Private && !user.AllowedRooms.Contains(room)))
{
return null;
}
return GetRoomInfoCore(room);
}
private async Task<RoomViewModel> GetRoomInfoCore(ChatRoom room)
{
var recentMessages = _recentMessageCache.GetRecentMessages(room.Name);
// If we haven't cached enough messages just populate it now
if (recentMessages.Count == 0)
{
var messages = await (from m in _repository.GetMessagesByRoom(room)
orderby m.When descending
select m).Take(50).ToListAsync();
// Reverse them since we want to get them in chronological order
messages.Reverse();
recentMessages = messages.Select(m => new MessageViewModel(m)).ToList();
_recentMessageCache.Add(room.Name, recentMessages);
}
// Get online users through the repository
List<ChatUser> onlineUsers = await _repository.GetOnlineUsers(room).ToListAsync();
return new RoomViewModel
{
Name = room.Name,
Users = from u in onlineUsers
select new UserViewModel(u),
Owners = from u in room.Owners.Online()
select u.Name,
RecentMessages = recentMessages,
Topic = room.Topic ?? String.Empty,
Welcome = room.Welcome ?? String.Empty,
Closed = room.Closed
};
}
public void PostNotification(ClientNotification notification)
{
PostNotification(notification, executeContentProviders: true);
}
public void PostNotification(ClientNotification notification, bool executeContentProviders)
{
string userId = Context.User.GetUserId();
ChatUser user = _repository.GetUserById(userId);
ChatRoom room = _repository.VerifyUserRoom(_cache, user, notification.Room);
// User must be an owner
if (room == null ||
!room.Owners.Contains(user) ||
(room.Private && !user.AllowedRooms.Contains(room)))
{
throw new HubException(LanguageResources.PostNotification_NotAllowed);
}
var chatMessage = new ChatMessage
{
Id = Guid.NewGuid().ToString("d"),
Content = notification.Content,
User = user,
Room = room,
HtmlEncoded = false,
ImageUrl = notification.ImageUrl,
Source = notification.Source,
When = DateTimeOffset.UtcNow,
MessageType = (int)MessageType.Notification
};
_repository.Add(chatMessage);
_repository.CommitChanges();
Clients.Group(room.Name).addMessage(new MessageViewModel(chatMessage), room.Name);
if (executeContentProviders)
{
var urls = UrlExtractor.ExtractUrls(chatMessage.Content);
if (urls.Count > 0)
{
_resourceProcessor.ProcessUrls(urls, Clients, room.Name, chatMessage.Id);
}
}
}
public void Typing(string roomName)
{
string userId = Context.User.GetUserId();
ChatUser user = _repository.GetUserById(userId);
ChatRoom room = _repository.VerifyUserRoom(_cache, user, roomName);
if (room == null || (room.Private && !user.AllowedRooms.Contains(room)))
{
return;
}
UpdateActivity(user, room);
var userViewModel = new UserViewModel(user);
Clients.Group(room.Name).setTyping(userViewModel, room.Name);
}
public void UpdateActivity()
{
string userId = Context.User.GetUserId();
ChatUser user = _repository.GetUserById(userId);
foreach (var room in user.Rooms)
{
UpdateActivity(user, room);
}
CheckStatus();
}
public void TabOrderChanged(string[] tabOrdering)
{
string userId = Context.User.GetUserId();
ChatUser user = _repository.GetUserById(userId);
ChatUserPreferences userPreferences = user.Preferences;
userPreferences.TabOrder = tabOrdering.ToList();
user.Preferences = userPreferences;
_repository.CommitChanges();
Clients.User(user.Id).updateTabOrder(tabOrdering);
}
private void LogOn(ChatUser user, string clientId, bool reconnecting)
{
if (!reconnecting)
{
// Update the client state
Clients.Caller.id = user.Id;
Clients.Caller.name = user.Name;
Clients.Caller.hash = user.Hash;
Clients.Caller.unreadNotifications = user.Notifications.Count(n => !n.Read);
}
var rooms = new List<RoomViewModel>();
var privateRooms = new List<LobbyRoomViewModel>();
var userViewModel = new UserViewModel(user);
var ownedRooms = user.OwnedRooms.Select(r => r.Key);
foreach (var room in user.Rooms)
{
var isOwner = ownedRooms.Contains(room.Key);
// Tell the people in this room that you've joined
Clients.Group(room.Name).addUser(userViewModel, room.Name, isOwner);
// Add the caller to the group so they receive messages
Groups.Add(clientId, room.Name);
if (!reconnecting)
{
// Add to the list of room names
rooms.Add(new RoomViewModel
{
Name = room.Name,
Private = room.Private,
Closed = room.Closed
});
}
}
if (!reconnecting)
{
foreach (var r in user.AllowedRooms)
{
privateRooms.Add(new LobbyRoomViewModel
{
Name = r.Name,
Count = _repository.GetOnlineUsers(r).Count(),
Private = r.Private,
Closed = r.Closed,
Topic = r.Topic
});
}
// Initialize the chat with the rooms the user is in
Clients.Caller.logOn(rooms, privateRooms, user.Preferences);
}
}
private void UpdateActivity(ChatUser user, ChatRoom room)
{
UpdateActivity(user);
OnUpdateActivity(user, room);
}
private void UpdateActivity(ChatUser user)
{
_service.UpdateActivity(user, Context.ConnectionId, UserAgent);
_repository.CommitChanges();
}
private bool TryHandleCommand(string command, string room)
{
string clientId = Context.ConnectionId;
string userId = Context.User.GetUserId();
var commandManager = new CommandManager(clientId, UserAgent, userId, room, _service, _repository, _cache, this);
return commandManager.TryHandleCommand(command);
}
private void DisconnectClient(string clientId, bool useThreshold = false)
{
string userId = _service.DisconnectClient(clientId);
if (String.IsNullOrEmpty(userId))
{
_logger.Log("Failed to disconnect {0}. No user found", clientId);
return;
}
if (useThreshold)
{
Thread.Sleep(_disconnectThreshold);
}
// Query for the user to get the updated status
ChatUser user = _repository.GetUserById(userId);
// There's no associated user for this client id
if (user == null)
{
_logger.Log("Failed to disconnect {0}:{1}. No user found", userId, clientId);
return;
}
_repository.Reload(user);
_logger.Log("{0}:{1} disconnected", user.Name, Context.ConnectionId);
// The user will be marked as offline if all clients leave
if (user.Status == (int)UserStatus.Offline)
{
_logger.Log("Marking {0} offline", user.Name);
foreach (var room in user.Rooms)
{
var userViewModel = new UserViewModel(user);
Clients.OthersInGroup(room.Name).leave(userViewModel, room.Name);
}
}
}
private void OnUpdateActivity(ChatUser user, ChatRoom room)
{
var userViewModel = new UserViewModel(user);
Clients.Group(room.Name).updateActivity(userViewModel, room.Name);
}
private void LeaveRoom(ChatUser user, ChatRoom room)
{
var userViewModel = new UserViewModel(user);
Clients.Group(room.Name).leave(userViewModel, room.Name);
foreach (var client in user.ConnectedClients)
{
Groups.Remove(client.Id, room.Name);
}
OnRoomChanged(room);
}
void INotificationService.LogOn(ChatUser user, string clientId)
{
LogOn(user, clientId, reconnecting: true);
}
void INotificationService.KickUser(ChatUser targetUser, ChatRoom room)
{
foreach (var client in targetUser.ConnectedClients)
{
// Kick the user from this room
Clients.Client(client.Id).kick(room.Name);
// Remove the user from this the room group so he doesn't get the leave message
Groups.Remove(client.Id, room.Name);
}
// Tell the room the user left
LeaveRoom(targetUser, room);
}
void INotificationService.OnUserCreated(ChatUser user)
{
// Set some client state
Clients.Caller.name = user.Name;
Clients.Caller.id = user.Id;
Clients.Caller.hash = user.Hash;
// Tell the client a user was created
Clients.Caller.userCreated();
}
void INotificationService.JoinRoom(ChatUser user, ChatRoom room)
{
var userViewModel = new UserViewModel(user);
var roomViewModel = new RoomViewModel
{
Name = room.Name,
Private = room.Private,
Welcome = room.Welcome ?? String.Empty,
Closed = room.Closed
};
var isOwner = user.OwnedRooms.Contains(room);
// Tell all clients to join this room
Clients.User(user.Id).joinRoom(roomViewModel);
// Tell the people in this room that you've joined
Clients.Group(room.Name).addUser(userViewModel, room.Name, isOwner);
// Notify users of the room count change
OnRoomChanged(room);
foreach (var client in user.ConnectedClients)
{
Groups.Add(client.Id, room.Name);
}
}
void INotificationService.AllowUser(ChatUser targetUser, ChatRoom targetRoom)
{
// Build a viewmodel for the room
var roomViewModel = new RoomViewModel
{
Name = targetRoom.Name,
Private = targetRoom.Private,
Closed = targetRoom.Closed,
Topic = targetRoom.Topic ?? String.Empty,
Count = _repository.GetOnlineUsers(targetRoom).Count()
};
// Tell this client it's allowed. Pass down a viewmodel so that we can add the room to the lobby.
Clients.User(targetUser.Id).allowUser(targetRoom.Name, roomViewModel);
// Tell the calling client the granting permission into the room was successful
Clients.Caller.userAllowed(targetUser.Name, targetRoom.Name);
}
void INotificationService.UnallowUser(ChatUser targetUser, ChatRoom targetRoom)
{
// Kick the user from the room when they are unallowed
((INotificationService)this).KickUser(targetUser, targetRoom);
// Tell this client it's no longer allowed
Clients.User(targetUser.Id).unallowUser(targetRoom.Name);
// Tell the calling client the granting permission into the room was successful
Clients.Caller.userUnallowed(targetUser.Name, targetRoom.Name);
}
void INotificationService.AddOwner(ChatUser targetUser, ChatRoom targetRoom)
{
// Tell this client it's an owner
Clients.User(targetUser.Id).makeOwner(targetRoom.Name);
var userViewModel = new UserViewModel(targetUser);
// If the target user is in the target room.
// Tell everyone in the target room that a new owner was added
if (_repository.IsUserInRoom(_cache, targetUser, targetRoom))
{
Clients.Group(targetRoom.Name).addOwner(userViewModel, targetRoom.Name);
}
// Tell the calling client the granting of ownership was successful
Clients.Caller.ownerMade(targetUser.Name, targetRoom.Name);
}
void INotificationService.RemoveOwner(ChatUser targetUser, ChatRoom targetRoom)
{
// Tell this client it's no longer an owner
Clients.User(targetUser.Id).demoteOwner(targetRoom.Name);
var userViewModel = new UserViewModel(targetUser);
// If the target user is in the target room.
// Tell everyone in the target room that the owner was removed
if (_repository.IsUserInRoom(_cache, targetUser, targetRoom))
{
Clients.Group(targetRoom.Name).removeOwner(userViewModel, targetRoom.Name);
}
// Tell the calling client the removal of ownership was successful
Clients.Caller.ownerRemoved(targetUser.Name, targetRoom.Name);
}
void INotificationService.ChangeGravatar(ChatUser user)
{
Clients.Caller.hash = user.Hash;
// Update the calling client
Clients.User(user.Id).gravatarChanged(user.Hash);
// Create the view model
var userViewModel = new UserViewModel(user);
// Tell all users in rooms to change the gravatar
foreach (var room in user.Rooms)
{
Clients.Group(room.Name).changeGravatar(userViewModel, room.Name);
}
}
void INotificationService.OnSelfMessage(ChatRoom room, ChatUser user, string content)
{
Clients.Group(room.Name).sendMeMessage(user.Name, content, room.Name);
}
void INotificationService.SendPrivateMessage(ChatUser fromUser, ChatUser toUser, string messageText)
{
// Send a message to the sender and the sendee
Clients.User(fromUser.Id).sendPrivateMessage(fromUser.Name, toUser.Name, messageText);
Clients.User(toUser.Id).sendPrivateMessage(fromUser.Name, toUser.Name, messageText);
}
void INotificationService.PostNotification(ChatRoom room, ChatUser user, string message)
{
Clients.User(user.Id).postNotification(message, room.Name);
}
void INotificationService.ListRooms(ChatUser user)
{
string userId = Context.User.GetUserId();
var userModel = new UserViewModel(user);
Clients.Caller.showUsersRoomList(userModel, user.Rooms.Allowed(userId).Select(r => r.Name));
}
void INotificationService.ListUsers()
{
var users = _repository.Users.Online().Select(s => s.Name).OrderBy(s => s);
Clients.Caller.listUsers(users);
}
void INotificationService.ListUsers(IEnumerable<ChatUser> users)
{
Clients.Caller.listUsers(users.Select(s => s.Name));
}
void INotificationService.ListUsers(ChatRoom room, IEnumerable<string> names)
{
Clients.Caller.showUsersInRoom(room.Name, names);
}
void INotificationService.ListAllowedUsers(ChatRoom room)
{
Clients.Caller.listAllowedUsers(room.Name, room.Private, room.AllowedUsers.Select(s => s.Name));
}
void INotificationService.LockRoom(ChatUser targetUser, ChatRoom room)
{
var userViewModel = new UserViewModel(targetUser);
// Tell everyone that the room's locked
Clients.Clients(_repository.GetAllowedClientIds(room)).lockRoom(userViewModel, room.Name, true);
Clients.AllExcept(_repository.GetAllowedClientIds(room).ToArray()).lockRoom(userViewModel, room.Name, false);
// Tell the caller the room was successfully locked
Clients.Caller.roomLocked(room.Name);
// Notify people of the change
OnRoomChanged(room);
}
void INotificationService.CloseRoom(IEnumerable<ChatUser> users, ChatRoom room)
{
// notify all members of room that it is now closed
foreach (var user in users)
{
Clients.User(user.Id).roomClosed(room.Name);
}
// notify everyone to update their lobby
OnRoomChanged(room);
}
void INotificationService.UnCloseRoom(IEnumerable<ChatUser> users, ChatRoom room)
{
// notify all members of room that it is now re-opened
foreach (var user in users)
{
Clients.User(user.Id).roomUnClosed(room.Name);
}
// notify everyone to update their lobby
OnRoomChanged(room);
}
void INotificationService.LogOut(ChatUser user, string clientId)
{
foreach (var client in user.ConnectedClients)
{
DisconnectClient(client.Id);
Clients.Client(client.Id).logOut();
}
}
void INotificationService.ShowUserInfo(ChatUser user)
{
string userId = Context.User.GetUserId();
Clients.Caller.showUserInfo(new
{
Name = user.Name,
OwnedRooms = user.OwnedRooms
.Allowed(userId)
.Where(r => !r.Closed)
.Select(r => r.Name),
Status = ((UserStatus)user.Status).ToString(),
LastActivity = user.LastActivity,
IsAfk = user.IsAfk,
AfkNote = user.AfkNote,
Note = user.Note,
Hash = user.Hash,
Rooms = user.Rooms.Allowed(userId).Select(r => r.Name)
});
}
void INotificationService.ShowHelp()
{
Clients.Caller.showCommands();
}
void INotificationService.Invite(ChatUser user, ChatUser targetUser, ChatRoom targetRoom)
{
// Send the invite message to the sendee
Clients.User(targetUser.Id).sendInvite(user.Name, targetUser.Name, targetRoom.Name);
// Send the invite notification to the sender
Clients.User(user.Id).sendInvite(user.Name, targetUser.Name, targetRoom.Name);
}
void INotificationService.NudgeUser(ChatUser user, ChatUser targetUser)
{
// Send a nudge message to the sender and the sendee
Clients.User(targetUser.Id).nudge(user.Name, targetUser.Name, null);
Clients.User(user.Id).nudge(user.Name, targetUser.Name, null);
}