client/lua: further development for post tags

This commit is contained in:
Ben Klein 2021-07-10 16:07:52 -04:00
parent 3ec013b210
commit 9fdeb293c1

View file

@ -18,6 +18,8 @@ if not placeholder then
else else
-- start the response -- start the response
ngx.print(before_content) ngx.print(before_content)
-- send partial content to the client to allow preloading the app
ngx.flush()
end end
ngx.req.set_header("Accept", "application/json") ngx.req.set_header("Accept", "application/json")
@ -31,21 +33,40 @@ local server_info = cjson.decode(server_info_resp.body)
local additional_tags = "" local additional_tags = ""
local function add_meta_tag (property, content) local function add_meta_tag (property, content)
additional_tags = additional_tags .. "<meta property=\"" .. property .. "\" content=\"" .. content:gsub('"', '\\"') .. "\"/>" additional_tags = additional_tags .. "<meta property=\"" .. property .. "\" content=\"" .. tostring(content):gsub('"', '\\"') .. "\"/>"
end end
-- Add the site name tag -- Add the site name tag
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) add_meta_tag("og:url", ngx.var.external_host_url .. ngx.var.request_uri)
if ngx.var.request_uri_path:match('^/post') then if ngx.var.request_uri_path:match('^/post') 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:
-- If no permission to access, fields will be nil, thus cannot be concat'd if server_info.config.privileges["posts:view"] == "anonymous" then
if post_info.contentUrl then local post_info = cjson.decode((ngx.location.capture("/_internal_api"..ngx.var.request_uri_path)).body)
add_meta_tag("og:image", ngx.var.external_host_url .. '/' .. post_info.contentUrl)
end
if post_info.id then
add_meta_tag("og:title", server_info.config.name .. " - Post " .. post_info.id) add_meta_tag("og:title", server_info.config.name .. " - Post " .. post_info.id)
local og_media_prefix
if post_info.type == "image" then
og_media_prefix = "og:image"
elseif post_info.type == "video" then
og_media_prefix = "og:video"
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)
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 .. " - User " .. username)
-- 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/"..username)).body)
-- TODO
else
-- TODO
end end
end end