pukuthek-frontend/assets/main.js
2023-06-02 20:26:03 +02:00

69 lines
No EOL
1.9 KiB
JavaScript

async function getConfig(window) {
await $.getJSON("config.json", function (data) {
window.config = data;
});
}
async function authenticate(window) {
var oauth_settings;
await $.ajax({
url: window.config.peertube_api + "/oauth-clients/local",
}).done(function (data) {
oauth_settings = data;
});
await $.ajax({
url: window.config.peertube_api + "/users/token",
type: "POST",
data: {
client_id: oauth_settings.client_id,
client_secret: oauth_settings.client_secret,
grant_type: "password",
response_type: "code",
username: window.config.peertube_user,
password: window.config.peertube_password,
},
}).done(function (data) {
window.config.access_token = data.access_token;
window.config.refresh_token = data.refresh_token;
console.log(data.access_token);
});
}
async function getVideos(window) {
await getConfig(window);
await authenticate(window);
await $.ajax({
url:
window.config.peertube_api +
"/users/me/videos?sort=-createdAt",
type: "GET",
// Add bearer token to header
beforeSend: function (xhr) {
xhr.setRequestHeader(
"Authorization",
"Bearer " + window.config.access_token
);
},
})
.fail(function (data) {
console.log(data);
})
.done(function (data) {
data.data.forEach(function (video) {
$(".table").append(
"<tr><td>" +
"<a href='" + window.config.peertube_api + "/../.." + video.embedPath + "'><img src='" + window.config.peertube_api + "/../.." + video.thumbnailPath + "'></a>" +
"</td><td>" +
"<a href='" + window.config.peertube_api + "/../.." + video.embedPath + "'>" + video.name + "</a><br>" + video.description.replace(/^\s*\n/gm, "").replace(/(?:\r\n|\r|\n)/g, '<br>') +
"</td></tr>"
);
});
});
}
$(document).ready(function () {
getVideos(window);
});