photo.rb 4.08 KB
Newer Older
1
#   Copyright (c) 2009, 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
class Photo < ActiveRecord::Base
6
  include Diaspora::Federated::Shareable
7 8 9
  include Diaspora::Commentable
  include Diaspora::Shareable

10 11 12 13 14 15 16 17 18 19 20 21
  # NOTE API V1 to be extracted
  acts_as_api
  api_accessible :backbone do |t|
    t.add :id
    t.add :guid
    t.add :created_at
    t.add :author
    t.add lambda { |photo|
      { :small => photo.url(:thumb_small),
        :medium => photo.url(:thumb_medium),
        :large => photo.url(:scaled_full) }
    }, :as => :sizes
22 23 24 25
    t.add lambda { |photo|
      { :height => photo.height,
        :width => photo.width }
    }, :as => :dimensions
26 27
  end

28 29
  mount_uploader :processed_image, ProcessedImage
  mount_uploader :unprocessed_image, UnprocessedImage
Jamie Wilkinson's avatar
Jamie Wilkinson committed
30

zhitomirskiyi's avatar
zhitomirskiyi committed
31 32
  xml_attr :remote_photo_path
  xml_attr :remote_photo_name
33

34
  xml_attr :text
35
  xml_attr :status_message_guid
maxwell's avatar
maxwell committed
36

37 38 39
  xml_attr :height
  xml_attr :width

40
  belongs_to :status_message, :foreign_key => :status_message_guid, :primary_key => :guid
41
  validates_associated :status_message
42
  delegate :author_name, to: :status_message, prefix: true
43 44

  validate :ownership_of_status_message
45

46
  before_destroy :ensure_user_picture
47
  after_destroy :clear_empty_status_message
48

Ivan Zotov's avatar
Ivan Zotov committed
49
  after_commit :on => :create do
50
    queue_processing_job if self.author.local?
51

52
  end
53

54 55 56 57
  scope :on_statuses, ->(post_guids) {
    where(:status_message_guid => post_guids)
  }

58
  def clear_empty_status_message
Jonne Haß's avatar
Jonne Haß committed
59
    if self.status_message && self.status_message.text_and_photos_blank?
60 61 62 63 64 65
      self.status_message.destroy
    else
      true
    end
  end

66
  def ownership_of_status_message
67 68
    message = StatusMessage.find_by_guid(self.status_message_guid)
    if self.status_message_guid && message
Raphael's avatar
Raphael committed
69
      self.diaspora_handle == message.diaspora_handle
70 71
    else
      true
72 73 74
    end
  end

75
  def self.diaspora_initialize(params = {})
James Fleming's avatar
James Fleming committed
76
    photo = self.new params.to_hash.slice(:text, :pending)
77 78 79 80 81
    photo.author = params[:author]
    photo.public = params[:public] if params[:public]
    photo.pending = params[:pending] if params[:pending]
    photo.diaspora_handle = photo.author.diaspora_handle

82
    photo.random_string = SecureRandom.hex(10)
83

84 85 86 87
    if photo.author.local?
      photo.unprocessed_image.strip_exif = photo.author.owner.strip_exif
    end

88 89 90 91 92 93 94 95
    if params[:user_file]
      image_file = params.delete(:user_file)
      photo.unprocessed_image.store! image_file
    elsif params[:image_url]
      photo.remote_unprocessed_image_url = params[:image_url]
      photo.unprocessed_image.store!
    end

96
    photo.update_remote_path
97

maxwell's avatar
maxwell committed
98 99
    photo
  end
100

101
  def processed?
102
    processed_image.path.present?
103 104 105
  end

  def update_remote_path
106
    unless self.unprocessed_image.url.match(/^https?:\/\//)
107
      remote_path = "#{AppConfig.pod_uri.to_s.chomp("/")}#{self.unprocessed_image.url}"
108
    else
109
      remote_path = self.unprocessed_image.url
110
    end
111

Raphael's avatar
Raphael committed
112
    name_start = remote_path.rindex '/'
maxwell's avatar
maxwell committed
113 114
    self.remote_photo_path = "#{remote_path.slice(0, name_start)}/"
    self.remote_photo_name = remote_path.slice(name_start + 1, remote_path.length)
Raphael's avatar
Raphael committed
115 116
  end

117
  def url(name = nil)
118
    if remote_photo_path
119
      name = name.to_s + '_' if name
Dennis Schubert's avatar
Dennis Schubert committed
120 121
      image_url = remote_photo_path + name.to_s + remote_photo_name
      if AppConfig.privacy.camo.proxy_remote_pod_images?
122
        Diaspora::Camo.image_url(image_url)
Dennis Schubert's avatar
Dennis Schubert committed
123 124 125
      else
        image_url
      end
126
    elsif processed?
127
      processed_image.url(name)
128 129
    else
      unprocessed_image.url(name)
Raphael's avatar
Raphael committed
130
    end
131
  end
132 133

  def ensure_user_picture
zhitomirskiyi's avatar
zhitomirskiyi committed
134
    profiles = Profile.where(:image_url => url(:thumb_large))
Raphael's avatar
Raphael committed
135 136 137
    profiles.each { |profile|
      profile.image_url = nil
      profile.save
Jamie Wilkinson's avatar
Jamie Wilkinson committed
138
    }
139
  end
140

141
  def queue_processing_job
Jonne Haß's avatar
Jonne Haß committed
142
    Workers::ProcessPhoto.perform_async(self.id)
143 144
  end

145 146 147
  def mutable?
    true
  end
148 149 150 151 152 153 154 155 156

  def self.visible(current_user, person, limit=:all, max_time=nil)
    photos = if current_user
               current_user.photos_from(person, limit: limit, max_time: max_time)
             else
               Photo.where(author_id: person.id, public: true)
             end
    photos.order("created_at desc")
  end
Raphael's avatar
Raphael committed
157
end