示例#1
0
        public async Task TryHosting(string roomId, string connId, ProfileReadDto spotifyProfile)
        {
            if (!_inMemoryDb.RoomIsHosted(roomId))
            {
                await _inMemoryDb.MakeThisRoomHosted(roomId, spotifyProfile);
            }
            await Task.WhenAll(new Task[]
            {
                _inMemoryDb.RegisterNewConnection(connId, roomId, spotifyProfile.SpotifyId),
                _roomManager.AddToRoomAsHostAsync(roomId, connId),
                _roomManager.NotifyThisClientOnSuccessHosting(connId)
            });

            var roomInformation = await _spotiRepository.GetRoomInformation(roomId);

            if (roomInformation == null)
            {
                await _spotiRepository.CreateRoomForAccount(spotifyProfile.SpotifyId);

                roomInformation = await _spotiRepository.GetRoomInformation(roomId);
            }

            await _spotiRepository.ToggleRoomActive(roomInformation.OwnerId, true);

            var uniqueSpotifyProfileIds = await _inMemoryDb.GetAllUniqueSpotifyProfilesOfRoom(roomId);

            await Task.WhenAll(new Task[]
            {
                _roomManager.SendInformationAboutProfilesOfRoomToConnection(connId, uniqueSpotifyProfileIds),
                _roomManager.SendInformationAboutRoomToHost(connId, roomInformation)
            });
        }
示例#2
0
        public async Task <string> SaveNewSongId(string accountId, string songId, ProfileReadDto spotifyProfile)
        {
            var userAccount = await _spotiRepository.FindTrackedAccount(accountId);

            if (userAccount != null)
            {
                if (userAccount.SongIdList.Contains(songId))
                {
                    return("song_exists");
                }

                await _spotiRepository.AddSongToTrackedAccount(accountId, songId);

                return("new_song");
            }

            if (userAccount == null)
            {
                await _spotiRepository.CreateTrackedAccount(accountId, songId);

                return("new_id");
            }

            return("unexpected_error");
        }
示例#3
0
        public async Task CreateAccount(ProfileReadDto userProfile)
        {
            Account account = new Account
            {
                SpotifyId = userProfile.SpotifyId,
                Nickname  = userProfile.DisplayName,
                Friends   = new BsonArray()
            };

            foreach (var image in userProfile.Images)
            {
                account.ProfileImages.Add(image);
            }

            await AddAccountAsync(account);
        }
示例#4
0
        public async Task <ProfileReadDto> GetProfileFromTokenSpotify(string accessToken)
        {
            AppendAuthHeader(accessToken);

            using var response = await _client.GetAsync("https://api.spotify.com/v1/me");

            if (response.IsSuccessStatusCode)
            {
                var responseContent = await response.Content.ReadAsStringAsync();

                ProfileReadDto userProfile = (ProfileReadDto)JsonSerializer.Deserialize(responseContent, typeof(ProfileReadDto));

                return(userProfile);
            }

            return(null);
        }
        public static async Task <ProfileReadDto> GetProfileFromTokenSpotify(string accessToken)
        {
            using var httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

            using var response = await httpClient.GetAsync("https://api.spotify.com/v1/me");

            if (response.IsSuccessStatusCode)
            {
                var responseContent = await response.Content.ReadAsStringAsync();

                ProfileReadDto userProfile = (ProfileReadDto)JsonSerializer.Deserialize(responseContent, typeof(ProfileReadDto));

                return(userProfile);
            }

            return(null);
        }
示例#6
0
        public void ParseRoomConnectionsDataShouldSucceed()
        {
            //Arrange
            ProfileReadDto personOne = new ProfileReadDto()
            {
                DisplayName = "Bob",
                SpotifyId   = "spotifyid11111",
                Images      = new List <ImageObject>()
                {
                    new ImageObject()
                    {
                        Width    = 1000,
                        Height   = 300,
                        ImageURL = "person1_im_url"
                    },
                    new ImageObject()
                    {
                        Width    = 500,
                        Height   = 300,
                        ImageURL = "person1_im_url2"
                    }
                }
            };

            ProfileReadDto hostPerson = new ProfileReadDto()
            {
                DisplayName = "Gob",
                SpotifyId   = "spotifyid22222",
                Images      = new List <ImageObject>()
                {
                    new ImageObject()
                    {
                        Width    = 1000,
                        Height   = 300,
                        ImageURL = "person2_im_url"
                    },
                    new ImageObject()
                    {
                        Width    = 500,
                        Height   = 300,
                        ImageURL = "person2_im_url2"
                    }
                }
            };

            personOne.ConnectionId  = "connidperson1";
            hostPerson.ConnectionId = "hostpersonconnid";

            hostPerson.IsHost = true;

            var rawFile = File.ReadAllText("D:/Projects/SpotifyDiscovery/SpotifyDiscoveryBackend/SpotifyDiscovery/Tests/XUnitTests/Resources/SampleData/redis_room_connections_sample.json");
            //var rawFile = File.ReadAllText(AppContext.BaseDirectory + "/Resources/SampleData/redis_room_connections_sample.json");

            //Act
            var receivedInformation = JsonSerializer.Deserialize <ConnectionDetailsDto>(rawFile);
            var actualName          = "";

            foreach (var profile in receivedInformation.ConnectionDetail)
            {
                if (profile.IsHost == true)
                {
                    actualName = profile.DisplayName;
                    break;
                }
            }

            //Assert
            Assert.Equal(hostPerson.DisplayName, actualName);
        }
示例#7
0
        public async Task <RoomJoinStatuses> TryJoinRoom(string roomId, string connId, ProfileReadDto spotifyProfile, string password)
        {
            //if (!_inMemoryDb.RoomIsHosted(roomId))
            //{
            //    return RoomJoinStatuses.ROOM_NOT_EXISTS;
            //}

            var roomInfoTask         = _spotiRepository.GetRoomInformation(roomId);
            var ownerAccountInfoTask = _spotiRepository.FindAccountBySpotifyIdAsync(roomId);

            await Task.WhenAll(new Task[] { roomInfoTask, ownerAccountInfoTask });

            if (roomInfoTask.Result == null)
            {
                return(RoomJoinStatuses.ROOM_NOT_EXISTS);
            }

            if (roomInfoTask.Result.IsFriendsOnly == true)
            {
                if (IsInFriendlistOfRoomOwner())
                {
                    await ConnectUserToRequestedRoom(connId, roomId, spotifyProfile.SpotifyId);

                    return(RoomJoinStatuses.SUCCESS);
                }
                else
                {
                    return(RoomJoinStatuses.NOT_IN_FRIENDLIST);
                }
            }
            else if (roomInfoTask.Result.Password != null)
            {
                if (roomInfoTask.Result.Password == "")
                {
                    await ConnectUserToRequestedRoom(connId, roomId, spotifyProfile.SpotifyId);

                    return(RoomJoinStatuses.SUCCESS);
                }
                else
                {
                    var personalRequirement = await GetRoomSettingsRequirements(roomId, spotifyProfile.SpotifyId);

                    if (personalRequirement.PasswordRequired == false)
                    {
                        await ConnectUserToRequestedRoom(connId, roomId, spotifyProfile.SpotifyId);

                        return(RoomJoinStatuses.SUCCESS);
                    }

                    if (personalRequirement.PasswordRequired == true && roomInfoTask.Result.Password == password)
                    {
                        await _spotiRepository.MakeUserAuthorizedToRoom(roomId, spotifyProfile.SpotifyId);
                        await ConnectUserToRequestedRoom(connId, roomId, spotifyProfile.SpotifyId);

                        return(RoomJoinStatuses.SUCCESS);
                    }

                    return(RoomJoinStatuses.INCORRECT_PASSWORD);
                }
            }

            return(RoomJoinStatuses.ROOM_NOT_EXISTS);

            bool IsInFriendlistOfRoomOwner()
            {
                if (ownerAccountInfoTask.Result.Friends != null && ownerAccountInfoTask.Result.Friends.Contains(spotifyProfile.SpotifyId))
                {
                    return(true);
                }

                return(false);
            }

            async Task <RoomJoinStatuses> ConnectUserToRequestedRoom(string connId, string targetRoomId, string spotifyIdOfConnection)
            {
                await Task.WhenAll(new Task[]
                {
                    _inMemoryDb.RegisterNewConnection(connId, targetRoomId, spotifyIdOfConnection),
                    _roomManager.AddToRoomAsListenerAsync(targetRoomId, connId),
                    _roomManager.NotifyOthersAboutConnectingAsListener(spotifyIdOfConnection, connId, targetRoomId),
                    _roomManager.NotifyThisClientOnSuccessRoomJoining(connId)
                });

                var uniqueSpotifyProfileIds = await _inMemoryDb.GetAllUniqueSpotifyProfilesOfRoom(targetRoomId);

                await _roomManager.SendInformationAboutProfilesOfRoomToConnection(connId, uniqueSpotifyProfileIds);

                return(RoomJoinStatuses.SUCCESS);
            }
        }
示例#8
0
        private async Task <(string Status, string Payload)> HandleProfileCreationIfNotExists(ProfileReadDto userProfile)
        {
            var account = await _spotiRepository.FindAccountBySpotifyIdAsync(userProfile.SpotifyId);

            if (account == null)
            {
                await _spotiRepository.CreateAccount(userProfile);

                var playlistId = await CreatePlaylist(AccessToken, userProfile.SpotifyId);

                if (playlistId == null)
                {
                    return(Status : "Error", Payload : "couldn't create playlist");
                }
                var playlistTask = _spotiRepository.ConnectPlaylistToAccountAsync(userProfile.SpotifyId, playlistId);
                Task.WaitAll(playlistTask);

                return(Status : "Success", Payload : playlistId);
            }

            return(Status : "Success", Payload : account.FreshPlaylistId);
        }
示例#9
0
        public async Task <string> FindUserGetDatabasePlaylist(ProfileReadDto spotifyProfile)
        {
            var(Status, Payload) = await HandleProfileCreationIfNotExists(spotifyProfile);

            return(Payload);
        }
示例#10
0
        public async Task <bool> SaveToPlaylist(AddSongToPlaylistDto addSongToPlaylistDto, ProfileReadDto spotifyProfile)
        {
            var uri         = $"spotify:track:{addSongToPlaylistDto.SongId}";
            var accessToken = addSongToPlaylistDto.AccessToken;

            AppendAuthHeader(accessToken);

            var isPlaylistFound = await FindSpotifyPlaylist(accessToken, addSongToPlaylistDto.PlaylistId);

            if (!isPlaylistFound)
            {
                addSongToPlaylistDto.PlaylistId = await CreatePlaylist(addSongToPlaylistDto.AccessToken, spotifyProfile.SpotifyId);

                if (addSongToPlaylistDto.PlaylistId == null)
                {
                    return(false);
                }

                await _spotiRepository.ConnectPlaylistToAccountAsync(spotifyProfile.SpotifyId, addSongToPlaylistDto.PlaylistId);
            }

            RemoveTrackFromSpotifyPlaylist(addSongToPlaylistDto.PlaylistId, uri, accessToken);

            using var response = await _client.PostAsync($"https://api.spotify.com/v1/playlists/{addSongToPlaylistDto.PlaylistId}/tracks?uris={uri}", null);

            if (response.IsSuccessStatusCode)
            {
                //Track was created in the playlist
                return(true);
            }
            //if status code 403 - error or playlist size is 10 000
            return(false);
        }