From 2b65d888deb5a50003d0e496c1e7a61b2614f5ca Mon Sep 17 00:00:00 2001 From: ketmar Date: Sun, 31 Oct 2021 01:47:56 +0000 Subject: [PATCH] show whole ancient threads if somebody was answered FossilOrigin-Name: e14a5e5e360de02ff560fcd626f46f149220c86052b5715744d2313032224208 --- chibackend/sqbase.d | 56 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/chibackend/sqbase.d b/chibackend/sqbase.d index 3adf246..ab59444 100644 --- a/chibackend/sqbase.d +++ b/chibackend/sqbase.d @@ -3653,9 +3653,24 @@ if (is(T:const(char)[]) || is(T:uint)) `); */ + // this need to add answers to some ancient crap + static auto statFirstUnreadTime = LazyStatement!"View"(` + SELECT MIN(time) AS time, parent AS parent + FROM threads + WHERE tagid=:tagidname AND appearance=:app + ;`); + + static auto statFindParentFor = LazyStatement!"View"(` + SELECT time AS time, parent AS parent + FROM threads + WHERE tagid=:tagidname AND uid=:uid + LIMIT 1 + ;`); + // clear it (should be faster than dropping and recreating) dbView.execute(`DELETE FROM treepane;`); + // this "%08X" will do up to 2038; i'm fine with it static auto statTrd = LazyStatement!"View"(` INSERT INTO treepane (uid, level, tagid) @@ -3693,18 +3708,6 @@ if (is(T:const(char)[]) || is(T:uint)) threads.time ;`); - // this need to add answers to some ancient crap - static auto statFuckMore = LazyStatement!"View"(` - INSERT INTO treepane - (uid, level, tagid) - SELECT - uid, 0, :tagidname - FROM threads - WHERE - tagid=:tagidname AND time>=:starttime AND appearance=:app - AND NOT EXISTS(SELECT 1 FROM treepane AS tp WHERE tp.uid=threads.uid) - ;`); - static if (is(T:const(char)[])) { immutable uint tid = chiroGetTagUid(tagidname); if (!tid) return 0; @@ -3746,10 +3749,35 @@ if (is(T:const(char)[]) || is(T:uint)) } } - // this "%08X" will do up to 2038; i'm fine with it + // check if we need to fix unread time + // required to show the whole ancient thread if somebody answered + if (startTime > 0) { + uint unTime = 0; + uint unParent = 0; + foreach (auto row; statFirstUnreadTime.st.bind(":tagidname", tid).bind(":app", Appearance.Unread).range) { + unTime = row.time!uint; + unParent = row.parent!uint; + } + if (unTime > 0 && unTime < startTime) { + // find root message, and start from it + startTime = unTime; + while (unParent && allowThreading) { + statFindParentFor.st + .bind(":tagidname", tid) + .bind(":uid", unParent); + unParent = 0; + unTime = 0; + foreach (auto row; statFindParentFor.st.range) { + unTime = row.time!uint; + unParent = row.parent!uint; + } + if (unTime > 0 && unTime < startTime) startTime = unTime; + } + } + } + if (allowThreading) { statTrd.st.bind(":tagidname", tid).bind(":starttime", startTime).doAll(); - statFuckMore.st.bind(":tagidname", tid).bind(":starttime", startTime).bind(":app", Appearance.Unread).doAll(); } else { statNoTrd.st.bind(":tagidname", tid).bind(":starttime", startTime).doAll(); } -- 2.11.4.GIT