1010from .emoji import emoji_codemap
1111from .markdown import MarkdownText
1212from .store import Store
13+ from sclack .utils .channel import is_group , is_channel , is_dm
1314
1415
1516MARK_READ_ALARM_PERIOD = 3
@@ -370,16 +371,21 @@ def handle_floating_date(self, size):
370371
371372
372373class Dm (urwid .AttrMap ):
373- def __init__ (self , id , name , user , you = False , unread = 0 ):
374+ def __init__ (self , id , name , user , you = False , unread = 0 , is_selected = False ):
374375 self .id = id
375376 self .user = user
376377 self .name = name
377378 self .you = you
378379 self .presence = 'away'
379380 self .unread = unread
380381 self .body = urwid .SelectableIcon (self .get_markup ())
381- self .is_selected = False
382- super (Dm , self ).__init__ (self .body , 'inactive' , 'active_channel' )
382+ self .is_selected = is_selected
383+
384+ attr_map = 'inactive'
385+ if is_selected :
386+ attr_map = 'selected_channel'
387+
388+ super (Dm , self ).__init__ (self .body , attr_map , 'active_channel' )
383389
384390 def get_markup (self , presence = 'away' ):
385391 if self .user == 'USLACKBOT' :
@@ -417,11 +423,11 @@ def get_markup(self, presence='away'):
417423
418424 def set_unread (self , count ):
419425 self .unread = count
420-
421- if count > 0 :
422- self .attr_map = {None : 'unread_channel' }
423- else :
424- self .attr_map = {None : 'inactive' }
426+ if not self . is_selected :
427+ if count > 0 :
428+ self .attr_map = {None : 'unread_channel' }
429+ else :
430+ self .attr_map = {None : 'inactive' }
425431
426432 self .body .set_text (self .get_markup (self .presence ))
427433
@@ -440,12 +446,15 @@ def select(self):
440446 'presence_away' : 'selected_channel'
441447 }
442448 self .set_presence (self .presence )
449+ self .attr_map = {None : 'selected_channel' }
450+ self .focus_map = {None : 'selected_channel' }
443451
444452 def deselect (self ):
445453 self .is_selected = False
446454 self .attr_map = {None : None }
447455 self .set_presence (self .presence )
448456
457+
449458class Fields (urwid .Pile ):
450459 def chunks (self , list , size ):
451460 for i in range (0 , len (list ), size ):
@@ -650,6 +659,7 @@ def text(self, text):
650659 self .prompt_widget .set_edit_text (text )
651660 self .prompt_widget .set_edit_pos (len (text ))
652661
662+
653663class MessagePrompt (urwid_readline .ReadlineEdit ):
654664 __metaclass__ = urwid .MetaSignals
655665 signals = ['submit_message' , 'go_to_last_message' ]
@@ -748,6 +758,7 @@ def __init__(self, profile, channels=(), dms=(), stars=(), title=''):
748758 self .channels = channels
749759 self .stars = stars
750760 self .dms = dms
761+ self .groups = ()
751762
752763 # Subscribe to receive message from channels to select them
753764 for channel in self .channels :
@@ -768,13 +779,72 @@ def __init__(self, profile, channels=(), dms=(), stars=(), title=''):
768779 self .listbox = urwid .ListBox (self .walker )
769780 super (SideBar , self ).__init__ (self .listbox , header = header , footer = footer )
770781
782+ def get_all_channels (self ):
783+ """
784+ List Channels including Starred items
785+ :return:
786+ """
787+ channels_starred = list (filter (
788+ lambda starred : is_channel (starred .id ),
789+ self .stars
790+ ))
791+ channels_starred .extend (self .channels )
792+
793+ return channels_starred
794+
795+ def get_all_dms (self ):
796+ """
797+ List DM including Starred items
798+ :return:
799+ """
800+ dms = list (filter (
801+ lambda starred : is_dm (starred .id ),
802+ self .stars
803+ ))
804+ dms .extend (self .dms )
805+
806+ return dms
807+
808+ def get_all_groups (self ):
809+ """
810+ List Groups including Starred items
811+ :return:
812+ """
813+ groups = list (filter (
814+ lambda starred : is_group (starred .id ),
815+ self .stars
816+ ))
817+ groups .extend (self .groups )
818+
819+ return groups
820+
821+ def get_targets_by_id (self , channel_id ):
822+ """
823+ For different channel_id we get different data from: Groups, DMs, Channels
824+ :param channel_id:
825+ :return:
826+ """
827+ targets = None
828+ if is_channel (channel_id ):
829+ targets = self .get_all_channels ()
830+ elif is_dm (channel_id ):
831+ targets = self .get_all_dms ()
832+ elif is_group (channel_id ):
833+ targets = self .get_all_groups ()
834+ return targets
835+
771836 def select_channel (self , channel_id ):
772- for channel in self .channels :
837+ """
838+ :param channel_id:
839+ :return:
840+ """
841+ for channel in self .get_all_channels ():
773842 if channel .id == channel_id :
774843 channel .select ()
775844 else :
776845 channel .deselect ()
777- for dm in self .dms :
846+
847+ for dm in self .get_all_dms ():
778848 if dm .id == channel_id :
779849 dm .select ()
780850 else :
@@ -787,11 +857,7 @@ def update_items(self, event):
787857 :return:
788858 """
789859 channel_id = event .get ('channel' )
790-
791- if channel_id [0 ] == 'D' :
792- target = self .dms
793- else :
794- target = self .channels
860+ target = self .get_targets_by_id (channel_id )
795861
796862 chat_detail = Store .instance .get_channel_info (event .get ('channel' ))
797863 new_count = chat_detail .get ('unread_count_display' , 0 )
0 commit comments