Class: Libertree::Model::Message

Inherits:
Object
  • Object
show all
Includes:
HasDisplayText
Defined in:
lib/libertree/model/message.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasDisplayText

#glimpse, #text_as_html, #text_to_nodeset

Class Method Details

.create_with_recipients(args) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/libertree/model/message.rb', line 114

def self.create_with_recipients(args)
  message = self.create(
    sender_member_id: args[:sender_member_id],
    remote_id: args[:remote_id],
    text: args[:text]
  )
  sender_member = Model::Member[ args[:sender_member_id].to_i ]

  recipient_member_ids = Array(args[:recipient_member_ids])
  recipient_member_ids.each do |member_id|
    DB.dbh[ "INSERT INTO message_recipients ( message_id, member_id ) VALUES ( ?, ? )", message.id, member_id.to_i ].get
    m = Member[member_id]
    if m.
      a = m.
      if ! a.ignoring?(sender_member)
        a.notify_about  'type' => 'message', 'message_id' => message.id
        if a.email && a.settings.forward_dms_via_email
          message.forward_via_email(a)
        end
      end
    end
  end
  message.distribute  if sender_member.local?
  message
end

Instance Method Details

#active_local_participantsObject

the subset of participants who have not deleted the message



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/libertree/model/message.rb', line 68

def active_local_participants
  recipients = Member.s(%{SELECT DISTINCT m.* FROM members m
                          JOIN accounts a ON (a.id = m.account_id)
                          JOIN message_recipients mr ON (m.id = mr.member_id)
                          WHERE mr.message_id = ?
                          AND NOT mr.deleted
                         }, self.id)
  if ! self.deleted && self.sender.local?
    ( recipients + [self.sender] ).uniq
  else
    recipients
  end
end

#delete_cascadeObject



140
141
142
# File 'lib/libertree/model/message.rb', line 140

def delete_cascade
  DB.dbh[ "SELECT delete_cascade_message(?)", self.id ].get
end

#delete_for_participant(local_member) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/libertree/model/message.rb', line 82

def delete_for_participant(local_member)
  # Delete the message for the local participant only, i.e. by
  # removing the assignment.  If this is the last local
  # participant, delete the whole message.  Note that other
  # recipients / the sender will not see a change in the number
  # of recipients when one of the recipients "deletes" their
  # "copy" of the Message
  participants = active_local_participants

  # not authorised to delete
  return  if participants.empty?

  if participants == [local_member]
    # This is the only member interested in this message; delete it completely.
    self.delete_cascade
  else
    # there are other local members with a pointer to the
    # message. Only mark as deleted for this recipient.
    DB.dbh[ "UPDATE message_recipients SET deleted=true WHERE message_id = ? AND member_id = ?",
            self.id, local_member.id ].get
    if local_member == self.sender
      self.deleted = true
      self.save
    end
  end
  return true
end

#distributeObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/libertree/model/message.rb', line 13

def distribute
  trees = self.recipients.reduce(Set.new) { |_trees, recipient|
    if recipient.tree
      _trees << recipient.tree
    end
    _trees
  }
  recipient_ids = self.recipients.map(&:id)

  trees.each do |tree|
    Libertree::Model::Job.create(
      {
        task: 'request:MESSAGE',
        params: {
          'message_id'           => self.id,
          'server_id'            => tree.id,
          'recipient_member_ids' => recipient_ids,
        }.to_json,
      }
    )
  end
end

#forward_via_email(account) ⇒ Object

forward direct message to the given email address of the provided account



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/libertree/model/message.rb', line 37

def forward_via_email()
  return  unless 
  return  unless self.visible_to?()

  Libertree::Model::Job.create(
    task: 'forward-via-email',
    params: {
      'username' => .username,
      'message_id' => self.id
    }.to_json
  )
end

#recipientsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/libertree/model/message.rb', line 50

def recipients
  return @recipients  if @recipients
  @recipients = Member.s(
    %{
      SELECT
        m.*
      FROM
          members m
        , message_recipients mr
      WHERE
        mr.message_id = ?
        AND m.id = mr.member_id
    },
    self.id
  )
end

#senderObject Also known as: member



8
9
10
# File 'lib/libertree/model/message.rb', line 8

def sender
  @sender ||= Member[self.sender_member_id]
end

#to_hashObject



144
145
146
147
148
149
150
151
# File 'lib/libertree/model/message.rb', line 144

def to_hash
  {
    'id'           => self.id,
    'time_created' => self.time_created,
    'to'           => self.recipients.map(&:name_display),
    'text'         => self.text,
  }
end

#visible_to?(account) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/libertree/model/message.rb', line 110

def visible_to?()
  self.sender == .member || recipients.include?(.member)
end