Class: TurboChat::Chat

Inherits:
ApplicationRecord show all
Defined in:
app/models/turbo_chat/chat.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.activity_window_seconds(window = nil, chat: nil) ⇒ Object

Raises:

  • (ArgumentError)


123
124
125
126
127
128
129
130
131
132
133
# File 'app/models/turbo_chat/chat.rb', line 123

def self.activity_window_seconds(window = nil, chat: nil)
  value = if window.nil?
            TurboChat::Configuration.config_value(:active_chat_window, default: 5.minutes, chat: chat)
          else
            window
          end
  seconds = value.to_i
  return seconds if seconds.positive?

  raise ArgumentError, "active chat window must be a positive duration"
end

.signal_window_seconds(window = nil, chat: nil) ⇒ Object

Raises:

  • (ArgumentError)


135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/turbo_chat/chat.rb', line 135

def self.signal_window_seconds(window = nil, chat: nil)
  value = if window.nil?
            TurboChat::Configuration.config_value(:signal_ttl_seconds, default: 60, chat: chat)
          else
            window
          end
  seconds = value.to_i
  return seconds if seconds.positive?

  raise ArgumentError, "signal ttl must be a positive duration"
end

Instance Method Details

#active?(window: nil, at: Time.current) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
# File 'app/models/turbo_chat/chat.rb', line 96

def active?(window: nil, at: Time.current)
  message_time = last_message_at
  return false if message_time.nil?

  message_time >= at - self.class.activity_window_seconds(window, chat: self)
end

#active_signals(window: nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/turbo_chat/chat.rb', line 38

def active_signals(window: nil)
  cutoff = Time.current - self.class.signal_window_seconds(window, chat: self)

  latest_message_at = chat_messages
                      .message
                      .where("created_at >= ?", cutoff)
                      .group(:participant_type, :participant_id)
                      .maximum(:created_at)

  latest_signal_ids = chat_messages.signal
                      .where("created_at >= ?", cutoff)
                      .group(:participant_type, :participant_id)
                      .maximum(:id)
                      .values

  return [] if latest_signal_ids.empty?

  signals = chat_messages.signal.where(id: latest_signal_ids).ordered

  signals.reject do |signal|
    participant_key = [signal.participant_type, signal.participant_id]
    last_msg_time = latest_message_at[participant_key]
    last_msg_time && last_msg_time >= signal.created_at
  end
end

#close!(at: Time.current) ⇒ Object



115
116
117
# File 'app/models/turbo_chat/chat.rb', line 115

def close!(at: Time.current)
  update!(closed_at: at)
end

#closed?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'app/models/turbo_chat/chat.rb', line 107

def closed?
  closed_at.present?
end

#find_active_membership(participant) ⇒ Object



84
85
86
87
88
89
90
# File 'app/models/turbo_chat/chat.rb', line 84

def find_active_membership(participant)
  if instance_variable_defined?(:@membership_lookup)
    membership_lookup[[participant.class.base_class.name, participant.id]]
  else
    chat_memberships.active.find_by(participant: participant)
  end
end

#inactive?(window: nil, at: Time.current) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'app/models/turbo_chat/chat.rb', line 103

def inactive?(window: nil, at: Time.current)
  !active?(window: window, at: at)
end

#last_message_atObject



92
93
94
# File 'app/models/turbo_chat/chat.rb', line 92

def last_message_at
  chat_messages.message.maximum(:created_at)
end

#membership_lookupObject



80
81
82
# File 'app/models/turbo_chat/chat.rb', line 80

def membership_lookup
  @membership_lookup ||= chat_memberships.active.index_by { |m| [m.participant_type, m.participant_id] }
end

#opened?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'app/models/turbo_chat/chat.rb', line 111

def opened?
  !closed?
end

#reopen!Object



119
120
121
# File 'app/models/turbo_chat/chat.rb', line 119

def reopen!
  update!(closed_at: nil)
end

#visible_messages(limit: :configured) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/turbo_chat/chat.rb', line 64

def visible_messages(limit: :configured)
  relation = chat_messages.timeline
  configured_limit = if limit == :configured
                       TurboChat::Configuration.config_value(:message_history_limit, default: 200, chat: self)
                     else
                       limit
                     end
  normalized_limit = normalize_message_limit(configured_limit)
  if normalized_limit
    recent_ids = relation.reorder(created_at: :desc, id: :desc).limit(normalized_limit).select(:id)
    relation = relation.where(id: recent_ids)
  end

  relation.ordered.preload(:participant)
end