This commit is contained in:
Christoph Haas 2025-04-11 09:39:33 +02:00
parent 7cd9c68a10
commit 36984ff1d4
20 changed files with 321 additions and 391 deletions

View file

@ -1,4 +1,4 @@
require 'open-uri' # allows to load URLs using open()
require 'open-uri' # allows to load URLs using open()
require 'json'
require 'deb_importer'
@ -8,26 +8,26 @@ class Package < ApplicationRecord
include PgSearch::Model
# TODO: Make search weighted on users' rating
pg_search_scope :general_search,
# :against => [:name, :description, :long_description],
:against => [
[:name, 'A'],
[:description, 'B'],
[:long_description, 'C']
],
:using => {
:tsearch => {:dictionary => "english"}
}
# :against => [:name, :description, :long_description],
against: [
[:name, 'A'],
[:description, 'B'],
[:long_description, 'C']
],
using: {
tsearch: { dictionary: 'english' }
}
# I am using "destroy_all" here so that when a package gets destroys the
# callbacks for all dependent screenshots are executed - thus removing the
# screenshot files from disk.
has_many :screenshots,
-> { order(id: :desc) },
:inverse_of=>:package,
:dependent => :destroy
-> { order(id: :desc) },
inverse_of: :package,
dependent: :destroy
# default_scope {
# order('name ASC')
# order('name ASC')
# }
# Define the parameter(s) used for a /package/:name URL to an instance of this model
@ -37,11 +37,7 @@ class Package < ApplicationRecord
# Return the first paragraph of the long description.
def long_description_first_paragraph
if self.long_description
self.long_description.split(/\n\.\n/).first
else
nil
end
long_description.split(/\n\.\n/).first if long_description
end
# Return a query of all packages that have screenshots
@ -72,7 +68,7 @@ class Package < ApplicationRecord
# Get all screenshots and have them sorted descendingly by their version number (Debian style).
def screenshots_sorted_by_version
self.screenshots.approved.to_a.sort { |x,y| version_compare(x.version,y.version) }
screenshots.approved.to_a.sort { |x, y| version_compare(x.version, y.version) }
end
# Return the newest screenshot that is not newer than the given version.
@ -83,29 +79,29 @@ class Package < ApplicationRecord
# This way the user does not see a screenshot of version 2.0 because
# 2.0 might contain features that were not there in version 1.5.
def best_screenshot_for_version(version)
sorted_screenshots = self.screenshots_sorted_by_version
sorted_screenshots = screenshots_sorted_by_version
sorted_screenshots.each do |ss|
logger.debug { "Comparing version #{version} against #{ss.version}" }
return ss if version_compare(version, ss.version)<=0
return ss if version_compare(version, ss.version) <= 0
end
return sorted_screenshots.last
sorted_screenshots.last
end
# Return the part of the version up to the first - or +
def upstream_version
self.version.split(/[\-\+]/).first
version.split(/[-+]/).first
end
private
def version_compare(x,y)
def version_compare(x, y)
x = '0' unless x.present?
y = '0' unless y.present?
version_x = DebImporter::Version.new(x)
version_y = DebImporter::Version.new(y)
if version_x<version_y
if version_x < version_y
1
elsif version_x>version_y
elsif version_x > version_y
-1
else
0

View file

@ -9,6 +9,7 @@ class Screenshot < ApplicationRecord
scope :approved, -> { where(approved: true) }
scope :visible, -> { where(hidden: false) }
scope :publicly, -> { where(hidden: false, approved: true) }
# Shrine
include ImageUploader::Attachment(:simage) # adds an `simage` virtual attribute
@ -17,10 +18,10 @@ class Screenshot < ApplicationRecord
# 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}"
if description.present?
"#{description}"
else
"#{self.package.description}"
"#{package.description}"
end
end
@ -37,25 +38,23 @@ class Screenshot < ApplicationRecord
def status
text = 'This image '
if self.approved
text << 'is public'
else
text << 'has to be moderated before being publicly visible'
end
text << if approved
'is public'
else
'has to be moderated before being publicly visible'
end
if self.markedfordelete
text << ' (and was requested to be removed)'
end
text << ' (and was requested to be removed)' if markedfordelete
return text
text
end
# Brief text describing the status of this screenshots (for admins)
def adminstatus
if self.approved
if approved
'Public'
else
#fa_icon('hourglass') + 'Waiting for approval'
# fa_icon('hourglass') + 'Waiting for approval'
'Unapproved'
end
end
@ -63,32 +62,33 @@ class Screenshot < ApplicationRecord
# Publish a screenshot from the moderation queue
def approve!
self.approved = true
self.save!
save!
end
# Hide a screenshot from the public. Moderator level can do this.
def hide!
self.hidden = true
self.save!
save!
end
def unhide!
self.hidden = false
self.save!
save!
end
# Get the newest screenshots regardless of the package it belongs to
def self.newest
self.order(created_at: :desc)
order(created_at: :desc)
end
# Return the part of the version up to the first - or +
def upstream_version
self.version.split(/[\-\+]/).first
version.split(/[-+]/).first
end
# Returns the path to this screenshot's image on disk
def disk_path
ActiveStorage::Blob.service.send(:path_for, self.image.key)
ActiveStorage::Blob.service.send(:path_for, image.key)
end
# Returns the MD5 hex digest of the current screenshot's data
@ -98,8 +98,8 @@ class Screenshot < ApplicationRecord
Digest::MD5.hexdigest(disk_path)
# remote files stored on another person's computer:
#url = attachment.url
#Digest::MD5.base64digest(Net::HTTP.get(URI(url)))
# url = attachment.url
# Digest::MD5.base64digest(Net::HTTP.get(URI(url)))
end
private
@ -112,11 +112,9 @@ class Screenshot < ApplicationRecord
# 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
if (screenshot = Screenshot.find_by(image_fingerprint: image_fingerprint,
package_id: package.id)) && !(screenshot.id == id)
errors.add(:image, 'has already been uploaded for this package')
end
end
end