Emojis \o/ and fix threads

This commit is contained in:
Thomas Sileo 2019-04-10 22:50:36 +02:00
parent e01891d364
commit c100796c86
5 changed files with 71 additions and 17 deletions

View file

@ -473,6 +473,7 @@ class MicroblogPubBackend(Backend):
# It means the activity is not in the inbox, and not in the outbox, we want to save it
self.save(Box.REPLIES, reply)
new_threads.append(reply.id)
# TODO(tsileo): parses the replies collection and import the replies?
while reply is not None:
in_reply_to = reply.inReplyTo

28
app.py
View file

@ -72,6 +72,7 @@ from config import BASE_URL
from config import DB
from config import DEBUG_MODE
from config import DOMAIN
from config import EMOJIS
from config import HEADERS
from config import ICON_URL
from config import ID
@ -91,6 +92,7 @@ from poussetaches import PousseTaches
phost = "http://" + os.getenv("COMPOSE_PROJECT_NAME", "")
p = PousseTaches(f"{phost}_poussetaches_1:7991", f"{phost}_web_1:5005")
# p = PousseTaches("http://localhost:7991", "http://localhost:5000")
back = activitypub.MicroblogPubBackend()
ap.use_backend(back)
@ -831,25 +833,22 @@ def with_replies():
)
def _build_thread(data, include_children=True):
def _build_thread(data, include_children=True): # noqa: C901
data["_requested"] = True
print(data)
root_id = data["meta"].get("thread_root_parent", data["activity"]["object"]["id"])
query = {
"$or": [
{"meta.thread_root_parent": root_id, "type": "Create"},
{"activity.object.id": root_id},
]
"meta.thread_root_parent": root_id,
"meta.deleted": False,
}
if data["activity"]["object"].get("inReplyTo"):
query["$or"].append(
{"activity.object.id": data["activity"]["object"]["inReplyTo"]}
)
# Fetch the root replies, and the children
replies = [data] + list(DB.activities.find(query))
replies = [data]
for dat in DB.activities.find(query):
if dat["type"][0] != ActivityType.CREATE.value:
# Make a Note/Question/... looks like a Create
dat = {"activity": {"object": dat["activity"]}, "meta": dat["meta"], "_id": dat["_id"]}
replies.append(dat)
replies = sorted(replies, key=lambda d: d["activity"]["object"]["published"])
# Index all the IDs in order to build a tree
idx = {}
replies2 = []
@ -1326,6 +1325,7 @@ def admin_lookup():
)
print(data)
app.logger.debug(data.to_dict())
return render_template(
"lookup.html", data=data, meta=meta, url=request.form.get("url")
)
@ -1383,7 +1383,7 @@ def admin_new():
content = f"@{actor.preferredUsername}@{domain} "
thread = _build_thread(data)
return render_template("new.html", reply=reply_id, content=content, thread=thread)
return render_template("new.html", reply=reply_id, content=content, thread=thread, emojis=EMOJIS.split(" "))
@app.route("/admin/notifications")

View file

@ -106,6 +106,10 @@ def create_indexes():
DB.command("compact", "activities")
DB.activities.create_index([("remote_id", pymongo.ASCENDING)])
DB.activities.create_index([("activity.object.id", pymongo.ASCENDING)])
DB.activities.create_index([("meta.thread_root_parent", pymongo.ASCENDING)])
DB.activities.create_index(
[("meta.thread_root_parent", pymongo.ASCENDING), ("meta.deleted", pymongo.ASCENDING)]
)
DB.activities.create_index(
[("activity.object.id", pymongo.ASCENDING), ("meta.deleted", pymongo.ASCENDING)]
)
@ -195,3 +199,6 @@ ME = {
},
"publicKey": KEY.to_dict(),
}
# TODO(tsileo): read the config from the YAML if set
EMOJIS = "😺 😸 😹 😻 😼 😽 🙀 😿 😾 😺 😸 😹 😻 😼 😽 🙀 😿 😾"

View file

@ -11,7 +11,11 @@
{% if not request.args.get("older_than") and not request.args.get("previous_than") %}<link rel="canonical" href="https://{{ config.DOMAIN }}{{ request.path }}">{% endif %}
{% block links %}{% endblock %}
{% if config.THEME_COLOR %}<meta name="theme-color" content="{{ config.THEME_COLOR }}">{% endif %}
<style>{{ config.CSS | safe }}</style>
<style>{{ config.CSS | safe }}
.emoji {
width: 20px;
}
</style>
{% block headers %}{% endblock %}
</head>
<body>
@ -39,5 +43,15 @@
Powered by <a href="https://microblog.pub">microblog.pub</a> <small class="microblogpub-version"><code>{{ microblogpub_version }}</code></small> (<a href="https://github.com/tsileo/microblog.pub">source code</a>) and the <a href="https://activitypub.rocks/">ActivityPub</a> protocol
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/twemoji@12.0.0/2/twemoji.min.js"></script>
<script>
window.onload = function() {
twemoji.base = "https://cdn.jsdelivr.net/npm/twemoji@12.0.0/2/";
twemoji.parse(document.body, {
folder: 'svg',
ext: '.svg'
});
}
</script>
</body>
</html>

View file

@ -1,6 +1,9 @@
{% extends "layout.html" %}
{% import 'utils.html' as utils %}
{% block title %}New - {{ config.NAME }}{% endblock %}
{% block headers %}
{% endblock %}
{% block content %}
<div id="container">
{% include "header.html" %}
@ -15,7 +18,14 @@
<input type="hidden" name="redirect" value="/">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
{% if reply %}<input type="hidden" name="reply" value="{{reply}}">{% endif %}
<textarea name="content" rows="10" cols="50" autofocus="autofocus">{{ content }}</textarea>
<p>
{% for emoji in emojis %}
<span class="ji">{{ emoji }}</span>
{% endfor %}
</p>
<textarea name="content" rows="10" cols="50" autofocus="autofocus" designMode="on">{{ content }}</textarea>
<input type="file" name="file">
<div style="margin-top:20px;">
<input type="submit" value="post">
@ -24,4 +34,26 @@
</div>
</div>
{% endblock %}
<script>
var ta = document.getElementsByTagName("textarea")[0];
function insertAtCursor (textToInsert) {
// get current text of the input
const value = ta.value;
// save selection start and end position
const start = ta.selectionStart;
const end = ta.selectionEnd;
// update the value with our text inserted
ta.value = value.slice(0, start) + textToInsert + value.slice(end);
// update cursor to be at the end of insertion
ta.selectionStart = ta.selectionEnd = start + textToInsert.length;
}
var ji = function (ev) {
insertAtCursor(ev.target.attributes.alt.value);
ta.focus()
//console.log(document.execCommand('insertText', false /*no UI*/, ev.target.attributes.alt.value));
}
var items = document.getElementsByClassName("ji")
for (var i = 0; i < items.length; i++) {
items[i].addEventListener('click', ji);
}
</script>{% endblock %}