aboutsummaryrefslogtreecommitdiffhomepage
path: root/js/main.js
diff options
context:
space:
mode:
authorCiaran <[email protected]>2021-01-26 17:57:04 +0000
committerCiaran <[email protected]>2021-01-26 17:57:04 +0000
commit88ca8bd1abcc25635bac1b4fda7c8250144c905d (patch)
tree557f8059e17c9721fc1272f87fb96b7559dde0ed /js/main.js
parentd98fb1409fdd2373b899a1370af477550490bd45 (diff)
downloadpasta-88ca8bd1abcc25635bac1b4fda7c8250144c905d.tar.gz
pasta-88ca8bd1abcc25635bac1b4fda7c8250144c905d.zip
first pass - works without PIN
Diffstat (limited to 'js/main.js')
-rw-r--r--js/main.js83
1 files changed, 81 insertions, 2 deletions
diff --git a/js/main.js b/js/main.js
index cb23a7f..7ddf1cf 100644
--- a/js/main.js
+++ b/js/main.js
@@ -191,7 +191,7 @@ function checkIfAuthTokenIsValid() {
$('#new-pin-container').hide();
$('#authed-pin-container').show();
$('#loggedInAs').text(data.username);
- getServers();
+ getUserAccounts();
},
"error": (data) => {
if (data.status == 401) {
@@ -309,12 +309,92 @@ function useLocalAddress (checkbox) {
window.location.reload();
}
+function getUserAccounts() {
+ // Get managed accounts
+ $.ajax({
+ "url": `https://plex.tv/api/v2/home/users`,
+ "method": "GET",
+ "headers": {
+ "X-Plex-Client-Identifier": clientIdentifier,
+ "X-Plex-Token": plexToken,
+ "accept": "application/json"
+ },
+ "success": (data) => {
+ if (data.users.length == 0) {
+ getServers();
+ } else {
+ let userAccounts = [];
+ for (let i = 0; i < data.users.length; i++) {
+ userAccounts.push(data.users[i]);
+ }
+ // Populate and show the users table
+ displayUserAccounts(userAccounts);
+ }
+ },
+ "error": (error) => {
+ console.log(error);
+ }
+ });
+}
+
+function displayUserAccounts(users) {
+ $("#userTable tbody").empty();
+ $("#serverTable tbody").empty();
+ $("#libraryTable tbody").empty();
+ $("#tvShowsTable tbody").empty();
+ $("#seasonsTable tbody").empty();
+ $("#episodesTable tbody").empty();
+ $("#audioTable tbody").empty();
+ $("#subtitleTable tbody").empty();
+
+ for (let i = 0; i < users.length; i++) {
+ let rowHTML = `<tr onclick="switchUser('${users[i].uuid}', this)">
+ <td>${users[i].title}</td>
+ </tr>`;
+ $("#userTable tbody").append(rowHTML);
+ }
+
+ $('#userTableContainer').show();
+}
+
+function switchUser(userId, row) {
+ $("#serverTable tbody").empty();
+ $("#libraryTable tbody").empty();
+ $("#tvShowsTable tbody").empty();
+ $("#seasonsTable tbody").empty();
+ $("#episodesTable tbody").empty();
+ $("#audioTable tbody").empty();
+ $("#subtitleTable tbody").empty();
+
+ $(row).siblings().removeClass("table-active");
+ $(row).addClass("table-active");
+
+ $.ajax({
+ "url": `https://plex.tv/api/v2/home/users/${userId}/switch`,
+ "method": "POST",
+ "headers": {
+ "X-Plex-Client-Identifier": clientIdentifier,
+ "X-Plex-Token": plexToken,
+ "accept": "application/json"
+ },
+ "success": (data) => {
+ // Change the plex token to the auth token of the chosen user
+ plexToken = data.authToken;
+ getServers();
+ },
+ "error": (error) => {
+ console.log(error);
+ }
+ });
+}
+
function getServers () {
// Choose whether or not to include https connections
let includeHttps = 1;
if (location.protocol == 'http:') {
includeHttps = 0;
}
+
// Get the servers for this user
$.ajax({
"url": `https://plex.tv/api/v2/resources?includeHttps=${includeHttps}&includeRelay=0`,
@@ -388,7 +468,6 @@ function displayServers(servers) {
</tr>`;
$("#serverTable tbody").append(rowHTML);
}
- $("#serverTableContainer").show();
}
async function chooseServer(number, row) {