-- vim: ft=terra
local terra 
render_profile(co: &lib.srv.convo, actor: &lib.store.actor)
	var aux: lib.str.acc
	var auxp: rawstring
	if co.aid ~= 0 and co.who.id == actor.id then
		auxp = '<a href="/conf/profile">alter</a>'
	elseif co.aid ~= 0 then
		aux:compose('<a href="/', actor.xid, '/follow">follow</a><a href="/',
			actor.xid, '/chat">chat</a>')
		if co.who.rights.powers:affect_users() then
			aux:push('<a href="/',11):push(actor.xid,0):push('/ctl">control</a>',17)
		end
		auxp = aux.buf
	else
		aux:compose('<a href="/', actor.xid, '/follow">remote follow</a>')
	end
	var avistr: lib.str.acc if actor.origin == 0 then
		avistr:compose('/avi/',actor.handle)
	end
	var timestr: int8[26] lib.osclock.ctime_r(&actor.knownsince, &timestr[0])

	var strfbuf: int8[28*4]
	var stats = co.srv:actor_stats(actor.id)
		var sn_posts = lib.math.decstr_friendly(stats.posts, &strfbuf[ [strfbuf.type.N - 1] ])
		var sn_follows = lib.math.decstr_friendly(stats.follows, sn_posts - 1)
		var sn_followers = lib.math.decstr_friendly(stats.followers, sn_follows - 1)
		var sn_mutuals = lib.math.decstr_friendly(stats.mutuals, sn_followers - 1)
	
	var profile = data.view.profile {
		nym = lib.coalesce(actor.nym, actor.handle);
		bio = lib.coalesce(actor.bio, "<em>tall, dark, and mysterious</em>");
		xid = actor.xid;
		avatar = lib.trn(actor.origin == 0, avistr.buf,
			lib.coalesce(actor.avatar, '/s/default-avatar.webp'));

		nposts = sn_posts, nfollows = sn_follows;
		nfollowers = sn_followers, nmutuals = sn_mutuals;
		tweetday = timestr;
		timephrase = lib.trn(actor.origin == 0, 'joined', 'known since');

		auxbtn = auxp;
	}

	var ret = profile:tostr()
	if actor.origin == 0 then avistr:free() end
	if not (co.aid ~= 0 and co.who.id == actor.id) then aux:free() end
	return ret
end

return render_profile
