diff options
author | clarkzjw <[email protected]> | 2023-02-27 15:48:09 -0800 |
---|---|---|
committer | clarkzjw <[email protected]> | 2023-02-27 15:59:37 -0800 |
commit | a1c7c5a223d22017508998599388c5adf9a90713 (patch) | |
tree | 9d917eeb71166cb85758ff119f33a89f0fa80ec2 /callback.py | |
parent | 3bf39b0d32d0e0580457d0f85aef90a9c449a6ad (diff) | |
download | swarm2fediverse-a1c7c5a223d22017508998599388c5adf9a90713.tar.gz |
bot: add check_user decorator to check user login statusfeature/user_permission
Diffstat (limited to 'callback.py')
-rw-r--r-- | callback.py | 49 |
1 files changed, 27 insertions, 22 deletions
diff --git a/callback.py b/callback.py index 7530afb..5fe4593 100644 --- a/callback.py +++ b/callback.py | |||
@@ -12,7 +12,7 @@ from config import BOT_SCOPE, ENCRYPT_KEY | |||
12 | from dbstore.peewee_store import User, db, TOOT_VISIBILITY_PRIVATE, TOOT_VISIBILITY_PUBLIC, TOOT_VISIBILITY_UNLISTED | 12 | from dbstore.peewee_store import User, db, TOOT_VISIBILITY_PRIVATE, TOOT_VISIBILITY_PUBLIC, TOOT_VISIBILITY_UNLISTED |
13 | import uuid | 13 | import uuid |
14 | from mastodon import Mastodon | 14 | from mastodon import Mastodon |
15 | from util import decrypt | 15 | from util import decrypt, check_user |
16 | 16 | ||
17 | 17 | ||
18 | def generate_uuid(): | 18 | def generate_uuid(): |
@@ -118,7 +118,8 @@ async def callback_generate_fedi_login_url(update: Update, context: ContextTypes | |||
118 | return FEDI_LOGIN | 118 | return FEDI_LOGIN |
119 | 119 | ||
120 | 120 | ||
121 | async def callback_location_sharing(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: | 121 | @check_user |
122 | async def callback_location_sharing(update: Update, context: ContextTypes.DEFAULT_TYPE, user: User) -> int: | ||
122 | if update.message.venue is not None: | 123 | if update.message.venue is not None: |
123 | context.user_data["fsq_id"] = update.message.venue.foursquare_id | 124 | context.user_data["fsq_id"] = update.message.venue.foursquare_id |
124 | context.user_data["title"] = update.message.venue.title | 125 | context.user_data["title"] = update.message.venue.title |
@@ -128,11 +129,10 @@ async def callback_location_sharing(update: Update, context: ContextTypes.DEFAUL | |||
128 | poi = query_poi_by_fsq_id(context.user_data.get("fsq_id")) | 129 | poi = query_poi_by_fsq_id(context.user_data.get("fsq_id")) |
129 | content = generate_toot_text(poi["name"], poi["locality"], poi["region"], poi["latitude"], poi["longitude"]) | 130 | content = generate_toot_text(poi["name"], poi["locality"], poi["region"], poi["latitude"], poi["longitude"]) |
130 | 131 | ||
131 | u = get_user_by_id(str(update.effective_user.id)) | 132 | content_type = "text/markdown" if user["home_instance_type"] == "pleroma" else None |
132 | content_type = "text/markdown" if u["home_instance_type"] == "pleroma" else None | ||
133 | 133 | ||
134 | status = get_mastodon_client(update.effective_user.id).status_post(content, | 134 | status = get_mastodon_client(update.effective_user.id).status_post(content, |
135 | visibility=u["tool_visibility"], | 135 | visibility=user["tool_visibility"], |
136 | content_type=content_type, | 136 | content_type=content_type, |
137 | media_ids=[]) | 137 | media_ids=[]) |
138 | 138 | ||
@@ -174,7 +174,8 @@ async def _process_location_search(keyword, lat, lon) -> list: | |||
174 | return keyboard | 174 | return keyboard |
175 | 175 | ||
176 | 176 | ||
177 | async def callback_location_keyword_search(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: | 177 | @check_user |
178 | async def callback_location_keyword_search(update: Update, context: ContextTypes.DEFAULT_TYPE, user: User) -> int: | ||
178 | await context.bot.delete_message(update.effective_chat.id, context.user_data.get(PROMPT_LOCATION_KEYWORD)) | 179 | await context.bot.delete_message(update.effective_chat.id, context.user_data.get(PROMPT_LOCATION_KEYWORD)) |
179 | key = update.effective_message.text | 180 | key = update.effective_message.text |
180 | 181 | ||
@@ -191,7 +192,8 @@ async def callback_location_keyword_search(update: Update, context: ContextTypes | |||
191 | return LOCATION_CONFIRMATION | 192 | return LOCATION_CONFIRMATION |
192 | 193 | ||
193 | 194 | ||
194 | async def callback_skip_location_keyword_search(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: | 195 | @check_user |
196 | async def callback_skip_location_keyword_search(update: Update, context: ContextTypes.DEFAULT_TYPE, user: User) -> int: | ||
195 | query = update.callback_query | 197 | query = update.callback_query |
196 | await query.answer() | 198 | await query.answer() |
197 | await query.message.delete() | 199 | await query.message.delete() |
@@ -202,7 +204,7 @@ async def callback_skip_location_keyword_search(update: Update, context: Context | |||
202 | return LOCATION_CONFIRMATION | 204 | return LOCATION_CONFIRMATION |
203 | 205 | ||
204 | 206 | ||
205 | async def _process_location_selection(context: ContextTypes.DEFAULT_TYPE) -> int: | 207 | async def _process_location_selection(context: ContextTypes.DEFAULT_TYPE, user: User) -> int: |
206 | poi_name = context.user_data.get("poi_name") | 208 | poi_name = context.user_data.get("poi_name") |
207 | if context.user_data.get("fsq_id") is not None: | 209 | if context.user_data.get("fsq_id") is not None: |
208 | poi = get_poi_by_fsq_id(context.user_data.get("fsq_id")) | 210 | poi = get_poi_by_fsq_id(context.user_data.get("fsq_id")) |
@@ -212,11 +214,10 @@ async def _process_location_selection(context: ContextTypes.DEFAULT_TYPE) -> int | |||
212 | content = generate_toot_text(poi_name, "", "", context.user_data.get("latitude"), | 214 | content = generate_toot_text(poi_name, "", "", context.user_data.get("latitude"), |
213 | context.user_data.get("longitude")) | 215 | context.user_data.get("longitude")) |
214 | 216 | ||
215 | u = get_user_by_id(context.user_data["user_id"]) | 217 | content_type = "text/markdown" if user["home_instance_type"] == "pleroma" else None |
216 | content_type = "text/markdown" if u["home_instance_type"] == "pleroma" else None | ||
217 | 218 | ||
218 | status = get_mastodon_client(context.user_data["user_id"]).status_post(content, | 219 | status = get_mastodon_client(context.user_data["user_id"]).status_post(content, |
219 | visibility=u["toot_visibility"], | 220 | visibility=user["toot_visibility"], |
220 | content_type=content_type, | 221 | content_type=content_type, |
221 | media_ids=[]) | 222 | media_ids=[]) |
222 | 223 | ||
@@ -236,7 +237,8 @@ async def _process_location_selection(context: ContextTypes.DEFAULT_TYPE) -> int | |||
236 | return ADD_COMMENT | 237 | return ADD_COMMENT |
237 | 238 | ||
238 | 239 | ||
239 | async def callback_location_confirmation(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: | 240 | @check_user |
241 | async def callback_location_confirmation(update: Update, context: ContextTypes.DEFAULT_TYPE, user: User) -> int: | ||
240 | query = update.callback_query | 242 | query = update.callback_query |
241 | await query.answer() | 243 | await query.answer() |
242 | context.user_data["fsq_id"] = query.data | 244 | context.user_data["fsq_id"] = query.data |
@@ -245,15 +247,16 @@ async def callback_location_confirmation(update: Update, context: ContextTypes.D | |||
245 | 247 | ||
246 | context.user_data["chat_id"] = update.effective_chat.id | 248 | context.user_data["chat_id"] = update.effective_chat.id |
247 | 249 | ||
248 | return await _process_location_selection(context) | 250 | return await _process_location_selection(context, user) |
249 | 251 | ||
250 | 252 | ||
251 | async def callback_manual_location(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: | 253 | @check_user |
254 | async def callback_manual_location(update: Update, context: ContextTypes.DEFAULT_TYPE, user: User) -> int: | ||
252 | context.user_data["poi_name"] = update.effective_message.text | 255 | context.user_data["poi_name"] = update.effective_message.text |
253 | context.user_data["chat_id"] = update.effective_chat.id | 256 | context.user_data["chat_id"] = update.effective_chat.id |
254 | context.user_data["user_id"] = update.effective_user.id | 257 | context.user_data["user_id"] = update.effective_user.id |
255 | 258 | ||
256 | return await _process_location_selection(context) | 259 | return await _process_location_selection(context, user) |
257 | 260 | ||
258 | 261 | ||
259 | async def _process_comment(context: ContextTypes.DEFAULT_TYPE) -> int: | 262 | async def _process_comment(context: ContextTypes.DEFAULT_TYPE) -> int: |
@@ -264,14 +267,13 @@ async def _process_comment(context: ContextTypes.DEFAULT_TYPE) -> int: | |||
264 | return ADD_MEDIA | 267 | return ADD_MEDIA |
265 | 268 | ||
266 | 269 | ||
267 | async def callback_add_comment(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: | 270 | @check_user |
271 | async def callback_add_comment(update: Update, context: ContextTypes.DEFAULT_TYPE, user: User) -> int: | ||
268 | context.user_data["chat_id"] = update.effective_chat.id | 272 | context.user_data["chat_id"] = update.effective_chat.id |
269 | context.user_data["user_id"] = update.effective_user.id | 273 | context.user_data["user_id"] = update.effective_user.id |
270 | await context.bot.delete_message(update.effective_chat.id, context.user_data.get(PROMPT_ADD_COMMENT)) | 274 | await context.bot.delete_message(update.effective_chat.id, context.user_data.get(PROMPT_ADD_COMMENT)) |
271 | 275 | ||
272 | with db.connection_context(): | 276 | content_type = "text/markdown" if user["home_instance_type"] == "pleroma" else None |
273 | u = User.get(User.telegram_user_id == context.user_data["user_id"]) | ||
274 | content_type = "text/markdown" if u.home_instance_type == "pleroma" else None | ||
275 | 277 | ||
276 | comment = update.effective_message.text | 278 | comment = update.effective_message.text |
277 | get_mastodon_client(update.effective_user.id).status_update(id=context.user_data.get(KEY_TOOT_STATUS_ID), | 279 | get_mastodon_client(update.effective_user.id).status_update(id=context.user_data.get(KEY_TOOT_STATUS_ID), |
@@ -283,14 +285,16 @@ async def callback_add_comment(update: Update, context: ContextTypes.DEFAULT_TYP | |||
283 | return await _process_comment(context) | 285 | return await _process_comment(context) |
284 | 286 | ||
285 | 287 | ||
286 | async def callback_skip_comment(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: | 288 | @check_user |
289 | async def callback_skip_comment(update: Update, context: ContextTypes.DEFAULT_TYPE, user: User) -> int: | ||
287 | context.user_data["chat_id"] = update.effective_chat.id | 290 | context.user_data["chat_id"] = update.effective_chat.id |
288 | await context.bot.delete_message(update.effective_chat.id, context.user_data.get(PROMPT_ADD_COMMENT)) | 291 | await context.bot.delete_message(update.effective_chat.id, context.user_data.get(PROMPT_ADD_COMMENT)) |
289 | 292 | ||
290 | return await _process_comment(context) | 293 | return await _process_comment(context) |
291 | 294 | ||
292 | 295 | ||
293 | async def callback_add_media(update: Update, context: CallbackContext): | 296 | @check_user |
297 | async def callback_add_media(update: Update, context: CallbackContext, user: User): | ||
294 | await update.message.reply_chat_action(ChatAction.TYPING) | 298 | await update.message.reply_chat_action(ChatAction.TYPING) |
295 | 299 | ||
296 | try: | 300 | try: |
@@ -342,7 +346,8 @@ async def callback_add_media(update: Update, context: CallbackContext): | |||
342 | await update.message.reply_text(text=PROMPT_DONE, reply_markup=MAIN_MENU) | 346 | await update.message.reply_text(text=PROMPT_DONE, reply_markup=MAIN_MENU) |
343 | 347 | ||
344 | 348 | ||
345 | async def callback_skip_media(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: | 349 | @check_user |
350 | async def callback_skip_media(update: Update, context: ContextTypes.DEFAULT_TYPE, user: User) -> int: | ||
346 | query = update.callback_query | 351 | query = update.callback_query |
347 | await query.answer() | 352 | await query.answer() |
348 | 353 | ||