diff options
Diffstat (limited to 'callback.py')
-rw-r--r-- | callback.py | 22 |
1 files changed, 11 insertions, 11 deletions
diff --git a/callback.py b/callback.py index d968523..a1b4b2d 100644 --- a/callback.py +++ b/callback.py | |||
@@ -9,10 +9,11 @@ from command import * | |||
9 | from dbstore.peewee_store import get_poi_by_fsq_id | 9 | from dbstore.peewee_store import get_poi_by_fsq_id |
10 | from foursquare.poi import OSM_ENDPOINT | 10 | from foursquare.poi import OSM_ENDPOINT |
11 | from foursquare.poi import query_poi | 11 | from foursquare.poi import query_poi |
12 | from config import BOT_SCOPE | 12 | from config import BOT_SCOPE, ENCRYPT_KEY |
13 | from dbstore.peewee_store import User, db, TOOT_VISIBILITY_PRIVATE, TOOT_VISIBILITY_PUBLIC, TOOT_VISIBILITY_UNLISTED | 13 | from dbstore.peewee_store import User, db, TOOT_VISIBILITY_PRIVATE, TOOT_VISIBILITY_PUBLIC, TOOT_VISIBILITY_UNLISTED |
14 | import uuid | 14 | import uuid |
15 | from mastodon import Mastodon | 15 | from mastodon import Mastodon |
16 | from util import decrypt | ||
16 | 17 | ||
17 | 18 | ||
18 | def generate_uuid(): | 19 | def generate_uuid(): |
@@ -23,7 +24,7 @@ def get_mastodon_client(user_id: int): | |||
23 | with db.connection_context(): | 24 | with db.connection_context(): |
24 | user = User.get(User.telegram_user_id == user_id) | 25 | user = User.get(User.telegram_user_id == user_id) |
25 | if user.home_instance and user.access_key: | 26 | if user.home_instance and user.access_key: |
26 | return Mastodon(access_token=user.access_key, api_base_url=user.home_instance) | 27 | return Mastodon(access_token=decrypt(user.access_key, ENCRYPT_KEY), api_base_url=user.home_instance) |
27 | 28 | ||
28 | 29 | ||
29 | def generate_toot_text(poi_name, poi_locality, poi_region, poi_lat, poi_lon): | 30 | def generate_toot_text(poi_name, poi_locality, poi_region, poi_lat, poi_lon): |
@@ -85,21 +86,20 @@ async def callback_generate_fedi_login_url(update: Update, context: ContextTypes | |||
85 | user_id = update.effective_user.id | 86 | user_id = update.effective_user.id |
86 | state = generate_uuid() | 87 | state = generate_uuid() |
87 | 88 | ||
88 | db.connect() | 89 | with db.connection_context(): |
89 | u = User.get_or_none(telegram_user_id=user_id) | 90 | u = User.get_or_none(telegram_user_id=user_id) |
90 | if u is None: | 91 | if u is None: |
91 | u = User.create(telegram_user_id=user_id, access_key="", home_instance=home_instance, | 92 | u = User.create(telegram_user_id=user_id, access_key="", home_instance=home_instance, |
92 | client_id=client_id, client_secret=client_secret, state=state) | 93 | client_id=client_id, client_secret=client_secret, state=state) |
93 | u.save() | 94 | u.save() |
94 | db.close() | ||
95 | 95 | ||
96 | oauth_url = m.auth_request_url(redirect_uris="{}{}".format(BOT_DOMAIN, FEDI_LOGIN_CALLBACK_URL), | 96 | oauth_url = m.auth_request_url(redirect_uris="{}{}".format(BOT_DOMAIN, FEDI_LOGIN_CALLBACK_URL), |
97 | scopes=BOT_SCOPE, | 97 | scopes=BOT_SCOPE, |
98 | state=state) | 98 | state=state) |
99 | 99 | ||
100 | msg = await update.message.reply_text(PROMPT_FEDI_LOGIN, | 100 | msg = await update.message.reply_text(PROMPT_FEDI_LOGIN, |
101 | reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("Login", url=oauth_url)]]), | 101 | reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("Login", url=oauth_url)]]), |
102 | parse_mode=ParseMode.MARKDOWN) | 102 | parse_mode=ParseMode.MARKDOWN) |
103 | 103 | ||
104 | context.user_data[PROMPT_FEDI_LOGIN] = msg.message_id | 104 | context.user_data[PROMPT_FEDI_LOGIN] = msg.message_id |
105 | return FEDI_LOGIN | 105 | return FEDI_LOGIN |