client/lua: stricter matches and home tags

This commit is contained in:
Ben Klein 2021-07-10 17:17:08 -04:00
parent 9f1bb53b4f
commit b302ce23bd

View file

@ -41,7 +41,29 @@ end
add_meta_tag("og:site_name", server_info.config.name) add_meta_tag("og:site_name", server_info.config.name)
add_meta_tag("og:url", ngx.var.external_host_url .. ngx.var.request_uri_path) add_meta_tag("og:url", ngx.var.external_host_url .. ngx.var.request_uri_path)
if ngx.var.request_uri_path:match('^/post') then -- Post metadata if ngx.var.request_uri_path:match('^/$') then -- Site root
add_meta_tag("og:title", server_info.config.name)
add_meta_tag("twitter:title", server_info.config.name)
-- if there's a featured post, let's use that as the image
if server_info.featuredPost then
local post_info = server_info.featuredPost
-- NOTE this is different from the normal Post tags,
-- notably we avoid setting the article type, author, time, etc
local og_media_prefix
if post_info.type == "image" then
og_media_prefix = "og:image"
add_meta_tag("twitter:card", "summary_large_image")
add_meta_tag("twitter:image", ngx.var.external_host_url .. '/' .. post_info.contentUrl)
elseif post_info.type == "video" then
og_media_prefix = "og:video"
-- some sites don't preview video, so at least provide a thumbnail
add_meta_tag("og:image", ngx.var.external_host_url .. '/' .. post_info.thumbnailUrl)
end
add_meta_tag(og_media_prefix..":url", ngx.var.external_host_url .. '/' .. post_info.contentUrl)
add_meta_tag(og_media_prefix..":width", post_info.canvasWidth)
add_meta_tag(og_media_prefix..":height", post_info.canvasHeight)
end
elseif ngx.var.request_uri_path:match('^/post/([0-9]+)/?$') then -- Post metadata
-- check if posts are accessible to anonymous users: -- check if posts are accessible to anonymous users:
if server_info.config.privileges["posts:view"] == "anonymous" then if server_info.config.privileges["posts:view"] == "anonymous" then
add_meta_tag("og:type", "article") add_meta_tag("og:type", "article")
@ -70,15 +92,22 @@ if ngx.var.request_uri_path:match('^/post') then -- Post metadata
-- no permission to retrieve post data -- no permission to retrieve post data
add_meta_tag("og:title", server_info.config.name .. " - Login required") add_meta_tag("og:title", server_info.config.name .. " - Login required")
end end
elseif ngx.var.request_uri_path:match('^/user/(.*)$') then -- User metadata elseif ngx.var.request_uri_path:match('^/user/([^/]+)/?$') then -- User metadata
local username = ngx.var.request_uri_path:match('^/user/(.*)/?$') local username = ngx.var.request_uri_path:match('^/user/([^/]+)/?$')
add_meta_tag("og:title", server_info.config.name .. " - User " .. username) add_meta_tag("og:title", server_info.config.name .. " - " .. username)
add_meta_tag("og:type", "profile")
-- check for permission to access user profiles -- check for permission to access user profiles
if server_info.config.privileges["users:view"] == "anonymous" then if server_info.config.privileges["users:view"] == "anonymous" then
local user_info = cjson.decode((ngx.location.capture("/_internal_api/"..username)).body) local user_info = cjson.decode((ngx.location.capture("/_internal_api/"..username)).body)
-- TODO add_meta_tag("profile:username", user_info.name)
local avatar_url = user_info.avatarUrl
if avatar_url:match("^https?://") then
add_meta_tag("og:image", avatar_url)
else
add_meta_tag("og:image", ngx.var.external_host_url .. '/' .. avatar_url)
end
else else
-- TODO -- no permission to view user data
end end
end end