]> git.openstreetmap.org Git - rails.git/blob - app/controllers/user_preference_controller.rb
Limit the rate at which messages can be sent.
[rails.git] / app / controllers / user_preference_controller.rb
1 # Update and read user preferences, which are arbitrayr key/val pairs
2 class UserPreferenceController < ApplicationController
3   before_filter :authorize
4   before_filter :require_allow_read_prefs, :only => [:read_one, :read]
5   before_filter :require_allow_write_prefs, :except => [:read_one, :read]
6
7   def read_one
8     pref = UserPreference.find(@user.id, params[:preference_key])
9
10     render :text => pref.v.to_s
11   rescue ActiveRecord::RecordNotFound => ex
12     render :text => 'OH NOES! PREF NOT FOUND!', :status => :not_found
13   end
14
15   def update_one
16     begin
17       pref = UserPreference.find(@user.id, params[:preference_key])
18       pref.v = request.raw_post.chomp
19       pref.save
20     rescue ActiveRecord::RecordNotFound 
21       pref = UserPreference.new
22       pref.user = @user
23       pref.k = params[:preference_key]
24       pref.v = request.raw_post.chomp
25       pref.save
26     end
27
28     render :nothing => true
29   end
30
31   def delete_one
32     UserPreference.delete(@user.id, params[:preference_key])
33
34     render :nothing => true
35   rescue ActiveRecord::RecordNotFound => ex
36     render :text => "param: #{params[:preference_key]} not found", :status => :not_found
37   end
38
39   # print out all the preferences as a big xml block
40   def read
41     doc = OSM::API.new.get_xml_doc
42
43     prefs = @user.preferences
44
45     el1 = XML::Node.new 'preferences'
46
47     prefs.each do |pref|
48       el1 <<  pref.to_xml_node
49     end
50
51     doc.root << el1
52     render :text => doc.to_s, :content_type => "text/xml"
53   end
54
55   # update the entire set of preferences
56   def update
57     begin
58       p = XML::Parser.string(request.raw_post)
59     rescue LibXML::XML::Error, ArgumentError => ex
60       raise OSM::APIBadXMLError.new("preferences", xml, ex.message)
61     end
62     doc = p.parse
63
64     prefs = []
65
66     keyhash = {}
67
68     doc.find('//preferences/preference').each do |pt|
69       pref = UserPreference.new
70
71       unless keyhash[pt['k']].nil? # already have that key
72         render :text => 'OH NOES! CAN HAS UNIQUE KEYS?', :status => :not_acceptable
73       end
74
75       keyhash[pt['k']] = 1
76
77       pref.k = pt['k']
78       pref.v = pt['v']
79       pref.user_id = @user.id
80       prefs << pref
81     end
82
83     if prefs.size > 150
84       render :text => 'Too many preferences', :status => :request_entity_too_large
85     end
86
87     # kill the existing ones
88     UserPreference.delete_all(['user_id = ?', @user.id])
89
90     # save the new ones
91     prefs.each do |pref|
92       pref.save!
93     end
94     render :nothing => true
95
96   rescue Exception => ex
97     render :text => 'OH NOES! FAIL!: ' + ex.to_s, :status => :internal_server_error
98   end
99 end