1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
109
110
111
112
113
114
115
116
117
118
119
120
|
# Copyright (C) 2007, 2008, 2009, 2010 Heiko Bernloehr (FreeIT.de).
#
# This file is part of ECS.
#
# ECS is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# ECS is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with ECS. If not, see <http://www.gnu.org/licenses/>.
class Membership < ApplicationRecord
belongs_to :participant
belongs_to :community
belongs_to :community_with_reduced_attributes,
-> { select("name, description, id") },
:class_name => "Community",
:foreign_key => "community_id"
has_many :messages, :through => :membership_messages
has_many :membership_messages, :dependent => :destroy
after_create :postroute
# returns memberships of the relation between a participant and a message
# if no relationship then returns empty array.
scope :receiver, lambda { |participant_id,message_id|
joins(:participant, :membership_messages => :message).
where(:participants => { :id => participant_id }, :messages => { :id => message_id })}
scope :receivers, lambda { |message_id|
joins(:membership_messages => :message).
select(:memberships.to_s+".id" + ", community_id, participant_id").
where(:messages => { :id => message_id })}
scope :for_participant_id, lambda { |participant_id|
joins(:participant).
where(:participants => { :id => participant_id })}
scope :for_participant_id_and_community_id, lambda { |participant_id,community_id|
joins(:participant, :community).
where(:participants => { :id => participant_id }, :communities => { :id => community_id })}
def self.senders(participant, message)
sender_mids=[]
Community.for_participant(participant).for_message(message).distinct.each do |comm|
sender_mids << Membership.find_by_participant_id_and_community_id(participant.id,comm.id)
end
if sender_mids.empty?
[]
else
sender_mids.flatten
end
end
def self.memberships(participant,itsyou=false,filter=nil)
memberships = []
Membership.for_participant_id(participant.id).each do |membership|
community= lambda { |memb|
attribs = memb.community_with_reduced_attributes.attributes
id = attribs["id"]; attribs.delete("id"); attribs["cid"] = id
attribs
}.call(membership)
logger.debug "**** Membership::memberships: community: #{community.inspect}"
if itsyou
participants_with_reduced_attribs= membership.community.participants.itsyou(participant.id).without_anonymous.reduced_attributes
logger.debug "**** Membership::memberships: participants_with_reduced_attribs: #{participants_with_reduced_attribs.inspect}"
else
participants_with_reduced_attribs= case
when filter[:all]
membership.community.participants.order_id_asc.reduced_attributes
when filter[:mainparticipants]
membership.community.participants.mainparticipants_with_reduced_attributes
when filter[:subparticipants]
membership.community.participants.subparticipants_with_reduced_attributes
when filter[:anonymous]
membership.community.participants.anonymous_participants_with_reduced_attributes
else
membership.community.participants.mainparticipants_with_reduced_attributes
end
end
participants= participants_with_reduced_attribs.map do |p|
attribs = p.attributes
attribs["mid"] = Membership.for_participant_id_and_community_id(p.id, membership.community.id).first.id
attribs["org"] = {"name" => p.organization.name, "abbr" => p.organization.abrev}
attribs["itsyou"] = p.id == participant.id
attribs["pid"] = p.id
attribs["type"] = p.ptype
attribs.delete("id")
attribs.delete("organization_id")
attribs.delete("ptype")
attribs
end
logger.debug "**** Membership::memberships: participants: #{participants.inspect}"
memberships <<
{ :community => community,
:participants => participants
}
end
memberships
end
private
# generate created events for all messages connected to this community membership
def postroute
community.messages.map{|m| m.ressource.postroute ? m : nil}.compact.each do |msg|
messages << msg
Event.make(:event_type_name => EvType.find_by_name("created").name, :participant => participant, :message => msg)
logger.info "**** postrouting message.id=#{msg.id} to participant:#{participant.name} (pid:#{participant.id})"
end
end
end
|