Improve poll/question support

This commit is contained in:
Thomas Sileo 2019-07-07 17:48:45 +02:00
parent 7048de7ba5
commit e24da84985
4 changed files with 43 additions and 15 deletions

View file

@ -287,6 +287,9 @@ class MicroblogPubBackend(Backend):
@ensure_it_is_me
def outbox_like(self, as_actor: ap.Person, like: ap.Like) -> None:
obj = like.get_object()
if obj.has_type(ap.ActivityType.QUESTION):
Tasks.fetch_remote_question(obj)
DB.activities.update_one(
{"activity.object.id": obj.id},
{"$inc": {"meta.count_like": 1}, "$set": {"meta.liked": like.id}},
@ -313,6 +316,9 @@ class MicroblogPubBackend(Backend):
)
return
if obj.has_type(ap.ActivityType.QUESTION):
Tasks.fetch_remote_question(obj)
DB.activities.update_one(
{"remote_id": announce.id},
{
@ -340,6 +346,9 @@ class MicroblogPubBackend(Backend):
@ensure_it_is_me
def outbox_announce(self, as_actor: ap.Person, announce: ap.Announce) -> None:
obj = announce.get_object()
if obj.has_type(ap.ActivityType.QUESTION):
Tasks.fetch_remote_question(obj)
DB.activities.update_one(
{"remote_id": announce.id},
{
@ -478,12 +487,7 @@ class MicroblogPubBackend(Backend):
# local copy)
question = create.get_object()
if question.has_type(ap.ActivityType.QUESTION):
now = datetime.now(timezone.utc)
dt = parser.parse(question.closed or question.endTime).astimezone(
timezone.utc
)
minutes = int((dt - now).total_seconds() / 60)
Tasks.fetch_remote_question(create.id, minutes)
Tasks.fetch_remote_question(question)
self._handle_replies(as_actor, create)

28
app.py
View file

@ -3018,12 +3018,20 @@ def task_fetch_remote_question():
try:
app.logger.info(f"Fetching remote question {iri}")
local_question = DB.activities.find_one(
{"box": Box.INBOX.value, "remote_id": iri}
{
"box": Box.INBOX.value,
"type": ActivityType.CREATE.value,
"activity.object.id": iri,
}
)
remote_question = get_backend().fetch_iri(iri, no_cache=True)
if (
local_question["meta"].get("voted_for")
or local_question["meta"]["subscribed"]
local_question
and (
local_question["meta"].get("voted_for")
or local_question["meta"]["subscribed"]
)
and not DB.notifications.find_one({"activity.id": remote_question["id"]})
):
DB.notifications.insert_one(
{
@ -3033,9 +3041,17 @@ def task_fetch_remote_question():
}
)
DB.activities.update_one(
{"remote_id": iri, "box": Box.INBOX.value},
{"$set": {"activity": remote_question}},
# Update the Create if we received it in the inbox
if local_question:
DB.activities.update_one(
{"remote_id": local_question["remote_id"], "box": Box.INBOX.value},
{"$set": {"activity.object": remote_question}},
)
# Also update all the cached copies (Like, Announce...)
DB.activities.update_many(
{"meta.object.id": remote_question["id"]},
{"$set": {"activity.object": remote_question}},
)
except HTTPError as err:

View file

@ -1,4 +1,8 @@
import os
from datetime import datetime
from datetime import timezone
from dateutil import parser
from poussetaches import PousseTaches
@ -55,7 +59,11 @@ class Tasks:
) # XXX: delay expects minutes
@staticmethod
def fetch_remote_question(iri: str, delay: int) -> None:
def fetch_remote_question(question) -> None:
now = datetime.now(timezone.utc)
dt = parser.parse(question.closed or question.endTime).astimezone(timezone.utc)
minutes = int((dt - now).total_seconds() / 60)
p.push(
iri, "/task/fetch_remote_question", delay=delay
question.id, "/task/fetch_remote_question", delay=minutes
) # XXX: delay expects minutes

View file

@ -58,7 +58,7 @@
{% if item | has_type('question_ended') %}
<p style="margin-left:70px;padding-bottom:5px;display:inline-block;"><span class="bar-item-no-hover">poll ended</span></p>
{{ utils.display_note(item.activity.object) }}
{{ utils.display_note(item.activity) }}
{% endif %}
{% endif %}