You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: user_notifications_extension.rb
    path of file: ./repos/discourse/plugins/chat/lib/chat
    
    the code of the file until where you have to start completion: # frozen_string_literal: true

module Chat
  module UserNotificationsExtension
    def chat_summary(user, _ = nil)
      guardian = Guardian.new(user)
      return unless guardian.can_chat?

      @messages =
        Chat::Message
          .joins(:user, :chat_channel)
          .where.not(user: user)
          .where("chat_messages.created_at > ?", 1.week.ago)
          .joins("LEFT OUTER JOIN chat_mentions cm ON cm.chat_message_id = chat_messages.id")
          .joins("LEFT OUTER JOIN chat_mention_notifications cmn ON cmn.chat_mention_id = cm.id")
          .joins("LEFT OUTER JOIN notifications n ON cmn.notification_id = n.id")
          .joins(
            "INNER JOIN user_chat_channel_memberships uccm ON uccm.chat_channel_id = chat_channels.id",
          )
          .where(<<~SQL, user_id: user.id)
          uccm.user_id = :user_id AND
          (uccm.last_read_message_id IS NULL OR chat_messages.id > uccm.last_read_message_id) AND
          (uccm.last_unread_mention_when_emailed_id IS NULL OR chat_messages.id > uccm.last_unread_mention_when_emailed_id) AND
          (
            (n.user_id = :user_id AND cmn.notification_id IS NOT NULL AND uccm.following IS true AND chat_channels.chatable_type = 'Category') OR
            (chat_channels.chatable_type = 'DirectMessage')
          )
        SQL
          .to_a

      return if @messages.empty?
      @grouped_messages = @messages.group_by { |message| message.chat_channel }
      @grouped_messages =
        @grouped_messages.select { |channel, _| guardian.can_join_chat_channel?(channel) }
      return if @grouped_messages.empty?

      @grouped_messages.each do |chat_channel, messages|
        @grouped_messages[chat_channel] = messages.sort_by(&:created_at)
      end
      @user = user
      @user_tz = UserOption.user_tzinfo(user.id)
      @display_usernames = SiteSetting.priorit
    
    def chat_summary(user, _ = nil)
      guardian = Guardian.new(user)
      return unless guardian.can_chat?

      @messages =
        Chat::Message
          .joins(:user, :chat_channel)
          .where.not(user: user)
          .where("chat_messages.created_at > ?", 1.week.ago)
          .joins("LEFT OUTER JOIN chat_mentions cm ON cm.chat_message_id = chat_messages.id")
          .joins("LEFT OUTER JOIN chat_mention_notifications cmn ON cmn.chat_mention_id = cm.id")
          .joins("LEFT OUTER JOIN notifications n ON cmn.notification_id = n.id")
          .joins(
            "INNER JOIN user_chat_channel_memberships uccm ON uccm.chat_channel_id = chat_channels.id",
          )
          .where(<<~SQL, user_id: user.id)
          uccm.user_id = :user_id AND
          (uccm.last_read_message_id IS NULL OR chat_messages.id > uccm.last_read_message_id) AND
          (uccm.last_unread_mention_when_emailed_id IS NULL OR chat_messages.id > uccm.last_unread_mention_when_emailed_id) AND
          (
            (n.user_id = :user_id AND cmn.notification_id IS NOT NULL AND uccm.following IS true AND chat_channels.chatable_type = 'Category') OR
            (chat_channels.chatable_type = 'DirectMessage')
          )
        SQL
          .to_a

      return if @messages.empty?
      @grouped_messages = @messages.group_by { |message| message.chat_channel }
      @grouped_messages =
        @grouped_messages.select { |channel, _| guardian.can_join_chat_channel?(channel) }
      return if @grouped_messages.empty?

      @grouped_messages.each do |chat_channel, messages|
        @grouped_messages[chat_channel] = messages.sort_by(&:created_at)
      end
      @user = user
      @user_tz = UserOption.user_tzinfo(user.id)
      @display_usernames = SiteSetting.prioritize_username_in_ux || !SiteSetting.enable_names

      build_summary_for(user)
      @preferences_path = "#{Discourse.base_url}/my/preferences/chat"

      opts = {
        from_alias: I18n.t("user_notifications.chat_summary.from", site_name: Email.site_title),
        subject: summary_subject(user, @grouped_messages),
      }

      build_email(user.email, opts)
    end