Skip to content

Commit

Permalink
fix(plex-api): Switch over to the new API to avoid deprecation & save… (
Browse files Browse the repository at this point in the history
#4986)

* fix(plex-api): Switch over to the new API to avoid deprecation & save the plex settings when modifying the servers

* Delete Ombi.sln
  • Loading branch information
tidusjar committed Jul 19, 2023
1 parent aacaa3e commit 2f2d35e
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/Ombi.Api.Plex/IPlexApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface IPlexApi
Task<PlexMetadata> GetMetadata(string authToken, string plexFullHost, string itemId);
Task<PlexMetadata> GetSeasons(string authToken, string plexFullHost, string ratingKey);
Task<PlexContainer> GetAllEpisodes(string authToken, string host, string section, int start, int retCount);
Task<PlexFriends> GetUsers(string authToken);
Task<PlexUsers> GetUsers(string authToken);
Task<PlexAccount> GetAccount(string authToken);
Task<PlexMetadata> GetRecentlyAdded(string authToken, string uri, string sectionId);
Task<OAuthContainer> GetPin(int pinId);
Expand Down
32 changes: 6 additions & 26 deletions src/Ombi.Api.Plex/Models/Friends/PlexFriends.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,26 @@

namespace Ombi.Api.Plex.Models.Friends
{
[XmlRoot(ElementName = "Server")]
public class Server
{
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[XmlAttribute(AttributeName = "serverId")]
public string ServerId { get; set; }
[XmlAttribute(AttributeName = "machineIdentifier")]
public string MachineIdentifier { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "lastSeenAt")]
public string LastSeenAt { get; set; }
[XmlAttribute(AttributeName = "numLibraries")]
public string NumLibraries { get; set; }
[XmlAttribute(AttributeName = "owned")]
public string Owned { get; set; }
}

[XmlRoot(ElementName = "User")]
public class UserFriends
{
[XmlElement(ElementName = "Server")]
public Server Server { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
/// <summary>
/// Title is for Home Users only
/// </summary>
[XmlAttribute(AttributeName = "title")]
public string Title { get; set; }
[XmlAttribute(AttributeName = "username")]
public string Username { get; set; }
[XmlAttribute(AttributeName = "email")]
public string Email { get; set; }
[XmlAttribute(AttributeName = "recommendationsPlaylistId")]
public string RecommendationsPlaylistId { get; set; }
[XmlAttribute(AttributeName = "thumb")]
public string Thumb { get; set; }
[XmlAttribute(AttributeName = "home")]
public bool HomeUser { get; set; }
}

[XmlRoot(ElementName = "MediaContainer")]
public class PlexFriends
public class PlexUsers
{
[XmlElement(ElementName = "User")]
public UserFriends[] User { get; set; }
Expand Down
6 changes: 3 additions & 3 deletions src/Ombi.Api.Plex/PlexApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private string ApplicationName
}

private const string SignInUri = "https://plex.tv/users/sign_in.json";
private const string FriendsUri = "https://plex.tv/pms/friends/all";
private const string FriendsUri = "https://plex.tv/api/users";
private const string GetAccountUri = "https://plex.tv/users/account.json";
private const string ServerUri = "https://plex.tv/pms/servers.xml";
private const string WatchlistUri = "https://metadata.provider.plex.tv/";
Expand Down Expand Up @@ -195,12 +195,12 @@ public async Task<PlexContainer> GetAllEpisodes(string authToken, string host, s
/// </summary>
/// <param name="authToken"></param>
/// <returns></returns>
public async Task<PlexFriends> GetUsers(string authToken)
public async Task<PlexUsers> GetUsers(string authToken)
{
var request = new Request(string.Empty, FriendsUri, HttpMethod.Get, ContentType.Xml);
await AddHeaders(request, authToken);

return await Api.Request<PlexFriends>(request);
return await Api.Request<PlexUsers>(request);
}

public async Task<PlexMetadata> GetRecentlyAdded(string authToken, string uri, string sectionId)
Expand Down
13 changes: 7 additions & 6 deletions src/Ombi.Schedule.Tests/PlexUserImporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public async Task Import_Doesnt_Import_Banned_Users()
{
_mocker.Setup<ISettingsService<UserManagementSettings>, Task<UserManagementSettings>>(x => x.GetSettingsAsync())
.ReturnsAsync(new UserManagementSettings { ImportPlexAdmin = false, ImportPlexUsers = true, BannedPlexUserIds = new List<string> { "Banned" } });
_mocker.Setup<IPlexApi, Task<PlexFriends>>(x => x.GetUsers(It.IsAny<string>())).ReturnsAsync(new PlexFriends
_mocker.Setup<IPlexApi, Task<PlexUsers>>(x => x.GetUsers(It.IsAny<string>())).ReturnsAsync(new PlexUsers
{
User = new UserFriends[]
{
Expand All @@ -213,7 +213,7 @@ public async Task Import_Doesnt_Import_Managed_User()
{
_mocker.Setup<ISettingsService<UserManagementSettings>, Task<UserManagementSettings>>(x => x.GetSettingsAsync())
.ReturnsAsync(new UserManagementSettings { ImportPlexAdmin = false, ImportPlexUsers = true });
_mocker.Setup<IPlexApi, Task<PlexFriends>>(x => x.GetUsers(It.IsAny<string>())).ReturnsAsync(new PlexFriends
_mocker.Setup<IPlexApi, Task<PlexUsers>>(x => x.GetUsers(It.IsAny<string>())).ReturnsAsync(new PlexUsers
{
User = new UserFriends[]
{
Expand All @@ -222,6 +222,7 @@ public async Task Import_Doesnt_Import_Managed_User()
Email = "email",
Id = "id",
Title = "title",
HomeUser = true
}
}
});
Expand All @@ -236,7 +237,7 @@ public async Task Import_Doesnt_Import_DuplicateEmail()
{
_mocker.Setup<ISettingsService<UserManagementSettings>, Task<UserManagementSettings>>(x => x.GetSettingsAsync())
.ReturnsAsync(new UserManagementSettings { ImportPlexAdmin = false, ImportPlexUsers = true });
_mocker.Setup<IPlexApi, Task<PlexFriends>>(x => x.GetUsers(It.IsAny<string>())).ReturnsAsync(new PlexFriends
_mocker.Setup<IPlexApi, Task<PlexUsers>>(x => x.GetUsers(It.IsAny<string>())).ReturnsAsync(new PlexUsers
{
User = new UserFriends[]
{
Expand Down Expand Up @@ -266,7 +267,7 @@ public async Task Import_Created_Plex_User()
OmbiRoles.RequestMovie
}
});
_mocker.Setup<IPlexApi, Task<PlexFriends>>(x => x.GetUsers(It.IsAny<string>())).ReturnsAsync(new PlexFriends
_mocker.Setup<IPlexApi, Task<PlexUsers>>(x => x.GetUsers(It.IsAny<string>())).ReturnsAsync(new PlexUsers
{
User = new UserFriends[]
{
Expand Down Expand Up @@ -303,7 +304,7 @@ public async Task Import_Update_Plex_User()
OmbiRoles.RequestMovie
}
});
_mocker.Setup<IPlexApi, Task<PlexFriends>>(x => x.GetUsers(It.IsAny<string>())).ReturnsAsync(new PlexFriends
_mocker.Setup<IPlexApi, Task<PlexUsers>>(x => x.GetUsers(It.IsAny<string>())).ReturnsAsync(new PlexUsers
{
User = new UserFriends[]
{
Expand Down Expand Up @@ -342,7 +343,7 @@ public async Task Import_Cleanup_Missing_Plex_Users()
},
CleanupPlexUsers = true,
});
_mocker.Setup<IPlexApi, Task<PlexFriends>>(x => x.GetUsers(It.IsAny<string>())).ReturnsAsync(new PlexFriends
_mocker.Setup<IPlexApi, Task<PlexUsers>>(x => x.GetUsers(It.IsAny<string>())).ReturnsAsync(new PlexUsers
{
User = new UserFriends[]
{
Expand Down
10 changes: 7 additions & 3 deletions src/Ombi.Schedule/Jobs/Plex/PlexUserImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,19 @@ private async Task<List<OmbiUser>> ImportPlexUsers(UserManagementSettings userMa
continue;
}

if (plexUser.HomeUser)
{
_log.LogInformation($"User '{plexUser.Title}' is a Plex Home User. Home Users are not supported");
continue;
}

// Check if this Plex User already exists
// We are using the Plex USERNAME and Not the TITLE, the Title is for HOME USERS
var existingPlexUser = allUsers.FirstOrDefault(x => x.ProviderUserId == plexUser.Id);
if (existingPlexUser == null)
{

if (!plexUser.Username.HasValue())
{
_log.LogInformation("Could not create Plex user since the have no username, PlexUserId: {0}", plexUser.Id);
_log.LogInformation($"Could not create Plex user since the have no username, PlexUserId: {plexUser.Id}");
continue;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Ombi/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
"plex",
"wizard"
],
"rpc.enabled": true
"rpc.enabled": true,
"dotnet.defaultSolution": "Ombi.sln"
}
5 changes: 4 additions & 1 deletion src/Ombi/ClientApp/src/app/settings/plex/plex.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,14 @@ export class PlexComponent implements OnInit, OnDestroy {
this.removeServer(server);
}
if (x.server) {
console.log(x.server);
var idx = this.settings.servers.findIndex(server => server.id === x.server.id);
if (idx >= 0) {
this.settings.servers[idx] = x.server;
} else {
this.settings.servers.push(x.server);
}

this.save();
}
});
}
Expand All @@ -163,6 +164,7 @@ export class PlexComponent implements OnInit, OnDestroy {
}
if (x.server) {
this.settings.servers.push(x.server);
this.save();
}
});
}
Expand All @@ -173,6 +175,7 @@ export class PlexComponent implements OnInit, OnDestroy {
this.settings.servers.splice(index, 1);
this.selected.setValue(this.settings.servers.length - 1);
}
this.save();
}

private runCacher(): void {
Expand Down

0 comments on commit 2f2d35e

Please sign in to comment.