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 answersRequest()
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"]) {
if (top_id == callback["hits"]["hits"][hit]["_source"]) {
answersRequest(top_id);
}
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};
}
}
// Make message HTML
function makeMessageHTML(msgItem) {
var t = new Date(msgItem['date'] * 1000);
// console.log(msgItem['date']);
// console.log(t);
var t_formatted = t.getFullYear() + "/" + (t.getMonth() + 1) + "/" + t.getDate() + " " + t.getHours() + ":" + t.getMinutes() + ":" + t.getSeconds();
var msg = msgItem['message'].replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"').replace(new RegExp("\n",'g'), "
\n");
var html = '
' + t_formatted + ' ' // Author to receiver + '' + msgItem['author'] + '' + ' '; if (msgItem['repto'] != "" ) { html += '' + msgItem['to'] + '
'; } else { html += msgItem['to'] + ''; } // Header html += '' + '' + msgItem['subg'] + '' + ' ' + msgItem['echo'] + '
' + '' + msg.autoLink() + '
' // Msgid + '' + msgItem['msgid'] + ''; // End message