mirror of
https://git.sr.ht/~tsileo/microblog.pub
synced 2024-11-15 03:04:28 +00:00
More caching
This commit is contained in:
parent
610262d7b6
commit
3e7b09b7a8
6 changed files with 64 additions and 23 deletions
|
@ -32,6 +32,15 @@ logger = logging.getLogger(__name__)
|
||||||
ACTORS_CACHE = LRUCache(maxsize=256)
|
ACTORS_CACHE = LRUCache(maxsize=256)
|
||||||
|
|
||||||
|
|
||||||
|
def _actor_to_meta(actor: ap.BaseActivity) -> Dict[str, Any]:
|
||||||
|
return {
|
||||||
|
"url": actor.url,
|
||||||
|
"icon": actor.icon,
|
||||||
|
"name": actor.name,
|
||||||
|
"preferredUsername": actor.preferredUsername,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def _remove_id(doc: ap.ObjectType) -> ap.ObjectType:
|
def _remove_id(doc: ap.ObjectType) -> ap.ObjectType:
|
||||||
"""Helper for removing MongoDB's `_id` field."""
|
"""Helper for removing MongoDB's `_id` field."""
|
||||||
doc = doc.copy()
|
doc = doc.copy()
|
||||||
|
@ -263,8 +272,15 @@ class MicroblogPubBackend(Backend):
|
||||||
{"activity.object.id": obj.id},
|
{"activity.object.id": obj.id},
|
||||||
{"$inc": {"meta.count_like": 1}, "$set": {"meta.liked": like.id}},
|
{"$inc": {"meta.count_like": 1}, "$set": {"meta.liked": like.id}},
|
||||||
)
|
)
|
||||||
|
|
||||||
DB.activities.update_one(
|
DB.activities.update_one(
|
||||||
{"remote_id": like.id}, {"$set": {"meta.object": obj.to_dict(embed=True)}}
|
{"remote_id": like.id},
|
||||||
|
{
|
||||||
|
"$set": {
|
||||||
|
"meta.object": obj.to_dict(embed=True),
|
||||||
|
"meta.object_actor": _actor_to_meta(obj.get_actor()),
|
||||||
|
}
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@ensure_it_is_me
|
@ensure_it_is_me
|
||||||
|
@ -291,7 +307,12 @@ class MicroblogPubBackend(Backend):
|
||||||
obj = announce.get_object()
|
obj = announce.get_object()
|
||||||
DB.activities.update_one(
|
DB.activities.update_one(
|
||||||
{"remote_id": announce.id},
|
{"remote_id": announce.id},
|
||||||
{"$set": {"meta.object": obj.to_dict(embed=True)}},
|
{
|
||||||
|
"$set": {
|
||||||
|
"meta.object": obj.to_dict(embed=True),
|
||||||
|
"meta.object_actor": _actor_to_meta(obj.get_actor()),
|
||||||
|
}
|
||||||
|
},
|
||||||
)
|
)
|
||||||
DB.activities.update_one(
|
DB.activities.update_one(
|
||||||
{"activity.object.id": obj.id}, {"$inc": {"meta.count_boost": 1}}
|
{"activity.object.id": obj.id}, {"$inc": {"meta.count_boost": 1}}
|
||||||
|
@ -313,8 +334,14 @@ class MicroblogPubBackend(Backend):
|
||||||
obj = announce.get_object()
|
obj = announce.get_object()
|
||||||
DB.activities.update_one(
|
DB.activities.update_one(
|
||||||
{"remote_id": announce.id},
|
{"remote_id": announce.id},
|
||||||
{"$set": {"meta.object": obj.to_dict(embed=True)}},
|
{
|
||||||
|
"$set": {
|
||||||
|
"meta.object": obj.to_dict(embed=True),
|
||||||
|
"meta.object_actor": _actor_to_meta(obj.get_actor()),
|
||||||
|
}
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
DB.activities.update_one(
|
DB.activities.update_one(
|
||||||
{"activity.object.id": obj.id}, {"$set": {"meta.boosted": announce.id}}
|
{"activity.object.id": obj.id}, {"$set": {"meta.boosted": announce.id}}
|
||||||
)
|
)
|
||||||
|
|
25
app.py
25
app.py
|
@ -679,6 +679,31 @@ def tmp_migrate4():
|
||||||
def tmp_migrate5():
|
def tmp_migrate5():
|
||||||
for activity in DB.activities.find():
|
for activity in DB.activities.find():
|
||||||
tasks.cache_actor.delay(activity["remote_id"], also_cache_attachments=False)
|
tasks.cache_actor.delay(activity["remote_id"], also_cache_attachments=False)
|
||||||
|
|
||||||
|
return "Done"
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/migration5")
|
||||||
|
@login_required
|
||||||
|
def tmp_migrate6():
|
||||||
|
for activity in DB.activities.find():
|
||||||
|
# tasks.cache_actor.delay(activity["remote_id"], also_cache_attachments=False)
|
||||||
|
try:
|
||||||
|
a = ap.parse_activity(activity["activity"])
|
||||||
|
if a.has_type([ActivityType.LIKE, ActivityType.FOLLOW]):
|
||||||
|
DB.activities.update_one(
|
||||||
|
{"remote_id": a.id},
|
||||||
|
{
|
||||||
|
"$set": {
|
||||||
|
"meta.object_actor": activitypub._actor_to_meta(
|
||||||
|
a.get_object().get_actor()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
app.logger.exception(f"processing {activity} failed")
|
||||||
|
|
||||||
return "Done"
|
return "Done"
|
||||||
|
|
||||||
|
|
||||||
|
|
13
tasks.py
13
tasks.py
|
@ -2,8 +2,6 @@ import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
from typing import Dict
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from celery import Celery
|
from celery import Celery
|
||||||
|
@ -64,15 +62,6 @@ def process_new_activity(self, iri: str) -> None:
|
||||||
self.retry(exc=err, countdown=int(random.uniform(2, 4) ** self.request.retries))
|
self.retry(exc=err, countdown=int(random.uniform(2, 4) ** self.request.retries))
|
||||||
|
|
||||||
|
|
||||||
def _actor_to_meta(actor: ap.BaseActivity) -> Dict[str, Any]:
|
|
||||||
return {
|
|
||||||
"url": actor.url,
|
|
||||||
"icon": actor.icon,
|
|
||||||
"name": actor.name,
|
|
||||||
"preferredUsername": actor.preferredUsername,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@app.task(bind=True, max_retries=12)
|
@app.task(bind=True, max_retries=12)
|
||||||
def cache_actor(self, iri: str, also_cache_attachments: bool = True) -> None:
|
def cache_actor(self, iri: str, also_cache_attachments: bool = True) -> None:
|
||||||
try:
|
try:
|
||||||
|
@ -83,7 +72,7 @@ def cache_actor(self, iri: str, also_cache_attachments: bool = True) -> None:
|
||||||
|
|
||||||
# Cache the actor info
|
# Cache the actor info
|
||||||
DB.activities.update_one(
|
DB.activities.update_one(
|
||||||
{"remote_id": iri}, {"$set": {"meta.actor": _actor_to_meta(actor)}}
|
{"remote_id": iri}, {"$set": {"meta.actor": activitypub._actor_to_meta(actor)}}
|
||||||
)
|
)
|
||||||
|
|
||||||
log.info(f"actor cached for {iri}")
|
log.info(f"actor cached for {iri}")
|
||||||
|
|
|
@ -24,7 +24,7 @@
|
||||||
{% for item in outbox_data %}
|
{% for item in outbox_data %}
|
||||||
|
|
||||||
{% if item | has_type('Announce') %}
|
{% if item | has_type('Announce') %}
|
||||||
{% set boost_actor = item.activity.actor | get_actor %}
|
{% set boost_actor = item.meta.actor %}
|
||||||
{% if session.logged_in %}
|
{% if session.logged_in %}
|
||||||
<div style="margin-left:65px;padding-bottom:5px;margin-bottom:15px;">
|
<div style="margin-left:65px;padding-bottom:5px;margin-bottom:15px;">
|
||||||
<span class="bar-item-no-hover"><a style="color:#808080;" href="{{ boost_actor.url | get_url }}">{{ boost_actor.name }}</a> boosted</span>
|
<span class="bar-item-no-hover"><a style="color:#808080;" href="{{ boost_actor.url | get_url }}">{{ boost_actor.name }}</a> boosted</span>
|
||||||
|
@ -41,7 +41,7 @@
|
||||||
</p>
|
</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if item.meta.object %}
|
{% if item.meta.object %}
|
||||||
{{ utils.display_note(item.meta.object, ui=False) }}
|
{{ utils.display_note(item.meta.object, ui=False, meta={'actor': item.meta.object_actor}) }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% elif item | has_type('Create') %}
|
{% elif item | has_type('Create') %}
|
||||||
{{ utils.display_note(item.activity.object, meta=item.meta, no_color=True) }}
|
{{ utils.display_note(item.activity.object, meta=item.meta, no_color=True) }}
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if item.meta.object %}
|
{% if item.meta.object %}
|
||||||
{{ utils.display_note(item.meta.object) }}
|
{{ utils.display_note(item.meta.object, meta={'actor': item.meta.object_actor}) }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{% extends "layout.html" %}
|
{% extends "layout.html" %}
|
||||||
{% import 'utils.html' as utils %}
|
{% import 'utils.html' as utils %}
|
||||||
{% block title %}Stream - {{ config.NAME }}{% endblock %}
|
{% block title %}{% if request.path == url_for('admin_stream') %}Stream{% else %}Notifications{% endif %} - {{ config.NAME }}{% endblock %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<div class="h-feed" id="container">
|
<div class="h-feed" id="container">
|
||||||
{% include "header.html" %}
|
{% include "header.html" %}
|
||||||
|
@ -13,7 +13,7 @@
|
||||||
{% else %}
|
{% else %}
|
||||||
|
|
||||||
{% if item | has_type('Announce') %}
|
{% if item | has_type('Announce') %}
|
||||||
{% set boost_actor = item.activity.actor | get_actor %}
|
{% set boost_actor = item.meta.actor %}
|
||||||
<p style="margin-left:70px;padding-bottom:5px;"><span class="bar-item-no-hover"><a style="color:#808080;" href="{{ boost_actor.url | get_url }}">{{ boost_actor.name or boost_actor.preferredUsername }}</a> boosted</span></p>
|
<p style="margin-left:70px;padding-bottom:5px;"><span class="bar-item-no-hover"><a style="color:#808080;" href="{{ boost_actor.url | get_url }}">{{ boost_actor.name or boost_actor.preferredUsername }}</a> boosted</span></p>
|
||||||
{% if item.meta.object %}
|
{% if item.meta.object %}
|
||||||
{{ utils.display_note(item.meta.object, ui=True) }}
|
{{ utils.display_note(item.meta.object, ui=True) }}
|
||||||
|
@ -23,19 +23,19 @@
|
||||||
{% if item | has_type('Follow') %}
|
{% if item | has_type('Follow') %}
|
||||||
<p style="margin-left:70px;padding-bottom:5px;"><span class="bar-item-no-hover">new follower</span> <!-- <a href="" class="bar-item">follow back</a></p> -->
|
<p style="margin-left:70px;padding-bottom:5px;"><span class="bar-item-no-hover">new follower</span> <!-- <a href="" class="bar-item">follow back</a></p> -->
|
||||||
<div style="height: 100px;">
|
<div style="height: 100px;">
|
||||||
{{ utils.display_actor_inline(item.activity.actor | get_actor, size=50) }}
|
{{ utils.display_actor_inline(item.meta.actor, size=50) }}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% elif item | has_type('Accept') %}
|
{% elif item | has_type('Accept') %}
|
||||||
<p style="margin-left:70px;padding-bottom:5px;"><span class="bar-item-no-hover">you started following</span></p>
|
<p style="margin-left:70px;padding-bottom:5px;"><span class="bar-item-no-hover">you started following</span></p>
|
||||||
<div style="height: 100px;">
|
<div style="height: 100px;">
|
||||||
{{ utils.display_actor_inline(item.activity.actor | get_actor, size=50) }}
|
{{ utils.display_actor_inline(item.meta.actor, size=50) }}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% elif item | has_type('Undo') %}
|
{% elif item | has_type('Undo') %}
|
||||||
<p style="margin-left:70px;padding-bottom:5px;"><span class="bar-item-no-hover">unfollowed you</span></p>
|
<p style="margin-left:70px;padding-bottom:5px;"><span class="bar-item-no-hover">unfollowed you</span></p>
|
||||||
<div style="height: 100px;">
|
<div style="height: 100px;">
|
||||||
{{ utils.display_actor_inline(item.activity.actor | get_actor, size=50) }}
|
{{ utils.display_actor_inline(item.meta.actor, size=50) }}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% else %}
|
{% else %}
|
||||||
|
|
Loading…
Reference in a new issue