Hide public communty.
[ecs.git] / app / models / participant.rb
blobdff6160072d2fcf7c211692e5c336acc6a927d96
1 # Copyright (C) 2007, 2008, 2009, 2010, 2014 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 Participant < ApplicationRecord
20   TTL = 1.hour # how long an anonymous participant lives
21   TYPE={ :main => "main", :sub => "sub", :anonym => "anonym" }
23   after_destroy :delete_messages
25   belongs_to  :organization
26   # TODO: warn user about possible deletions of messages.
27   has_many    :memberships, :dependent => :destroy
28   has_many    :communities, :through => :memberships
29   has_many    :identities, :dependent => :destroy
30   has_many    :events, :dependent => :destroy
31   has_many    :childs,
32               -> { order 'id ASC' },
33               :class_name => "Subparticipant",
34               :foreign_key => "parent_id",
35               :dependent => :destroy
36   has_one     :subparticipant, :dependent => :destroy
38   validates_presence_of :name, :organization_id
39   validates_uniqueness_of :name
41   accepts_nested_attributes_for :identities, :allow_destroy => true, :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
42   accepts_nested_attributes_for :communities, :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
43   accepts_nested_attributes_for :subparticipant, :allow_destroy => true
45   scope :order_id_asc, -> { order("participants.id ASC") }
46   scope :without_anonymous, -> { where(:anonymous => false) }
47   scope :anonymous, -> { where(:anonymous => true) }
48   scope :for_message, lambda { |message|
49     joins(:memberships => {:membership_messages => :message}).
50     where("messages.id = ?", message.id) }
51   scope :for_community, lambda { |community|
52     joins(:memberships => :community).
53     where("communities.id = ?", community.id) }
54   scope :itsyou, lambda { |itsyoupid| where(:id => itsyoupid) }
55   scope :only_subparticipants, -> { where(:ptype => TYPE[:sub]) }
56   scope :only_mainparticipants, -> { where(:ptype => TYPE[:main]) }
57   scope :only_anonymous, -> { where(:ptype => TYPE[:anonym]) }
59   def self.reduced_attributes
60     all.select("participants.id, participants.name, participants.description, participants.email, participants.dns, participants.organization_id, participants.ptype")
61   end
63   def self.mainparticipants_with_reduced_attributes
64     only_mainparticipants.order_id_asc.reduced_attributes
65   end
67   def self.subparticipants_with_reduced_attributes
68     only_subparticipants.order_id_asc.reduced_attributes
69   end
71   def self.anonymous_participants_with_reduced_attributes
72     only_anonymous.order_id_asc.reduced_attributes
73   end
75   def destroy_receiver_messages
76     Message.for_participant_receiver(self).each do |m|
77       m.destroy_as_receiver(self)
78     end
79   end
81   def destroy_sender_messages
82     Message.for_participant_sender(self).each do |m|
83       m.destroy_as_sender
84     end
85   end
87   def destroy_events
88     self.events.each do |e|
89       e.destroy
90     end
91   end
93   def mainparticipant?
94     if not anonymousparticipant? and subparticipant.nil?
95       true
96     else
97       false
98     end
99   end
101   def subparticipant?
102     if not anonymousparticipant? and not subparticipant.nil?
103       true
104     else
105       false
106     end
107   end
109   def anonymousparticipant?
110     anonymous?
111   end
113   # test if the participant is the initial sender of the message in question.
114   def sender?(message)
115     if message.sender == id
116       true
117     else
118       false
119     end
120   end
122   def receiver?(message)
123     not Membership.receiver(id, message.id).empty?
124   end
126   def events?
127     self.events_.blank? ? false : true
128   end
130   def anonymous?
131     anonymous
132   end
134   def self.generate_anonymous_participant
135     cookie = Digest::SHA1.hexdigest('something secret'+Time.now.to_s+rand.to_s)
136     params = {
137         "name"=> "#{cookie}",
138         "identities_attributes"=>{"0"=>{"name"=>"#{cookie}", "description"=>"Cookie Identifier"}},
139         "community_ids"=>[Community.find_by_name("public").id],
140         "description"=>"Anonymous Participant",
141         "dns"=>"N/A",
142         "organization_id"=>Organization.find_by_name("not available").id,
143         "email"=>"N/A",
144         "ttl"=> DateTime.now.utc + TTL,
145         "anonymous"=>true,
146         "ptype"=>TYPE[:anonym]
147     }
148     ap = new(params)
149     ap.save!
150     return ap, cookie
151   end
153   def self.touch_ttl(participant)
154     participant.ttl = DateTime.now.utc + TTL
155     participant.save
156   end
158   def mid(community)
159     Membership.for_participant_id_and_community_id(self, community.id).first.id
160   end
162 private
164   def delete_messages
165     Message.where(["sender = ?", self.id]).destroy_all
166   end