From 26efd09304e2cf281c438de1709fe22bb4610849 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Mon, 5 Dec 2022 21:58:13 +0100 Subject: [PATCH] Add task to import Mastodon following export --- app/boxes.py | 2 +- tasks.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/app/boxes.py b/app/boxes.py index 1fd7e34..7438623 100644 --- a/app/boxes.py +++ b/app/boxes.py @@ -950,7 +950,7 @@ async def compute_all_known_recipients(db_session: AsyncSession) -> set[str]: } -async def _get_following(db_session: AsyncSession) -> list[models.Follower]: +async def _get_following(db_session: AsyncSession) -> list[models.Following]: return ( ( await db_session.scalars( diff --git a/tasks.py b/tasks.py index 2dd57bf..201e5cd 100644 --- a/tasks.py +++ b/tasks.py @@ -353,3 +353,40 @@ def check_config(ctx): sys.exit(1) else: print("Config is OK") + + +@task +def import_mastodon_following_accounts(ctx, path): + # type: (Context, str) -> None + from loguru import logger + + from app.boxes import _get_following + from app.boxes import _send_follow + from app.database import async_session + from app.utils.mastodon import get_actor_urls_from_following_accounts_csv_file + + async def _import_following() -> int: + count = 0 + async with async_session() as db_session: + followings = { + following.ap_actor_id for following in await _get_following(db_session) + } + for ( + handle, + actor_url, + ) in await get_actor_urls_from_following_accounts_csv_file(path): + if actor_url in followings: + logger.info(f"Already following {handle}") + continue + + logger.info(f"Importing {actor_url=}") + + await _send_follow(db_session, actor_url) + count += 1 + + await db_session.commit() + + return count + + count = asyncio.run(_import_following()) + logger.info(f"Import done, {count} follow requests sent")