public async Task <BotResponseCode> TryAddTrackToPlaylist(UpdateDto updateDto) { if (string.IsNullOrEmpty(updateDto.ParsedTrackId)) { return(BotResponseCode.NoAction); } var existingTrackInPlaylist = await _trackRepository.Get(updateDto.ParsedTrackId, updateDto.Chat.PlaylistId); // Check if the track already exists in the playlist. if (existingTrackInPlaylist != null) { string text; if (existingTrackInPlaylist.State == TrackState.RemovedByDownvotes) { text = $"This track was previously posted, but it was downvoted and removed from the {_spotifyLinkHelper.GetMarkdownLinkToPlaylist(updateDto.Chat.PlaylistId, "playlist")}."; } else { text = $"This track is already added to the {_spotifyLinkHelper.GetMarkdownLinkToPlaylist(updateDto.Chat.PlaylistId, "playlist")}!"; } await _sendMessageService.SendTextMessage(updateDto.Chat.Id, text); return(BotResponseCode.TrackAlreadyExists); } var spotifyClient = await _spotifyClientFactory.Create(updateDto.Chat.AdminUserId); // We can't continue if we can't use the spotify api. if (spotifyClient == null) { await _sendMessageService.SendTextMessage(updateDto.Chat.Id, "Spoti-bot is not authorized to add this track to the Spotify playlist."); return(BotResponseCode.NoAction); } // Get the track from the spotify api. var newTrack = await _spotifyClientService.GetTrack(spotifyClient, updateDto.ParsedTrackId); if (newTrack == null) { await _sendMessageService.SendTextMessage(updateDto.Chat.Id, $"Track not found in Spotify api :("); return(BotResponseCode.NoAction); } await AddTrack(spotifyClient, updateDto.ParsedUser, newTrack, updateDto.Chat.PlaylistId); // Reply that the message has been added successfully. await SendReplyMessage(updateDto, newTrack); // Add the track to my queue. await _spotifyClientService.AddToQueue(spotifyClient, newTrack); return(BotResponseCode.TrackAddedToPlaylist); }
/// <summary> /// When a track is added to the playlist, get a nice response text to send to the chat. /// </summary> /// <param name="message">The message that contains the added trackId.</param> /// <param name="track">The track that was added.</param> /// <returns>A text to reply to the chat with.</returns> public string GetSuccessResponseText(UpdateDto updateDto, Track track) { var successMessage = $"*{track.Name}*\n" + $"{track.FirstArtistName} · {track.AlbumName}\n\n" + $"Track added to the {_spotifyLinkHelper.GetMarkdownLinkToPlaylist(track.PlaylistId, "playlist")}!"; // TODO: inject random wrapper so this service is testable. var random = new Random(); if (!ShouldAddAwesomeResponse(random)) { return(successMessage); } var firstName = updateDto.User?.FirstName; if (string.IsNullOrEmpty(firstName)) { return(successMessage); } return(GetRandomAwesomeResponse(random, successMessage, firstName, track)); }
/// <summary> /// Validates if all the commands requirements were met. /// </summary> /// <returns>An empty string if all requirements were met, or an error message.</returns> private async Task <string> ValidateRequirements(TCommand command, UpdateDto updateDto) { // TODO: replace with fluent validation. if (command.HasAttribute <TCommand, RequiresPrivateChatAttribute>() && updateDto.ParsedChat?.Type != Chats.ChatType.Private) { return($"The {command} command is only supported in private chats."); } if (command.HasAttribute <TCommand, RequiresChatAttribute>() && updateDto.Chat == null) { return($"Spoti-bot first needs to be added to this chat by sending the {Command.Start.ToDescriptionString()} command."); } if (command.HasAttribute <TCommand, RequiresNoChatAttribute>() && updateDto.Chat != null) { var admin = await _userRepository.Get(updateDto.Chat.AdminUserId); // A chat should always have an admin. if (admin == null) { throw new ChatAdminNullException(updateDto.Chat.Id, updateDto.Chat.AdminUserId); } if (updateDto.User.Id != admin.Id) { return($"Spoti-bot is already added to this chat, {admin.FirstName} is it's admin."); } else { return($"Spoti-bot is already added to this chat, you are it's admin."); } } if (command.HasAttribute <TCommand, RequiresChatAdminAttribute>()) { if (updateDto.Chat == null) { return($"Spoti-bot first needs to be added to this chat by sending the {Command.Start.ToDescriptionString()} command."); } var admin = await _userRepository.Get(updateDto.Chat.AdminUserId); // A chat should always have an admin. if (admin == null) { throw new ChatAdminNullException(updateDto.Chat.Id, updateDto.Chat.AdminUserId); } if (updateDto.User.Id != admin.Id) { return($"Only the chat admin ({admin.FirstName}) can use this command."); } } if (command.HasAttribute <TCommand, RequiresPlaylistAttribute>() && updateDto.Playlist == null) { return($"Please set a playlist first, with the {Command.SetPlaylist.ToDescriptionString()} command."); } if (command.HasAttribute <TCommand, RequiresNoPlaylistAttribute>() && updateDto.Playlist != null) { return($"This chat already has a { _spotifyLinkHelper.GetMarkdownLinkToPlaylist(updateDto.Playlist.Id, "playlist")} set."); } if (command.HasAttribute <TCommand, RequiresQueryAttribute>() && !_commandsService.HasQuery(updateDto.ParsedTextMessage, command)) { var text = $"Please add a query after the {command.ToDescriptionString()} command."; if (command.GetType() == typeof(Command) && (Command)(object)command == Command.SetPlaylist) { text += $"\n\nFor example:\n{command.ToDescriptionString()} https://open.spotify.com/playlist/37i9dQZEVXbMDoHDwVN2tF"; } return(text); } return(string.Empty); }