コード例 #1
0
ファイル: PartyHub.cs プロジェクト: hibroseph/SpotSync
        public async Task ConnectToParty(string partyCode)
        {
            PartyGoer partier = await _partyGoerService.GetCurrentPartyGoerAsync();

            if (!await _partyService.IsUserPartyingAsync(partier) && !await _partyService.IsUserHostingAPartyAsync(partier))
            {
                await _partyService.JoinPartyAsync(new Domain.DTO.PartyCodeDTO {
                    PartyCode = partyCode
                }, partier);

                await Groups.AddToGroupAsync(Context.ConnectionId, partyCode);

                await Clients.Group(partyCode).SendAsync("UpdateParty", $"{Context.UserIdentifier} has joined the party {partyCode}");
            }

            // Add the partier to real-time connection group
            await Groups.AddToGroupAsync(Context.ConnectionId, partyCode);

            await Clients.GroupExcept(partyCode, new List <string> {
                Context.ConnectionId
            }).SendAsync("NewListener", Context.UserIdentifier);

            Party party = await _partyService.GetPartyWithAttendeeAsync(partier);

            // Update the view of the partier to the current playlist
            await Clients.Client(Context.ConnectionId).SendAsync("InitialPartyLoad",
                                                                 new
            {
                Song     = party.GetCurrentSong(),
                Position = party.GetCurrentPositionInSong()
            },
                                                                 party.GetHistory(),
                                                                 party.GetQueue(),
                                                                 new
            {
                PartyCode = party.GetPartyCode(),
                Listeners = ConvertToListenerModel(party.GetListeners()),
                Host      = party.GetHost().GetId()
            }
                                                                 );;

            // check for explicit music
            if (!partier.CanListenToExplicitSongs() && party.HasExplicitTracks())
            {
                await Clients.Client(Context.ConnectionId).SendAsync("ExplicitSong", "You have filtering explicit music turned on in Spotify and there are explicit songs in the queue. We will not play the explicit song for you but continue playback when a non explicit song comes on.");
            }

            await Clients.Client(Context.ConnectionId).SendAsync("InitializeWebPlayer", await _partyGoerService.GetPartyGoerAccessTokenAsync(partier));

            // make sure that the users spotify is connected
            if (string.IsNullOrEmpty(await _spotifyHttpClient.GetUsersActiveDeviceAsync(partier.GetId())))
            {
                await Clients.Client(Context.ConnectionId).SendAsync("ConnectSpotify", "");
            }

            await _logService.LogUserActivityAsync(partier, $"Joined real time collobration in party with code {partyCode}");

            return;
        }