1 # Copyright (C) 2007, 2008, 2009, 2010 Heiko Bernloehr (FreeIT.de).
3 # This file is part of ECS.
5 # ECS is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of
8 # the License, or (at your option) any later version.
10 # ECS is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Affero General Public License for more details.
15 # You should have received a copy of the GNU Affero General Public
16 # License along with ECS. If not, see <http://www.gnu.org/licenses/>.
19 class Membership < ActiveRecord::Base
20 belongs_to :participant
22 belongs_to :community_with_reduced_attributes,
23 :class_name => "Community",
24 :foreign_key => "community_id",
25 :select => "name, description, id"
26 has_many :messages, :through => :membership_messages
27 has_many :membership_messages, :dependent => :destroy
29 after_create :postroute
31 # returns memberships of the relation between a participant and a message
32 # if no relationship then returns empty array.
33 scope :receiver, lambda { |participant_id,message_id| {
34 :joins => [:participant, {:membership_messages => :message}],
35 :conditions => { :participants => { :id => participant_id }, :messages => { :id => message_id } } } }
37 scope :receivers, lambda { |message_id| {
38 :joins => [:membership_messages => :message],
39 :select => :memberships.to_s+".id" + ", community_id, participant_id",
40 :conditions => { :messages => { :id => message_id } } } }
42 scope :for_participant_id, lambda { |participant_id| {
43 :joins => [:participant],
44 :conditions => { :participants => { :id => participant_id } } } }
46 scope :for_participant_id_and_community_id, lambda { |participant_id,community_id| {
47 :joins => [:participant, :community],
48 :conditions => { :participants => { :id => participant_id }, :communities => { :id => community_id } } } }
50 def self.senders(participant, message)
52 Community.for_participant(participant).for_message(message).uniq.each do |comm|
53 sender_mids << Membership.find_by_participant_id_and_community_id(participant.id,comm.id)
62 def self.memberships(participant,itsyou=false,filter=nil)
64 Membership.for_participant_id(participant.id).each do |membership|
65 community= lambda { |memb|
66 attribs = memb.community_with_reduced_attributes.attributes
67 id = attribs["id"]; attribs.delete("id"); attribs["cid"] = id
70 logger.debug "**** Membership::memberships: community: #{community.inspect}"
72 participants_with_reduced_attribs= membership.community.participants.itsyou(participant.id).without_anonymous.reduced_attributes
73 logger.debug "**** Membership::memberships: participants_with_reduced_attribs: #{participants_with_reduced_attribs.inspect}"
75 participants_with_reduced_attribs= case
77 membership.community.participants.order_id_asc.reduced_attributes
78 when filter[:mainparticipants]
79 membership.community.participants.mainparticipants_with_reduced_attributes
80 when filter[:subparticipants]
81 membership.community.participants.subparticipants_with_reduced_attributes
82 when filter[:anonymous]
83 membership.community.participants.anonymous_participants_with_reduced_attributes
85 membership.community.participants.mainparticipants_with_reduced_attributes
88 participants= participants_with_reduced_attribs.map do |p|
89 attribs = p.attributes
90 attribs["mid"] = Membership.for_participant_id_and_community_id(p.id, membership.community.id).first.id
91 attribs["org"] = {"name" => p.organization.name, "abbr" => p.organization.abrev}
92 attribs["itsyou"] = p.id == participant.id
94 attribs["type"] = p.ptype
96 attribs.delete("organization_id")
97 attribs.delete("ptype")
100 logger.debug "**** Membership::memberships: participants: #{participants.inspect}"
102 { :community => community,
103 :participants => participants
111 # generate created events for all messages connected to this community membership
113 community.messages.map{|m| m.ressource.postroute ? m : nil}.compact.each do |msg|
115 Event.make(:event_type_name => EvType.find_by_name("created").name, :participant => participant, :message => msg)
116 logger.info "**** postrouting message.id=#{msg.id} to participant:#{participant.name} (pid:#{participant.id})"