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
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")
63 def self.mainparticipants_with_reduced_attributes
64 only_mainparticipants.order_id_asc.reduced_attributes
67 def self.subparticipants_with_reduced_attributes
68 only_subparticipants.order_id_asc.reduced_attributes
71 def self.anonymous_participants_with_reduced_attributes
72 only_anonymous.order_id_asc.reduced_attributes
75 def destroy_receiver_messages
76 Message.for_participant_receiver(self).each do |m|
77 m.destroy_as_receiver(self)
81 def destroy_sender_messages
82 Message.for_participant_sender(self).each do |m|
88 self.events.each do |e|
94 if not anonymousparticipant? and subparticipant.nil?
102 if not anonymousparticipant? and not subparticipant.nil?
109 def anonymousparticipant?
113 # test if the participant is the initial sender of the message in question.
115 if message.sender == id
122 def receiver?(message)
123 not Membership.receiver(id, message.id).empty?
127 self.events_.blank? ? false : true
134 def self.generate_anonymous_participant
135 cookie = Digest::SHA1.hexdigest('something secret'+Time.now.to_s+rand.to_s)
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",
142 "organization_id"=>Organization.find_by_name("not available").id,
144 "ttl"=> DateTime.now.utc + TTL,
146 "ptype"=>TYPE[:anonym]
153 def self.touch_ttl(participant)
154 participant.ttl = DateTime.now.utc + TTL
159 Membership.for_participant_id_and_community_id(self, community.id).first.id
165 Message.where(["sender = ?", self.id]).destroy_all