Move web app manifest into its own controller

This commit is contained in:
Karina Kwiatek 2022-07-18 15:30:45 +02:00 committed by Karina Kwiatek
parent 5bd186bbe2
commit 7fdb2168a6
5 changed files with 93 additions and 85 deletions

View File

@ -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

View File

@ -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

View File

@ -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]

View File

@ -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

View File

@ -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