    def filter_archived(list, user, archived: true)
      list = list.joins(<<~SQL)
      LEFT JOIN group_archived_messages gm ON gm.topic_id = topics.id
      LEFT JOIN user_archived_messages um
        ON um.user_id = #{user.id.to_i}
        AND um.topic_id = topics.id
      SQL

      list =
        if archived
          list.where("um.user_id IS NOT NULL OR gm.topic_id IS NOT NULL")
        else
          list.where("um.user_id IS NULL AND gm.topic_id IS NULL")
        end

      list
    end