[twitter] Require access tokens to be set in model

This commit is contained in:
Cathy Hu 2020-07-06 16:11:05 +02:00 committed by acipm
parent c5781200c7
commit 8af6205146
3 changed files with 13 additions and 14 deletions

View file

@ -98,13 +98,13 @@ class TwitterBot(Censor):
return messages return messages
async def _filter_text(self, entities, text): async def _filter_text(self, entities, text):
remove_indices = [] remove_indices = set()
for user in entities.user_mentions: 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: 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: 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 = "" filtered_text = ""
for index, character in enumerate(text): for index, character in enumerate(text):
if index not in remove_indices: if index not in remove_indices:
@ -117,7 +117,7 @@ class TwitterBot(Censor):
message = await self.receive() message = await self.receive()
logger.debug( logger.debug(
'Received message from censor (%s): %s' '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'): if hasattr(message, 'twitter_mention_id'):
await self._retweet(message.twitter_mention_id) await self._retweet(message.twitter_mention_id)

View file

@ -11,8 +11,8 @@ class Twitter(Model):
hood: ForeignKey(Hood) hood: ForeignKey(Hood)
dms_since_id: Integer(allow_null=True) = None dms_since_id: Integer(allow_null=True) = None
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()
access_token_secret: Text(allow_null=True) = None access_token_secret: Text()
successful_verified: Boolean() = False successful_verified: Boolean() = False
class Mapping(Mapping): class Mapping(Mapping):

View file

@ -46,18 +46,17 @@ async def twitter_delete(twitter=Depends(get_twitter)):
@router.post('/', status_code=status.HTTP_201_CREATED) @router.post('/', status_code=status.HTTP_201_CREATED)
async def twitter_create(response: Response, hood=Depends(get_hood)): async def twitter_create(response: Response, hood=Depends(get_hood)):
try: try:
twitter = await Twitter.objects.create(hood=hood) request_token = await get_oauth_token(
oauth_token = await get_oauth_token(
config['twitter_consumer_key'], config['twitter_consumer_key'],
config['twitter_consumer_secret'], config['twitter_consumer_secret'],
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 request_token['oauth_callback_confirmed'] != 'true':
await twitter.delete()
raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE) raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE)
await twitter.update( twitter = await Twitter.objects.create(
access_token=oauth_token['oauth_token'], hood=hood,
access_token_secret=oauth_token['oauth_token_secret'], access_token=request_token['oauth_token'],
access_token_secret=request_token['oauth_token_secret'],
) )
response.headers['Location'] = '%d' % twitter.id response.headers['Location'] = '%d' % twitter.id
return twitter return twitter