parsav  tweet.t at [c774e2c5a9]

File api/lp/tweet.t artifact cbbfbd7e42 part of check-in c774e2c5a9


-- vim: ft=terra
local pstr = lib.str.t

local obj = lib.tpl.mk [[{
	"\@context": "https://@+domain/s/litepub.jsonld",
	"type": "Note",
	"id": "https://@+domain/post/@^pid",
	"content": @$html,
	"source": @$raw,
	"attributedTo": "https://@+domain/user/@^uid",
	"actor": "https://@+domain/user/@^uid",
	"published": "@pubtime",
	"sensitive": false,
	"directMessage": false,
	"to": ["https://www.w3.org/ns/activitystreams#Public"],
	"summary": @$subj
	@extra
}]]

local wrap = lib.tpl.mk [[{
	"\@context": "https://@+domain/s/litepub.jsonld",
	"type": "@kind",
	"actor": "https://@+domain/user/@^uid",
	"published": "@pubtime",
	"id": "https://@+domain/api/lp/act/@^aid",
	"to": ["https://www.w3.org/ns/activitystreams#Public"],
	"object": @obj
}]]

local terra 
lp_tweet(co: &lib.srv.convo, p: &lib.store.post, act_wrap: bool): pstr
	var opdate = lib.munge.datetime(&co.srv.pool, p.posted)

	var extra: lib.str.acc extra:pool(&co.srv.pool,256)
	
	if p.parent ~= 0 then
		extra:lpush ',"inReplyTo":"'
		var par = co.srv:post_fetch(p.parent)
		if not par then
			lib.warn('database integrity violation: broken parent reference')
		else defer par:free()
			if par().localpost then -- gen uri for parent
				extra:lpush'https://':qpush(co.srv.cfg.domain):lpush'/post/':shpush(p.parent)
			else extra:push(par().uri,0) end
		end
		extra:lpush'"'
	end

	extra:lpush ',"conversation":"'
	if p.convoheaduri ~= nil then
		extra:qpush(p.convoheaduri)
	else
		var cid: uint64 = 0
		if p.parent ~= 0 then
			var top = co.srv:thread_top_find(p.parent)
			var tp = co.srv:post_fetch(top)
			if not tp then
				lib.warn('database integrity violation: missing thread parent')
				cid = p.id
			else
				if tp().convoheaduri ~= nil then
					extra:push(tp().convoheaduri,0)
				elseif tp().localpost == false then
					extra:push(tp().uri,0)
				else cid = top end
			end
		else
			cid = p.id
		end
		if cid ~= 0 then
			extra:lpush'https://':qpush(co.srv.cfg.domain)
			     :lpush'/post/':shpush(cid):lpush'/tree'
		end
	end
	extra:lpush'"'

	var tweet = (obj {
		domain = co.srv.cfg.domain, uid = p.author, pid = p.id;
		html = lib.smackdown.html(&co.srv.pool, p.body, false);
		raw = p.body, pubtime = opdate, extra = extra:finalize();
		subj = lib.trn(p.subject ~= nil, pstr(p.subject), pstr'');
	}):poolstr(&co.srv.pool)

	if act_wrap then
		return (wrap {
			domain = co.srv.cfg.domain, obj = tweet;
			kind = lib.trn(p.rtdby == 0, 'Create', 'Announce');
			uid = lib.trn(p.rtdby == 0, p.author, p.rtdby);
			aid = lib.trn(p.rtdby == 0, p.id, p.rtact);
			pubtime = lib.trn(p.rtdby == 0, opdate,
				lib.munge.datetime(&co.srv.pool,p.rtdat));
		}):poolstr(&co.srv.pool)
	else
		return tweet
	end
end

return lp_tweet