Class: Photo

Inherits:
ApplicationRecord show all
Includes:
Diaspora::Federated::Base, Diaspora::Shareable
Defined in:
app/models/photo.rb

Overview

Copyright © 2009, Diaspora Inc. This file is

licensed under the Affero General Public License version 3 or later.  See
the COPYRIGHT file.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Diaspora::Shareable

included, #receive, #subscribed_pods_uris, #subscribers

Methods included from Diaspora::Federated::Base

#object_to_receive, #subscribers

Class Method Details

.diaspora_initialize(params = {}) ⇒ Object


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/photo.rb', line 75

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

  photo.unprocessed_image.strip_exif = photo.author.owner.strip_exif

  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

  photo.update_remote_path

  photo
end

.visible(current_user, person, limit = :all, max_time = nil) ⇒ Object


138
139
140
141
142
143
144
145
# File 'app/models/photo.rb', line 138

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.where(pending: false).order("created_at DESC")
end

Instance Method Details

#clear_empty_status_messageObject


60
61
62
63
64
65
66
# File 'app/models/photo.rb', line 60

def clear_empty_status_message
  if self.status_message && self.status_message.text_and_photos_blank?
    self.status_message.destroy
  else
    true
  end
end

#ensure_user_pictureObject


126
127
128
129
130
131
132
# File 'app/models/photo.rb', line 126

def ensure_user_picture
  profiles = Profile.where(:image_url => url(:thumb_large))
  profiles.each { |profile|
    profile.image_url = nil
    profile.save
  }
end

#ownership_of_status_messageObject


68
69
70
71
72
73
# File 'app/models/photo.rb', line 68

def ownership_of_status_message
  message = StatusMessage.find_by_guid(self.status_message_guid)
  return unless status_message_guid && message && diaspora_handle != message.diaspora_handle

  errors.add(:base, "Photo must have the same owner as status message")
end

#processed?Boolean

Returns:

  • (Boolean)

94
95
96
# File 'app/models/photo.rb', line 94

def processed?
  processed_image.path.present?
end

#queue_processing_jobObject


134
135
136
# File 'app/models/photo.rb', line 134

def queue_processing_job
  Workers::ProcessPhoto.perform_async(self.id)
end

#update_remote_pathObject


98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/photo.rb', line 98

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

  name_start = remote_path.rindex '/'
  self.remote_photo_path = "#{remote_path.slice(0, name_start)}/"
  self.remote_photo_name = remote_path.slice(name_start + 1, remote_path.length)
end

#url(name = nil) ⇒ Object


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/models/photo.rb', line 110

def url(name = nil)
  if remote_photo_path
    name = name.to_s + '_' if name
    image_url = remote_photo_path + name.to_s + remote_photo_name
    if AppConfig.privacy.camo.proxy_remote_pod_images?
      Diaspora::Camo.image_url(image_url)
    else
      image_url
    end
  elsif processed?
    processed_image.url(name)
  else
    unprocessed_image.url(name)
  end
end