lib.itmens/users/jobs/sync.py

44 lines
1.4 KiB
Python
Raw Normal View History

from datetime import timedelta
from django.utils import timezone
from loguru import logger
from common.models import BaseJob, JobManager
2023-12-28 22:12:07 -05:00
from users.models import User
@JobManager.register
class MastodonUserSync(BaseJob):
batch = 16
2023-12-28 22:12:07 -05:00
interval_hours = 3
interval = timedelta(hours=interval_hours)
def run(self):
logger.info("Mastodon User Sync start.")
2024-02-10 23:39:49 -05:00
inactive_threshold = timezone.now() - timedelta(days=90)
qs = (
User.objects.exclude(
2023-11-11 00:52:16 -05:00
preference__mastodon_skip_userinfo=True,
preference__mastodon_skip_relationship=True,
)
.filter(
2023-12-28 22:12:07 -05:00
mastodon_last_refresh__lt=timezone.now()
- timedelta(hours=self.interval_hours * self.batch)
)
.filter(
username__isnull=False,
is_active=True,
)
.exclude(mastodon_token__isnull=True)
.exclude(mastodon_token="")
)
2023-12-28 22:12:07 -05:00
for user in qs.iterator():
2024-02-10 23:39:49 -05:00
skip_detail = False
2024-01-19 16:46:16 -05:00
if not user.last_login or user.last_login < inactive_threshold:
last_usage = user.last_usage
if not last_usage or last_usage < inactive_threshold:
2024-02-10 23:39:49 -05:00
logger.warning(f"Skip {user} detail because of inactivity.")
skip_detail = True
user.refresh_mastodon_data(skip_detail)
2024-04-06 00:13:50 -04:00
logger.info("Mastodon User Sync finished.")