Merge pull request 'Fetch/store Web App metadata and icons, finish RS integration' (#153) from feature/142-webapp_database into master
Reviewed-on: #153 Reviewed-by: galfert <garret.alfert@gmail.com>
This commit was merged in pull request #153.
This commit is contained in:
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
|
||||
16
app/models/app_catalog/web_app.rb
Normal file
16
app/models/app_catalog/web_app.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
class AppCatalog::WebApp < ApplicationRecord
|
||||
store :metadata, coder: JSON
|
||||
|
||||
has_many :remote_storage_authorizations
|
||||
|
||||
has_one_attached :icon
|
||||
has_one_attached :apple_touch_icon
|
||||
|
||||
validates :url, presence: true, uniqueness: true
|
||||
validates :url, format: { with: URI.regexp },
|
||||
if: Proc.new { |a| a.url.present? }
|
||||
|
||||
def update_metadata
|
||||
AppCatalogManager::UpdateMetadata.call(self)
|
||||
end
|
||||
end
|
||||
@@ -1,5 +1,6 @@
|
||||
class RemoteStorageAuthorization < ApplicationRecord
|
||||
belongs_to :user
|
||||
belongs_to :web_app, class_name: "AppCatalog::WebApp", optional: true
|
||||
|
||||
serialize :permissions
|
||||
|
||||
@@ -15,22 +16,36 @@ class RemoteStorageAuthorization < ApplicationRecord
|
||||
|
||||
before_create :generate_token
|
||||
before_create :store_token_in_redis
|
||||
before_create :find_or_create_web_app
|
||||
after_create :schedule_token_expiry
|
||||
after_create :notify_user
|
||||
before_destroy :delete_token_from_redis
|
||||
after_destroy :remove_token_expiry_job
|
||||
|
||||
def url
|
||||
if self.redirect_uri
|
||||
uri = URI.parse self.redirect_uri
|
||||
"#{uri.scheme}://#{client_id}"
|
||||
uri = URI.parse self.redirect_uri
|
||||
"#{uri.scheme}://#{client_id}"
|
||||
end
|
||||
|
||||
def launch_url
|
||||
return url unless web_app && web_app.metadata[:start_url].present?
|
||||
|
||||
start_url = web_app.metadata[:start_url]
|
||||
|
||||
if start_url.match("^https?:\/\/")
|
||||
return start_url.start_with?(url) ? start_url : url
|
||||
else
|
||||
"http://#{client_id}"
|
||||
path = start_url.gsub(/^\.\.\//, "").gsub(/^\.\//, "").gsub(/^\//, "")
|
||||
"#{url}/#{path}"
|
||||
end
|
||||
end
|
||||
|
||||
def delete_token_from_redis
|
||||
key = "rs:authorizations:#{user.address}:#{token}"
|
||||
key = "authorizations:#{user.cn}:#{token}"
|
||||
redis.srem? key, redis.smembers(key)
|
||||
rescue => e
|
||||
Rails.logger.error e
|
||||
Sentry.capture_exception(e) if Setting.sentry_enabled?
|
||||
end
|
||||
|
||||
private
|
||||
@@ -44,7 +59,7 @@ class RemoteStorageAuthorization < ApplicationRecord
|
||||
end
|
||||
|
||||
def store_token_in_redis
|
||||
redis.sadd "rs:authorizations:#{user.address}:#{token}", permissions
|
||||
redis.sadd "authorizations:#{user.cn}:#{token}", permissions
|
||||
end
|
||||
|
||||
def schedule_token_expiry
|
||||
@@ -60,4 +75,40 @@ class RemoteStorageAuthorization < ApplicationRecord
|
||||
job.delete if job.display_args == [id]
|
||||
end
|
||||
end
|
||||
|
||||
def find_or_create_web_app
|
||||
if looks_like_hosted_origin?
|
||||
web_app = AppCatalog::WebApp.find_or_create_by!(url: self.url)
|
||||
web_app.update_metadata unless web_app.name.present?
|
||||
self.web_app = web_app
|
||||
self.app_name = web_app.name.presence || client_id
|
||||
else
|
||||
self.app_name = client_id
|
||||
end
|
||||
end
|
||||
|
||||
def looks_like_hosted_origin?
|
||||
uri = URI.parse self.redirect_uri
|
||||
!!(uri.host =~ /(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)/)
|
||||
rescue URI::InvalidURIError
|
||||
false
|
||||
end
|
||||
|
||||
def notify_user
|
||||
notify = user.preferences[:remotestorage_notify_auth_created]
|
||||
|
||||
case notify
|
||||
when "xmpp"
|
||||
router = Router.new
|
||||
payload = {
|
||||
type: "normal", to: user.address,
|
||||
from: Setting.xmpp_notifications_from_address,
|
||||
body: "You have just granted '#{self.client_id}' access to your Kosmos Storage. Visit your Storage dashboard to check on your connected apps and revoke permissions anytime: #{router.services_storage_url}"
|
||||
}
|
||||
XmppSendMessageJob.perform_later(payload)
|
||||
when "email"
|
||||
NotificationMailer.with(user: user, auth: self)
|
||||
.remotestorage_auth_created.deliver_later
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user