Class: EltController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/elt_controller.rb

Overview

This is the central element to parlement

An element is just the name for a poll/message/issue

Instance Method Summary collapse

Instance Method Details

#choicesObject



254
255
256
257
258
# File 'app/controllers/elt_controller.rb', line 254

def choices
	@elt = Elt.find params[:id]
rescue ActiveRecord::RecordNotFound => e
	render :status => 404, :file => "#{RAILS_ROOT}/public/404.html"
end

#createObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'app/controllers/elt_controller.rb', line 165

def create
	@elt = Elt.new(params[:elt])
	@elt.person = Person.find_by_id(session[:person_id])

	if !@elt.parent then
		# Rejected if you are not coming from a valid elt
		render :status => 404, :file => "#{RAILS_ROOT}/public/404.html"
     return
	end

	if ((!@elt.person \
			and (@elt.subject =~ /([<>\/]|href)/ \
			 or @elt.body =~ /(.*(http|href)(.*\n)*)/ \
			 or @elt.body =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i \
			 or @elt.body =~ /([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}.*){3}/i)) \
		or ((!@elt.person or @elt.person.elts.size <= 1) and @elt.body =~ /(http|href)/)) then
		logger.error red { underline { "SPAM! '#{@elt.subject}'" } }
		logger.error "SPAM! '#{@elt.body}'"
		flash[:error] = _('Sorry, to fight spam "<" ">" or "href" are forbidden in the subject, and there can not be more than 3 links in the body, you also can\'t input one simple email or more than 3 emails!')
		headers["Status"] = "404 Post considered as spam"
		render :controller => 'elt', :action => 'new', :status => 404

	else
		if not @elt.person and params[:person] and  = params[:person][:name] and !.empty? then
			@elt.person = Person.find_by_name() || Person.new(params[:person])
			if @elt.person.user
				flash[:warning] = _('This name is password protected, login in top right box')
				@elt.person = nil
			elsif params[:submit] != "preview"
				@elt.person.save!
			end
		elsif params[:person] and email = params[:person][:email] and !email.empty? then
				@elt.person.save!
		end

		if params[:submit] == "preview" or (@elt.publish and headers["Status"] = "201 Created") then
			render :partial => '/elt/elt', :locals => { :elt => @elt, :eltTop => false, :created => true }
		else
			logger.error "Strange error, can't preview or save an element"
			puts "Strange error, can't preview or save an element"
			flash[:notice] = 'Error'
			render :controller => 'elt', :action => 'new'
		end
	end
end

#indexObject



7
8
9
# File 'app/controllers/elt_controller.rb', line 7

def index
	show
end

#listObject



43
44
45
46
# File 'app/controllers/elt_controller.rb', line 43

def list
	@elt = Elt.find(params[:id]) if @elt == nil
	render :partial => '/elt/list/children', :locals => { :elt => @elt }
end

#listByDateObject



48
49
50
51
# File 'app/controllers/elt_controller.rb', line 48

def listByDate
	@elt = Elt.find params[:id] unless @elt
	render :partial => '/elt/list/byDate'
end

#listByVoteObject



53
54
55
56
# File 'app/controllers/elt_controller.rb', line 53

def listByVote
	@elt = Elt.find params[:id] unless @elt
	render :partial => '/elt/list/byVote'
end

#listSubscribersObject



63
64
65
66
# File 'app/controllers/elt_controller.rb', line 63

def listSubscribers
	@elt = Elt.find params[:id] unless @elt
	render :partial => '/elt/list/subscribers'
end

#listVisitorsObject



58
59
60
61
# File 'app/controllers/elt_controller.rb', line 58

def listVisitors
	@elt = Elt.find params[:id] unless @elt
	render :partial => '/elt/list/visitors'
end

#newObject

Used to initialise the elt, its subject mainly



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/controllers/elt_controller.rb', line 150

def new
	@elt = Elt.new(:parent_id => params[:id], :body => "")

	if @elt.parent then
		if @elt.parent.subject.include? 'Re: '
			@elt.subject = @elt.parent.subject 
		else
			@elt.subject = 'Re: '+@elt.parent.subject 
		end
	else
		# Rejected if you are not coming from a valid elt
		render :status => 404, :file => "#{RAILS_ROOT}/public/404.html"
	end
end

#raw_eltObject



211
212
213
214
215
# File 'app/controllers/elt_controller.rb', line 211

def raw_elt
	@mail = Elt.find(params[:id]).mail
	#@elt = TMail::Mail.parse(Elt.find(params[:id]).mail.id)
	render :inline => "<pre><%= @mail.file %></pre>", :layout => 'top'
end

#rssObject



125
126
127
128
129
130
131
132
133
134
135
# File 'app/controllers/elt_controller.rb', line 125

def rss
	params[:id] = nil if params[:id].size == 0
	params[:id] = request.subdomains[0] unless params[:id]
	params[:id] = Elt.find(:first, :conditions => "parent_id = 'ROOT'", :order => 'created_on').id unless params[:id]
	params[:id] = params[:id].gsub(/.rss/, '')
	headers["Content-Type"] = "text/xml; charset=utf-8" 
	@elt = Elt.find(params[:id]) if @elt == nil

rescue ActiveRecord::RecordNotFound => e
	redirect_to '/404.html'
end

#showObject



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
# File 'app/controllers/elt_controller.rb', line 11

def show
	params[:id] = request.subdomains[0] unless params[:id]
	params[:id] = Elt.find(:first, :conditions => "parent_id = 'ROOT'", :order => 'created_on').id unless params[:id]
	params[:id] = params[:id].gsub(/.html/, '')

	@elt = Elt.find(params[:id])
	@title = @elt.subject
	@title += " (parlement)" if !@title.downcase.include? "parlement"

	render :layout => 'top', :template => 'elt/show'

#rescue ActiveRecord::StatementInvalid => e
#	load 'db/schema.rb'
#	redirect_to '/'

rescue ActiveRecord::RecordNotFound => e
	root = Elt.find_by_id('ROOT')
	if root and root.children.size > 0
		render :status => 404, :file => "#{RAILS_ROOT}/public/404.html"
	else
		if root
			@elt = root
		else
			@elt = Elt.new :subject => '', :lft => 0, :rgt => 1, :last_activity => Time.now
			@elt.id = 'ROOT'
			@elt.save
		end
		flash[:notice] = _("You need to create a top object (click on the new icon below)")
		render :layout => 'top'
	end
end

#updateViewObject



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
121
122
123
# File 'app/controllers/elt_controller.rb', line 68

def updateView
	session[:lastUpdatedView] ||= Time.now - UPDATE_VIEW_PERIOD

	@elt = Elt.find(params[:id])
	acts = ""
	acts += " \
		<script type=\"text/javascript\"> \
			new Ajax.Updater('listByDate', '/elt/listByDate/#{params[:id]}', \
				{asynchronous:true, evalScripts:true}); \
			new Ajax.Updater('listByVote', '/elt/listByVote/#{params[:id]}', \
				{asynchronous:true, evalScripts:true}); \
		</script> \
		" if @elt.last_activity > session[:lastUpdatedView]

	if person = Person.find_by_id(session[:person_id])
		visits = Visit.count \
			:joins => "JOIN elts e1 ON e1.id = '#{params[:id]}' \
				JOIN elts e2 ON visits.elt_id = e2.id \
					AND ((e1.lft <= e2.lft AND e2.rgt <= e1.rgt) \
						OR (e1.lft > e2.lft AND e2.rgt > e1.rgt))",
			:conditions => "visits.updated_on >= '#{session[:lastUpdatedView]}'"
		acts += " \
			<script type=\"text/javascript\"> \
				new Ajax.Updater('listVisitors', '/elt/listVisitors/#{params[:id]}', \
					{asynchronous:true, evalScripts:true}); \
			</script> \
			" if visits > 0

		subscribers = Subscription.count \
			:joins => "JOIN elts e1 ON e1.id = '#{params[:id]}' \
				JOIN elts e2 ON subscriptions.elt_id = e2.id \
					AND ((e1.lft <= e2.lft AND e2.rgt <= e1.rgt) \
						OR (e1.lft > e2.lft AND e2.rgt > e1.rgt))",
			:conditions => "subscriptions.created_on >= '#{session[:lastUpdatedView]}'"
		acts += " \
			<script type=\"text/javascript\"> \
				new Ajax.Updater('listSubscribers', '/elt/listSubscribers/#{params[:id]}', \
					{asynchronous:true, evalScripts:true}); \
			</script> \
			" if subscribers > 0

		visit = Visit.find_by_person_id_and_elt_id(person, params[:id])
		if visit and person. and person. > visit.created_on then
			logger.info yellow { "New visit" }
			visit.destroy
			visit = nil
		end
		visit = Visit.new(:person => person, :elt_id => params[:id]) unless visit
		visit.filter = filter
		visit.save!
		logger.info magenta { "#{visits} visit(s), #{subscribers} subscriber(s)" }
	end

	session[:lastUpdatedView] = Time.now
	render :inline => acts
end

#voteObject



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'app/controllers/elt_controller.rb', line 217

def vote
	@elt = Elt.find params[:id]
	if !session[:person_id] then
		flash[:error] = 'Sorry, need to be authenticated to vote!'
		render :partial => '/elt/choice', :locals => { :elt => @elt }
		return
	end

	if !params[:choice][:value] =~ /^\s*(-1|0|\+1)(\s|$)/ then
		logger.error red { underline { "SPAM! through the vote" } }
		logger.error params[:choice][:value]
		flash[:error] = 'Sorry, you can only vote here!'
		render :partial => '/elt/choice', :locals => { :elt => @elt }
		return
	end

	vote = @elt.children.build
	vote.person = Person.find_by_id(session[:person_id])
	vote.subject = @elt.subject
	vote.subject = 'Re: '+vote.subject if vote.subject and !vote.subject.include? 'Re: '
	vote.body = params[:choice][:value]

	choice = Choice.find_by_elt_id_and_person_id @elt.id, session[:person_id]

	if choice and choice.value == vote.body.to_i then
		logger.info "#{session[:person_id]} voting 0"
		vote.body = "0"
	else
		logger.info "#{session[:person_id]} voting #{params[:choice][:value]}"
	end

	vote.publish
	@elt.add_child vote

	render :partial => '/elt/choice', :locals => { :elt => @elt }
end

#vote_rssObject



137
138
139
140
141
142
143
144
145
146
147
# File 'app/controllers/elt_controller.rb', line 137

def vote_rss
	params[:id] = nil if params[:id].size == 0
	params[:id] = request.subdomains[0] unless params[:id]
	params[:id] = Elt.find(:first, :conditions => "parent_id = 'ROOT'", :order => 'created_on').id unless params[:id]
	params[:id] = params[:id].gsub(/.rss/, '')
	headers["Content-Type"] = "text/xml; charset=utf-8" 
	@elt = Elt.find(params[:id]) if @elt == nil

rescue ActiveRecord::RecordNotFound => e
	redirect_to '/404.html'
end