[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
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)

View file

@ -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):

View file

@ -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