Class: TurboChat::Chat
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- TurboChat::Chat
- Defined in:
- app/models/turbo_chat/chat.rb
Class Method Summary collapse
- .activity_window_seconds(window = nil, chat: nil) ⇒ Object
- .signal_window_seconds(window = nil, chat: nil) ⇒ Object
Instance Method Summary collapse
- #active?(window: nil, at: Time.current) ⇒ Boolean
- #active_signals(window: nil) ⇒ Object
- #close!(at: Time.current) ⇒ Object
- #closed? ⇒ Boolean
- #find_active_membership(participant) ⇒ Object
- #inactive?(window: nil, at: Time.current) ⇒ Boolean
- #last_message_at ⇒ Object
- #membership_lookup ⇒ Object
- #opened? ⇒ Boolean
- #reopen! ⇒ Object
- #visible_messages(limit: :configured) ⇒ Object
Class Method Details
.activity_window_seconds(window = nil, chat: nil) ⇒ Object
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
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
96 97 98 99 100 101 |
# File 'app/models/turbo_chat/chat.rb', line 96 def active?(window: nil, at: Time.current) = return false if .nil? >= 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) = . .where("created_at >= ?", cutoff) .group(:participant_type, :participant_id) .maximum(:created_at) latest_signal_ids = .signal .where("created_at >= ?", cutoff) .group(:participant_type, :participant_id) .maximum(:id) .values return [] if latest_signal_ids.empty? signals = .signal.where(id: latest_signal_ids).ordered signals.reject do |signal| participant_key = [signal.participant_type, signal.participant_id] last_msg_time = [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
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
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_at ⇒ Object
92 93 94 |
# File 'app/models/turbo_chat/chat.rb', line 92 def ..maximum(:created_at) end |
#membership_lookup ⇒ Object
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
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 (limit: :configured) relation = .timeline configured_limit = if limit == :configured TurboChat::Configuration.config_value(:message_history_limit, default: 200, chat: self) else limit end normalized_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 |