parsav  tweet.t at [f9559a83fc]

File render/tweet.t artifact 71eb9101d9 part of check-in f9559a83fc


-- vim: ft=terra
local pstr = lib.mem.ptr(int8)
local terra cs(s: rawstring)
	return pstr { ptr = s, ct = lib.str.sz(s) }
end

local terra 
render_tweet(co: &lib.srv.convo, p: &lib.store.post, acc: &lib.str.acc)
	var author: &lib.store.actor
	for j = 0, co.actorcache.top do
		lib.io.fmt('scanning cache for author %llu (%llu/%llu)\n', p.author, j, co.actorcache.top)
		if p.author == co.actorcache(j).ptr.id then
			author = co.actorcache(j).ptr
			lib.io.fmt('cache hit on idx %llu, skipping db lookup\n', j) 
			goto foundauth
		end
	end
	lib.io.fmt('cache miss, checking db for id %llu\n', p.author) 
	author = co.actorcache:insert(co.srv:actor_fetch_uid(p.author)).ptr
	lib.io.fmt('got author %s\n', author.handle) 

	::foundauth::
	var avistr: lib.str.acc if author.origin == 0 then
		avistr:compose('/avi/',author.handle)
	end
	var timestr: int8[26] lib.osclock.ctime_r(&p.posted, &timestr[0])
	lib.io.fmt('got body %s\n', author.handle) 

	var bhtml = lib.smackdown.html([lib.mem.ptr(int8)] {ptr=p.body,ct=0}) defer bhtml:free()

	var idbuf: int8[lib.math.shorthand.maxlen]
	var idlen = lib.math.shorthand.gen(p.id, idbuf)
	var permalink: lib.str.acc permalink:compose('/post/',{idbuf,idlen})

	var tpl = data.view.tweet {
		text = bhtml;
		subject = cs(lib.coalesce(p.subject,''));
		nym = cs(lib.coalesce(author.nym, author.handle));
		xid = cs(author.xid);
		when = cs(&timestr[0]);
		avatar = cs(lib.trn(author.origin == 0, avistr.buf,
			lib.coalesce(author.avatar, '/s/default-avatar.webp')));
		acctlink = cs(author.xid);
		permalink = permalink:finalize();
	}
	defer tpl.permalink:free()
	if acc ~= nil then tpl:append(acc) return [lib.mem.ptr(int8)]{ptr=nil,ct=0} end
	var txt = tpl:tostr()
	return txt
end
return render_tweet