class Screenshot < ApplicationRecord belongs_to :package, inverse_of: :screenshots belongs_to :user, inverse_of: :screenshots has_attached_file :image, styles: { :large => '800x600>', :thumb => '160x120>' }, default_url: '/images/dummy/no-screenshots-available.svg', path: ':rails_root/public/screenshots/:id_partition/:style.png', url: '/screenshots/:id_partition/:style.png' validates_attachment_content_type :image, :content_type => 'image/png' validates_with AttachmentSizeValidator, :attributes => :image, :less_than => 5.megabytes # validate :validate_image_is_unique validates :delete_reason, length: { in: 5..100 }, allow_nil: true # Validator that checks if the image has already been uploaded. # A generic uniqueness validator does not work because it would attribute # the error to the :image_fingerprint field and not the actual :image field. # Currently this check makes sure that the same screenshot is not uploaded # twice for the same package. However it allows the screenshot to be uploaded # for two different packages. Restricting that further may happen later. def validate_image_is_unique # Look for images with the same checksum / image_fingerprint if screenshot=Screenshot.find_by(image_fingerprint: image_fingerprint, package_id: self.package.id) unless screenshot.id == self.id errors.add(:image, "has already been uploaded for this package") end end end # Calculate how many days ago this screenshot has been uploaded def age_days if self.created_at seconds = Time.now - self.created_at else seconds = 0 end days = (seconds/86400).to_i days ? "#{days} days ago" : 'today' end # Return caption for full-screen screenshots. # Takes the description of a screenshot if available. # Otherwise it falls back to the general description of its package. def caption if self.description.present? "#{self.description}" else "#{self.package.description}" end end def uploader if self.user self.user.name else 'Anonymous' end end # Return Debshots 1.x path to allow migration of images into Paperclip filesystem schema def image_url(size) "live/screenshots/approved/#{self.package.name[0]}/#{self.package.name}/#{self.id}_#{size}.png" end # Long status text for public users def status text = 'This image ' if self.approved text << 'is public' else text << 'has to be moderated before being publicly visible' end if self.markedfordelete text << ' (and was requested to be removed)' end return text end # Short status text for admins def adminstatus if self.markedfordelete "Removal requested > #{self.delete_reason}" elsif self.approved 'Public' else 'Waiting for approval' end end # Check if the user is allowed to change this screenshot def user_can_alter? # - Is this the user's own screenshot? (anonymous) if self.uploaderhash == session[:token] logger.debug "Screenshot belongs to current anonymous user" return true end # - Is this the user's own screenshot? (anonymous) if self.user = current_user logger.debug "Screenshot belongs to user who is currently logged in" return true end # - Is the user an admin (=logged in)? if current_user.can_admin? logger.debug "User is an administrator and has super powers" return true end return false end # Publish a screenshot from the moderation queue def approve_screenshot! self.delete_reason = nil self.markedfordelete = false self.approved = true self.save! end def self.newest_upload self.order(updated_at: :desc).first end def self.approved self.find_by(approved: true) end # Query for screenshots being uploaded by a certain user (by their token) def self.uploaded_by(token) self.where(approved: false, uploaderhash: token) end # Check whether the user has administrative permissions def can_admin? self.admin == 1 end # Check whether an upload would require moderation def can_upload_without_moderation? # Admins can true if self.can_admin # Authenticated users with at least one approved screenshot true if self.screenshots.where(approved: true).count >= 1 # Other visitors require moderation false end def can_delete? # Admins can true if self.can_admin # Authenticated users with at least one approved screenshot true if self.screenshots.where(approved: true).count >= 1 # Other visitors require moderation false end end