video.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. let liked = false;
  2. let disliked = false;
  3. async function like(id) {
  4. if(!Cookies.get("user")) {
  5. return alert("you must be loggefd in to like!");
  6. }
  7. if (liked)
  8. return alert(
  9. "you already like video you can not like it anymore beacuse you aleady like video!!!"
  10. );
  11. axios.post(`/api/like/${id}`, {}).then(response => {
  12. if (response.data.status == 204) {
  13. liked = true;
  14. document.getElementById("video-likes").innerText =
  15. parseInt(document.getElementById("video-likes").innerText) + 1;
  16. } else {
  17. document.getElementById("video-likes").innerText =
  18. "ERROR ERROR ERROR ERROR ERRROR SERVCER ERROR";
  19. }
  20. }).catch(error => {
  21. throw new Error(`like() error: ${error}`);
  22. })
  23. }
  24. async function dislike(id) {
  25. if(!Cookies.get("user")) {
  26. return alert("you must be loggefd in to dislike!");
  27. }
  28. if (disliked)
  29. return alert(
  30. "you already dislike video you can not dislike it anymore beacuse you aleady dislike video!!!"
  31. );
  32. axios.post(`/api/dislike/${id}`, {}).then(response => {
  33. if (response.data.status == 204) {
  34. disliked = true;
  35. document.getElementById("video-dislikes").innerText =
  36. parseInt(document.getElementById("video-dislikes").innerText) + 1;
  37. } else {
  38. document.getElementById("video-dislikes").innerText =
  39. "ERROR ERROR ERROR ERROR ERRROR SERVCER ERROR";
  40. }
  41. }).catch(error => {
  42. throw new Error(`dislike() error: ${error}`);
  43. })
  44. }
  45. function gotoUser(user) {
  46. window.location.pathname = `/user/${user}`;
  47. }
  48. document.addEventListener("DOMContentLoaded", async () => {
  49. let video = document.getElementById("video");
  50. let id = window.location.pathname.split("/")[2];
  51. document.getElementById("account-button-anchor").href = `/user/${encodeURIComponent(Cookies.get('user'))}`
  52. if (Cookies.get("user") != null) {
  53. document.getElementById("login-button").classList.add("disabled");
  54. document.getElementById("logout-button").classList.remove("disabled");
  55. document.getElementById("account-button").classList.remove("disabled");
  56. document.getElementById("upload-button").classList.remove("disabled");
  57. }
  58. document
  59. .getElementById("comment-form")
  60. .addEventListener("submit", async (e) => {
  61. e.preventDefault();
  62. var formData = new FormData(document.getElementById("comment-form"));
  63. var json = Object.fromEntries(formData);
  64. const user = Cookies.get("user");
  65. if (!user) {
  66. alert("You must log in to comment.");
  67. return;
  68. }
  69. await axios.post(`/api/comment`, {
  70. commenter: user,
  71. videoID: id,
  72. text: json["text"]
  73. }, {
  74. headers: {
  75. 'Authorization': getToken(Cookies.get("user"))
  76. }
  77. }).then(response => {
  78. if (response.data.status == 201) {
  79. document.getElementById("comment-textarea").innerText = ""
  80. makeComment(Cookies.get("user"), "right now", json["text"]);
  81. } else {
  82. alert("ERROR ERROR ERROR ERORROR SERVCER ERROR!!!");
  83. }
  84. }).catch(error => {
  85. throw new Error(`comment error: ${error}`)
  86. })
  87. });
  88. // Load video
  89. let source = document.createElement("source");
  90. source.setAttribute("src", `/api/video/${id}`)
  91. source.setAttribute("type", "video/mp4");
  92. video.appendChild(source);
  93. video.setAttribute("poster", `/api/thumbnail/${id}`);
  94. // Load video info
  95. const info = await getInfo(id);
  96. document.getElementById("video-title").innerText = info.title;
  97. document.getElementById(
  98. "video-author"
  99. ).innerText = `Uploaded by: ${info.uploader}`;
  100. document.getElementById("video-author-anchor").href = `/user/${encodeURIComponent(info.uploader)}`;
  101. document.getElementById("video-date").innerText =
  102. parseTimestamp(info.uploaded_at).date +
  103. " " +
  104. parseTimestamp(info.uploaded_at).timestamp;
  105. document.getElementById("video-description").innerText = info.description;
  106. document.getElementById("video-likes").innerText = info.likes;
  107. document.getElementById("video-dislikes").innerText = info.dislikes;
  108. // Load author info
  109. const authorInfo = await getUserInfo(info.uploader);
  110. if(authorInfo.verified) document.getElementById("verified").classList.remove('disabled')
  111. document.getElementById("author-info").innerText = `${authorInfo.subscribers} subscribers ඞ ${authorInfo.social_score} social credit score`;
  112. // Check if the user is already subscribed
  113. if(!localStorage.getItem("subscribed")) localStorage.setItem("subscribed", JSON.stringify([]))
  114. // If so, disable the subscribe button
  115. if(JSON.parse(localStorage.getItem("subscribed")).includes(authorInfo.name)) document.getElementById("subscribe").setAttribute("disabled", "true");
  116. document.getElementById("subscribe").onclick = function(event) { subscribe(authorInfo) }
  117. // Load comments
  118. const comments = await getComments(id);
  119. comments.forEach((comment) => {
  120. makeComment(
  121. comment.commenter,
  122. parseTimestamp(comment.created_at).date +
  123. " " +
  124. parseTimestamp(comment.created_at).timestamp,
  125. comment.text
  126. );
  127. });
  128. document.getElementById("loading").classList.add("disabled")
  129. });
  130. function makeComment(username, timestamp, content) {
  131. let comment = document.createElement("div");
  132. comment.setAttribute("class", "comment");
  133. let commentSpan = document.createElement("div");
  134. commentSpan.setAttribute("class", "comment-span");
  135. comment.appendChild(commentSpan);
  136. let userAnchor = document.createElement("a");
  137. userAnchor.href = `/user/${encodeURIComponent(username)}`;
  138. userAnchor.classList.add("clickable-user")
  139. commentSpan.appendChild(userAnchor);
  140. let user = document.createElement("h2");
  141. user.innerText = username;
  142. user.setAttribute("class", "clickable-user");
  143. userAnchor.appendChild(user);
  144. let date = document.createElement("h6");
  145. date.innerText = timestamp;
  146. commentSpan.appendChild(date);
  147. let text = document.createElement("p");
  148. text.innerText = content;
  149. comment.appendChild(text);
  150. document.getElementById("comments").appendChild(comment);
  151. }
  152. function home() {
  153. window.location.pathname = "/";
  154. }
  155. // TODO: for now subscribe function is fine but maybe polish it up a little
  156. function subscribe(authorInfo) {
  157. return axios.get(`/api/subscribe/${encodeURIComponent(authorInfo.name)}`).then(data => {
  158. if(data.status != 200) {
  159. return document.getElementById("author-info").innerText = "ERROR ERROR ERROE SEREVER ERROR!!!!"
  160. }
  161. // Write to localStorage
  162. if(!localStorage.getItem("subscribed")) localStorage.setItem("subscribed", JSON.stringify([]))
  163. const subscribed = JSON.parse(localStorage.getItem("subscribed"));
  164. subscribed.push(authorInfo.name);
  165. localStorage.setItem("subscribed", JSON.stringify(subscribed))
  166. // Disable subscribed button
  167. document.getElementById("subscribe").setAttribute("disabled", "true");
  168. // Update subscribed counter
  169. document.getElementById("author-info").innerText = `${authorInfo.subscribers + 1} subscribers ඞ ${authorInfo.social_score} social credit score`;
  170. }).catch(error => {
  171. console.error(error);
  172. return document.getElementById("author-info").innerText = "ERROR ERROR ERROE SEREVER ERROR!!!!"
  173. })
  174. }