forked from sudara/alonetone
/
users_controller.rb
206 lines (180 loc) · 7.53 KB
/
users_controller.rb
1
2
3
4
5
6
7
8
9
10
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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
class UsersController < ApplicationController
skip_before_filter :update_last_seen_at, :only => [:create, :new, :activate, :sudo]
before_filter :find_user, :except => [:new, :create]
before_filter :login_required, :except => [:index, :show, :new, :create, :activate, :bio]
skip_before_filter :login_by_token, :only => :sudo
#rescue_from NoMethodError, :with => :user_not_found
def index
@page_title = "#{params[:sort] ? params[:sort].titleize+' - ' : ''} Musicians and Listeners"
@tab = 'browse'
respond_to do |format|
format.html do
@users = User.paginate_by_params(params)
@user_count = User.count
@active = User.count(:all, :conditions => "assets_count > 0", :include => :pic)
end
format.fbml do
@users = User.paginate_by_params(params)
end
format.xml do
@users = User.activated.search(params[:q], :limit => 1000)
render :xml => @users.to_xml
end
format.rss do
@users = User.activated.geocoded.find(:all, :limit => 1000)
end
# API
format.json do
users = User.musicians.find(:all,:include => :pic)
render :json => '{ records : ' + users.to_json(:methods => [:name, :type], :only => [:id,:name,:comments_count,:bio_html,:website,:login]) + '}'
end
# format.fbml do
# @users = User.paginate(:all, :per_page => 10, :order => 'listens_count DESC', :page => params[:page])
# end
end
end
def show
respond_to do |format|
format.html do
@page_title = (@user.name)
@keywords = "#{@user.name}, latest, upload, music, tracks, mp3, mp3s, playlists, download, listen"
@description = "#{@user.name}'s music and albums"
@tab = 'your_stuff' if current_user == @user
@popular_tracks = @user.assets.find(:all, :limit => 5, :order => 'assets.listens_count DESC')
@assets = @user.assets.find(:all, :limit => 5)
@playlists = @user.playlists.public.find(:all)
@listens = @user.listens.find(:all, :limit =>5)
@track_plays = @user.track_plays.from_user.find(:all, :limit =>10)
@favorites = Track.favorites.find_all_by_user_id(@user.id, :limit => 5)
@mostly_listens_to = @user.mostly_listens_to
@comments = @user.comments.public.find(:all, :limit => 5) unless display_private_comments_of?(@user)
@comments = @user.comments.include_private.find(:all, :limit => 5) if display_private_comments_of?(@user)
render
end
format.xml { @assets = @user.assets.find(:all, :order => 'created_at DESC', :limit => (params[:limit] || 10))}
format.rss { @assets = @user.assets.find(:all, :order => 'created_at DESC')}
format.fbml do
@assets = @user.assets.find(:all)
end
format.js do render :update do |page|
page.replace 'user_latest', :partial => "latest"
end
end
end
end
def new
@user = User.new
@page_title = "Join alonetone to upload your music in mp3 format"
flash.now[:error] = "Join alonetone to upload and create playlists (it is quick: about 45 seconds)" if params[:new]
end
def create
respond_to do |format|
format.html do
@user = params[:user].blank? ? User.find_by_email(params[:email]) : User.new(params[:user])
flash[:error] = "I could not find an account with the email address '#{CGI.escapeHTML params[:email]}'. Did you type it correctly?" if params[:email] and not @user
redirect_to login_path and return unless @user
@user.login = params[:user][:login] unless params[:user].blank?
@user.reset_token!
begin
UserMailer.deliver_signup(@user)
rescue Net::SMTPFatalError => e
flash[:error] = "A permanent error occured while sending the signup message to '#{CGI.escapeHTML @user.email}'. Please check the e-mail address."
redirect_to :action => "new"
rescue Net::SMTPServerBusy, Net::SMTPUnknownError, \
Net::SMTPSyntaxError, TimeoutError => e
flash[:error] = "The signup message cannot be sent to '#{CGI.escapeHTML @user.email}' at this moment. Please, try again later."
redirect_to :action => "new"
end
flash[:ok] = "We just sent you an email to '#{CGI.escapeHTML @user.email}'.<br/><br/>You just have to click the link in the email, and the hard work is over! <br/> Note: check your junk/spam inbox if you don't see a new email right away."
end
end
rescue ActiveRecord::RecordInvalid
flash[:error] = "Whups, there was a small issue"
render :action => 'new'
end
def activate
self.current_user = User.find_by_activation_code(params[:activation_code])
if current_user != false && !current_user.activated?
current_user.activate
flash[:ok] = "Whew! All done, your account is activated. Go ahead and upload your first track."
redirect_to new_user_track_path(current_user)
else
flash[:error] = "Hm. Activation didn't work. Maybe your account is already activated?"
redirect_to default_url
end
end
def edit
end
def bio
@page_title = "#{@user.name}'s Profile"
@mostly_listens_to = @user.mostly_listens_to
end
def attach_pic
@pic = @user.build_pic(params[:pic])
if @pic.save
flash[:ok] = 'Pic updated!'
else
flash[:error] = 'Pic not updated!'
end
redirect_to edit_user_path(@user)
end
def update
@user.attributes = params[:user]
# temp fix to let people with dumb usernames change them
@user.login = params[:user][:login] if not @user.valid? and @user.errors.on(:login)
respond_to do |format|
format.html do
if @user.save
flash[:ok] = "Sweet, updated"
redirect_to edit_user_path(@user)
else
flash[:error] = "Not so fast, young one"
render :action => :edit
end
end
format.js do
@user.save ? (return head(:ok)) : (return head(:bad_request))
end
end
end
def toggle_favorite
return false unless Asset.find(params[:asset_id]) # no bullshit
existing_track = @user.tracks.find(:first, :conditions => {:asset_id => params[:asset_id], :is_favorite => true})
if existing_track
existing_track.destroy && Asset.decrement_counter(:favorites_count, params[:asset_id])
else
favs = Playlist.find_or_create_by_user_id_and_is_favorite(:user_id => @user.id, :is_favorite => true)
added_fav = favs.tracks.create(:asset_id => params[:asset_id], :is_favorite => true, :user_id => @user.id)
Asset.increment_counter(:favorites_count, params[:asset_id]) if added_fav
end
render :nothing => true
end
def toggle_follow
current_user.add_or_remove_followee(params[:followee_id])
render :nothing => true
end
def destroy
return unless admin?
@user.destroy
respond_to do |format|
format.html { redirect_to users_path }
format.xml { head 200 }
end
end
def sudo
redirect_to user_home_path(current_user) and return false unless @user && (current_user.admin? || session[:sudo])
flash[:ok] = "Sudo to #{@user.name}" if sudo_to(@user)
redirect_to :back
end
protected
def authorized?
admin? || (!%w(destroy admin).include?(action_name) && logged_in? && (current_user.id.to_s == @user.id.to_s)) || (action_name == 'sudo')
end
def display_user_home_or_index
if params[:login] && User.find_by_login(params[:login])
redirect_to user_home_url(params[:user])
else
redirect_to users_url
end
end
end