public async Task <IActionResult> AddFriend(Guid userId)
        {
            try
            {
                bool areFriends = await _friendApiAccess.CheckIfAreFriends(Guid.Parse(_userId), userId);

                if (!areFriends)
                {
                    await _notificationApiAccess.AddNotification(new NotificationToAdd
                    {
                        FromWho          = Guid.Parse(_userId),
                        UserId           = userId,
                        WasSeen          = false,
                        NotificationType = NotificationType.FriendRequiest
                    });
                }
                return(RedirectToAction(nameof(FindFriends)));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong during adding friend requiest notification by user {_userId} to user {userId}");
                _logger.LogError($"Exception info: {ex.Message} {ex.Source}");
                return(RedirectToAction("Error", "Error"));
            }
        }
Ejemplo n.º 2
0
        public async Task SendPrivateNotificaion(string toUserId, string notificationType, string fromWho, string eventId, bool wasLiked = false)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(toUserId) && !string.IsNullOrWhiteSpace(fromWho))
                {
                    if (notificationType == "like")
                    {
                        if (!wasLiked)
                        {
                            await _postApiAccess.AddPostLike(fromWho, new PostLike
                            {
                                WhenAdded = DateTime.Now,
                                FromWho   = Guid.Parse(fromWho),
                                PostId    = Guid.Parse(eventId)
                            });

                            await _notificationApiAccess.AddNotification(new NotificationToAdd
                            {
                                FromWho          = Guid.Parse(fromWho),
                                UserId           = Guid.Parse(toUserId),
                                WasSeen          = false,
                                NotificationType = NotificationType.Like,
                                EventId          = Guid.Parse(eventId)
                            });
                        }
                        else
                        {
                            await _postApiAccess.DeletePostLike(eventId, fromWho, toUserId);

                            Notification notificationToDelete = await _notificationApiAccess.GetNotification(Guid.Parse(toUserId), Guid.Parse(eventId), Guid.Parse(fromWho));

                            await _notificationApiAccess.DeleteNotification(toUserId, notificationToDelete.Id.ToString());

                            return;
                        }
                    }

                    else if (notificationType == "friendRequest")
                    {
                        if (await _friendApiAccess.CheckIfAreFriends(Guid.Parse(toUserId), Guid.Parse(fromWho)))
                        {
                            return;
                        }
                        await _notificationApiAccess.AddNotification(new NotificationToAdd
                        {
                            FromWho          = Guid.Parse(fromWho),
                            UserId           = Guid.Parse(toUserId),
                            WasSeen          = false,
                            NotificationType = NotificationType.FriendRequiest
                        });
                    }
                    else if (notificationType == "comment")
                    {
                        await _notificationApiAccess.AddNotification(new NotificationToAdd
                        {
                            FromWho          = Guid.Parse(fromWho),
                            UserId           = Guid.Parse(toUserId),
                            WasSeen          = false,
                            NotificationType = NotificationType.Comment,
                            EventId          = Guid.Parse(eventId)
                        });
                    }

                    // send message if user is online
                    if (_onlineUsers.IsUserOnline(toUserId))
                    {
                        string toWhomConnectionId = _onlineUsers.GetOnlineUser(toUserId).NotificationConnectionId;
                        await Clients.Client(toWhomConnectionId).SendAsync("ReceiveNotification");
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong in NotificationHub during sending private notifications. toUserId: {toUserId}, notificationType: {notificationType}, fromWho: {fromWho}, eventId {eventId}");
                _logger.LogError($"Exception info: {ex.Message} {ex.Source}");
            }
        }