fix: formatting, refs NOISSUE
This commit is contained in:
parent
2e876ad628
commit
5b72e25636
@ -17,5 +17,5 @@ public class DataContext : DbContext
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
=> optionsBuilder.UseNpgsql($"Host={AppConfiguration.Instance.DatabaseUri}:{AppConfiguration.Instance.DatabasePort};"
|
||||
+ $"Username={AppConfiguration.Instance.DatabaseUser};Password={AppConfiguration.Instance.DatabasePW};"
|
||||
+ $"Database={AppConfiguration.Instance.DatabaseName}");
|
||||
+ $"Database={AppConfiguration.Instance.DatabaseName}");
|
||||
}
|
@ -17,7 +17,7 @@ public class User
|
||||
return (string.IsNullOrEmpty(NickName) ? Name : NickName).ToString();
|
||||
}
|
||||
}
|
||||
public List<Song> LikedSongs { get; set; }
|
||||
public required List<Song> LikedSongs { get; set; }
|
||||
|
||||
public string? SpotifyAuthAccessToken { get; set; }
|
||||
public int? SpotifyAuthExpiresAfterSeconds { get; set; }
|
||||
|
@ -21,21 +21,22 @@ public class IndexModel : PageModel
|
||||
|
||||
public User CurrentUser
|
||||
{
|
||||
get {
|
||||
if(_currentUser == null)
|
||||
get
|
||||
{
|
||||
if (_currentUser == null)
|
||||
{
|
||||
var userName = this.User.Identity.Name;
|
||||
using(var dci = DataContext.Instance)
|
||||
using (var dci = DataContext.Instance)
|
||||
{
|
||||
_currentUser = dci.Users.Include(u => u.LikedSongs).Where(u => u.LdapUserName == userName).SingleOrDefault();
|
||||
}
|
||||
}
|
||||
|
||||
return _currentUser;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public bool HasUserLikedThisSong(Song song)
|
||||
{
|
||||
return CurrentUser.LikedSongs.Where(s => s.SongId == song.SongId).FirstOrDefault() != default(Song);
|
||||
@ -68,7 +69,7 @@ public class IndexModel : PageModel
|
||||
|
||||
public async Task<IActionResult> OnPostUnlikeSong(int? songId)
|
||||
{
|
||||
using(var dci = DataContext.Instance)
|
||||
using (var dci = DataContext.Instance)
|
||||
{
|
||||
var user = dci.Users.Include(u => u.LikedSongs).Where(u => u.UserId == CurrentUser.UserId).SingleOrDefault();
|
||||
var songToRemove = user.LikedSongs.Where(s => s.SongId == songId).SingleOrDefault();
|
||||
|
@ -79,19 +79,20 @@ public class SongSubmissionModel : PageModel
|
||||
|
||||
public User CurrentUser
|
||||
{
|
||||
get {
|
||||
if(_currentUser == null)
|
||||
get
|
||||
{
|
||||
if (_currentUser == null)
|
||||
{
|
||||
var userName = this.User.Identity.Name;
|
||||
using(var dci = DataContext.Instance)
|
||||
using (var dci = DataContext.Instance)
|
||||
{
|
||||
_currentUser = dci.Users.Include(u => u.LikedSongs).Where(u => u.LdapUserName == userName).SingleOrDefault();
|
||||
}
|
||||
}
|
||||
|
||||
return _currentUser;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public List<SpotifyAPI.Web.FullTrack> SpotifySuggestions
|
||||
@ -262,7 +263,7 @@ public class SongSubmissionModel : PageModel
|
||||
|
||||
public async Task<IActionResult> OnPostUnlikeSong(int? songId)
|
||||
{
|
||||
using(var dci = DataContext.Instance)
|
||||
using (var dci = DataContext.Instance)
|
||||
{
|
||||
var user = dci.Users.Include(u => u.LikedSongs).Where(u => u.UserId == CurrentUser.UserId).SingleOrDefault();
|
||||
var songToRemove = user.LikedSongs.Where(s => s.SongId == songId).SingleOrDefault();
|
||||
|
@ -60,10 +60,10 @@ public class UserModel : PageModel
|
||||
var loginRequest = new LoginRequest(
|
||||
new Uri(spotifyClient.GetLoginRedirectUri()),
|
||||
AppConfiguration.Instance.SpotifyClientId,
|
||||
LoginRequest.ResponseType.Code)
|
||||
{
|
||||
Scope = new[] { Scopes.PlaylistReadPrivate, Scopes.PlaylistReadCollaborative, Scopes.PlaylistModifyPrivate, Scopes.PlaylistModifyPublic, Scopes.UgcImageUpload }
|
||||
};
|
||||
LoginRequest.ResponseType.Code)
|
||||
{
|
||||
Scope = new[] { Scopes.PlaylistReadPrivate, Scopes.PlaylistReadCollaborative, Scopes.PlaylistModifyPrivate, Scopes.PlaylistModifyPublic, Scopes.UgcImageUpload }
|
||||
};
|
||||
var redirectUri = loginRequest.ToUri().ToString() + $"&finalRedirect={HttpUtility.UrlEncode($"{Request.Scheme}://{Request.Host}:{Request.Host.Port ?? 80}{Request.Path}")}";
|
||||
|
||||
return this.Redirect(redirectUri);
|
||||
|
@ -291,7 +291,8 @@ app.MapControllerRoute(
|
||||
name: "logout",
|
||||
pattern: "{controller=Auth}/{action=Logout}"
|
||||
);
|
||||
app.MapGet("SpotifyLogin", async (HttpRequest request, HttpResponse response) => {
|
||||
app.MapGet("SpotifyLogin", async (HttpRequest request, HttpResponse response) =>
|
||||
{
|
||||
var spotifyClient = app.Services.GetService<SpotifyApiClient>();
|
||||
var code = request.Query["code"];
|
||||
var oAuthResponse = await new OAuthClient().RequestToken(
|
||||
|
@ -15,11 +15,13 @@ public class Base64UrlImageBuilder
|
||||
|
||||
private string _fileContents = string.Empty;
|
||||
|
||||
public string FileContents {
|
||||
get {
|
||||
public string FileContents
|
||||
{
|
||||
get
|
||||
{
|
||||
return _fileContents;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
|
@ -87,31 +87,31 @@ public class NavidromeValidator : SongValidatorBase
|
||||
public class NavidromeShareInfoData
|
||||
{
|
||||
[JsonPropertyName("id")]
|
||||
public string Id { get; set; }
|
||||
public required string Id { get; set; }
|
||||
|
||||
[JsonPropertyName("description")]
|
||||
public string Description { get; set; }
|
||||
public required string Description { get; set; }
|
||||
|
||||
[JsonPropertyName("downloadable")]
|
||||
public bool Downloadable { get; set; }
|
||||
|
||||
[JsonPropertyName("tracks")]
|
||||
public List<NavidromeTrackInfoData> Tracks { get; set; }
|
||||
public required List<NavidromeTrackInfoData> Tracks { get; set; }
|
||||
}
|
||||
|
||||
public class NavidromeTrackInfoData
|
||||
{
|
||||
[JsonPropertyName("id")]
|
||||
public string Id { get; set; }
|
||||
public required string Id { get; set; }
|
||||
|
||||
[JsonPropertyName("title")]
|
||||
public string Title { get; set; }
|
||||
public required string Title { get; set; }
|
||||
|
||||
[JsonPropertyName("artist")]
|
||||
public string Artist { get; set; }
|
||||
public required string Artist { get; set; }
|
||||
|
||||
[JsonPropertyName("album")]
|
||||
public string Album { get; set; }
|
||||
public required string Album { get; set; }
|
||||
|
||||
[JsonPropertyName("updatedAt")]
|
||||
public DateTime UpdatedAt { get; set; }
|
||||
|
@ -43,7 +43,7 @@ public class PlayListSynchronizer
|
||||
|
||||
public async Task SynchronizePlaylistsAsync(IList<SmartPlaylistDefinition> playlists)
|
||||
{
|
||||
foreach(var playlist in playlists)
|
||||
foreach (var playlist in playlists)
|
||||
{
|
||||
await SynchronizePlaylistAsync(playlist);
|
||||
}
|
||||
@ -51,7 +51,7 @@ public class PlayListSynchronizer
|
||||
|
||||
public async Task SynchronizeUserPlaylistsAsync(User user)
|
||||
{
|
||||
using(var dci = DataContext.Instance)
|
||||
using (var dci = DataContext.Instance)
|
||||
{
|
||||
var userPlayLists = dci.SmartPlaylistDefinitions
|
||||
.Include(pl => pl.ExplicitlyIncludedSongs)
|
||||
|
@ -5,7 +5,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
public class SpotifyApiClient
|
||||
{
|
||||
private SpotifyClient _spotifyClient;
|
||||
private SpotifyClient _userAuthorizedSpotifyClient;
|
||||
private SpotifyClient? _userAuthorizedSpotifyClient;
|
||||
private ILogger<SpotifyApiClient> _logger;
|
||||
|
||||
public SpotifyApiClient(ILogger<SpotifyApiClient> logger)
|
||||
@ -25,7 +25,7 @@ public class SpotifyApiClient
|
||||
var refreshResponse = await new OAuthClient().RequestToken(
|
||||
new AuthorizationCodeRefreshRequest(
|
||||
AppConfiguration.Instance.SpotifyClientId,
|
||||
AppConfiguration.Instance.SpotifyClientSecret,
|
||||
AppConfiguration.Instance.SpotifyClientSecret,
|
||||
user.SpotifyAuthRefreshToken)
|
||||
);
|
||||
var config = SpotifyClientConfig
|
||||
@ -33,7 +33,8 @@ public class SpotifyApiClient
|
||||
.WithAuthenticator(new AuthorizationCodeAuthenticator(
|
||||
AppConfiguration.Instance.SpotifyClientId,
|
||||
AppConfiguration.Instance.SpotifyClientSecret,
|
||||
new AuthorizationCodeTokenResponse() {
|
||||
new AuthorizationCodeTokenResponse()
|
||||
{
|
||||
RefreshToken = refreshResponse.RefreshToken,
|
||||
AccessToken = refreshResponse.AccessToken,
|
||||
TokenType = refreshResponse.TokenType,
|
||||
@ -46,8 +47,10 @@ public class SpotifyApiClient
|
||||
return this;
|
||||
}
|
||||
|
||||
private SpotifyClient UserAuthorizedSpotifyClient {
|
||||
get {
|
||||
private SpotifyClient UserAuthorizedSpotifyClient
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_userAuthorizedSpotifyClient == null)
|
||||
{
|
||||
throw new Exception("Cannot perform Spotify API call without user authorization. Authorize Spotify access from your user page first!");
|
||||
@ -89,13 +92,13 @@ public class SpotifyApiClient
|
||||
|
||||
public async Task DeAuthorizeUserAsync(User user)
|
||||
{
|
||||
using(var dci = DataContext.Instance)
|
||||
using (var dci = DataContext.Instance)
|
||||
{
|
||||
var isEntityTracked = dci.Entry(user).State != EntityState.Detached;
|
||||
|
||||
if(!isEntityTracked)
|
||||
if (!isEntityTracked)
|
||||
{
|
||||
user = dci.Users.Find(user.UserId);
|
||||
user = dci.Users.Find(user.UserId);
|
||||
}
|
||||
user.SpotifyAuthAccessToken = string.Empty;
|
||||
user.SpotifyAuthRefreshToken = string.Empty;
|
||||
@ -107,13 +110,13 @@ public class SpotifyApiClient
|
||||
|
||||
public async Task<string> GetValidAuthorizationTokenAsync(User user)
|
||||
{
|
||||
if(string.IsNullOrEmpty(user.SpotifyAuthAccessToken))
|
||||
if (string.IsNullOrEmpty(user.SpotifyAuthAccessToken))
|
||||
{
|
||||
// user either never connected Spotify or we failed to refresh token - user needs to re-authenticate
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
if(!this.IsAuthTokenExpired(user))
|
||||
if (!this.IsAuthTokenExpired(user))
|
||||
{
|
||||
return user.SpotifyAuthAccessToken;
|
||||
}
|
||||
@ -123,12 +126,12 @@ public class SpotifyApiClient
|
||||
|
||||
var isEntityTracked = dci.Entry(user).State != EntityState.Detached;
|
||||
|
||||
if(!isEntityTracked)
|
||||
if (!isEntityTracked)
|
||||
{
|
||||
user = dci.Users.Find(user.UserId);
|
||||
user = dci.Users.Find(user.UserId);
|
||||
}
|
||||
|
||||
try
|
||||
try
|
||||
{
|
||||
var oAuthResponse = await new OAuthClient().RequestToken(
|
||||
new AuthorizationCodeRefreshRequest(AppConfiguration.Instance.SpotifyClientId, AppConfiguration.Instance.SpotifyClientSecret, user.SpotifyAuthRefreshToken)
|
||||
@ -139,7 +142,7 @@ public class SpotifyApiClient
|
||||
user.SpotifyAuthRefreshToken = oAuthResponse.RefreshToken;
|
||||
return user.SpotifyAuthAccessToken;
|
||||
}
|
||||
catch(Exception ex)
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning($"Failed to refresh SpotifyAuth token for user {user.LdapUserName}: {ex.Message}");
|
||||
await DeAuthorizeUserAsync(user);
|
||||
@ -186,7 +189,7 @@ public class SpotifyApiClient
|
||||
var playlist = await UserAuthorizedSpotifyClient.Playlists.Create(currentUser.Id, playlistCreationRequest);
|
||||
|
||||
_logger.LogWarning($"Creating new playlist '{playlistTitle}'");
|
||||
using(var dci = DataContext.Instance)
|
||||
using (var dci = DataContext.Instance)
|
||||
{
|
||||
var trackedUserEntity = dci.Users.Find(createdBy.UserId);
|
||||
var newPlaylist = new SmartPlaylistDefinition()
|
||||
@ -240,18 +243,18 @@ public class SpotifyApiClient
|
||||
return string.Empty;
|
||||
}
|
||||
try
|
||||
{
|
||||
// for now hardcoded with my user ID
|
||||
var addItemRequest = new PlaylistAddItemsRequest(songIds.Select(id => $"spotify:track:{id}").ToList());
|
||||
_logger.LogWarning($"Adding songs to playlist with id '{playlistId}'");
|
||||
var response = await UserAuthorizedSpotifyClient.Playlists.AddItems(playlistId, addItemRequest);
|
||||
return response.SnapshotId;
|
||||
{
|
||||
// for now hardcoded with my user ID
|
||||
var addItemRequest = new PlaylistAddItemsRequest(songIds.Select(id => $"spotify:track:{id}").ToList());
|
||||
_logger.LogWarning($"Adding songs to playlist with id '{playlistId}'");
|
||||
var response = await UserAuthorizedSpotifyClient.Playlists.AddItems(playlistId, addItemRequest);
|
||||
return response.SnapshotId;
|
||||
|
||||
}
|
||||
catch (APIException ex)
|
||||
{
|
||||
throw new Exception($"Error adding songs to playlist with id: {playlistId}: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
catch (APIException ex)
|
||||
{
|
||||
throw new Exception($"Error adding songs to playlist with id: {playlistId}: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string> RemoveSongsFromPlaylist(string playlistId, List<string> songIds)
|
||||
@ -274,7 +277,7 @@ public class SpotifyApiClient
|
||||
};
|
||||
removeItemsRequest.Tracks.Add(item);
|
||||
}
|
||||
|
||||
|
||||
_logger.LogWarning($"Removing songs from playlist with id '{playlistId}'");
|
||||
var response = await UserAuthorizedSpotifyClient.Playlists.RemoveItems(playlistId, removeItemsRequest);
|
||||
return response.SnapshotId;
|
||||
|
Loading…
x
Reference in New Issue
Block a user