diaspora/app/presenters/post_presenter.rb
Denis Hovart bcace2def2 6840 : meta tags update (#6998)
* Adds a new metadata helper and methods to PostPresenter to have metas on post pages.

* Adds tests to post controller to check correctness of metas

* Add methods to PersonPresenter to have metas on profile pages

* Correct meta data helper test

* Update PersonPresenter, add test to PeopleController

* Creates TagPresenter. Display tag metas on tag index page

* Updata meta data helper spec

* Not displaying bio as the description meta on profile page for now. Privacy concerns to be cleared.

* Set meta info as hashes in presenters

* Move original hardcoded metas info to config/defaults.yml

* metas_tags include by default the general metas, update views

* Update code style, clean views

* Renames TagPresenter StreamTagPresenter, updates TagController spec

* Add a default_metas entry to diaspora.yml.example

* Align metas hash in presenters, refactor meta data helper

* Use bio as description meta if user has a public profile

* Rename StreamTagPresenter to TagStreamPresenter
2016-08-18 21:52:39 +02:00

157 lines
4.2 KiB
Ruby

class PostPresenter < BasePresenter
include PostsHelper
include MetaDataHelper
attr_accessor :post
def initialize(presentable, current_user=nil)
@post = presentable
super
end
def as_json(_options={})
@post.as_json(only: directly_retrieved_attributes)
.merge(non_directly_retrieved_attributes)
end
def metas_attributes
{
keywords: {name: "keywords", content: comma_separated_tags},
description: {name: "description", content: description},
og_url: {property: "og:url", content: url},
og_title: {property: "og:title", content: title},
og_image: {property: "og:image", content: images},
og_description: {property: "og:description", content: description},
og_article_tag: {property: "og:article:tag", content: tags},
og_article_author: {property: "og:article:author", content: author_name},
og_article_modified: {property: "og:article:modified_time", content: modified_time_iso8601},
og_article_published: {property: "og:article:published_time", content: published_time_iso8601}
}
end
def page_title
post_page_title @post
end
private
def directly_retrieved_attributes
%i(id guid public created_at interacted_at provider_display_name)
end
def non_directly_retrieved_attributes
{
text: build_text,
post_type: @post.post_type,
nsfw: @post.nsfw,
author: @post.author.as_api_response(:backbone),
o_embed_cache: @post.o_embed_cache.try(:as_api_response, :backbone),
open_graph_cache: build_open_graph_cache,
mentioned_people: build_mentioned_people_json,
photos: build_photos_json,
root: root,
title: title,
location: @post.post_location,
poll: @post.poll,
already_participated_in_poll: already_participated_in_poll,
participation: participate?,
interactions: build_interactions_json
}
end
def title
@post.message.present? ? @post.message.title : I18n.t("posts.presenter.title", name: @post.author_name)
end
def build_text
if @post.message
@post.message.plain_text_for_json
else
@post.text
end
end
def build_open_graph_cache
@post.open_graph_cache.try(:as_api_response, :backbone)
end
def build_mentioned_people_json
@post.mentioned_people.as_api_response(:backbone)
end
def build_photos_json
@post.photos.map {|p| p.as_api_response(:backbone) }
end
def root
if @post.respond_to?(:absolute_root) && @post.absolute_root.present?
PostPresenter.new(@post.absolute_root, current_user).as_json
end
end
def build_interactions_json
{
likes: [user_like].compact,
reshares: [user_reshare].compact,
comments_count: @post.comments_count,
likes_count: @post.likes_count,
reshares_count: @post.reshares_count
}
end
def user_like
@post.like_for(current_user).try(:as_api_response, :backbone)
end
def user_reshare
@post.reshare_for(current_user)
end
def already_participated_in_poll
if @post.poll && user_signed_in?
@post.poll.already_participated?(current_user)
end
end
def participate?
user_signed_in? && current_user.participations.where(target_id: @post).exists?
end
def user_signed_in?
current_user.present?
end
def person
current_user.person
end
def images
photos.any? ? photos.map(&:url) : default_image_url
end
def published_time_iso8601
created_at.to_time.iso8601
end
def modified_time_iso8601
updated_at.to_time.iso8601
end
def tags
tags = @post.is_a?(Reshare) ? @post.root.tags : @post.tags
return tags.map(&:name) if tags
[]
end
def comma_separated_tags
tags.join(", ")
end
def url
post_url @post
end
def description
message.plain_text_without_markdown(truncate: 1000)
end
end