forked from doubaniux/boofilsic
-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate and sync user relationship in Mastodon
- Loading branch information
Her Email
committed
Nov 8, 2023
1 parent
edcd80d
commit d38325c
Showing
13 changed files
with
181 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .sync import MastodonUserSync |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import pprint | ||
from datetime import timedelta | ||
from time import sleep | ||
|
||
from django.utils import timezone | ||
from loguru import logger | ||
|
||
from common.models import BaseJob, JobManager | ||
from users.models import Preference, User | ||
|
||
|
||
@JobManager.register | ||
class MastodonUserSync(BaseJob): | ||
interval = timedelta(hours=2) | ||
|
||
def run(self): | ||
logger.info("Mastodon User Sync start.") | ||
count = 0 | ||
ttl_hours = 12 | ||
qs = ( | ||
User.objects.exclude( | ||
preference__mastodon_skip_userinfo=True, mastodon_skip_relationship=True | ||
) | ||
.filter( | ||
mastodon_last_refresh__lt=timezone.now() - timedelta(hours=ttl_hours) | ||
) | ||
.filter( | ||
is_active=True, | ||
) | ||
.exclude(mastodon_token__isnull=True) | ||
.exclude(mastodon_token="") | ||
) | ||
for user in qs: | ||
user.refresh_mastodon_data() | ||
logger.info(f"Mastodon User Sync finished.") |
This file was deleted.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
users/migrations/0014_preference_mastodon_skip_relationship_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Generated by Django 4.2.7 on 2023-11-06 01:46 | ||
|
||
from django.db import migrations, models | ||
from loguru import logger | ||
from tqdm import tqdm | ||
|
||
|
||
def migrate_relationships(apps, schema_editor): | ||
User = apps.get_model("users", "User") | ||
APIdentity = apps.get_model("users", "APIdentity") | ||
logger.info(f"Migrate user relationship") | ||
for user in tqdm(User.objects.all()): | ||
for target in user.local_following: | ||
user.identity.follow(User.objects.get(pk=target).identity) | ||
for target in user.local_blocking: | ||
user.identity.block(User.objects.get(pk=target).identity) | ||
for target in user.local_muting: | ||
user.identity.block(User.objects.get(pk=target).identity) | ||
user.sync_relationship() | ||
for user in tqdm(User.objects.all()): | ||
for req in user.identity.following_request: | ||
target_identity = APIdentity.objects.get(pk=req) | ||
target_identity.accept_follow_request(user.identity) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("users", "0013_init_identity"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(migrate_relationships), | ||
migrations.AddField( | ||
model_name="preference", | ||
name="mastodon_skip_relationship", | ||
field=models.BooleanField(default=False), | ||
), | ||
migrations.AddField( | ||
model_name="preference", | ||
name="mastodon_skip_userinfo", | ||
field=models.BooleanField(default=False), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters