people_controller.rb 2.61 KB
Newer Older
Raphael's avatar
Raphael committed
1
#   Copyright (c) 2010, Diaspora Inc.  This file is
Raphael's avatar
Raphael committed
2
#   licensed under the Affero General Public License version 3 or later.  See
Raphael's avatar
Raphael committed
3
#   the COPYRIGHT file.
4

5 6
class PeopleController < ApplicationController
  before_filter :authenticate_user!
7 8 9

  respond_to :html
  respond_to :json, :only => [:index, :show]
Jamie Wilkinson's avatar
Jamie Wilkinson committed
10

11
  def index
12
    @aspect = :search
13
    params[:q] ||= params[:term]
Raphael's avatar
Raphael committed
14
    limit = params[:limit] || 15
Raphael's avatar
Raphael committed
15

Raphael's avatar
Raphael committed
16
    @people = Person.search(params[:q], current_user).paginate :page => params[:page], :per_page => limit
17 18
    @hashes = hashes_for_people(@people, @aspects) unless request.format == :json

maxwell's avatar
maxwell committed
19 20 21
    #only do it if it is an email address
    if params[:q].try(:match, Devise.email_regexp)
      webfinger(params[:q])
Raphael's avatar
Raphael committed
22
    end
23
    respond_with @people
Raphael's avatar
Raphael committed
24
  end
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  def hashes_for_people people, aspects
    ids = people.map{|p| p.id}
    requests = {}
    Request.where(:sender_id => ids, :recipient_id => current_user.person.id).each do |r|
      requests[r.id] = r
    end
    contacts = {}
    Contact.where(:user_id => current_user.id, :person_id => ids).each do |contact|
      contacts[contact.person_id] = contact
    end

    people.map{|p|
      {:person => p,
        :contact => contacts[p.id],
        :request => requests[p.id],
        :aspects => aspects}
    }
  end

45
  def show
Raphael's avatar
Raphael committed
46
    @person = Person.where(:id => params[:id]).first
47
    @post_type = :all
48
    @share_with = (params[:share_with] == 'true')
49

50
    if @person
Raphael's avatar
Raphael committed
51
      @incoming_request = current_user.request_from(@person)
52

53
      @profile = @person.profile
54
      @contact = current_user.contact_for(@person)
55
      @aspects_with_person = []
56 57 58

      if @contact
        @aspects_with_person = @contact.aspects
59
        @contacts_of_contact = @contact.contacts
60 61 62
      else
        @contact ||= Contact.new
        @contacts_of_contact = []
63 64
      end

65 66 67 68 69
      if (@person != current_user.person) && (!@contact || @contact.pending)
        @commenting_disabled = true
      else
        @commenting_disabled = false
      end
70

71
      @posts = current_user.posts_from(@person).where(:type => "StatusMessage").paginate  :per_page => 15, :page => params[:page]
72
      @fakes = PostsFake.new(@posts)
73 74
      respond_with @person, :locals => {:post_type => :all}

75
    else
MrZYX's avatar
MrZYX committed
76
      flash[:error] = I18n.t 'people.show.does_not_exist'
77
      redirect_to people_path
78
    end
79
  end
Jamie Wilkinson's avatar
Jamie Wilkinson committed
80

81 82 83 84 85 86 87 88 89 90 91
  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
  def webfinger(account, opts = {})
92
    Resque.enqueue(Job::SocketWebfinger, current_user.id, account, opts)
93
  end
94
end