var thread_html = ""; var top_id = ""; var answers = {}; $(function() { var jumpLocation = parseLocation(); // Cleanup content if (jumpLocation['matched']) { // Cleanup content $("#mes").html(''); findTopMessage(jumpLocation["hash"], false); } }) function topMessageRequest(data, id) { $.ajax({ url:"https://dynamic.lessmore.pw/search", type:"POST", data: data, contentType: "application/json", success: function(messages) { findTopMessage(id, messages); // $("#mes").html(thread_html); // $("#preloader").remove(); } }) } function makeDataString(id) { console.log("ID " + id) var str = '{"sort": [ { "date": { "order": "desc" }}, { "_score": { "order": "desc" }}], "query": {"query_string" : {"fields" : ["msgid"], "query" :"' + id + '"}}, "size": 1 }'; return str } function findTopMessage(id, callback) { data = makeDataString(id); if (!callback) { thread_html = ""; topMessageRequest(data, id); } else { if (callback["hits"]["hits"].length > 0) { repto = callback["hits"]["hits"][0]["_source"]["repto"]; msgid = callback["hits"]["hits"][0]["_source"]["msgid"]; if (repto) { topMessageRequest(makeDataString(repto)); } else { thread_html += makeMessageHTML(callback["hits"]["hits"][0]["_source"]); top_id = msgid; console.log("Top id found! " + top_id); proccessThread(msgid); } } } } function proccessThreadRequest(data, msgid) { $.ajax({ url:"https://dynamic.lessmore.pw/search", type:"POST", data: data, contentType: "application/json", success: function(messages) { proccessThread(msgid, messages); $("#mes").html(thread_html); $("#preloader").remove(); } }) } function proccessThread(msgid, callback) { data = '{"sort": [ { "date": { "order": "asc" }}, { "_score": { "order": "desc" }}], "query": {"query_string" : {"fields" : ["repto"], "query" :"' + msgid + '"}}, "size": 100 }'; if (callback) { for (hit in callback["hits"]["hits"]) { msg = callback["hits"]["hits"][hit]["_source"]; thread_html += makeMessageHTML(callback["hits"]["hits"][0]["_source"]); proccessThread(msg["msgid"], false); } } else { proccessThreadRequest(data, msgid); } } // Parsing functions function parseLocation() { var h = location.search.replace(/\?j=/, ""); if (h) { console.log("Matched jump to " + h); return {matched: true, hash: h}; } else { return {matched: false}; } }