Class: ConversationService

Inherits:
Object
  • Object
show all
Defined in:
app/services/conversation_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(user = nil) ⇒ ConversationService

Returns a new instance of ConversationService.


4
5
6
# File 'app/services/conversation_service.rb', line 4

def initialize(user=nil)
  @user = user
end

Instance Method Details

#all_for_user(filter = {}) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/services/conversation_service.rb', line 8

def all_for_user(filter={})
  conversation_filter = {}
  unless filter[:only_after].nil?
    conversation_filter = \
      "conversations.created_at >= ?", filter[:only_after]
  end

  visibility_filter = if filter[:unread]
                        {
                          person_id: @user.person_id,
                          unread:    1
                        }
                      else
                        {person_id: @user.person_id}
                      end

  Conversation.where(conversation_filter)
              .joins(:conversation_visibilities)
              .where(conversation_visibilities: visibility_filter)
              .all
end

#build(subject, text, recipients) ⇒ Object


30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/services/conversation_service.rb', line 30

def build(subject, text, recipients)
  person_ids = @user.contacts
                    .mutual
                    .where(person_id: recipients)
                    .pluck(:person_id)

  opts = {
    subject:         subject,
    message:         {text: text},
    participant_ids: person_ids
  }

  @user.build_conversation(opts)
end

#destroy!(conversation_guid) ⇒ Object


55
56
57
58
# File 'app/services/conversation_service.rb', line 55

def destroy!(conversation_guid)
  conversation = find!(conversation_guid)
  conversation.destroy!
end

#find!(conversation_guid) ⇒ Object


45
46
47
48
49
50
51
52
53
# File 'app/services/conversation_service.rb', line 45

def find!(conversation_guid)
  conversation = Conversation.find_by!(guid: conversation_guid)
  @user.conversations
       .joins(:conversation_visibilities)
       .where(conversation_visibilities: {
                person_id:       @user.person_id,
                conversation_id: conversation.id
              }).first!
end

#get_visibility(conversation_guid) ⇒ Object


60
61
62
63
64
65
66
# File 'app/services/conversation_service.rb', line 60

def get_visibility(conversation_guid)
  conversation = find!(conversation_guid)
  ConversationVisibility.where(
    person_id:       @user.person.id,
    conversation_id: conversation.id
  ).first!
end