[twitter] Add column to track twitter oauth verification status

This commit is contained in:
Cathy Hu 2020-07-06 14:35:20 +02:00 committed by acipm
parent cd62b5b0a7
commit fae170ffb9
2 changed files with 7 additions and 2 deletions

View file

@ -3,7 +3,7 @@
# SPDX-License-Identifier: 0BSD # SPDX-License-Identifier: 0BSD
from kibicara.model import Hood, Mapping from kibicara.model import Hood, Mapping
from ormantic import Integer, ForeignKey, Model, Text from ormantic import Boolean, Integer, ForeignKey, Model, Text
class Twitter(Model): class Twitter(Model):
@ -13,6 +13,7 @@ class Twitter(Model):
mentions_since_id: Integer(allow_null=True) = None mentions_since_id: Integer(allow_null=True) = None
access_token: Text(allow_null=True) = None access_token: Text(allow_null=True) = None
access_token_secret: Text(allow_null=True) = None access_token_secret: Text(allow_null=True) = None
successful_verified: Boolean() = False
class Mapping(Mapping): class Mapping(Mapping):
table_name = 'twitterbots' table_name = 'twitterbots'

View file

@ -9,6 +9,7 @@ from kibicara.platforms.twitter.model import Twitter
from kibicara.webapi.hoods import get_hood from kibicara.webapi.hoods import get_hood
from logging import getLogger from logging import getLogger
from sqlite3 import IntegrityError from sqlite3 import IntegrityError
from ormantic.exceptions import NoMatch
from peony.oauth_dance import get_oauth_token, get_access_token from peony.oauth_dance import get_oauth_token, get_access_token
@ -36,6 +37,7 @@ async def twitter_create(response: Response, hood=Depends(get_hood)):
callback_uri='http://127.0.0.1:8000/api/twitter/callback', callback_uri='http://127.0.0.1:8000/api/twitter/callback',
) )
if oauth_token['oauth_callback_confirmed'] != 'true': if oauth_token['oauth_callback_confirmed'] != 'true':
# TODO delete twitter
raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE) raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE)
await twitter.update( await twitter.update(
access_token=oauth_token['oauth_token'], access_token=oauth_token['oauth_token'],
@ -61,9 +63,11 @@ async def twitter_read_callback(oauth_token: str, oauth_verifier: str):
await twitter.update( await twitter.update(
access_token=access_token['oauth_token'], access_token=access_token['oauth_token'],
access_token_secret=access_token['oauth_token_secret'], access_token_secret=access_token['oauth_token_secret'],
successful_verified=True,
) )
spawner.start(twitter) spawner.start(twitter)
response.headers['Location'] = '%d' % twitter.id
return [] return []
except IntegrityError: except IntegrityError:
raise HTTPException(status_code=status.HTTP_409_CONFLICT) raise HTTPException(status_code=status.HTTP_409_CONFLICT)
except NoMatch:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)