Add web app model, service to fetch metadata
This commit is contained in:
		
							parent
							
								
									e1b7e1b2ef
								
							
						
					
					
						commit
						e56c9bd0d5
					
				
							
								
								
									
										5
									
								
								app/models/app_catalog.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								app/models/app_catalog.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,5 @@
 | 
			
		||||
module AppCatalog
 | 
			
		||||
  def self.table_name_prefix
 | 
			
		||||
    "app_catalog_"
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										9
									
								
								app/models/app_catalog/web_app.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								app/models/app_catalog/web_app.rb
									
									
									
									
									
										Normal 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
 | 
			
		||||
							
								
								
									
										26
									
								
								app/services/app_catalog_manager/fetch_metadata.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								app/services/app_catalog_manager/fetch_metadata.rb
									
									
									
									
									
										Normal 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
 | 
			
		||||
							
								
								
									
										2
									
								
								app/services/app_catalog_manager_service.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								app/services/app_catalog_manager_service.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,2 @@
 | 
			
		||||
class AppCatalogManagerService < ApplicationService
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										11
									
								
								db/migrate/20231019125135_create_app_catalog_web_apps.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								db/migrate/20231019125135_create_app_catalog_web_apps.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,11 @@
 | 
			
		||||
class CreateAppCatalogWebApps < ActiveRecord::Migration[7.0]
 | 
			
		||||
  def change
 | 
			
		||||
    create_table :app_catalog_web_apps do |t|
 | 
			
		||||
      t.string :url
 | 
			
		||||
      t.string :name
 | 
			
		||||
      t.text   :metadata
 | 
			
		||||
 | 
			
		||||
      t.timestamps
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										10
									
								
								db/schema.rb
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								db/schema.rb
									
									
									
									
									
								
							@ -10,7 +10,7 @@
 | 
			
		||||
#
 | 
			
		||||
# It's strongly recommended that you check this file into your version control system.
 | 
			
		||||
 | 
			
		||||
ActiveRecord::Schema[7.0].define(version: 2023_09_06_073324) do
 | 
			
		||||
ActiveRecord::Schema[7.0].define(version: 2023_10_19_125135) do
 | 
			
		||||
  create_table "active_storage_attachments", force: :cascade do |t|
 | 
			
		||||
    t.string "name", null: false
 | 
			
		||||
    t.string "record_type", null: false
 | 
			
		||||
@ -39,6 +39,14 @@ ActiveRecord::Schema[7.0].define(version: 2023_09_06_073324) do
 | 
			
		||||
    t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  create_table "app_catalog_web_apps", force: :cascade do |t|
 | 
			
		||||
    t.string "url"
 | 
			
		||||
    t.string "name"
 | 
			
		||||
    t.text "metadata"
 | 
			
		||||
    t.datetime "created_at", null: false
 | 
			
		||||
    t.datetime "updated_at", null: false
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  create_table "donations", force: :cascade do |t|
 | 
			
		||||
    t.integer "user_id"
 | 
			
		||||
    t.integer "amount_sats"
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										14
									
								
								spec/factories/app_catalog/web_apps.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								spec/factories/app_catalog/web_apps.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,14 @@
 | 
			
		||||
FactoryBot.define do
 | 
			
		||||
  factory :app_catalog_web_app, class: 'AppCatalog::WebApp' do
 | 
			
		||||
    url { "https://myfavoritedrinks.remotestorage.io/" }
 | 
			
		||||
    name { "My Favorite Drinks" }
 | 
			
		||||
    short_name { "Drinks" }
 | 
			
		||||
    description { nil }
 | 
			
		||||
    theme_color { nil }
 | 
			
		||||
    background_color { nil }
 | 
			
		||||
    display { nil }
 | 
			
		||||
    start_url { nil }
 | 
			
		||||
    scope { nil }
 | 
			
		||||
    share_target { nil }
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										5
									
								
								spec/models/app_catalog/web_app_spec.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								spec/models/app_catalog/web_app_spec.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,5 @@
 | 
			
		||||
require 'rails_helper'
 | 
			
		||||
 | 
			
		||||
RSpec.describe AppCatalog::WebApp, type: :model do
 | 
			
		||||
  pending "add some examples to (or delete) #{__FILE__}"
 | 
			
		||||
end
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user