Class: Convo::Topic
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Convo::Topic
- Includes:
- MetadataExtension
- Defined in:
- app/models/convo/topic.rb
Class Method Summary collapse
- .find_or_create_by_participant_ids(model_class, participant_ids, metadata: {}) ⇒ Object
- .send_message(sender:, recipients:, body:, context: nil, tenant: nil, metadata: {}) ⇒ Object
Class Method Details
.find_or_create_by_participant_ids(model_class, participant_ids, metadata: {}) ⇒ Object
27 28 29 30 31 32 33 |
# File 'app/models/convo/topic.rb', line 27 def self.find_or_create_by_participant_ids(model_class, participant_ids, metadata: {}) sorted_ids = participant_ids.map(&:to_i).sort transaction do find_by(model_class:, participant_ids: sorted_ids) || create!(model_class:, participant_ids: sorted_ids, metadata:) end end |
.send_message(sender:, recipients:, body:, context: nil, tenant: nil, metadata: {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/models/convo/topic.rb', line 35 def self.(sender:, recipients:, body:, context: nil, tenant: nil, metadata: {}) recipients = Array(recipients) validate_participants_class!(sender, recipients) participant_ids = ([ sender.id ] + recipients.map(&:id)).sort topic = find_or_create_by_participant_ids( sender.class.name, participant_ids, metadata: ) transaction do recipients.each do |recipient| topic..create!( sender:, recipient:, body:, context:, tenant:, metadata: ) end end topic end |