docs: def get_user_preferences_for_ui(user, message_filter=None, messenger_filter=None, new_messengers_titles=None):
      """Returns a two element tuple with user subscription preferences to render in UI.
  
      Message types with the same titles are merged into one row.
  
      First element:
          A list of messengers titles.
  
      Second element:
          User preferences dictionary indexed by message type titles.
          Preferences (dictionary values) are lists of tuples:
              (preference_alias, is_supported_by_messenger_flag, user_subscribed_flag)
  
          Example:
              {'My message type': [('test_message|smtp', True, False), ...]}
  
      :param User user:
      :param callable|None message_filter: A callable accepting a message object to filter out message types
      :param callable|None messenger_filter: A callable accepting a messenger object to filter out messengers
      :rtype: tuple
  
      """
      if new_messengers_titles is None:
          new_messengers_titles = {}
  
      msgr_to_msg = defaultdict(set)
      msg_titles = OrderedDict()
      msgr_titles = OrderedDict()
  
      for msgr in get_registered_messenger_objects().values():
          if not (messenger_filter is None or messenger_filter(msgr)) or not msgr.allow_user_subscription:
              continue
  
          msgr_alias = msgr.alias
          msgr_title = new_messengers_titles.get(msgr.alias) or msgr.title
  
          for msg in get_registered_message_types().values():
              if not (message_filter is None or message_filter(msg)) or not msg.allow_user_subscription:
                  continue
  
              msgr_supported = msg.supported_messengers
              is_supported = (not msgr_supported or msgr.alias in msgr_supported)
  
              if not is_supported:
                  continue
  
              msg_alias = msg.alias
              msg_titles.setdefault('%s' % msg.title, []).append(msg_alias)
  
              msgr_to_msg[msgr_alias].update((msg_alias,))
              msgr_titles[msgr_title] = msgr_alias
  
      def sort_titles(titles):
          return OrderedDict(sorted([(k, v) for k, v in titles.items()], key=itemgetter(0)))
  
      msgr_titles = sort_titles(msgr_titles)
  
      user_prefs = OrderedDict()
  
      user_subscriptions = ['%s%s%s' % (pref.message_cls, _ALIAS_SEP, pref.messenger_cls)
                            for pref in Subscription.get_for_user(user)]
  
      for msg_title, msg_aliases in sort_titles(msg_titles).items():
  
          for __, msgr_alias in msgr_titles.items():
              msg_candidates = msgr_to_msg[msgr_alias].intersection(msg_aliases)
  
              alias = ''
              msg_supported = False
              subscribed = False
  
              if msg_candidates:
                  alias = '%s%s%s' % (msg_candidates.pop(), _ALIAS_SEP, msgr_alias)
                  msg_supported = True
                  subscribed = alias in user_subscriptions
  
              user_prefs.setdefault(msg_title, []).append((alias, msg_supported, subscribed))
  
      return msgr_titles.keys(), user_prefs