2019-07-04 16:23:40 -05:00
|
|
|
import os
|
2019-07-07 10:48:45 -05:00
|
|
|
from datetime import datetime
|
|
|
|
from datetime import timezone
|
2019-08-17 03:27:20 -05:00
|
|
|
from typing import Any
|
|
|
|
from typing import Dict
|
2019-07-07 10:48:45 -05:00
|
|
|
|
2019-07-04 16:23:40 -05:00
|
|
|
from poussetaches import PousseTaches
|
2019-08-01 12:55:30 -05:00
|
|
|
|
2019-08-11 06:56:18 -05:00
|
|
|
from config import MEDIA_CACHE
|
2019-07-12 17:38:51 -05:00
|
|
|
from utils import parse_datetime
|
2019-07-04 16:23:40 -05:00
|
|
|
|
|
|
|
p = PousseTaches(
|
|
|
|
os.getenv("MICROBLOGPUB_POUSSETACHES_HOST", "http://localhost:7991"),
|
|
|
|
os.getenv("MICROBLOGPUB_INTERNAL_HOST", "http://localhost:5000"),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class Tasks:
|
|
|
|
@staticmethod
|
|
|
|
def cache_object(iri: str) -> None:
|
|
|
|
p.push(iri, "/task/cache_object")
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def cache_actor(iri: str, also_cache_attachments: bool = True) -> None:
|
|
|
|
p.push(
|
|
|
|
{"iri": iri, "also_cache_attachments": also_cache_attachments},
|
|
|
|
"/task/cache_actor",
|
|
|
|
)
|
|
|
|
|
2019-08-11 05:07:30 -05:00
|
|
|
@staticmethod
|
2019-08-11 06:56:18 -05:00
|
|
|
def cache_actor_icon(icon_url: str, actor_iri: str) -> None:
|
|
|
|
if MEDIA_CACHE.is_actor_icon_cached(icon_url):
|
|
|
|
return None
|
|
|
|
|
2019-08-11 05:07:30 -05:00
|
|
|
p.push({"icon_url": icon_url, "actor_iri": actor_iri}, "/task/cache_actor_icon")
|
|
|
|
|
2019-07-04 16:23:40 -05:00
|
|
|
@staticmethod
|
|
|
|
def post_to_remote_inbox(payload: str, recp: str) -> None:
|
|
|
|
p.push({"payload": payload, "to": recp}, "/task/post_to_remote_inbox")
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def forward_activity(iri: str) -> None:
|
|
|
|
p.push(iri, "/task/forward_activity")
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def fetch_og_meta(iri: str) -> None:
|
|
|
|
p.push(iri, "/task/fetch_og_meta")
|
|
|
|
|
2019-08-16 04:52:53 -05:00
|
|
|
@staticmethod
|
|
|
|
def process_reply(iri: str) -> None:
|
|
|
|
p.push(iri, "/task/process_reply")
|
|
|
|
|
2019-07-04 16:23:40 -05:00
|
|
|
@staticmethod
|
|
|
|
def process_new_activity(iri: str) -> None:
|
|
|
|
p.push(iri, "/task/process_new_activity")
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def cache_attachments(iri: str) -> None:
|
|
|
|
p.push(iri, "/task/cache_attachments")
|
|
|
|
|
2019-08-17 03:27:20 -05:00
|
|
|
@staticmethod
|
|
|
|
def cache_attachment(attachment: Dict[str, Any], iri: str) -> None:
|
|
|
|
p.push({"iri": iri, "attachment": attachment}, "/task/cache_attachment")
|
|
|
|
|
2019-07-04 16:23:40 -05:00
|
|
|
@staticmethod
|
|
|
|
def finish_post_to_inbox(iri: str) -> None:
|
|
|
|
p.push(iri, "/task/finish_post_to_inbox")
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def finish_post_to_outbox(iri: str) -> None:
|
|
|
|
p.push(iri, "/task/finish_post_to_outbox")
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def update_question_outbox(iri: str, open_for: int) -> None:
|
|
|
|
p.push(
|
|
|
|
iri, "/task/update_question", delay=open_for
|
|
|
|
) # XXX: delay expects minutes
|
|
|
|
|
|
|
|
@staticmethod
|
2019-07-07 10:48:45 -05:00
|
|
|
def fetch_remote_question(question) -> None:
|
|
|
|
now = datetime.now(timezone.utc)
|
2019-07-12 15:03:04 -05:00
|
|
|
dt = parse_datetime(question.closed or question.endTime)
|
2019-07-07 10:48:45 -05:00
|
|
|
minutes = int((dt - now).total_seconds() / 60)
|
|
|
|
|
2019-07-07 10:50:28 -05:00
|
|
|
if minutes > 0:
|
|
|
|
# Only push the task if the poll is not ended yet
|
|
|
|
p.push(
|
|
|
|
question.id, "/task/fetch_remote_question", delay=minutes
|
|
|
|
) # XXX: delay expects minutes
|