/** * * @source: https://git.0ut0f.space/doesnm/mxblog/src/branch/main/blog.js * * @licstart The following is the entire license notice for the * JavaScript code in this page. * * Copyright (C) 2014 Loic J. Duros * * * The JavaScript code in this page is free software: you can * redistribute it and/or modify it under the terms of the GNU * General Public License (GNU GPL) as published by the Free Software * Foundation, either version 3 of the License, or (at your option) * any later version. The code is distributed WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU GPL for more details. * * As additional permission under GNU GPL version 3 section 7, you * may distribute non-source (e.g., minimized or compacted) forms of * that code without the copy of the GNU GPL normally required by * section 4, provided you include this license notice and a URL * through which recipients can access the Corresponding Source. * * @licend The above is the entire license notice * for the JavaScript code in this page. * */ let posts = []; const table = document.querySelector("table"); function getReactions(posts,p){ let reactions = []; const preReactions = posts.filter(f => f.type == "m.reaction" && f.content["m.relates_to"].event_id == p.event_id).map(m => m.content["m.relates_to"]); preReactions.forEach(pr => { let r2 = reactions.find(f => f.key == pr.key); if(!r2) return reactions.push({ key: pr.key, count: 1 }); reactions.find(f => f.key == pr.key).count++; }); return '
' + reactions.map(m => `${m.count > 1 ? m.count: ''} ${DOMPurify.sanitize(m.key)}`).join("") + "
"; } if(!localStorage.access_token){ fetch("https://matrix.org/_matrix/client/v3/register?kind=guest", {method: "post", body: "{}"}).then(r => r.json()).then(json => { if(!json.access_token) console.log(json); localStorage.access_token = json.access_token; }); } function printPost(post,roomId,noButton){ if(post.type != "m.room.message") return false; //console.log(post) let body = DOMPurify.sanitize(post.content.formatted_body ?? post.content.body); body = body.split("\n").join("
"); // body = body.replace(/mxc\:\/\//, "https://matrix.org/_matrix/media/v3/download/") table.innerHTML += `${post.sender}
${body}${post.content.msgtype == "m.image" ? `
` : ''}${getReactions(posts,post)}${!noButton ? `
Открыть` : ''}`; } function loadPosts(roomId,start){ const ignored_events = []; posts = []; table.innerHTML = `Контент`; if(posts.length < 1 || start != undefined){ fetch("https://matrix.org/_matrix/client/v3/rooms/" + encodeURIComponent(roomId) + "/messages?limit=50&access_token=" + localStorage.access_token + (start ? `&from=${start}`: '') + "&dir=b").then(r => r.json()).then(json => { if(json.errcode == "M_FORBIDDEN"){ table.innerHTML = `ЗапрещеноГостевой аккаунт не может получить доступ к этой комнате`; return false; } json.chunk.forEach(c => { if(!["m.room.message","m.reaction"].includes(c.type)) return; if(c.redacted_because) return; if(ignored_events.includes(c.event_id)) return; if(c.type == "m.room.message"){ if(c.content.formatted_body != undefined){ c.content.body = ""; if(c.content["m.new_content"]) c.content["m.new_content"].body = ""; } } if(c.content.formatted_body != undefined){ c.content.body = ""; if(c.content["m.new_content"]) c.content["m.new_content"].body = ""; } if(c.content["m.new_content"]){ if(ignored_events.includes(c.content["m.relates_to"].event_id)) return; ignored_events.push(c.content["m.relates_to"].event_id); c.content = c.content["m.new_content"]; } posts.push(c); }) posts.forEach((p,i,a) => { printPost(p,roomId); }); if(json.end) table.innerHTML += ``; }) } } window.onhashchange = () => { let hash = location.hash.slice(1); if(hash.startsWith("!") && !hash.includes("/")){ return loadPosts(hash,undefined); }else if(('#' + hash).match(/\#(.*)\:(.*)\.(\w*)/)){ fetch(`https://matrix.org/_matrix/client/v3/directory/room/%23${hash.replace(":","%3A")}`).then(r => r.json()).then(json => { if(!json.room_id){ table.innerHTML = `Ошибка!Алиас не найден`; }else{ loadPosts(json.room_id) } }) } if(hash.includes("/")){ hash = hash.split("/"); fetch(`https://matrix.org/_matrix/client/v3/rooms/${hash[0]}/context/${hash[1]}?limit=0`, { headers: { Authorization: `Bearer ${localStorage.access_token}` } }).then(r => r.json()).then(json => { if(json.event){ const post = json.event; table.innerHTML = `Контент`; printPost(post,hash[0],true); } }); }else{ table.innerHTML = ` Добро пожаловать Это блог работающий поверх децентрализованного мессенджера Matrix Известные блоги: `; } } window.addEventListener("load", () => { window.onhashchange(); });