Beispiel #1
0
 public static UserModel From(User user)
 {
     return new UserModel
     {
         Id = user.Id,
         FirstName = user.Name.FirstName,
         LastName = user.Name.LastName,
         ScreenName = user.ScreenName,
         Photo = user.Photo,
         Followers = user.Followers.Select(s => new FollowerModel {Id = s.Id, FirstName = s.Name.FirstName, LastName = s.Name.LastName, ScreenName = s.ScreenName, Photo = s.Photo }).ToList(),
         Following = user.Following.Select(s => new FollowerModel { Id = s.Id, FirstName = s.Name.FirstName, LastName = s.Name.LastName, ScreenName = s.ScreenName, Photo = s.Photo }).ToList(),
         PlaylistFollowing = user.PlaylistFollowing.Where(p => p.IsFavorite)
                                 .Select(pf => new PlaylistLikeModel
                                 {
                                     Name = pf.Playlist.Name,
                                     Id = pf.Playlist.Id,
                                     IsFavorite = pf.IsFavorite,
                                     IsLiked = pf.IsLiked,
                                     Duration = pf.Playlist.Tracks.Sum(t => long.Parse(t.Duration)),
                                     Songs = pf.Playlist.Tracks.Count,
                                     Genres = pf.Playlist.Tracks.Select(x => x.Genre)
                                             .GroupBy(x => x, (key, values) => new { Name = key, Count = values.Count() })
                                             .OrderByDescending(x => x.Count)
                                             .Where(x => x.Name.ToLower() != "other" && x.Name.ToLower() != "undefined")
                                             .Select(x => x.Name)
                                             .Take(5)
                                             .ToList(),
                                 }).ToList()
     };
 }
        public Notification CreateNotification(Notifications type, User user, User recentlyUser, Playlist recentlyPlaylist)
        {
            var notification = new Notification
            {
                NotificationType = type,
                User = user,
                RecentlyUser = recentlyUser,
                RecentlyPlaylist = recentlyPlaylist,
                NotificationDate = DateTime.Now
            };

            var notificationDto = new NotificationDto();
            Mapper.Map(notification, notificationDto); 
            notificationDto.Message = GetMessage(notification);

            List<long> list = user.Followers.Select(s => s.Id).ToList();

            _notificationHub.SendNotification(user.Id, notificationDto, list);

            return notification;
        }
Beispiel #3
0
        public void AddSharing()
        {
            using (var unitOfWork = _kernel.Get<IUnitOfWork>())
            {
                var playlistRepo = unitOfWork.GetRepository<Playlist>();
                var userRepo = unitOfWork.GetRepository<User>();

                var sysUser = new User
                {
                    Name = new Name{FirstName = "Admin", LastName = "Admin"}
                };
                userRepo.AddItem(sysUser);

                var playlist = playlistRepo.GetAll().First();

                var fakePlaylist = new Playlist
                {
                    Creator = sysUser, Name = "Fake", Accessibilty = Accessibilty.Public
                };

                foreach (var track in playlist.Tracks)
                {
                    fakePlaylist.Tracks.Add(track);
                }

                playlistRepo.AddItem(fakePlaylist);

                var sharedPlaylist = new SharedPlaylist
                {
                    Guid = Guid.NewGuid().ToString(), Playlist = fakePlaylist
                };

                var spRepo = unitOfWork.GetRepository<SharedPlaylist>();

                spRepo.AddItem(sharedPlaylist);

                unitOfWork.Commit();
            }
        }
Beispiel #4
0
        public bool SaveOrUpdateUserData(User user, UserCredential userCredential, AzimuthIdentity loggedIdentity)
        {
            using (var unitOfWork = _unitOfWorkFactory.NewUnitOfWork())
            {
                var _userSNRepository = unitOfWork.GetTypedRepository<IUserSocialNetworkRepository>();
                var _snRepository = unitOfWork.GetTypedRepository<ISocialNetworkRepository>();
                var _playlistRepository = unitOfWork.GetRepository<Playlist>();
                try
                {
                    User loggedUser = null;
                    if (loggedIdentity != null)
                    {
                        loggedUser = unitOfWork.UserRepository.GetOne(x => x.Email == loggedIdentity.UserCredential.Email);
                    }
                    var userSn = _userSNRepository.GetByThirdPartyId(userCredential.SocialNetworkId);
                    if (userSn != null)
                    {
                        if (loggedUser != null)
                        {
                            user.Id = loggedUser.Id;
                            // If we login again with the same social network, skip updating (merging accounts)
                            if (loggedUser.Id != userSn.User.Id)
                            {
                                var playlistLikerRepository =
                                        unitOfWork.GetTypedRepository<PlaylistLikerRepository>();
                                var notificationRepository =
                                    unitOfWork.GetTypedRepository<INotificationRepository>();
                                var userRepository = unitOfWork.GetTypedRepository<IUserRepository>();

                                var allAccounts =
                                    _userSNRepository.Get(account => account.User.Id == userSn.User.Id).ToList();
                                var userToDelete = userSn.User;
                                foreach (var userSocialNetwork in allAccounts)
                                {
                                    // Getting unlogged user's account playlists
                                    var userPlaylists = _playlistRepository.Get(s => s.Creator.Id == userSocialNetwork.User.Id).ToList();
                                    // Taking playlists which he liked or favourited


                                    var notUserPlaylists = playlistLikerRepository.Get(s => s.Liker.Id == userSocialNetwork.User.Id).ToList();
                                    if (notUserPlaylists.Any())
                                        userPlaylists.AddRange(notUserPlaylists.Select(s => s.Playlist));

                                    // Find if user liked/favourited his another account's playlists 
                                    var recordsTodelete =
                                        userPlaylists.SelectMany(s => s.PlaylistLikes).Where(
                                            item => item.Playlist.Creator.Id == loggedUser.Id).ToList();
                                    // Getting user's unauthorized account activity
                                    var notifications =
                                        notificationRepository.Get(item => item.User.Id == userSocialNetwork.User.Id).ToList();

                                    foreach (var playlistLike in recordsTodelete)
                                    {
                                        // Notifications like/unlike and favourite/unfavourited of his another account playlists should be deleted
                                        var notificationsToDelete = notifications.Where(
                                            s =>
                                                s.RecentlyPlaylist == playlistLike.Playlist &&
                                                s.NotificationType == Notifications.FavoritedPlaylist ||
                                                s.NotificationType == Notifications.LikedPlaylist ||
                                                s.NotificationType == Notifications.UnfavoritedPlaylist ||
                                                s.NotificationType == Notifications.UnlikedPlaylist).ToList();

                                        // Get all records of unauthorizedd user's account from likes table
                                        var likeRecord = playlistLikerRepository.GetOne(item => item.Id == playlistLike.Id);
                                        // Kill connection between notifications which would be deleted with playlists
                                        foreach (var notification in notificationsToDelete)
                                        {
                                            likeRecord.Playlist.Notifications.Remove(notification);
                                        }

                                        // Delete notifications
                                        notifications.Where(
                                            s =>
                                                s.RecentlyPlaylist == playlistLike.Playlist &&
                                                (s.NotificationType == Notifications.FavoritedPlaylist ||
                                                s.NotificationType == Notifications.LikedPlaylist ||
                                                s.NotificationType == Notifications.UnfavoritedPlaylist ||
                                                s.NotificationType == Notifications.UnlikedPlaylist)).ForEach(item => notificationRepository.DeleteItem(item));

                                        // Kill connection playlist -> like record of playlist (like/unlike && favourite/unfavouriteown playlists of another account)
                                        likeRecord.Playlist.PlaylistLikes.Remove(playlistLike);
                                        // Kill connection user -> like record of user (like/unlike && favourite/unfavouriteowne own playlists of another account)
                                        likeRecord.Liker.PlaylistFollowing.Remove(playlistLike);
                                        // Delete like/unlike && favourite/unfavouriteowne  playlists of another users's account 
                                        playlistLikerRepository.DeleteItem(playlistLike);
                                    }
                                    // Normal user's activity should change user link
                                    userPlaylists.SelectMany(list => list.PlaylistLikes).ForEach(item => item.Liker = loggedUser);
                                    // Added normal user's playlists should also change user link
                                    userPlaylists.ForEach(item => item.Creator = loggedUser);
                                    // Other notifications have to change their user
                                    notifications.Where(
                                        s =>
                                            s.User.Id == userSocialNetwork.User.Id).ForEach(item => item.User = loggedUser);

                                    userSocialNetwork.User = loggedUser;
                                    _userSNRepository.ChangeUserId(userSocialNetwork);

                                }
                                userRepository.DeleteItem(userToDelete);
                            }
                            else
                            {
                                userSn.AccessToken = userCredential.AccessToken;
                            }
                        }
                        else
                        {
                            // If user exists in database check his data fields for updating
                            if (user.ToString() != userSn.User.ToString())
                            {
                                userSn.User.Name = new Name
                                {
                                    FirstName = user.Name.FirstName,
                                    LastName = user.Name.LastName
                                };
                                userSn.User.ScreenName = user.ScreenName;
                                userSn.User.Gender = user.Gender;
                                userSn.User.Email = user.Email;
                                userSn.User.Birthday = user.Birthday;
                                userSn.User.Location = new Location
                                {
                                    Country = user.Location.Country,
                                    City = user.Location.City
                                };
                                userSn.User.Timezone = user.Timezone;
                                userSn.User.Photo = userSn.Photo = user.Photo ?? String.Empty;
                                userSn.UserName = (user.Name.FirstName ??
                                    String.Empty) + ((user.Name.LastName != null) ? (" " + user.Name.LastName) : String.Empty);
                            }
                            user.Id = userSn.User.Id;
                        }
                    }
                    else
                    {
                        var currentSN = _snRepository.GetByName(userCredential.SocialNetworkName);

                        if (loggedIdentity == null)
                        {
                            unitOfWork.UserRepository.AddItem(user);
                        }
                        _userSNRepository.AddItem(new UserSocialNetwork
                        {
                            User = loggedUser ?? user, 
                            SocialNetwork = currentSN,
                            ThirdPartId = userCredential.SocialNetworkId,
                            AccessToken = userCredential.AccessToken,
                            TokenExpires = userCredential.AccessTokenExpiresIn,
                            Photo = user.Photo,
                            UserName = (user.Name.FirstName ??
                                    String.Empty) + ((user.Name.LastName != null) ? (" " + user.Name.LastName) : String.Empty)
                        });
                    }

                    unitOfWork.Commit();
                }
                catch (Exception)
                {
                    unitOfWork.Rollback();
                    return false;
                }
            }
            return true;
        }
Beispiel #5
0
        public void SignIn(AzimuthIdentity identity, User userInfo)
        {
            identity.AddClaim(new Claim(ClaimTypes.Name, userInfo.Name.FirstName + " " + userInfo.Name.LastName));
            identity.AddClaim(new Claim(AzimuthClaims.PHOTO_BIG, userInfo.Photo));
            identity.AddClaim(new Claim(AzimuthClaims.ID, userInfo.Id.ToString()));
            identity.AddClaim(new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider",
                identity.UserCredential.SocialNetworkName, Rights.PossessProperty));

            AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = true }, identity);
        }