Newer
Older
# licensed under the Affero General Public License version 3 or later. See
Daniel Vincent Grippi
a validé
danielvincent
a validé
class PeopleController < ApplicationController
before_filter :authenticate_user!
respond_to :html
respond_to :json, :only => [:index, :show]
danielvincent
a validé
def index
@aspect = :search
@people = Person.search(params[:q]).paginate :page => params[:page], :per_page => 25, :order => 'created_at DESC'
danielvincent
a validé
if @people.count == 1
redirect_to @people.first
else
@hashes = hashes_for_people(@people, @aspects)
#only do it if it is an email address
if params[:q].try(:match, Devise.email_regexp)
webfinger(params[:q])
end
end
end
def hashes_for_people people, aspects
ids = people.map{|p| p.id}
requests = {}
Request.all(:to_id.in => ids, :from_id => current_user.person.id).each do |r|
requests[r.to_id] = r
end
contacts = {}
Contact.all(:user_id => current_user.id, :person_id.in => ids).each do |contact|
contacts[contact.person_id] = contact
danielvincent
a validé
end
people.map{|p|
{:person => p,
:contact => contacts[p.id],
:request => requests[p.id],
:aspects => aspects}
}
danielvincent
a validé
end
danielvincent
a validé
def show
@person = Person.find(params[:id].to_id)
maxwell
a validé
@post_type = :all
@profile = @person.profile
@contact = current_user.contact_for(@person)
@is_contact = @person != current_user.person && @contact
if @contact
@aspects_with_person = @contact.aspects
end
@commenting_disabled = !@contact
@posts = current_user.posts_from(@person).paginate :page => params[:page]
maxwell
a validé
respond_with @person, :locals => {:post_type => :all}
flash[:error] = I18n.t 'people.show.does_not_exist'
redirect_to people_path
Raphael
a validé
end
danielvincent
a validé
end
danielvincent
a validé
def destroy
current_user.disconnect(current_user.visible_person_by_id(params[:id]))
danielvincent
a validé
end
danielvincent
a validé
def edit
@aspect = :person_edit
@person = current_user.person
@profile = @person.profile
end
def update
danielvincent
a validé
# upload and set new profile photo
params[:profile] ||= {}
params[:profile][:searchable] ||= false
if params[:profile][:image].present?
raw_image = params[:profile].delete(:image)
danielvincent
a validé
params[:profile_image_hash] = { :user_file => raw_image, :to => "all" }
photo = current_user.build_post(:photo, params[:profile_image_hash])
if photo.save!
params[:profile][:image_url] = photo.url(:thumb_large)
params[:profile][:image_url_medium] = photo.url(:thumb_medium)
params[:profile][:image_url_small] = photo.url(:thumb_small)
danielvincent
a validé
end
if current_user.update_profile params[:profile]
danielvincent
a validé
else
danielvincent
a validé
end
redirect_to getting_started_path(:step => params[:getting_started].to_i+1)
else
redirect_to edit_person_path
end
danielvincent
a validé
end
def retrieve_remote
if params[:diaspora_handle]
webfinger(params[:diaspora_handle], :single_aspect_form => true)
render :nothing => true
else
render :nothing => true, :status => 422
end
end
private
post_ids = posts.map{|p| p.id}
comment_hash = Comment.hash_from_post_ids post_ids
person_hash = Person.from_post_comment_hash comment_hash
photo_hash = Photo.hash_from_post_ids post_ids
posts.map do |post|
{:post => post,
:person => @person,
:comments => comment_hash[post.id].map do |comment|
{:comment => comment,
:person => person_hash[comment.person_id],
}
end,
}
end
end
def webfinger(account, opts = {})
Resque.enqueue(Jobs::SocketWebfinger, current_user.id, account, opts)
end