dynamic/js/threads.js
2018-01-02 22:22:01 +03:00

133 lines
4.6 KiB
JavaScript

var thread_html = "";
var top_id = "";
$(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};
}
}
// 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, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(new RegExp("\n",'g'), "<br>\n");
var html = '<div class="msg">'
+ '<p align="left"><i class="fa fa-hourglass-half"></i> ' + t_formatted + '<i class="fa fa-angle-right"></i> <!--/p-->'
// Author to receiver
+ '<a name="' + msgItem["msgid"] + '" style="text-decoration:underline; text-decoration-style:dotted;" title="' + msgItem["address"] + '" "href="javascript:void(0)" onclick="requestAuthorMessages(\'' + msgItem['author'] + '\');">' + msgItem['author'] + '</a>'
+ ' <i class="fa fa-mail-forward"></i> ';
if (msgItem['repto'] != "" ) {
html += '<a href="#' + msgItem['repto'] + '">' + msgItem['to'] + '</a></p>';
} else {
html += msgItem['to'] + '</p>';
}
// Header
html += ' <p style="color:#0099cc;font: 1.5em Open Sans, sans-serif;">'
+ '<a class="headerLink" href="/?j=' + msgItem['msgid'] + '">'
+ msgItem['subg'] + '</a>'
+ ' <i class="fa fa-at"></i> <i><a class="headerLink" href="javascript:void(0)" onclick="requestTmpEcho(\'' + msgItem['echo'] + '\')">' + msgItem['echo']
+ '</a></i></p></p>'
+ '<p>' + msg.autoLink()
+ '</p><p align="right">'
// Msgid
+ '<a href="?' + msgItem['msgid'] +'" onclick="requestMessage(\'' + msgItem['msgid'] + '\');"><i class="fa fa-link"></i>' + msgItem['msgid'] + '</a>';
// End message <div> block
html += '</p></div>';
return html
}