user.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. document.addEventListener("DOMContentLoaded", async () => {
  2. const id = decodeURIComponent(window.location.pathname.split("/")[2]);
  3. document.getElementById("account-button-anchor").href = `/user/${encodeURIComponent(Cookies.get('user'))}`
  4. document.getElementById(
  5. "no-videos"
  6. ).innerText = `There seems to be no videos published by user of the site "skibidihub" by the name of "${id}".`;
  7. const user = await getUserInfo(id)
  8. if(user.verified) document.getElementById("verified").classList.remove('disabled');
  9. if(Cookies.get("user") === id) document.getElementById("edit-profile").classList.remove("disabled")
  10. if(user.website) {
  11. document.getElementById("website-anchor").href = user.website;
  12. } else {
  13. document.getElementById("website-anchor").remove();
  14. }
  15. document.getElementById("subscribers").innerText = `${user.subscribers} subscribers`;
  16. document.getElementById("social-score").innerText = `${user.social_score} social credit score`
  17. if(user.description) {
  18. if(user.description.length > 18) {
  19. document.getElementById("description").innerText = (user.description.slice(0, -(user.description.length - 15)) + "...").replace(/(\r\n|\n|\r)/gm, " ");
  20. document.getElementById("description").classList.add("description-click");
  21. document.getElementById("description").addEventListener("click", () => {
  22. document.getElementById("description-dialog-text").innerText = user.description;
  23. document.getElementById("description-dialog").showModal()
  24. })
  25. } else {
  26. document.getElementById("description").innerText = user.description.replace(/(\r\n|\n|\r)/gm, " ")
  27. }
  28. } else {
  29. document.getElementById("description").innerText = "no description set"
  30. }
  31. // Check if the user is already subscribed
  32. if(!localStorage.getItem("subscribed")) localStorage.setItem("subscribed", JSON.stringify([]))
  33. // If so, disable the subscribe button
  34. if(JSON.parse(localStorage.getItem("subscribed")).includes(user.name)) document.getElementById("subscribe").setAttribute("disabled", "true");
  35. document.getElementById("subscribe").onclick = function(event) { subscribe(user) }
  36. if (Cookies.get("user") != null) {
  37. document.getElementById("user-info-name").innerText = id;
  38. document.getElementById("login-button").classList.add("disabled");
  39. document.getElementById("logout-button").classList.remove("disabled");
  40. document.getElementById("account-button").classList.remove("disabled");
  41. document.getElementById("upload-button").classList.remove("disabled");
  42. } else {
  43. document.getElementById("user-info-name").innerText =
  44. "⚠️⚠️⚠️⚠️SIGN IN to see this EPIC content❌❌💋🩻";
  45. }
  46. const videos = await getAllUserVideos(encodeURIComponent(id))
  47. document.getElementById("loading").classList.add("disabled");
  48. if (videos.data.length > 0) {
  49. videos.data.forEach((video) => {
  50. makeVideo(video.id, video);
  51. });
  52. } else {
  53. document.getElementById("no-videos").classList.remove("disabled");
  54. }
  55. });
  56. async function makeVideo(id, info) {
  57. const video = document.createElement("a");
  58. video.href = `/video/${id}`
  59. video.classList.add("video");
  60. const img = document.createElement("img");
  61. img.classList.add("thumbnail");
  62. img.setAttribute("src", `/api/thumbnail/${id}`);
  63. video.appendChild(img);
  64. const videoInfoContainer = document.createElement("div");
  65. videoInfoContainer.classList.add("video-info-container");
  66. video.appendChild(videoInfoContainer);
  67. const videoTitle = document.createElement("h3");
  68. videoTitle.classList.add("video-title");
  69. const title = info.title
  70. if(info.title.length > 25) {
  71. videoTitle.innerText = title.slice(0, -(info.title.length - 22)) + "...";
  72. } else {
  73. videoTitle.innerText = title
  74. }
  75. videoTitle.onclick = function (event) {
  76. window.location.pathname = `/video/${id}`;
  77. };
  78. videoInfoContainer.appendChild(videoTitle);
  79. const videoUploader = document.createElement("a");
  80. videoUploader.href = `/user/${encodeURIComponent(info.uploader)}`
  81. videoUploader.classList.add("video-uploader");
  82. videoUploader.innerText = `published by: ${info.uploader}`;
  83. videoInfoContainer.appendChild(videoUploader);
  84. document.getElementById("videos").appendChild(video);
  85. }
  86. // TODO: for now subscribe function is fine but maybe polish it up a little
  87. function subscribe(authorInfo) {
  88. return axios.get(`/api/subscribe/${encodeURIComponent(authorInfo.name)}`).then(data => {
  89. if(data.status != 200) {
  90. return document.getElementById("subscribers").innerText = "ERROR ERROR ERROE SEREVER ERROR!!!!"
  91. }
  92. // Write to localStorage
  93. if(!localStorage.getItem("subscribed")) localStorage.setItem("subscribed", JSON.stringify([]))
  94. const subscribed = JSON.parse(localStorage.getItem("subscribed"));
  95. subscribed.push(authorInfo.name);
  96. localStorage.setItem("subscribed", JSON.stringify(subscribed))
  97. // Disable subscribed button
  98. document.getElementById("subscribe").setAttribute("disabled", "true");
  99. // Update subscribed counter
  100. document.getElementById("subscribers").innerText = `${authorInfo.subscribers + 1} subscribers`;
  101. }).catch(error => {
  102. console.error(error);
  103. return document.getElementById("subscribers").innerText = "ERROR ERROR ERROE SEREVER ERROR!!!!"
  104. })
  105. }