photo.rb 3.91 KB
Newer Older
1 2
# frozen_string_literal: true

3
#   Copyright (c) 2009, Diaspora Inc.  This file is
Raphael's avatar
Raphael committed
4
#   licensed under the Affero General Public License version 3 or later.  See
Raphael's avatar
Raphael committed
5
#   the COPYRIGHT file.
6

Benjamin Neff's avatar
Benjamin Neff committed
7
class Photo < ApplicationRecord
8
  include Diaspora::Federated::Base
9 10 11
  include Diaspora::Commentable
  include Diaspora::Shareable

12 13 14 15 16 17 18 19
  # 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|
20 21 22
      {
        small:  photo.url(:thumb_small),
        medium: photo.url(:thumb_medium),
flaburgan's avatar
flaburgan committed
23 24
        large:  photo.url(:scaled_full),
        raw:    photo.url
25
      }
26
    }, :as => :sizes
27
    t.add lambda { |photo|
28 29 30 31 32 33 34 35 36 37
      {
        height: photo.height,
        width:  photo.width
      }
    }, as: :dimensions
    t.add lambda { |photo|
      {
        id: photo.status_message.id
      } if photo.status_message
    }, as: :status_message
38 39
  end

40 41
  mount_uploader :processed_image, ProcessedImage
  mount_uploader :unprocessed_image, UnprocessedImage
Jamie Wilkinson's avatar
Jamie Wilkinson committed
42

43
  belongs_to :status_message, foreign_key: :status_message_guid, primary_key: :guid, optional: true
44
  validates_associated :status_message
45
  delegate :author_name, to: :status_message, prefix: true
46 47

  validate :ownership_of_status_message
48

49
  before_destroy :ensure_user_picture
50
  after_destroy :clear_empty_status_message
51

Ivan Zotov's avatar
Ivan Zotov committed
52
  after_commit :on => :create do
53
    queue_processing_job if self.author.local?
54

55
  end
56

57 58 59 60
  scope :on_statuses, ->(post_guids) {
    where(:status_message_guid => post_guids)
  }

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

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

78 79
  def self.diaspora_initialize(params={})
    photo = new(params.to_hash.stringify_keys.slice(*column_names, "author"))
80
    photo.random_string = SecureRandom.hex(10)
81

82
    photo.unprocessed_image.strip_exif = photo.author.owner.strip_exif
83

84 85 86 87 88 89 90 91
    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

92
    photo.update_remote_path
93

maxwell's avatar
maxwell committed
94 95
    photo
  end
96

97
  def processed?
98
    processed_image.path.present?
99 100 101
  end

  def update_remote_path
102
    unless self.unprocessed_image.url.match(/^https?:\/\//)
103
      remote_path = "#{AppConfig.pod_uri.to_s.chomp("/")}#{self.unprocessed_image.url}"
104
    else
105
      remote_path = self.unprocessed_image.url
106
    end
107

Raphael's avatar
Raphael committed
108
    name_start = remote_path.rindex '/'
maxwell's avatar
maxwell committed
109 110
    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
111 112
  end

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

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

137
  def queue_processing_job
Jonne Haß's avatar
Jonne Haß committed
138
    Workers::ProcessPhoto.perform_async(self.id)
139 140
  end

141 142 143 144 145 146
  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
Benjamin Neff's avatar
Benjamin Neff committed
147
    photos.where(pending: false).order("created_at DESC")
148
  end
Raphael's avatar
Raphael committed
149
end