Skip to content

Commit

Permalink
Check if the guest tokens created more than 30 days ago are expired (…
Browse files Browse the repository at this point in the history
…and delete them if it is the case).
  • Loading branch information
j-fbriere committed Feb 13, 2024
1 parent 0903f59 commit e7271da
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/client/client_account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ class TwitterAccount {
// TODO: remove eventually this call
// temporary fix: it is possible that there are still expired tokens du to old version mismanagement
await _deleteExpiredTokens();

// TODO: remove eventually this call
await _checkExpirationOfGuestTokens();
}

}
Expand Down Expand Up @@ -236,6 +239,31 @@ class TwitterAccount {
}
}

// TODO: remove eventually this method
// Check if the guest tokens created more than 30 days ago are expired (and delete them if it is the case).
// Check no more than 3 tokens to avoid a long wait.
static Future<void> _checkExpirationOfGuestTokens() async {
List<TwitterTokenEntity> lst = _twitterTokenLst.where((tt) => tt.guest && DateTime.now().difference(tt.createdAt).inDays > 30).toList();

var queryParameters = {
'id': '1'
};
for (int i = 0; i < 3 && i < lst.length; i++) {
_currentTwitterToken = lst[i];
try {
Uri uri = Uri.https('api.twitter.com', '/1.1/trends/place.json', queryParameters);
// no init of the fetchContext so that the current token won't be selected
RateFetchContext fetchContext = RateFetchContext(uri.path, 1);
// if the oauth token is expired, the delete token is taken care automatically
await fetch(uri, fetchContext: fetchContext).timeout(Duration(seconds: 5));
}
catch (err, _) {
// nothing to do
}
}
_currentTwitterToken = null;
}

static Future<DateTime?> _getLastGuestTwitterTokenCreationAttempted() async {
final prefs = await SharedPreferences.getInstance();
List<String>? lastGuestAccountsCreationAttemptedLst = prefs.getStringList('lastGuestAccountsCreationsAttempted');
Expand Down

0 comments on commit e7271da

Please sign in to comment.