From 7fdb2168a6d68294697c09a7c73a38e542a28f24 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Mon, 18 Jul 2022 15:30:45 +0200 Subject: [PATCH] Move web app manifest into its own controller --- app/controllers/manifests_controller.rb | 47 +++++++++++++++++++ app/controllers/static_controller.rb | 44 ----------------- config/routes.rb | 2 +- spec/controllers/manifests_controller_spec.rb | 45 ++++++++++++++++++ spec/controllers/static_controller_spec.rb | 40 ---------------- 5 files changed, 93 insertions(+), 85 deletions(-) create mode 100644 app/controllers/manifests_controller.rb create mode 100644 spec/controllers/manifests_controller_spec.rb diff --git a/app/controllers/manifests_controller.rb b/app/controllers/manifests_controller.rb new file mode 100644 index 00000000..80c7ce8b --- /dev/null +++ b/app/controllers/manifests_controller.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +class ManifestsController < ApplicationController + include ThemeHelper + + def show + render json: { + name: APP_CONFIG["site_name"], + description: t("static.front.subtitle"), + start_url: root_url(source: "pwa"), + scope: root_url, + display: "standalone", + categories: %w[social], + lang: I18n.locale, + shortcuts: [ + webapp_shortcut(inbox_url, t("views.navigation.inbox"), "inbox") + ], + icons: webapp_icons, + theme_color: theme_color, + background_color: mobile_theme_color + } + end + + private + + def webapp_shortcut(url, name, icon_name) + { + name: name, + url: url, + icons: [ + { + src: "/icons/shortcuts/#{icon_name}.svg", + sizes: "96x96" + } + ] + } + end + + def webapp_icons + %i[1024 512 384 192 128 96 72 48].map do |size| + [ + { src: "/icons/maskable_icon_x#{size}.webp", size: "#{size}x#{size}", type: "image/webp" }, + { src: "/icons/maskable_icon_x#{size}.png", size: "#{size}x#{size}", type: "image/png" } + ] + end.flatten + end +end diff --git a/app/controllers/static_controller.rb b/app/controllers/static_controller.rb index d4ec3a3c..abea2047 100644 --- a/app/controllers/static_controller.rb +++ b/app/controllers/static_controller.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true class StaticController < ApplicationController - include ThemeHelper - def index if user_signed_in? @timeline = current_user.cursored_timeline(last_id: params[:last_id]) @@ -55,46 +53,4 @@ class StaticController < ApplicationController def terms end - - def webapp_manifest - render json: { - name: APP_CONFIG["site_name"], - description: t(".front.subtitle"), - start_url: root_url(source: "pwa"), - scope: root_url, - display: "standalone", - categories: %w[social], - lang: I18n.locale, - shortcuts: [ - webapp_shortcut(inbox_url, t("views.navigation.inbox"), "inbox") - ], - icons: webapp_icons, - theme_color: theme_color, - background_color: mobile_theme_color - } - end - - private - - def webapp_shortcut(url, name, icon_name) - { - name: name, - url: url, - icons: [ - { - src: "/icons/shortcuts/#{icon_name}.svg", - sizes: "96x96" - } - ] - } - end - - def webapp_icons - %i[1024 512 384 192 128 96 72 48].map do |size| - [ - { src: "/icons/maskable_icon_x#{size}.webp", size: "#{size}x#{size}", type: "image/webp" }, - { src: "/icons/maskable_icon_x#{size}.png", size: "#{size}x#{size}", type: "image/png" } - ] - end.flatten - end end diff --git a/config/routes.rb b/config/routes.rb index 195b3762..1f38aa06 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -42,7 +42,7 @@ Rails.application.routes.draw do match '/privacy', to: 'static#privacy_policy', via: 'get', as: :privacy_policy match '/terms', to: 'static#terms', via: 'get', as: :terms match '/linkfilter', to: 'static#linkfilter', via: 'get', as: :linkfilter - match '/manifest.json', to: 'static#webapp_manifest', via: 'get', as: :webapp_manifest + match '/manifest.json', to: 'manifests#show', via: 'get', as: :webapp_manifest # Devise routes devise_for :users, path: 'user', skip: [:sessions, :registrations] diff --git a/spec/controllers/manifests_controller_spec.rb b/spec/controllers/manifests_controller_spec.rb new file mode 100644 index 00000000..66d40640 --- /dev/null +++ b/spec/controllers/manifests_controller_spec.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require "rails_helper" + +describe ManifestsController, type: :controller do + describe "#show" do + subject { get :show } + + before do + stub_const("APP_CONFIG", { + "site_name" => "Specspring", + "hostname" => "test.host", + "https" => false, + "items_per_page" => 5 + }) + end + + it "returns a web app manifest" do + subject + expect(response).to have_http_status(200) + body = JSON.parse(response.body) + + expect(body["name"]).to eq("Specspring") + expect(body["start_url"]).to eq("http://test.host/?source=pwa") + expect(body["scope"]).to eq("http://test.host/") + expect(body["theme_color"]).to eq("#5e35b1") + end + + context "user with a theme is logged in" do + let(:user) { FactoryBot.create(:user) } + let!(:theme) { FactoryBot.create(:theme, user: user) } + + before do + sign_in(user) + end + + it "uses the user's theme" do + subject + expect(response).to have_http_status(200) + body = JSON.parse(response.body) + expect(body["theme_color"]).to eq("#8e8cd8") + end + end + end +end \ No newline at end of file diff --git a/spec/controllers/static_controller_spec.rb b/spec/controllers/static_controller_spec.rb index 917fe404..4b43c7b1 100644 --- a/spec/controllers/static_controller_spec.rb +++ b/spec/controllers/static_controller_spec.rb @@ -38,44 +38,4 @@ describe StaticController, type: :controller do end end end - - describe "#webapp_manifest" do - subject { get :webapp_manifest } - - before do - stub_const("APP_CONFIG", { - "site_name" => "Specspring", - "hostname" => "test.host", - "https" => false, - "items_per_page" => 5 - }) - end - - it "returns a web app manifest" do - subject - expect(response).to have_http_status(200) - body = JSON.parse(response.body) - - expect(body["name"]).to eq("Specspring") - expect(body["start_url"]).to eq("http://test.host/?source=pwa") - expect(body["scope"]).to eq("http://test.host/") - expect(body["theme_color"]).to eq("#5e35b1") - end - - context "user with a theme is logged in" do - let(:user) { FactoryBot.create(:user) } - let!(:theme) { FactoryBot.create(:theme, user: user) } - - before do - sign_in(user) - end - - it "uses the user's theme" do - subject - expect(response).to have_http_status(200) - body = JSON.parse(response.body) - expect(body["theme_color"]).to eq("#8e8cd8") - end - end - end end \ No newline at end of file