public async Task <IActionResult> RejectFriendRequiest(Guid notificationId, Guid userId)
        {
            try
            {
                if (notificationId == Guid.Empty || userId == Guid.Empty)
                {
                    return(NotFound());
                }

                await _notificationApiAccess.DeleteNotification(userId.ToString(), notificationId.ToString());

                return(RedirectToAction("Notifications", "Profile"));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong during removing friend requiest by user {_userId} to user {userId}. Notification id: {notificationId}");
                _logger.LogError($"Exception info: {ex.Message} {ex.Source}");
                return(RedirectToAction("Error", "Error"));
            }
        }
示例#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}");
            }
        }