11import json
2+ import logging
23from enum import Enum , auto
34
45from telegram .ext import (
@@ -33,8 +34,7 @@ def view_my_profile(update, context):
3334 return
3435
3536 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 )
3838 user_info_view = views .UserProfile (response ).display ()
3939 update .effective_message .reply_text (text = user_info_view )
4040
@@ -187,8 +187,7 @@ def _get_posts(context, payload):
187187 # keep a copy of post_payload in user_data for future calls - TODO is it used?
188188 context .user_data [user_data .VIEW_POST_PAYLOAD ] = payload
189189 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 )
192191 return posts
193192
194193
@@ -320,6 +319,7 @@ def _handle_post_id_choice(update, context, user_choice):
320319 _update_user_post_id (context , post_id = post_id )
321320 _show_user_single_post (
322321 update = update ,
322+ context = context ,
323323 post_id = post_id ,
324324 )
325325
@@ -354,17 +354,23 @@ def _get_real_post_id(context, user_choice):
354354 return context .user_data [user_data .VIEW_POST_IDS ][int (user_choice ) - 1 ]
355355
356356
357- def _show_user_single_post (update , post_id ):
357+ def _show_user_single_post (update , context , post_id ):
358358 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 )
361360 reply_text = views .Post (post_json = post ).display ()
362- update .effective_message .reply_text (
361+ util .reply (
362+ update = update ,
363+ context = context ,
363364 text = reply_text ,
364- reply_markup = keyboards .display_selected_post (),
365+ keyboard = keyboards .display_selected_post (),
365366 )
366367
367368
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+
368374def _get_header_message_with_categories (context ):
369375 formatted_categories = ", " .join (context .user_data [user_data .POST_CATEGORIES ])
370376 page = _get_current_user_page (context )
@@ -377,7 +383,42 @@ def _get_header_message_user_posts(context):
377383 return f"Page { page } of your posts"
378384
379385
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+
380419ViewMyProfileQueryHandler = CallbackQueryHandler (view_my_profile , pattern = patterns .VIEW_MY_PROFILE )
381420ViewMyPostsQueryHandler = CallbackQueryHandler (view_my_posts , pattern = patterns .VIEW_MY_POSTS )
382421DisplaySelectedPostsQueryHandler = CallbackQueryHandler (display_selected_post , pattern = patterns .DISPLAY_SELECTED_POST )
383422ViewPostsConvHandler = 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