|
| 1 | +""" |
| 2 | +Set up our tasks for celery to run |
| 3 | +
|
| 4 | +Task to verify accounts on XIVAPI. |
| 5 | +""" |
| 6 | +# stdlib |
| 7 | +from datetime import timedelta |
| 8 | +# lib |
| 9 | +from asgiref.sync import async_to_sync |
| 10 | +from celery import shared_task |
| 11 | +from celery.utils.log import get_task_logger |
| 12 | +from channels.layers import get_channel_layer |
| 13 | +from django.core.management import call_command |
| 14 | +from django.db.models import Q |
| 15 | +from django.utils import timezone |
| 16 | +# local |
| 17 | +from . import notifier |
| 18 | +from .lodestone_scraper import LodestoneScraper |
| 19 | +from .models import Character, Notification, Team |
| 20 | + |
| 21 | +logger = get_task_logger(__name__) |
| 22 | + |
| 23 | + |
| 24 | +def assimilate_proxies(real_char: Character): |
| 25 | + # Find all Proxy characters that have the same lodestone ID as this one |
| 26 | + proxies = Character.objects.filter( |
| 27 | + Q(user__isnull=True) | Q(user_id=real_char.user_id), |
| 28 | + lodestone_id=real_char.lodestone_id, |
| 29 | + ) |
| 30 | + |
| 31 | + # For each Character (which should only ever be in one team each); |
| 32 | + # - Notify the Team Leader that the claim has happened |
| 33 | + # - Move the BIS List to the real Character, name it using the Team's name |
| 34 | + # - Update the TeamMember object to point to this character |
| 35 | + for char in proxies: |
| 36 | + for tm in char.teammember_set.all(): |
| 37 | + if char.user is None: |
| 38 | + notifier.team_proxy_claim(tm) |
| 39 | + |
| 40 | + bis = tm.bis_list |
| 41 | + bis.owner = real_char |
| 42 | + bis.name = f'BIS From {tm.team.name}' |
| 43 | + bis.save() |
| 44 | + |
| 45 | + tm.character = real_char |
| 46 | + tm.save() |
| 47 | + |
| 48 | + |
| 49 | +@shared_task(name='verify_character') |
| 50 | +def verify_character(pk: int): |
| 51 | + """ |
| 52 | + Verify the character has the expected token in the bio on xivapi. |
| 53 | +
|
| 54 | + If so, update the flag to True, and delete all other unverified characters with the same lodestone id |
| 55 | + """ |
| 56 | + # Check that the character is unverified and exists |
| 57 | + logger.info(f'Commencing verification attempt for Character #{pk}.') |
| 58 | + try: |
| 59 | + obj = Character.objects.get(pk=pk, verified=False) |
| 60 | + except Character.DoesNotExist: |
| 61 | + logger.warn(f'Character #{pk} either does not exist or is verified. Exiting.') |
| 62 | + return |
| 63 | + |
| 64 | + # Call the xivapi function in a sync context |
| 65 | + logger.debug('calling lookup function') |
| 66 | + err = LodestoneScraper.get_instance().check_token(obj.lodestone_id, obj.token) |
| 67 | + logger.debug('finished lookup function') |
| 68 | + |
| 69 | + if err is not None: |
| 70 | + notifier.verify_fail(obj, err) |
| 71 | + logger.info(f'Character #{pk} could not be verified. Exiting. ({err})') |
| 72 | + return |
| 73 | + |
| 74 | + logger.info(f'Character #{pk} verified. Updating DB.') |
| 75 | + # First we update the flag on the object specified |
| 76 | + obj.verified = True |
| 77 | + obj.save() |
| 78 | + |
| 79 | + # Before we go deleting any Characters, we need to sort all the Proxies that share the lodestone ID |
| 80 | + assimilate_proxies(obj) |
| 81 | + |
| 82 | + # Next delete all unverified instances of the character (this includes proxies) |
| 83 | + logger.info(f'Deleting unverified instances of Character #{obj.lodestone_id} (#{pk}) owned by {obj.user_id}.') |
| 84 | + objs = Character.objects.filter( |
| 85 | + Q(user__isnull=True) | Q(user_id=obj.user_id), |
| 86 | + verified=False, |
| 87 | + lodestone_id=obj.lodestone_id, |
| 88 | + ).exclude(pk=pk) |
| 89 | + ids_to_delete = [o.pk for o in objs] |
| 90 | + logger.info(f'Found {objs.count()} instances of Character #{obj.lodestone_id} to delete.\n{ids_to_delete}') |
| 91 | + objs.delete() |
| 92 | + # Then we're done! |
| 93 | + notifier.verify_success(obj) |
| 94 | + # Also send websocket details |
| 95 | + channel_layer = get_channel_layer() |
| 96 | + if channel_layer is not None: |
| 97 | + async_to_sync(channel_layer.group_send)(f'user-updates-{obj.user.id}', {'type': 'character', 'id': obj.pk}) |
| 98 | + |
| 99 | + |
| 100 | +@shared_task(name='verify_reminder') |
| 101 | +def remind_users_to_verify(): |
| 102 | + """ |
| 103 | + Find non-verified Characters that are 5 days old. |
| 104 | + Send Notifications to remind the User to verify. |
| 105 | + """ |
| 106 | + logger.debug(f'Running at: {timezone.now()}') |
| 107 | + older_than = timezone.now() - timedelta(days=5) |
| 108 | + logger.debug(f'Reminding unverified characters older than {older_than}.') |
| 109 | + |
| 110 | + characters = Character.objects.filter(verified=False, user__isnull=False, created__lt=older_than) |
| 111 | + logger.debug(f'Found {characters.count()} characters. Reminding their Users.') |
| 112 | + for char in characters: |
| 113 | + # Check that there wasn't already a reminder sent about this Character |
| 114 | + if not Notification.objects.filter(type='verify_reminder', link=f'/characters/{char.id}/').exists(): |
| 115 | + notifier.verify_reminder(char) |
| 116 | + |
| 117 | + |
| 118 | +@shared_task(name='cleanup') |
| 119 | +def cleanup(): |
| 120 | + """ |
| 121 | + Cleanup the DB of all unverified (non-proxy) characters made more than 7 days ago |
| 122 | + """ |
| 123 | + logger.debug(f'Running at: {timezone.now()}') |
| 124 | + older_than = timezone.now() - timedelta(days=7) |
| 125 | + logger.debug(f'Deleting unverified characters older than {older_than}.') |
| 126 | + |
| 127 | + objs = Character.objects.filter(verified=False, user__isnull=False, created__lt=older_than) |
| 128 | + logger.debug(f'Found {objs.count()} characters. Deleting them.') |
| 129 | + for char in objs: |
| 130 | + # Remove them from every team they are a member of |
| 131 | + teams = Team.objects.filter(members__character=char).distinct() |
| 132 | + for team in teams: |
| 133 | + team.remove_character(char, False) |
| 134 | + |
| 135 | + char.bis_lists.all().delete() |
| 136 | + char.delete() |
| 137 | + |
| 138 | + |
| 139 | +@shared_task(name='refresh_tokens') |
| 140 | +def refresh_tokens(): |
| 141 | + """ |
| 142 | + Refresh any tokens that are about to expire |
| 143 | + """ |
| 144 | + call_command('refresh_tokens') |
| 145 | + |
| 146 | + |
| 147 | +@shared_task(name='check_game_version') |
| 148 | +def check_game_version(): |
| 149 | + """ |
| 150 | + Check if a new, unseeded, game version has been added to xivapi that we don't have yet |
| 151 | + """ |
| 152 | + call_command('check_game_version', '--latest', '--notify') |
0 commit comments