Add web app model, service to fetch metadata

This commit is contained in:
Râu Cao
2023-10-19 16:18:13 +02:00
parent e1b7e1b2ef
commit e56c9bd0d5
8 changed files with 81 additions and 1 deletions

View File

@@ -0,0 +1,5 @@
module AppCatalog
def self.table_name_prefix
"app_catalog_"
end
end

View File

@@ -0,0 +1,9 @@
class AppCatalog::WebApp < ApplicationRecord
store :metadata, coder: JSON
validates :url, presence: true, uniqueness: true
validates :url, format: { with: URI.regexp },
if: Proc.new { |a| a.url.present? }
end

View File

@@ -0,0 +1,26 @@
require 'manifique'
module AppCatalogManager
class FetchMetadata < AppCatalogManagerService
def initialize(app)
@app = app
end
def call
agent = Manifique::Agent.new(url: @app.url)
metadata = agent.fetch_metadata
@app.name = metadata.name
[:name, :short_name, :description, :theme_color, :background_color,
:display, :start_url, :scope, :share_target].each do |prop|
@app.metadata[prop] = metadata.send(prop) if prop
end
rescue Manifique::Error => e
msg = "Fetching web app manifest failed for #{e.url}: #{e.type}"
Rails.logger.warn(msg)
Sentry.capture_message(msg) if Setting.sentry_enabled?
false
end
end
end

View File

@@ -0,0 +1,2 @@
class AppCatalogManagerService < ApplicationService
end