client/lua: wrap tags with error handler

This commit is contained in:
Ben Klein 2021-07-11 15:06:40 -04:00
parent 372b40a6b5
commit 6e07c61d3f

View file

@ -41,81 +41,88 @@ 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('^/$') then -- Site root local function generate_meta_tags ()
add_meta_tag("og:type", "website") if ngx.var.request_uri_path:match('^/$') then -- Site root
add_meta_tag("og:title", server_info.config.name) add_meta_tag("og:type", "website")
add_meta_tag("twitter:title", server_info.config.name) add_meta_tag("og:title", server_info.config.name)
-- if there's a featured post, let's use that as the image add_meta_tag("twitter:title", server_info.config.name)
if server_info.featuredPost then -- if there's a featured post, let's use that as the image
local post_info = server_info.featuredPost if server_info.featuredPost then
-- NOTE this is different from the normal Post tags, local post_info = server_info.featuredPost
-- notably we avoid setting the article type, author, time, etc -- NOTE this is different from the normal Post tags,
local og_media_prefix -- notably we avoid setting the article type, author, time, etc
if post_info.type == "image" then local og_media_prefix
og_media_prefix = "og:image" if post_info.type == "image" then
add_meta_tag("twitter:card", "summary_large_image") og_media_prefix = "og:image"
add_meta_tag("twitter:image", ngx.var.external_host_url .. '/' .. post_info.contentUrl) add_meta_tag("twitter:card", "summary_large_image")
elseif post_info.type == "video" then add_meta_tag("twitter:image", ngx.var.external_host_url .. '/' .. post_info.contentUrl)
og_media_prefix = "og:video" elseif post_info.type == "video" then
-- some sites don't preview video, so at least provide a thumbnail og_media_prefix = "og:video"
add_meta_tag("og:image", ngx.var.external_host_url .. '/' .. post_info.thumbnailUrl) -- 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 end
add_meta_tag(og_media_prefix..":url", ngx.var.external_host_url .. '/' .. post_info.contentUrl) elseif ngx.var.request_uri_path:match('^/post/([0-9]+)/?$') then -- Post metadata
add_meta_tag(og_media_prefix..":width", post_info.canvasWidth) -- check if posts are accessible to anonymous users:
add_meta_tag(og_media_prefix..":height", post_info.canvasHeight) if server_info.config.privileges["posts:view"] == "anonymous" then
end add_meta_tag("og:type", "article")
elseif ngx.var.request_uri_path:match('^/post/([0-9]+)/?$') then -- Post metadata local post_info = cjson.decode((ngx.location.capture("/_internal_api"..ngx.var.request_uri_path)).body)
-- check if posts are accessible to anonymous users: add_meta_tag("og:title", server_info.config.name .. " - Post " .. post_info.id)
if server_info.config.privileges["posts:view"] == "anonymous" then add_meta_tag("twitter:title", server_info.config.name .. " - Post " .. post_info.id)
add_meta_tag("og:type", "article") add_meta_tag("article:published_time", post_info.creationTime)
local post_info = cjson.decode((ngx.location.capture("/_internal_api"..ngx.var.request_uri_path)).body) local og_media_prefix
add_meta_tag("og:title", server_info.config.name .. " - Post " .. post_info.id) if post_info.type == "video" then
add_meta_tag("twitter:title", server_info.config.name .. " - Post " .. post_info.id) og_media_prefix = "og:video"
add_meta_tag("article:published_time", post_info.creationTime) add_meta_tag("twitter:card", "player")
local og_media_prefix add_meta_tag("twitter:player:width", post_info.canvasWidth)
if post_info.type == "image" then add_meta_tag("twitter:player:height", post_info.canvasHeight)
og_media_prefix = "og:image" -- some sites don't preview video, so at least provide a thumbnail
add_meta_tag("twitter:card", "summary_large_image") add_meta_tag("og:image", ngx.var.external_host_url .. '/' .. post_info.thumbnailUrl)
add_meta_tag("twitter:image", ngx.var.external_host_url .. '/' .. post_info.contentUrl) else
elseif post_info.type == "video" then og_media_prefix = "og:image"
og_media_prefix = "og:video" add_meta_tag("twitter:card", "summary_large_image")
add_meta_tag("twitter:card", "player") add_meta_tag("twitter:image", ngx.var.external_host_url .. '/' .. post_info.contentUrl)
add_meta_tag("twitter:player:width", post_info.canvasWidth) end
add_meta_tag("twitter:player:height", post_info.canvasHeight) add_meta_tag(og_media_prefix..":url", ngx.var.external_host_url .. '/' .. post_info.contentUrl)
-- some sites don't preview video, so at least provide a thumbnail add_meta_tag(og_media_prefix..":width", post_info.canvasWidth)
add_meta_tag("og:image", ngx.var.external_host_url .. '/' .. post_info.thumbnailUrl) add_meta_tag(og_media_prefix..":height", post_info.canvasHeight)
end -- user is not present for anonymous uploads:
add_meta_tag(og_media_prefix..":url", ngx.var.external_host_url .. '/' .. post_info.contentUrl) if post_info.user then
add_meta_tag(og_media_prefix..":width", post_info.canvasWidth) add_meta_tag("article:author", post_info.user.name)
add_meta_tag(og_media_prefix..":height", post_info.canvasHeight) end
-- user is not present for anonymous uploads:
if post_info.user then
add_meta_tag("article:author", post_info.user.name)
end
else
-- no permission to retrieve post data
add_meta_tag("og:title", server_info.config.name .. " - Login required")
end
elseif ngx.var.request_uri_path:match('^/user/([^/]+)/?$') then -- User metadata
local username = ngx.var.request_uri_path:match('^/user/([^/]+)/?$')
add_meta_tag("og:title", server_info.config.name .. " - " .. username)
add_meta_tag("og:type", "profile")
-- check for permission to access user profiles
if server_info.config.privileges["users:view"] == "anonymous" then
local user_info = cjson.decode((ngx.location.capture("/_internal_api/user/"..username)).body)
add_meta_tag("profile:username", user_info.name)
local avatar_url
avatar_url = user_info.avatarUrl
if avatar_url:match("^https?://") then
add_meta_tag("og:image", avatar_url)
else else
add_meta_tag("og:image", ngx.var.external_host_url .. '/' .. avatar_url) -- no permission to retrieve post data
add_meta_tag("og:title", server_info.config.name .. " - Login required")
end
elseif ngx.var.request_uri_path:match('^/user/([^/]+)/?$') then -- User metadata
local username = ngx.var.request_uri_path:match('^/user/([^/]+)/?$')
add_meta_tag("og:title", server_info.config.name .. " - " .. username)
add_meta_tag("og:type", "profile")
-- check for permission to access user profiles
if server_info.config.privileges["users:view"] == "anonymous" then
local user_info = cjson.decode((ngx.location.capture("/_internal_api/user/"..username)).body)
add_meta_tag("profile:username", user_info.name)
local avatar_url
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
-- no permission to view user data
end end
else
-- no permission to view user data
end end
end end
local status, err = pcall(generate_meta_tags)
if not status then
ngx.log(ngx.STDERR, "Failed to generate meta tags: "..tostring(err))
end
-- Once tags have been generated, write them and then finish the response -- Once tags have been generated, write them and then finish the response
ngx.print(additional_tags) ngx.print(additional_tags)
ngx.print(after_content) ngx.print(after_content)