From 192ac128cd1af63080be39f43070223efc129cd9 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Sat, 20 Oct 2018 17:15:29 -0700 Subject: [PATCH] add additional meta tags for seo --- app/logical/downloads/file.rb | 2 + app/logical/post_sets/base.rb | 4 ++ app/logical/post_sets/post.rb | 7 ++- app/views/posts/index.html.erb | 16 +----- .../partials/index/_seo_meta_tags.html.erb | 49 +++++++++++++++++ db/populate.rb | 18 ++---- db/structure.sql | 55 ++++++++++++++++++- 7 files changed, 121 insertions(+), 30 deletions(-) create mode 100644 app/views/posts/partials/index/_seo_meta_tags.html.erb diff --git a/app/logical/downloads/file.rb b/app/logical/downloads/file.rb index 983d2e613..45298969b 100644 --- a/app/logical/downloads/file.rb +++ b/app/logical/downloads/file.rb @@ -22,6 +22,8 @@ module Downloads end def is_cloudflare? + return false if ENV["SKIP_CLOUDFLARE_CHECK"] + Cache.get("is_cloudflare:#{file_url.origin}", 4.hours) do res = HTTParty.head(file_url, httparty_options) raise Error.new("HTTP error code: #{res.code} #{res.message}") unless res.success? diff --git a/app/logical/post_sets/base.rb b/app/logical/post_sets/base.rb index 1c965be70..0b5a335c3 100644 --- a/app/logical/post_sets/base.rb +++ b/app/logical/post_sets/base.rb @@ -36,6 +36,10 @@ module PostSets false end + def best_post + nil + end + def presenter raise NotImplementedError end diff --git a/app/logical/post_sets/post.rb b/app/logical/post_sets/post.rb index f61ae04e8..78e401668 100644 --- a/app/logical/post_sets/post.rb +++ b/app/logical/post_sets/post.rb @@ -26,7 +26,7 @@ module PostSets end def has_wiki? - is_single_tag? && ::WikiPage.titled(tag_string).exists? && wiki_page.visible? + is_single_tag? && ::WikiPage.titled(tag_string).exists? && wiki_page.visible? && wiki_page.body.present? end def has_blank_wiki? @@ -184,5 +184,10 @@ module PostSets def presenter @presenter ||= ::PostSetPresenters::Post.new(self) end + + def best_post + # be smarter about this in the future + posts[0] + end end end diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index 7a341cd84..d6b7692d4 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -65,22 +65,12 @@ <% end %> <%= content_for(:html_header) do %> - - <%#= tag.link rel: "prefetch", href: next_page_url, as: "html", crossorigin: "use-credentials" %> + <%= tag.link href: posts_path(format: "atom", tags: params[:tags]), rel: "alternate", title: "ATOM", type: "application/atom+xml" %> + <%= tag.link rel: "prefetch", href: next_page_url, as: "html", crossorigin: "use-credentials" %> <%= tag.link rel: "next", href: next_page_url %> <% if prev_page_url %> <%= tag.link rel: "prev", href: prev_page_url %> <% end %> - <% if @post_set.has_wiki? %> - - <% else %> - - <% end %> <%= missed_post_search_count_js %> - <% if @post_set.hide_from_crawler? %> - - <% end %> - <% if @post_set.has_explicit? %> - - <% end %> + <%= render "posts/partials/index/seo_meta_tags" %> <% end %> diff --git a/app/views/posts/partials/index/_seo_meta_tags.html.erb b/app/views/posts/partials/index/_seo_meta_tags.html.erb new file mode 100644 index 000000000..ef03593cb --- /dev/null +++ b/app/views/posts/partials/index/_seo_meta_tags.html.erb @@ -0,0 +1,49 @@ +<% if @post_set.has_wiki? %> + <% strip_dtext(@post_set.wiki_page.presenter.excerpt).tap do |title| %> + <%= tag.meta name: "og:title", content: title %> + <%= tag.meta name: "twitter:title", content: title %> + <% end %> +<% elsif @post_set.tag_string.present? %> + <% "#{@post_set.humanized_tag_string} - #{Danbooru.config.app_name}".tap do |title| %> + <%= tag.meta name: "og:title", content: title %> + <%= tag.meta name: "twitter:title", content: title %> + <% end %> +<% else %> + <% Danbooru.config.description.tap do |title| %> + <%= tag.meta name: "og:title", content: title %> + <%= tag.meta name: "twitter:title", content: title %> + <% end %> +<% end %> + +<% if @post_set.hide_from_crawler? %> + +<% end %> + +<% if @post_set.has_explicit? %> + +<% end %> + +<%= tag.meta name: "canonical", content: posts_url(tags: params[:tags], host: Danbooru.config.domain) %> + +<% if params[:tags].present? %> + <% "Find the best art of #{params[:tags].tr('_', ' ').titleize} on #{Danbooru.config.app_name}".tap do |desc| %> + <%= tag.meta name: "description", content: desc %> + <%= tag.meta name: "og:description", content: desc %> + <%= tag.meta name: "twitter:description", content: desc %> + <% end %> +<% else %> + <% Danbooru.config.description.tap do |desc| %> + <%= tag.meta name: "description", content: desc %> + <%= tag.meta name: "og:description", content: desc %> + <%= tag.meta name: "twitter:description", content: desc %> + <% end %> +<% end %> + +<%= tag.meta name: "og:type", content: "website" %> +<%= tag.meta name: "og:site", content: Danbooru.config.app_name %> + +<% if @post_set.best_post.present? %> + <%= tag.meta name: "twitter:image", content: @post_set.best_post.file_url %> + <%= tag.meta name: "og:image", content: @post_set.best_post.file_url %> + <%= tag.meta name: "twitter:card", content: "summary_large_image" %> +<% end %> \ No newline at end of file diff --git a/db/populate.rb b/db/populate.rb index bc7199140..e98ca3148 100644 --- a/db/populate.rb +++ b/db/populate.rb @@ -96,15 +96,17 @@ end CurrentUser.as_admin if Upload.count == 0 + ENV["SKIP_CLOUDFLARE_CHECK"] = "true" + puts "Creating uploads" 1.upto(50) do |i| color1 = rand(4096).to_s(16) color2 = rand(4096).to_s(16) width = rand(2000) + 100 - height = rand(2000) + 100 + height = (width * (rand(0.5) + 1)).to_i url = "http://ipsumimage.appspot.com/#{width}x#{height},#{color1}" tags = rand(1_000_000_000).to_s.scan(/../).join(" ") - + puts url service = UploadService.new(source: url, tag_string: tags, rating: "s") service.start! end @@ -112,18 +114,6 @@ else puts "Skipping uploads" end -if Post.count == 0 - puts "Creating posts" - Upload.all.each do |upload| - upload.process! - end - if Post.count == 0 - raise "Uploads failed conversion" - end -else - puts "Skipping posts" -end - if Comment.count == 0 puts "Creating comments" Post.all.each do |post| diff --git a/db/structure.sql b/db/structure.sql index 650beb267..dbe979da3 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -443,6 +443,7 @@ CREATE TABLE public.advertisement_hits ( -- CREATE SEQUENCE public.advertisement_hits_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -481,6 +482,7 @@ CREATE TABLE public.advertisements ( -- CREATE SEQUENCE public.advertisements_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -512,6 +514,7 @@ CREATE TABLE public.amazon_backups ( -- CREATE SEQUENCE public.amazon_backups_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -543,6 +546,7 @@ CREATE TABLE public.anti_voters ( -- CREATE SEQUENCE public.anti_voters_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -575,6 +579,7 @@ CREATE TABLE public.api_keys ( -- CREATE SEQUENCE public.api_keys_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -622,6 +627,7 @@ CREATE TABLE public.artist_commentaries ( -- CREATE SEQUENCE public.artist_commentaries_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -659,6 +665,7 @@ CREATE TABLE public.artist_commentary_versions ( -- CREATE SEQUENCE public.artist_commentary_versions_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -693,6 +700,7 @@ CREATE TABLE public.artist_urls ( -- CREATE SEQUENCE public.artist_urls_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -732,6 +740,7 @@ CREATE TABLE public.artist_versions ( -- CREATE SEQUENCE public.artist_versions_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -769,6 +778,7 @@ CREATE TABLE public.artists ( -- CREATE SEQUENCE public.artists_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -803,6 +813,7 @@ CREATE TABLE public.bans ( -- CREATE SEQUENCE public.bans_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -840,6 +851,7 @@ CREATE TABLE public.bulk_update_requests ( -- CREATE SEQUENCE public.bulk_update_requests_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -873,6 +885,7 @@ CREATE TABLE public.comment_votes ( -- CREATE SEQUENCE public.comment_votes_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -914,6 +927,7 @@ CREATE TABLE public.comments ( -- CREATE SEQUENCE public.comments_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -953,6 +967,7 @@ CREATE TABLE public.delayed_jobs ( -- CREATE SEQUENCE public.delayed_jobs_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -985,6 +1000,7 @@ CREATE TABLE public.dmail_filters ( -- CREATE SEQUENCE public.dmail_filters_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -1025,6 +1041,7 @@ CREATE TABLE public.dmails ( -- CREATE SEQUENCE public.dmails_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -1060,6 +1077,7 @@ CREATE TABLE public.favorite_groups ( -- CREATE SEQUENCE public.favorite_groups_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2090,6 +2108,7 @@ INHERITS (public.favorites); -- CREATE SEQUENCE public.favorites_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2159,6 +2178,7 @@ CREATE TABLE public.forum_posts ( -- CREATE SEQUENCE public.forum_posts_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2191,6 +2211,7 @@ CREATE TABLE public.forum_subscriptions ( -- CREATE SEQUENCE public.forum_subscriptions_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2224,6 +2245,7 @@ CREATE TABLE public.forum_topic_visits ( -- CREATE SEQUENCE public.forum_topic_visits_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2264,6 +2286,7 @@ CREATE TABLE public.forum_topics ( -- CREATE SEQUENCE public.forum_topics_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2297,6 +2320,7 @@ CREATE TABLE public.ip_bans ( -- CREATE SEQUENCE public.ip_bans_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2331,6 +2355,7 @@ CREATE TABLE public.janitor_trials ( -- CREATE SEQUENCE public.janitor_trials_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2364,6 +2389,7 @@ CREATE TABLE public.mod_actions ( -- CREATE SEQUENCE public.mod_actions_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2397,6 +2423,7 @@ CREATE TABLE public.news_updates ( -- CREATE SEQUENCE public.news_updates_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2438,6 +2465,7 @@ CREATE TABLE public.note_versions ( -- CREATE SEQUENCE public.note_versions_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2478,6 +2506,7 @@ CREATE TABLE public.notes ( -- CREATE SEQUENCE public.notes_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2509,6 +2538,7 @@ CREATE TABLE public.pixiv_ugoira_frame_data ( -- CREATE SEQUENCE public.pixiv_ugoira_frame_data_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2547,6 +2577,7 @@ CREATE TABLE public.pools ( -- CREATE SEQUENCE public.pools_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2581,6 +2612,7 @@ CREATE TABLE public.post_appeals ( -- CREATE SEQUENCE public.post_appeals_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2613,6 +2645,7 @@ CREATE TABLE public.post_approvals ( -- CREATE SEQUENCE public.post_approvals_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2647,6 +2680,7 @@ CREATE TABLE public.post_disapprovals ( -- CREATE SEQUENCE public.post_disapprovals_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2682,6 +2716,7 @@ CREATE TABLE public.post_flags ( -- CREATE SEQUENCE public.post_flags_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2726,6 +2761,7 @@ CREATE TABLE public.post_replacements ( -- CREATE SEQUENCE public.post_replacements_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2768,6 +2804,7 @@ CREATE TABLE public.post_votes ( -- CREATE SEQUENCE public.post_votes_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2838,6 +2875,7 @@ CREATE TABLE public.posts ( -- CREATE SEQUENCE public.posts_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2871,6 +2909,7 @@ CREATE TABLE public.saved_searches ( -- CREATE SEQUENCE public.saved_searches_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2911,6 +2950,7 @@ CREATE TABLE public.super_voters ( -- CREATE SEQUENCE public.super_voters_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2950,6 +2990,7 @@ CREATE TABLE public.tag_aliases ( -- CREATE SEQUENCE public.tag_aliases_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2989,6 +3030,7 @@ CREATE TABLE public.tag_implications ( -- CREATE SEQUENCE public.tag_implications_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -3026,6 +3068,7 @@ CREATE TABLE public.tag_subscriptions ( -- CREATE SEQUENCE public.tag_subscriptions_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -3062,6 +3105,7 @@ CREATE TABLE public.tags ( -- CREATE SEQUENCE public.tags_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -3126,6 +3170,7 @@ CREATE TABLE public.uploads ( -- CREATE SEQUENCE public.uploads_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -3160,6 +3205,7 @@ CREATE TABLE public.user_feedback ( -- CREATE SEQUENCE public.user_feedback_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -3197,6 +3243,7 @@ CREATE TABLE public.user_name_change_requests ( -- CREATE SEQUENCE public.user_name_change_requests_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -3229,6 +3276,7 @@ CREATE TABLE public.user_password_reset_nonces ( -- CREATE SEQUENCE public.user_password_reset_nonces_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -3287,6 +3335,7 @@ furry -rating:s'::text, -- CREATE SEQUENCE public.users_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -3325,6 +3374,7 @@ CREATE TABLE public.wiki_page_versions ( -- CREATE SEQUENCE public.wiki_page_versions_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -3364,6 +3414,7 @@ CREATE TABLE public.wiki_pages ( -- CREATE SEQUENCE public.wiki_pages_id_seq + AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -7125,7 +7176,7 @@ CREATE INDEX index_tags_on_name_pattern ON public.tags USING btree (name text_pa -- Name: index_tags_on_name_prefix; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX index_tags_on_name_prefix ON public.tags USING gin (regexp_replace((name)::text, '([a-z0-9])[a-z0-9'']*($|[^a-z0-9'']+)'::text, ''::text, 'g'::text) public.gin_trgm_ops) WHERE (post_count > 0); +CREATE INDEX index_tags_on_name_prefix ON public.tags USING gin (regexp_replace((name)::text, '([a-z0-9])[a-z0-9'']*($|[^a-z0-9'']+)'::text, '\1'::text, 'g'::text) public.gin_trgm_ops); -- @@ -7363,7 +7414,7 @@ CREATE TRIGGER trigger_wiki_pages_on_update_for_other_names BEFORE INSERT OR UPD -- PostgreSQL database dump complete -- -SET search_path TO "$user", public; +SET search_path TO public; INSERT INTO "schema_migrations" (version) VALUES ('20100204211522'),