예제 #1
0
        public async Task ReadMessage(Guid messageId, Guid contactUserId)
        {
            var identity             = _claimsService.GetUserClaims();
            var contactConnectionIds = await _cacheService.Get <List <string> >(CachingHelpers.BuildKey("Chat", contactUserId)) ?? new List <string>();

            var userConnectionIds = await _cacheService.Get <List <string> >(CachingHelpers.BuildKey("Notification", identity.Id)) ?? new List <string>();

            var messageRes = await _messageService.ReadMessage(messageId, identity.Id);

            var unreadMessages = await _messageService.GetUnreadMessages(identity.Id);

            await _pubSub.Publish(Channels.PrivateMessageChannel, new PrivateMessageContract
            {
                ActionType    = PrivateMessageActionType.SeenMessage,
                ConnectionIds = contactConnectionIds,
                SeenMessage   = messageRes
            });

            await _pubSub.Publish(Channels.NotififcationMessageChannel, new NewNotificationMessageContract
            {
                ActionType          = NotificationActionType.UnreadMessage,
                ConnectionIds       = userConnectionIds,
                TotalUnreadMessages = unreadMessages
            });
        }
예제 #2
0
        public override async Task OnConnectedAsync()
        {
            var user            = _claimsService.GetUserClaims();
            var userConnections = await _cacheService.Get <List <string> >(CachingHelpers.BuildKey("Notification", user.Id)) ?? new List <string>();

            userConnections.Add(Context.ConnectionId);
            await _cacheService.Set(CachingHelpers.BuildKey("Notification", user.Id), userConnections);

            var numOfNotifications = await _notificationService.GetUserNumberOfNotifications(user.Id);

            var unreadMessages = await _messageService.GetUnreadMessages(user.Id);

            await _pubSub.Publish(Channels.NotififcationMessageChannel, new NewNotificationMessageContract
            {
                ActionType          = NotificationActionType.UnreadMessage,
                ConnectionIds       = userConnections,
                TotalUnreadMessages = unreadMessages
            });

            await _pubSub.Publish(Channels.NotififcationMessageChannel, new NewNotificationMessageContract
            {
                ActionType          = NotificationActionType.NewNoti,
                ConnectionIds       = userConnections,
                TotalUnreadMessages = numOfNotifications
            });

            await base.OnConnectedAsync();
        }
예제 #3
0
        public override async Task OnDisconnectedAsync(Exception exception)
        {
            var user          = _claimsService.GetUserClaims();
            var connectionIds = await _cacheService.Get <List <string> >(CachingHelpers.BuildKey("Notification", user.Id)) ?? new List <string>();

            var ids = connectionIds.Where(x => x != Context.ConnectionId).ToList();
            await _cacheService.Set(CachingHelpers.BuildKey("Notification", user.Id), ids);

            await base.OnDisconnectedAsync(exception);
        }
예제 #4
0
        public override async Task OnConnectedAsync()
        {
            var user = _claimsService.GetUserClaims();
            var key  = CachingHelpers.BuildKey("Chat", user.Id);

            var userConnections = await _cacheService.Get <List <string> >(key) ?? new List <string>();

            userConnections.Add(Context.ConnectionId);
            await _cacheService.Set(key, userConnections);

            await base.OnConnectedAsync();
        }
예제 #5
0
        public async Task MessageStopTyping(Guid conversationId, Guid contactUserId)
        {
            var identity             = _claimsService.GetUserClaims();
            var contactConnectionIds = await _cacheService.Get <List <string> >(CachingHelpers.BuildKey("Chat", contactUserId)) ?? new List <string>();

            await _pubSub.Publish(Channels.PrivateMessageChannel, new PrivateMessageContract
            {
                ActionType           = PrivateMessageActionType.StopTyping,
                ConnectionIds        = contactConnectionIds,
                TypingOnConversation = new TypingOnConversationContract
                {
                    ConversationId = conversationId,
                    ContactUserId  = identity.Id
                }
            });
        }
예제 #6
0
        public async Task SendMessage(string message, string fileUrl, Guid contactUserId, Guid conversationId)
        {
            var identity = _claimsService.GetUserClaims();
            var payload  = new SendMessageRequestContract
            {
                SenderId       = identity.Id,
                ContactUserId  = contactUserId,
                Message        = message,
                MessageType    = string.IsNullOrEmpty(fileUrl) ? 0 : 1,
                AttachmentUrl  = fileUrl,
                ConversationId = conversationId
            };

            var messageId = await _messageService.CreateMessageAsync(payload);

            payload.MessageId = messageId;
            var userConnectionIds = await _cacheService.Get <List <string> >(CachingHelpers.BuildKey("Chat", identity.Id)) ?? new List <string>();

            var contactConnectionIds = await _cacheService.Get <List <string> >(CachingHelpers.BuildKey("Chat", contactUserId)) ?? new List <string>();

            var connectionIds = userConnectionIds.Union(contactConnectionIds).ToList();

            await _pubSub.Publish(Channels.PrivateMessageChannel,
                                  new
                                  PrivateMessageContract
            {
                ConnectionIds = connectionIds,
                NewMessage    = payload
            });

            var unreadMessages = await _messageService.GetUnreadMessages(contactUserId);

            await _pubSub.Publish(Channels.NotififcationMessageChannel, new NewNotificationMessageContract
            {
                ActionType          = NotificationActionType.UnreadMessage,
                ConnectionIds       = await _cacheService.Get <List <string> >(CachingHelpers.BuildKey("Notification", contactUserId)) ?? new List <string>(),
                TotalUnreadMessages = unreadMessages
            });
        }