def do_mark_stream_messages_as_read(user_profile: UserProfile, client: Client, stream: Stream, topic_name: Optional[str]=None) -> int:
    log_statsd_event('mark_stream_as_read')
    msgs = UserMessage.objects.filter(user_profile=user_profile)
    recipient = stream.recipient
    msgs = msgs.filter(message__recipient=recipient)
    if topic_name:
        msgs = filter_by_topic_name_via_message(query=msgs, topic_name=topic_name)
    msgs = msgs.extra(where=[UserMessage.where_unread()])
    message_ids = list(msgs.values_list('message__id', flat=True))
    count = msgs.update(flags=F('flags').bitor(UserMessage.flags.read))
    event = dict(type='update_message_flags', operation='add', flag='read', messages=message_ids, all=False)
    event_time = timezone_now()
    send_event(user_profile.realm, event, [user_profile.id])
    do_clear_mobile_push_notifications_for_ids([user_profile.id], message_ids)
    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_mark_stream_messages_as_read(user_profile: UserProfile, client: Client, stream: Stream, topic_name: Optional[str]=None) -> int:
    log_statsd_event('mark_stream_as_read')
    msgs = UserMessage.objects.filter(user_profile=user_profile)
    recipient = stream.recipient
    msgs = msgs.filter(message__recipient=recipient)
    if topic_name:
        msgs = filter_by_topic_name_via_message(query=msgs, topic_name=topic_name)
    msgs = msgs.extra(where=[UserMessage.where_unread()])
    message_ids = list(msgs.values_list('message__id', flat=True))
    count = msgs.update(flags=F('flags').bitor(UserMessage.flags.read))
    event = dict(type='update_message_flags', operation='add', flag='read', messages=message_ids, all=False)
    event_time = timezone_now()
    send_event(user_profile.realm, event, [user_profile.id])
    do_clear_mobile_push_notifications_for_ids([user_profile.id], message_ids)
    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