2022-11-15 23:49:04 +08:00
|
|
|
|
|
|
|
|
|
timezoneChangingOne = (x)=>{var date= new Date(x.innerHTML);
|
|
|
|
|
var year = date.getFullYear().toString();
|
|
|
|
|
var month = (date.getMonth()+1).toString().padStart(2, '0');
|
|
|
|
|
var day = date.getDate().toString().padStart(2, '0');
|
|
|
|
|
var hour = date.getHours().toString().padStart(2, '0');
|
|
|
|
|
var min = date.getMinutes().toString().padStart(2, '0');
|
|
|
|
|
var sec = date.getSeconds().toString().padStart(2, '0');
|
|
|
|
|
local_date_string = `${year}-${month}-${day} ${hour}:${min}:${sec}`;
|
|
|
|
|
x.innerHTML=local_date_string;}
|
|
|
|
|
|
|
|
|
|
function timezoneChanging(){
|
|
|
|
|
document.querySelectorAll(".post-time").forEach(
|
|
|
|
|
x => timezoneChangingOne(x)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-26 05:01:20 +08:00
|
|
|
|
var requestNote = {"type" : "home", "arg" : ""};
|
|
|
|
|
var pathName = window.location.pathname;
|
|
|
|
|
var homePattern = /[/]home[/]/;
|
|
|
|
|
var userPagePattern = /[/]user[/]([^/]+)/;
|
|
|
|
|
var userIdName = "";
|
|
|
|
|
if (pathName.match(userPagePattern)){
|
|
|
|
|
userIdName = pathName.match(userPagePattern)[1];
|
|
|
|
|
requestNote = {"type" : "user", "arg" : userIdName};
|
|
|
|
|
}
|
2022-11-15 23:49:04 +08:00
|
|
|
|
|
|
|
|
|
|
2022-11-24 00:24:57 +08:00
|
|
|
|
|
2022-11-15 23:49:04 +08:00
|
|
|
|
$name = (x) => document.getElementsByName(x);
|
|
|
|
|
$ = (x) => document.getElementById(x);
|
2022-11-24 00:24:57 +08:00
|
|
|
|
|
|
|
|
|
csrf_token = $("_token").content;
|
|
|
|
|
|
|
|
|
|
|
2022-11-15 23:49:04 +08:00
|
|
|
|
var httpRequest;
|
2022-11-26 05:01:20 +08:00
|
|
|
|
|
|
|
|
|
|
2022-11-15 23:49:04 +08:00
|
|
|
|
|
|
|
|
|
async function make_req() {
|
|
|
|
|
|
|
|
|
|
post_text = $('post_text').value;
|
|
|
|
|
post_privilage = $('privil_choosing').value;
|
|
|
|
|
|
2022-11-24 00:24:57 +08:00
|
|
|
|
|
|
|
|
|
|
2022-11-15 23:49:04 +08:00
|
|
|
|
if (post_text != ""){
|
|
|
|
|
|
|
|
|
|
await fetch('/api/post', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
2022-11-24 00:24:57 +08:00
|
|
|
|
"X-CSRFToken": csrf_token,
|
2022-11-15 23:49:04 +08:00
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
"privilage" : post_privilage,
|
2022-11-26 05:01:20 +08:00
|
|
|
|
"text" : post_text,
|
|
|
|
|
"request_note" : requestNote,
|
2022-11-15 23:49:04 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}).then(response => {if (response.ok == true)
|
|
|
|
|
{$('post_text').value = "";
|
|
|
|
|
getLatestPosts(); // wait for a moment then get latest posts
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function auto_expand(element) {
|
|
|
|
|
element.style.height = 6+"em";
|
|
|
|
|
element.style.height = (element.scrollHeight)+"px";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
timezoneChanging();
|
|
|
|
|
|
|
|
|
|
function adjust_post_text(){
|
|
|
|
|
var post_text = $("post_text");
|
|
|
|
|
post_text.style.height = 6+"em";
|
|
|
|
|
post_text.style.height = (post_text.scrollHeight)+"px";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function getRecentPostsCounter(){
|
|
|
|
|
latest_time_string = $("latest_time").innerHTML;
|
|
|
|
|
await fetch('/api/get_recent_posts_counter', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
2022-11-24 00:24:57 +08:00
|
|
|
|
"X-CSRFToken": csrf_token,
|
2022-11-15 23:49:04 +08:00
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
"latest_time" : latest_time_string,
|
2022-11-26 05:01:20 +08:00
|
|
|
|
"request_note" : requestNote,
|
2022-11-15 23:49:04 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}).then(response => response.json())
|
2022-11-26 05:01:20 +08:00
|
|
|
|
.then(the_json => {let newer_posts_num = the_json['newer_posts_num'];
|
2022-11-15 23:49:04 +08:00
|
|
|
|
if (newer_posts_num == 1)
|
|
|
|
|
{ $("new_post_notifier").style.display = "block";
|
|
|
|
|
$("new_post_notifier").innerHTML = `1 new post`}
|
|
|
|
|
if (newer_posts_num > 1)
|
|
|
|
|
{ $("new_post_notifier").style.display = "block";
|
|
|
|
|
$("new_post_notifier").innerHTML = `${newer_posts_num} new posts`} });
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var intervalID = window.setInterval(getRecentPostsCounter, 20000);
|
|
|
|
|
|
|
|
|
|
function appendPostToTheEnd(post){
|
|
|
|
|
var public_tl = $("public_timeline");
|
|
|
|
|
var new_div = createPostDiv(post);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public_tl.insertBefore(new_div, $('previous_post_loader'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getPreviousPosts(){
|
|
|
|
|
var new_oldest_time;
|
|
|
|
|
oldest_time_string = $("oldest_time").innerHTML;
|
|
|
|
|
await fetch('/api/get_previous_posts', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
2022-11-24 00:24:57 +08:00
|
|
|
|
"X-CSRFToken": csrf_token,
|
2022-11-15 23:49:04 +08:00
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
"oldest_time" : oldest_time_string,
|
2022-11-26 05:01:20 +08:00
|
|
|
|
"request_note" : requestNote,
|
|
|
|
|
|
2022-11-15 23:49:04 +08:00
|
|
|
|
}) }).then(response => response.json())
|
|
|
|
|
.then(item => {new_oldest_time_raw = item['oldest_time'];
|
|
|
|
|
new_oldest_time = new_oldest_time_raw.substring(0,10) + " " + new_oldest_time_raw.substring(11,26) + "+0000";
|
|
|
|
|
return item['older_posts'];})
|
|
|
|
|
.then(posts => {if (posts.length == 0)
|
|
|
|
|
{$('previous_post_loader').style.display = 'none';}
|
2022-11-27 01:34:44 +08:00
|
|
|
|
else{posts.forEach(post =>
|
|
|
|
|
appendPostToTheEnd(post));
|
|
|
|
|
updateClickedIconEvent();
|
|
|
|
|
$('oldest_time').innerHTML = new_oldest_time;}})
|
2022-11-15 23:49:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-11-26 05:01:20 +08:00
|
|
|
|
async function make_follow_req(){
|
|
|
|
|
requestValue = $("follow-user-button").value;
|
|
|
|
|
await fetch(`/api/follow_request/${requestValue}/${userIdName}`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
"X-CSRFToken": csrf_token,
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
|
|
|
|
body : ""
|
|
|
|
|
}).then(response => response.json())
|
|
|
|
|
.then(
|
|
|
|
|
item =>{
|
|
|
|
|
returnValue = item["status"];
|
|
|
|
|
if (returnValue == "request sent"){
|
|
|
|
|
$("follow-user-button").value = "cancel-following-request";
|
|
|
|
|
$("follow-user-button").innerHTML = "Cancel Following Request";
|
|
|
|
|
}
|
|
|
|
|
else if(returnValue == "request cancelled" || returnValue == "unfollowed"){
|
|
|
|
|
$("follow-user-button").value = "send-following-request";
|
|
|
|
|
$("follow-user-button").innerHTML = "Follow";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-15 23:49:04 +08:00
|
|
|
|
async function getLatestPosts(){
|
|
|
|
|
latest_time_string = $("latest_time").innerHTML;
|
|
|
|
|
await fetch('/api/get_latest_posts', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
2022-11-24 00:24:57 +08:00
|
|
|
|
"X-CSRFToken": csrf_token,
|
2022-11-15 23:49:04 +08:00
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
"latest_time" : latest_time_string,
|
2022-11-26 05:01:20 +08:00
|
|
|
|
"request_note" : requestNote,
|
|
|
|
|
}) })
|
|
|
|
|
.then(response => response.json())
|
2022-11-15 23:49:04 +08:00
|
|
|
|
.then(item => item['newer_posts'])
|
|
|
|
|
.then(posts => {posts.forEach(post => addPostToTheBeginning(post));
|
|
|
|
|
$('new_post_notifier').style.display = "none";
|
|
|
|
|
var nowTime = new Date();
|
|
|
|
|
var nowTimeString = nowTime.toISOString();
|
2022-11-27 01:34:44 +08:00
|
|
|
|
updateClickedIconEvent();
|
2022-11-15 23:49:04 +08:00
|
|
|
|
$("latest_time").innerHTML = nowTimeString.substring(0,10) + " " + nowTimeString.substring(11,23) + "000+0000"; });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createPostDiv(post){
|
|
|
|
|
|
|
|
|
|
var new_div = document.createElement("div");
|
|
|
|
|
new_div.id = `post-${post.id}`;
|
|
|
|
|
new_div.className = "post";
|
|
|
|
|
post_text_replaced = post.text.replace(/(\r\n|\n\r|\r|\n)/g, "<br>" );
|
2022-11-27 01:34:44 +08:00
|
|
|
|
new_div.innerHTML = `
|
|
|
|
|
<img class="timeline-avatar-img" width="100" src="${post.poster_avatar}">
|
|
|
|
|
<div class="post-content">
|
|
|
|
|
<a href="/user/${post.poster_username}">${post.poster_shown_name}</a>
|
2022-11-15 23:49:04 +08:00
|
|
|
|
at <a href="/post/${post.id}" class="post-time">${post.post_time}</a><br>
|
|
|
|
|
${post_text_replaced}<br>
|
2022-11-27 01:34:44 +08:00
|
|
|
|
<span id="reply-${post.id}" data-value="${post.id}" class="reply">↩️</span>
|
|
|
|
|
- <span id="repost-${post.id}" data-value="${post.id}" class="${(post.is_reposted == "0" ? "unchecked" : "")} repost">🔁</span>
|
|
|
|
|
- <span id="fav-${post.id}" data-value="${post.id}" class="${(post.is_faved == "0" ? "unchecked" : "")} fav">⭐</span>
|
|
|
|
|
</div>`;
|
2022-11-15 23:49:04 +08:00
|
|
|
|
|
|
|
|
|
new_div_timestamp = new_div.getElementsByClassName('post-time')[0];
|
|
|
|
|
|
|
|
|
|
timezoneChangingOne(new_div_timestamp);
|
|
|
|
|
|
|
|
|
|
return new_div;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function addPostToTheBeginning(post){
|
|
|
|
|
var public_tl = $("public_timeline");
|
|
|
|
|
var new_div = createPostDiv(post);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public_tl.insertBefore(new_div, $('latest_time').nextSibling);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$("new_post_notifier").addEventListener('click', getLatestPosts);
|
|
|
|
|
$("previous_post_loader").addEventListener('click', getPreviousPosts);
|
|
|
|
|
|
2022-11-27 01:34:44 +08:00
|
|
|
|
async function makeRepostRequest(event){
|
|
|
|
|
var postId = event.target.getAttribute('data-value');
|
|
|
|
|
await fetch(`/api/repost_request/${postId}`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
"X-CSRFToken": csrf_token,
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
|
|
|
|
body : ""
|
|
|
|
|
}).then(response => response.json())
|
|
|
|
|
.then(item => item['status'])
|
|
|
|
|
.then(status =>{
|
|
|
|
|
if (status == "reposted"){
|
|
|
|
|
event.target.classList.remove("unchecked");
|
|
|
|
|
}else if (status == "deleted"){
|
|
|
|
|
event.target.classList.add("unchecked");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function makeFavRequest(event){
|
|
|
|
|
var postId = event.target.getAttribute('data-value');
|
|
|
|
|
await fetch(`/api/fav_request/${postId}`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
"X-CSRFToken": csrf_token,
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
|
|
|
|
body : ""
|
|
|
|
|
}).then(response => response.json())
|
|
|
|
|
.then(item => item['status'])
|
|
|
|
|
.then(status =>{
|
|
|
|
|
if (status == "faved"){
|
|
|
|
|
event.target.classList.remove("unchecked");
|
|
|
|
|
}else if (status == "deleted"){
|
|
|
|
|
event.target.classList.add("unchecked");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateClickedIconEvent(){
|
|
|
|
|
var reposted_items = document.getElementsByClassName('repost');
|
|
|
|
|
for (var i = 0; i < reposted_items.length; i++) {
|
|
|
|
|
reposted_items[i].addEventListener('click', makeRepostRequest);
|
|
|
|
|
}
|
|
|
|
|
var reposted_items = document.getElementsByClassName('fav');
|
|
|
|
|
for (var i = 0; i < reposted_items.length; i++) {
|
|
|
|
|
reposted_items[i].addEventListener('click', makeFavRequest);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateClickedIconEvent()
|
2022-11-15 23:49:04 +08:00
|
|
|
|
|
2022-11-26 05:01:20 +08:00
|
|
|
|
if (requestNote["type"] == "home"){
|
|
|
|
|
$("submit_post").addEventListener('click', make_req);
|
|
|
|
|
adjust_post_text();
|
|
|
|
|
}
|
|
|
|
|
if (requestNote["type"] == "user"){
|
2022-11-27 01:34:44 +08:00
|
|
|
|
if ($("follow-user-button")){
|
|
|
|
|
$("follow-user-button").addEventListener('click', make_follow_req);
|
|
|
|
|
}
|
2022-11-26 05:01:20 +08:00
|
|
|
|
}
|