Strip spaces.
[ecs.git] / app / models / membership_message.rb
blobb62da0a7ea9733ad20961945dcec8d4c7348cd84
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 MembershipMessage < ActiveRecord::Base
20   belongs_to  :membership
21   belongs_to  :message
23   # Populate the memberships_messages jointable if sender joins community with receiver
24   def self.populate_jointable(record, x_ecs_receiver_memberships, x_ecs_receiver_communities, sender_participant)
25     rec_mids = extract_x_ecs_receiver_memberships(x_ecs_receiver_memberships)
26     rec_cids = extract_x_ecs_receiver_communities(x_ecs_receiver_communities)
27     if rec_cids.blank? and rec_mids.blank?
28       raise Ecs::MissingReceiverHeaderException,
29             "You must at least specify one of X-EcsReceiverMemberships or X-EcsReceiverCommunities header\r\n"
30     end
32     pop_succ = false
33     memberships_for_sender_participant_id = Membership.for_participant_id(sender_participant.id)
35     rec_mids.each do |rmid|
36       # Test if sender joins same community as receiver
37       if memberships_for_sender_participant_id.map{|m| m.community.id}.include?(Membership.find(rmid).community.id)
38         Membership.find(rmid).messages << record unless MembershipMessage.find_by_membership_id_and_message_id(rmid, record.id)
39         pop_succ = true
40       end
41     end
43     rec_cids.each do |rcid|
44       # Test if sender joins same community as receiver
45       if memberships_for_sender_participant_id.map{|m| m.community.id}.include?(rcid)
46         Community.find(rcid).memberships.each do |membership|
47           if !MembershipMessage.find_by_membership_id_and_message_id(membership.id, record.id) and # relation already made
48              (sender_participant.community_selfrouting or # address sender through community
49                membership.participant.id != sender_participant.id) # address sender through community
50               membership.messages << record
51           end
52         end
53         pop_succ = true
54       end
55     end
57     unless pop_succ
58       raise Ecs::AuthorizationException,
59             "You are not joining at least one of the community to which you are addressing.\r\n" +
60             "or\r\n" +
61             "You are not joining at least one of the same community as the receiving membership.\r\n"
62     end
63   rescue ActiveRecord::RecordNotFound
64     raise Ecs::InvalidMessageException,
65           "Membership id in X-EcsReceiverMemberships header not found."
66   end
68   # Depopulate the memberships_messages jointable
69   def self.de_populate_jointable(record)
70     record.membership_messages.each do |mm|
71       mm.destroy
72     end
73   end
75   def self.extract_x_ecs_receiver_communities(erc)
76     receiver_communities= []
77     erc.split(',').map {|e| e.strip}.each do |comm_str|
78       if comm_str =~ /\d{#{comm_str.length}}/
79         # comm_str has only digits
80         receiver_communities << comm_str.to_i
81       else
82         # comm_str should be a community name
83         comm= Community.find_by_name(comm_str)
84         if comm == nil then
85           raise Ecs::InvalidMessageException, "community id/name in X-EcsReceiverCommunities header not found: #{comm_str}"
86         end
87         receiver_communities << comm.id
88       end
89     end unless erc.blank?
90     receiver_communities.uniq!
91     receiver_communities
92   end
94   def self.extract_x_ecs_receiver_memberships(erm)
95     receiver_memberships= []
96     erm.split(',').map {|e| e.strip}.each do |memb_str|
97       if memb_str =~ /\d{#{memb_str.length}}/
98         # memb_str has only digits
99         #receiver_memberships.concat Membership.find(memb_str.to_i).community.memberships.map{ |m| m.id }
100         receiver_memberships << memb_str.to_i if  Membership.find(memb_str.to_i)
101       else
102         # memb_str is invalid, because it's not an integer value
103         # raise Exception
104       end
105     end unless erm.blank?
106     receiver_memberships.uniq!
107     receiver_memberships
108   end
111 private
113   # Deletes all records with relations between a record and the given
114   # memberships or all record with relation to the given message
115   # (memberships=nil)
116   def self.delete_relations(message, memberships=nil)
117     if memberships
118       memberships.each do |m|
119         destroy_all ["membership_id = ? and message_id = ?", m.id, message.id]
120       end
121     else
122       destroy_all ["message_id = ?", message.id]
123     end
124   end