thread_updater.js: listen to ajax_after_post event

This commit is contained in:
towards-a-new-leftypol 2024-02-26 13:55:36 -05:00 committed by 0
parent 61b93aa927
commit da03753a72
5 changed files with 77 additions and 46 deletions

View File

@ -384,6 +384,7 @@ class Post extends PostProps {
private $raw_body;
private $has_file;
private $tracked_cites;
private $filesize;
public function __construct($post, $root=null, $mod=false) {
global $config;

View File

@ -255,7 +255,7 @@ globalThis.LCNThread = class LCNThread {
this.#element = thread
this.#op = LCNPost.assign(this.#element.querySelector(".post.op"))
assert.equal(this.#op.getParent(), null, "Op should not have parent.")
//assert.equal(this.#op.getParent(), null, "Op should not have parent.")
this.#op.__setParent(this)
}
@ -401,10 +401,7 @@ globalThis.LCNToggleSetting = class LCNToggleSetting extends LCNSetting {
const div = document.createElement("div")
const chk = document.createElement("input")
const txt = document.createElement("label")
const id = `${this.#id}_input`
txt.for = id
txt.innerText = this.getLabel()
chk.id = id
chk.type = "checkbox"
chk.checked = this.getValue()
chk.addEventListener("click", e => {

View File

@ -35,7 +35,7 @@ $().ready(() => {
}
const updateSecondsByTSLP = post_info => {
secondsCounter = Math.floor(((Date.now() - post_info.getCreatedAt().getTime()) / 120000))
secondsCounter = Math.floor(((Date.now() - post_info.getCreatedAt().getTime()) / 30000))
secondsCounter = secondsCounter > 1000 ? 1000 : secondsCounter
secondsCounter = secondsCounter < 11 ? 11 : secondsCounter
}
@ -58,11 +58,8 @@ $().ready(() => {
}
}
const updateThreadFn = async (thread) => {
const handleThreadUpdate = async (thread) => {
const threadPost = thread.getContent()
const threadReplies = thread.getReplies()
const lastPostC = threadReplies.at(-1).getParent()
const lastPostTs = lastPostC.getContent().getInfo().getCreatedAt().getTime()
const res = await fetch(location.href, {
"signal": abortable.signal
@ -71,29 +68,7 @@ $().ready(() => {
if (res.ok) {
const dom = parser.parseFromString(await res.text(), "text/html")
const livePCList = Array.prototype.map.apply(dom.querySelectorAll(`#thread_${threadPost.getInfo().getThreadId()} > .postcontainer`), [ pc => LCNPostContainer.assign(pc) ])
const documentPCList = [ threadPost, ...threadReplies.map(p => p.getParent()) ]
const missingPCList = []
for (const pc of livePCList.reverse()) {
if (pc.getContent().getInfo().getCreatedAt().getTime() > lastPostTs) {
missingPCList.unshift(pc)
} else {
break
}
}
if (missingPCList.length) {
for (const pc of missingPCList) {
documentPCList.at(-1).getElement().after(pc.getElement())
documentPCList.push(pc)
}
for (const pc of missingPCList) {
$(document).trigger("new_post", [ pc.getContent().getElement() ])
}
LCNSite.INSTANCE.setUnseen(LCNSite.INSTANCE.getUnseen() + missingPCList.length)
}
updateThreadFn(thread, livePCList);
} else if (res.status == 404) {
threadState = String(res.status)
} else {
@ -101,6 +76,39 @@ $().ready(() => {
}
}
function updateThreadFn(thread, lcn_pc_list) {
const threadPost = thread.getContent()
const threadReplies = thread.getReplies()
const lastPostC = threadReplies.at(-1).getParent()
const lastPostTs = lastPostC.getContent().getInfo().getCreatedAt().getTime()
const livePCList = lcn_pc_list;
const documentPCList = [ threadPost, ...threadReplies.map(p => p.getParent()) ]
const missingPCList = []
for (const pc of livePCList.reverse()) {
if (pc.getContent().getInfo().getCreatedAt().getTime() > lastPostTs) {
missingPCList.unshift(pc)
} else {
break
}
}
if (missingPCList.length) {
for (const pc of missingPCList) {
documentPCList.at(-1).getElement().after(pc.getElement())
documentPCList.push(pc)
}
for (const pc of missingPCList) {
$(document).trigger("new_post", [ pc.getContent().getElement() ])
}
LCNSite.INSTANCE.setUnseen(LCNSite.INSTANCE.getUnseen() + missingPCList.length)
}
}
const onTickClean = () => {
if (onTickId != null) {
clearTimeout(onTickId)
@ -121,7 +129,7 @@ $().ready(() => {
try {
await updateStatsFn(thread)
if (threadState == null && threadStats.last_modified > (thread.getReplies().at(-1).getInfo().getCreatedAt().getTime() / 1000)) {
await updateThreadFn(thread)
await handleThreadUpdate(thread)
}
const threadEl = thread.getElement()
@ -140,13 +148,6 @@ $().ready(() => {
}
}
$(document).on("thread_manual_refresh", () => {
if (secondsCounter >= 0) {
secondsCounter = 0
onTickFn()
}
})
let floaterLinkBox = null
const onStateChangeFn = v => {
onTickClean()
@ -163,7 +164,10 @@ $().ready(() => {
threadUpdateStatus.innerText = "…"
threadUpdateLink.addEventListener("click", e => {
e.preventDefault()
$(document).trigger("thread_manual_refresh")
if (secondsCounter >= 0) {
secondsCounter = 0
onTickFn()
}
})
threadUpdateLink.href = "#"
threadUpdateLink.appendChild(new Text("Refresh: "))
@ -224,5 +228,30 @@ $().ready(() => {
kIsEnabled.onChange(onStateChangeFn)
onStateChangeFn(kIsEnabled.getValue())
$(document).on("ajax_after_post", onNewPost);
function onNewPost(_, post_response) {
if (post_response == null) {
console.log("onNewPost data is null, can't do anything.");
return;
}
const post_dom = parser.parseFromString(
post_response['post'],
"text/html");
const post_elem = post_dom.querySelector(".postcontainer");
console.log(post_elem);
const post_container = LCNPostContainer.assign(post_elem);
console.log("lcn_post_container:", post_container);
const thread_id_sel = "#thread_" + post_response['thread_id'];
const thread = document.querySelector(thread_id_sel);
console.log("thread:", thread);
const lcn_thread = new LCNThread(thread);
console.log("lcn_thread: ", lcn_thread);
updateThreadFn(lcn_thread, [ post_container ]);
}
}
})

View File

@ -8,7 +8,8 @@ const assert = {
if (actual !== expected) {
const err = new Error(`Assertion Failed. ${message}`)
err.data = { actual, expected}
Error.captureStackTrace(err, assert.equal)
// Seems like there's no such thing as captureStackTrace in firefox?
//Error.captureStackTrace(err, assert.equal)
debugger
throw err
}
@ -17,7 +18,7 @@ const assert = {
if (!actual) {
const err = new Error(`Assertion Failed. ${message}`)
err.data = { actual }
Error.captureStackTrace(err, assert.ok)
// Error.captureStackTrace(err, assert.ok)
debugger
throw err
}

View File

@ -1475,7 +1475,9 @@ function handle_post(){
}
buildThread($post['op'] ? $id : $post['thread']);
$thread_id = $post['op'] ? $id : $post['thread'];
buildThread($thread_id);
if ($config['syslog'])
_syslog(LOG_INFO, 'New post: /' . $board['dir'] . $config['dir']['res'] .
@ -1487,12 +1489,13 @@ function handle_post(){
header('Location: ' . $redirect, true, $config['redirect_http']);
} else {
header('Content-Type: text/json; charset=utf-8');
$api = new Api();
echo json_encode(array(
'redirect' => $redirect,
'noko' => $noko,
'id' => $id,
'post' => $api->translatePost(new Post($post))
'post' => (new Post($post))->build(),
'thread_id' => $thread_id
));
}