Newer
Older
# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
Daniel Vincent Grippi
a validé
danielvincent
a validé
class PeopleController < ApplicationController
before_action :authenticate_user!, except: %i(show stream hovercard)
before_action :find_person, only: %i(show stream hovercard)
respond_to :json, :only => [:index, :show]
rescue_from ActiveRecord::RecordNotFound do
render :file => Rails.root.join('public', '404').to_s,
:format => :html, :layout => false, :status => 404
rescue_from Diaspora::AccountClosed do
respond_to do |format|
format.any { redirect_to :back, :notice => t("people.show.closed_account") }
format.json { render :nothing => true, :status => 410 } # 410 GONE
end
end
danielvincent
a validé
def index
@aspect = :search
limit = params[:limit] ? params[:limit].to_i : 15
@people = Person.search(search_query, current_user)
respond_to do |format|
format.json do
render :json => @people
end
#only do it if it is an email address
if diaspora_id?(search_query)
@people = Person.where(:diaspora_handle => search_query.downcase)
if @people.empty?
Workers::FetchWebfinger.perform_async(search_query)
@background_query = search_query.downcase
end
@people = @people.paginate(:page => params[:page], :per_page => 15)
@hashes = hashes_for_people(@people, @aspects)
danielvincent
a validé
def refresh_search
@aspect = :search
@people = Person.where(:diaspora_handle => search_query.downcase)
@answer_html = ""
unless @people.empty?
@hashes = hashes_for_people(@people, @aspects)
@answer_html = render_to_string :partial => 'people/person', :locals => @hashes.first
end
render :json => { :search_count => @people.count, :search_html => @answer_html }.to_json
end
danielvincent
a validé
def show
mark_corresponding_notifications_read if user_signed_in?
cmrd Senya
a validé
@person_json = PersonPresenter.new(@person, current_user).as_json
maxwell
a validé
if user_signed_in?
@contact = current_user.contact_for(@person)
end
gon.preloads[:person] = @person_json
gon.preloads[:photos_count] = Photo.visible(current_user, @person).count(:all)
gon.preloads[:contacts_count] = Contact.contact_contacts_for(current_user, @person).count(:all)
respond_with @person, layout: "with_header"
format.mobile do
@post_type = :all
person_stream
respond_with @person
end
format.json { render json: @person_json }
end
end
def stream
respond_to do |format|
format.all { redirect_to person_path(@person) }
format.json {
render json: person_stream.stream_posts.map { |p| LastThreeCommentsDecorator.new(PostPresenter.new(p, current_user)) }
}
Raphael
a validé
end
danielvincent
a validé
end
# hovercards fetch some the persons public profile data via json and display
# it next to the avatar image in a nice box
def hovercard
respond_to do |format|
format.all do
redirect_to :action => "show", :id => params[:person_id]
end
format.json do
render json: PersonPresenter.new(@person, current_user).hovercard
def retrieve_remote
if params[:diaspora_handle]
Workers::FetchWebfinger.perform_async(params[:diaspora_handle])
render :nothing => true
else
render :nothing => true, :status => 422
end
end
respond_to do |format|
format.json { render nothing: true, status: 406 }
format.any do
@person = Person.find_by_guid(params[:person_id])
if @person
@contact = current_user.contact_for(@person)
@contacts_of_contact = Contact.contact_contacts_for(current_user, @person)
cmrd Senya
a validé
gon.preloads[:person] = PersonPresenter.new(@person, current_user).as_json
gon.preloads[:photos_count] = Photo.visible(current_user, @person).count(:all)
gon.preloads[:contacts_count] = @contacts_of_contact.count(:all)
@contacts_of_contact = @contacts_of_contact.paginate(page: params[:page], per_page: (params[:limit] || 15))
@hashes = hashes_for_people @contacts_of_contact, @aspects
respond_with @person, layout: "with_header"
else
flash[:error] = I18n.t "people.show.does_not_exist"
redirect_to people_path
end
end
Ilyaaaaaaaaaaaaa Zhitomirskiy
a validé
def find_person
username = params[:username]
@person = if diaspora_id?(username)
Person.where({
diaspora_handle: username.downcase
}).first
else
Person.find_from_guid_or_username({
id: params[:id] || params[:person_id],
username: username
})
end
# view this profile on the home pod, if you don't want to sign in...
authenticate_user! if remote_profile_with_no_user_session?
raise ActiveRecord::RecordNotFound if @person.nil?
raise Diaspora::AccountClosed if @person.closed_account?
end
def hashes_for_people(people, aspects)
people.map {|person|
{
person: person,
contact: current_user.contact_for(person) || Contact.new(person: person),
aspects: aspects
}.tap {|hash|
gon_load_contact(hash[:contact])
}
def search_query
@search_query ||= params[:q] || params[:term] || ''
end
def diaspora_id?(query)
!query.try(:match, /^(\w)*@([a-zA-Z0-9]|[-]|[.]|[:])*$/).nil?
end
def remote_profile_with_no_user_session?
@person.try(:remote?) && !user_signed_in?
end
def mark_corresponding_notifications_read
Notification.where(recipient_id: current_user.id, target_type: "Person", target_id: @person.id, unread: true).each do |n|
n.set_read_state( true )
end
end
def person_stream
@stream ||= Stream::Person.new(current_user, @person, max_time: max_time)
end