You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Ombi/src/Ombi/ClientApp/src/app/settings/plex/plex.component.ts

211 lines
7.6 KiB

import { Component, OnDestroy, OnInit, ViewChild } from "@angular/core";
import { Subject } from "rxjs";
import { takeUntil } from "rxjs/operators";
import { IPlexLibrariesSettings, IPlexServer, IPlexServerResponse, IPlexServerViewModel, IPlexSettings } from "../../interfaces";
import { JobService, NotificationService, PlexService, SettingsService, TesterService } from "../../services";
import { MatTabChangeEvent, MatTabGroup } from "@angular/material/tabs";
import {UntypedFormControl} from '@angular/forms';
import { MatDialog } from "@angular/material/dialog";
import { PlexWatchlistComponent } from "./components/watchlist/plex-watchlist.component";
import { PlexCreds, PlexSyncType } from "./components/models";
@Component({
templateUrl: "./plex.component.html",
styleUrls: ["./plex.component.scss"]
})
export class PlexComponent implements OnInit, OnDestroy {
public settings: IPlexSettings;
public loadedServers: IPlexServerViewModel; // This comes from the api call for the user to select a server
public serversButton = false;
selected = new UntypedFormControl(0);
@ViewChild("tabGroup", {static: false}) public tagGroup: MatTabGroup;
public advanced = false;
private subscriptions = new Subject<void>();
constructor(
private settingsService: SettingsService,
private notificationService: NotificationService,
private plexService: PlexService,
private testerService: TesterService,
private jobService: JobService,
private dialog: MatDialog) { }
public ngOnInit() {
this.settingsService.getPlex().subscribe(x => {
this.settings = x;
});
}
public requestServers({ username, password }: PlexCreds) {
this.plexService.getServers(username, password).pipe(
takeUntil(this.subscriptions),
).subscribe(x => {
if (x.success) {
this.loadedServers = x;
this.serversButton = true;
this.notificationService.success("Found the servers! Please select one!");
} else {
this.notificationService.warning("Error When Requesting Plex Servers", "Please make sure your username and password are correct");
}
});
}
public selectServer(selectedServer: IPlexServerResponse, server: IPlexServer) {
var splitServers = selectedServer.localAddresses.split(",");
if (splitServers.length > 1) {
server.ip = splitServers[splitServers.length - 1];
} else {
server.ip = selectedServer.localAddresses;
}
server.name = selectedServer.name;
server.machineIdentifier = selectedServer.machineIdentifier;
server.plexAuthToken = selectedServer.accessToken;
server.port = parseInt(selectedServer.port);
server.ssl = selectedServer.scheme === "http" ? false : true;
server.serverHostname = "";
this.notificationService.success(`Selected ${server.name}!`);
}
public testPlex(server: IPlexServer) {
this.testerService.plexTest(server).subscribe(x => {
if (x === true) {
this.notificationService.success(`Successfully connected to the Plex server ${server.name}!`);
} else {
this.notificationService.error(`We could not connect to the Plex server ${server.name}!`);
}
});
}
public addTab(event: MatTabChangeEvent) {
const tabName = event.tab.textLabel;
if (tabName == "Add Server"){
if (this.settings.servers == null) {
this.settings.servers = [];
}
this.settings.servers.push(<IPlexServer> { name: "New" + this.settings.servers.length + "*", id: Math.floor(Math.random() * (99999 - 0 + 1) + 1) });
//this.tagGroup.selectedIndex = (0);
this.selected.setValue(this.settings.servers.length - 1);
}
}
public removeServer(server: IPlexServer) {
const index = this.settings.servers.indexOf(server, 0);
if (index > -1) {
this.settings.servers.splice(index, 1);
this.selected.setValue(this.settings.servers.length - 1);
}
}
public loadLibraries(server: IPlexServer) {
if (server.ip == null) {
this.notificationService.error("Plex is not yet configured correctly");
return;
}
this.plexService.getLibraries(server).subscribe(x => {
server.plexSelectedLibraries = [];
if (x.successful) {
x.data.mediaContainer.directory.forEach((item) => {
const lib: IPlexLibrariesSettings = {
key: item.key,
title: item.title,
enabled: false,
};
server.plexSelectedLibraries.push(lib);
});
} else {
this.notificationService.error(x.message);
}
},
err => { this.notificationService.error(err); });
}
public save() {
const filtered = this.settings.servers.filter(x => x.name !== "");
this.settings.servers = filtered;
let invalid = false;
this.settings.servers.forEach(server => {
if (server.serverHostname && server.serverHostname.length > 0 && !server.serverHostname.startsWith("http")) {
invalid = true;
}
});
if (invalid) {
this.notificationService.error("Please ensure that your External Hostname is a full URL including the Scheme (http/https)")
return;
}
this.settingsService.savePlex(this.settings).subscribe(x => {
if (x) {
this.notificationService.success("Successfully saved Plex settings");
} else {
this.notificationService.success("There was an error when saving the Plex settings");
}
});
}
public runSync(type: PlexSyncType) {
switch (type) {
case PlexSyncType.Full:
this.runCacher();
return;
case PlexSyncType.RecentlyAdded:
this.runRecentlyAddedCacher();
return;
case PlexSyncType.ClearAndReSync:
this.clearDataAndResync();
return;
case PlexSyncType.WatchlistImport:
this.runWatchlistImport();
return;
}
}
private runCacher(): void {
this.jobService.runPlexCacher().subscribe(x => {
if (x) {
this.notificationService.success("Triggered the Plex Full Sync");
}
});
}
private runRecentlyAddedCacher(): void {
this.jobService.runPlexRecentlyAddedCacher().subscribe(x => {
if (x) {
this.notificationService.success("Triggered the Plex Recently Added Sync");
}
});
}
private clearDataAndResync(): void {
this.jobService.clearMediaserverData().subscribe(x => {
if (x) {
this.notificationService.success("Triggered the Clear MediaServer Resync");
}
});
}
private runWatchlistImport(): void {
this.jobService.runPlexWatchlistImport().subscribe(x => {
if (x) {
this.notificationService.success("Triggered the Watchlist Import");
}
});
}
public openWatchlistUserLog(): void {
this.dialog.open(PlexWatchlistComponent, { width: "700px", panelClass: 'modal-panel' });
}
public ngOnDestroy() {
this.subscriptions.next();
this.subscriptions.complete();
}
}