def do_update_message_flags(user_profile: UserProfile, client: Client, operation: str, flag: str, messages: List[int]) -> int:
    valid_flags = [item for item in UserMessage.flags if (item not in UserMessage.NON_API_FLAGS)]
    if (flag not in valid_flags):
        raise JsonableError(_("Invalid flag: '{}'").format(flag))
    if (flag in UserMessage.NON_EDITABLE_FLAGS):
        raise JsonableError(_("Flag not editable: '{}'").format(flag))
    flagattr = getattr(UserMessage.flags, flag)
    msgs = UserMessage.objects.filter(user_profile=user_profile, message__id__in=messages)
    if (msgs.count() == 0):
        if (not (len(messages) == 1)):
            raise JsonableError(_('Invalid message(s)'))
        if (flag != 'starred'):
            raise JsonableError(_('Invalid message(s)'))
        message = access_message(user_profile, messages[0])[0]
        UserMessage.objects.create(user_profile=user_profile, message=message, flags=(UserMessage.flags.historical | UserMessage.flags.read))
    if (operation == 'add'):
        count = msgs.update(flags=F('flags').bitor(flagattr))
    elif (operation == 'remove'):
        count = msgs.update(flags=F('flags').bitand((~ flagattr)))
    else:
        raise AssertionError('Invalid message flags operation')
    event = {'type': 'update_message_flags', 'operation': operation, 'flag': flag, 'messages': messages, 'all': False}
    send_event(user_profile.realm, event, [user_profile.id])
    if ((flag == 'read') and (operation == 'add')):
        event_time = timezone_now()
        do_clear_mobile_push_notifications_for_ids([user_profile.id], messages)
        do_increment_logging_stat(user_profile, COUNT_STATS['messages_read::hour'], None, event_time, increment=count)
        do_increment_logging_stat(user_profile, COUNT_STATS['messages_read_interactions::hour'], None, event_time, increment=min(1, count))
    return count
def do_update_message_flags(user_profile: UserProfile, client: Client, operation: str, flag: str, messages: List[int]) -> int:
    valid_flags = [item for item in UserMessage.flags if (item not in UserMessage.NON_API_FLAGS)]
    if (flag not in valid_flags):
        raise JsonableError(_("Invalid flag: '{}'").format(flag))
    if (flag in UserMessage.NON_EDITABLE_FLAGS):
        raise JsonableError(_("Flag not editable: '{}'").format(flag))
    flagattr = getattr(UserMessage.flags, flag)
    msgs = UserMessage.objects.filter(user_profile=user_profile, message__id__in=messages)
    if (msgs.count() == 0):
        if (not (len(messages) == 1)):
            raise JsonableError(_('Invalid message(s)'))
        if (flag != 'starred'):
            raise JsonableError(_('Invalid message(s)'))
        message = access_message(user_profile, messages[0])[0]
        UserMessage.objects.create(user_profile=user_profile, message=message, flags=(UserMessage.flags.historical | UserMessage.flags.read))
    if (operation == 'add'):
        count = msgs.update(flags=F('flags').bitor(flagattr))
    elif (operation == 'remove'):
        count = msgs.update(flags=F('flags').bitand((~ flagattr)))
    else:
        raise AssertionError('Invalid message flags operation')
    event = {'type': 'update_message_flags', 'operation': operation, 'flag': flag, 'messages': messages, 'all': False}
    send_event(user_profile.realm, event, [user_profile.id])
    if ((flag == 'read') and (operation == 'add')):
        event_time = timezone_now()
        do_clear_mobile_push_notifications_for_ids([user_profile.id], messages)
        do_increment_logging_stat(user_profile, COUNT_STATS['messages_read::hour'], None, event_time, increment=count)
        do_increment_logging_stat(user_profile, COUNT_STATS['messages_read_interactions::hour'], None, event_time, increment=min(1, count))
    return count