parsav  profile.t at [f09cd18161]

File render/profile.t artifact 5d5ed1c86e part of check-in f09cd18161


-- 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_profile(co: &lib.srv.convo, actor: &lib.store.actor)
	var aux: lib.str.acc
	var followed = false -- FIXME
	if co.aid ~= 0 and co.who.id == actor.id then
		aux:compose('<a class="button" href="/conf/profile?go=/@',actor.handle,'">alter</a>')
	elseif co.aid ~= 0 then
		if not followed then
			aux:compose('<button method="post" class="pos" name="act" value="follow">follow</button>')
		elseif followed then
			aux:compose('<button method="post" class="neg" name="act" value="unfollow">unfollow</button>')
		end
		aux:lpush('<a class="button" href="/'):push(actor.xid,0):lpush('/chat">chat</a>')
		if co.who.rights.powers:affect_users() and co.who:overpowers(actor) then
			aux:lpush('<a class="button" href="/'):push(actor.xid,0):lpush('/ctl">control</a>')
		end
	else
		aux:compose('<a class="button" href="/', actor.xid, '/follow">remote follow</a>')
	end
	var auxp = aux:finalize()
	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     = cs(lib.math.decstr_friendly(stats.posts, &strfbuf[ [strfbuf.type.N - 1] ]))
		var sn_follows   = cs(lib.math.decstr_friendly(stats.follows, sn_posts.ptr - 1))
		var sn_followers = cs(lib.math.decstr_friendly(stats.followers, sn_follows.ptr - 1))
		var sn_mutuals   = cs(lib.math.decstr_friendly(stats.mutuals, sn_followers.ptr - 1))
	var bio = lib.str.plit "<em>tall, dark, and mysterious</em>"
	if actor.bio ~= nil then
		bio = lib.smackdown.html(cs(actor.bio))
	end
	var fullname = lib.render.nym(actor,0,nil) defer fullname:free()
	var comments: lib.str.acc comments:init(64)
	-- this is really more what epithets are for, i think
	--if actor.rights.rank > 0 then comments:lpush('<li>staff member</li>') end
	if co.aid ~= 0 and actor.rights.rank ~= 0 then
		if co.who:outranks(actor) then
			comments:lpush('<li style="--co:50">underling</li>')
		elseif actor:outranks(co.who) then
			comments:lpush('<li style="--co:-50">outranks you</li>')
		end
	end

	var profile = data.view.profile {
		nym = fullname;
		bio = bio;
		xid = cs(actor.xid);
		avatar = cs(actor.avatar);

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

		remarks = '';

		auxbtn = auxp;
	}
	if comments.sz > 0 then profile.remarks = comments:finalize() end

	var ret = profile:tostr()
	auxp:free() 
	if actor.bio ~= nil then bio:free() end
	if comments.sz > 0 then profile.remarks:free() end
	return ret
end

return render_profile