1
1
import json
2
+ import logging
2
3
from enum import Enum , auto
3
4
4
5
from telegram .ext import (
@@ -33,8 +34,7 @@ def view_my_profile(update, context):
33
34
return
34
35
35
36
response = fpapi .get_current_user_profile (token = token )
36
- if isinstance (response , fpapi .Error ): # TODO handle error better
37
- raise ConnectionError ("Could not get user profile" )
37
+ _assert_not_error (response )
38
38
user_info_view = views .UserProfile (response ).display ()
39
39
update .effective_message .reply_text (text = user_info_view )
40
40
@@ -187,8 +187,7 @@ def _get_posts(context, payload):
187
187
# keep a copy of post_payload in user_data for future calls - TODO is it used?
188
188
context .user_data [user_data .VIEW_POST_PAYLOAD ] = payload
189
189
posts = fpapi .get_posts (payload )
190
- if isinstance (posts , fpapi .Error ): # TODO handle error better
191
- raise ConnectionError ("Could not get posts" )
190
+ _assert_not_error (posts )
192
191
return posts
193
192
194
193
@@ -320,6 +319,7 @@ def _handle_post_id_choice(update, context, user_choice):
320
319
_update_user_post_id (context , post_id = post_id )
321
320
_show_user_single_post (
322
321
update = update ,
322
+ context = context ,
323
323
post_id = post_id ,
324
324
)
325
325
@@ -354,17 +354,23 @@ def _get_real_post_id(context, user_choice):
354
354
return context .user_data [user_data .VIEW_POST_IDS ][int (user_choice ) - 1 ]
355
355
356
356
357
- def _show_user_single_post (update , post_id ):
357
+ def _show_user_single_post (update , context , post_id ):
358
358
post = fpapi .get_post (post_id )
359
- if isinstance (post , fpapi .Error ): # TODO handle error better
360
- raise ConnectionError ("Could not get post" )
359
+ _assert_not_error (post )
361
360
reply_text = views .Post (post_json = post ).display ()
362
- update .effective_message .reply_text (
361
+ util .reply (
362
+ update = update ,
363
+ context = context ,
363
364
text = reply_text ,
364
- reply_markup = keyboards .display_selected_post (),
365
+ keyboard = keyboards .display_selected_post (),
365
366
)
366
367
367
368
369
+ def _assert_not_error (post ):
370
+ if isinstance (post , fpapi .Error ): # TODO handle error better
371
+ raise ConnectionError ("Could not get post" )
372
+
373
+
368
374
def _get_header_message_with_categories (context ):
369
375
formatted_categories = ", " .join (context .user_data [user_data .POST_CATEGORIES ])
370
376
page = _get_current_user_page (context )
@@ -377,7 +383,42 @@ def _get_header_message_user_posts(context):
377
383
return f"Page { page } of your posts"
378
384
379
385
386
+
387
+ def view_author_profile (update , context ):
388
+ post_id = context .user_data .get (user_data .VIEW_POST_ID )
389
+ if post_id is None :
390
+ logging .warning ("No post ID to view author for" )
391
+ return
392
+ author = _get_author_profile_from_post_id (post_id = post_id )
393
+ util .reply (
394
+ update = update ,
395
+ context = context ,
396
+ text = author .display (),
397
+ keyboard = keyboards .view_author (),
398
+ )
399
+
400
+
401
+ def handle_go_back_view_author (update , context ):
402
+ post_id = context .user_data [user_data .VIEW_POST_ID ]
403
+ _show_user_single_post (
404
+ update = update ,
405
+ context = context ,
406
+ post_id = post_id ,
407
+ )
408
+
409
+
410
+ def _get_author_profile_from_post_id (post_id ):
411
+ raw_post = fpapi .get_post (post_id )
412
+ _assert_not_error (raw_post )
413
+ post = views .Post (post_json = raw_post )
414
+ response = fpapi .get_user_profile (user_id = post .author_id )
415
+ _assert_not_error (response )
416
+ return views .UserProfile (response )
417
+
418
+
380
419
ViewMyProfileQueryHandler = CallbackQueryHandler (view_my_profile , pattern = patterns .VIEW_MY_PROFILE )
381
420
ViewMyPostsQueryHandler = CallbackQueryHandler (view_my_posts , pattern = patterns .VIEW_MY_POSTS )
382
421
DisplaySelectedPostsQueryHandler = CallbackQueryHandler (display_selected_post , pattern = patterns .DISPLAY_SELECTED_POST )
383
422
ViewPostsConvHandler = view_posts_conversation ()
423
+ ViewAuthorProfileQueryHandler = CallbackQueryHandler (view_author_profile , pattern = patterns .VIEW_AUTHOR_PROFILE )
424
+ GoBackViewAuthorQueryHandler = CallbackQueryHandler (handle_go_back_view_author , pattern = patterns .GO_BACK_VIEW_AUTHOR )
0 commit comments