From 8af62051461e4e3e512ad70525edcc99e0272c93 Mon Sep 17 00:00:00 2001 From: Cathy Hu Date: Mon, 6 Jul 2020 16:11:05 +0200 Subject: [PATCH] [twitter] Require access tokens to be set in model --- kibicara/platforms/twitter/bot.py | 10 +++++----- kibicara/platforms/twitter/model.py | 4 ++-- kibicara/platforms/twitter/webapi.py | 13 ++++++------- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/kibicara/platforms/twitter/bot.py b/kibicara/platforms/twitter/bot.py index 56bcc6a..8012232 100644 --- a/kibicara/platforms/twitter/bot.py +++ b/kibicara/platforms/twitter/bot.py @@ -98,13 +98,13 @@ class TwitterBot(Censor): return messages async def _filter_text(self, entities, text): - remove_indices = [] + remove_indices = set() for user in entities.user_mentions: - remove_indices.extend(list(range(user.indices[0], user.indices[1] + 1))) + remove_indices.update(range(user.indices[0], user.indices[1] + 1)) for url in entities.urls: - remove_indices.extend(list(range(url.indices[0], url.indices[1] + 1))) + remove_indices.update(range(url.indices[0], url.indices[1] + 1)) for symbol in entities.symbols: - remove_indices.extend(list(range(symbol.indices[0], symbol.indices[1] + 1))) + remove_indices.update(range(symbol.indices[0], symbol.indices[1] + 1)) filtered_text = "" for index, character in enumerate(text): if index not in remove_indices: @@ -117,7 +117,7 @@ class TwitterBot(Censor): message = await self.receive() logger.debug( 'Received message from censor (%s): %s' - % (self.twitter_model.hood.name, message) + % (self.twitter_model.hood.name, message.text) ) if hasattr(message, 'twitter_mention_id'): await self._retweet(message.twitter_mention_id) diff --git a/kibicara/platforms/twitter/model.py b/kibicara/platforms/twitter/model.py index 1e38931..592f4c8 100644 --- a/kibicara/platforms/twitter/model.py +++ b/kibicara/platforms/twitter/model.py @@ -11,8 +11,8 @@ class Twitter(Model): hood: ForeignKey(Hood) dms_since_id: Integer(allow_null=True) = None mentions_since_id: Integer(allow_null=True) = None - access_token: Text(allow_null=True) = None - access_token_secret: Text(allow_null=True) = None + access_token: Text() + access_token_secret: Text() successful_verified: Boolean() = False class Mapping(Mapping): diff --git a/kibicara/platforms/twitter/webapi.py b/kibicara/platforms/twitter/webapi.py index 4ca50ed..0b10484 100644 --- a/kibicara/platforms/twitter/webapi.py +++ b/kibicara/platforms/twitter/webapi.py @@ -46,18 +46,17 @@ async def twitter_delete(twitter=Depends(get_twitter)): @router.post('/', status_code=status.HTTP_201_CREATED) async def twitter_create(response: Response, hood=Depends(get_hood)): try: - twitter = await Twitter.objects.create(hood=hood) - oauth_token = await get_oauth_token( + request_token = await get_oauth_token( config['twitter_consumer_key'], config['twitter_consumer_secret'], callback_uri='http://127.0.0.1:8000/api/twitter/callback', ) - if oauth_token['oauth_callback_confirmed'] != 'true': - await twitter.delete() + if request_token['oauth_callback_confirmed'] != 'true': raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE) - await twitter.update( - access_token=oauth_token['oauth_token'], - access_token_secret=oauth_token['oauth_token_secret'], + twitter = await Twitter.objects.create( + hood=hood, + access_token=request_token['oauth_token'], + access_token_secret=request_token['oauth_token_secret'], ) response.headers['Location'] = '%d' % twitter.id return twitter