-- vim: ft=terra
local tpl = lib.tpl.mk {
	sigil = '%';
	body = [[{
		"@context": "https://%+domain/s/litepub.jsonld",
		"type": "Person",
		"id": "%lpid",
		"url": "https://%+domain/@%+handle",
		"preferredUsername": %$handle,
		"name": %$nym,
		"summary": %$desc,
		"alsoKnownAs": ["https://%+domain/@%+handle"],
		"publicKey": {
			"id": "%lpid#ident-rsa",
			"owner": "%lpid",
			"publicKeyPem": %$rsa
		},
		"icon": {
			"type": "Image",
			"url": "https://%+domain%+avi"
		},
		"capabilities": { "acceptsChatMessages": false },
		"discoverable": true,
		"manuallyApprovedFollowers": %locked,
		"inbox": "https://%+domain/api/lp/inbox/user/%uid",
		"outbox": "https://%+domain/api/lp/outbox/user/%uid",
		"followers": "https://%+domain/api/lp/rel/%uid/followers",
		"following": "https://%+domain/api/lp/rel/%uid/following"
	}]];
}

local pstr = lib.str.t
terra cs(s: rawstring) return pstr {s, lib.trn(s == nil,0,lib.str.sz(s))} end

local terra 
api_lp_actor(co: &lib.srv.convo, actor: &lib.store.actor)
	var lpid = co:stra(64)
	lpid:lpush'https://':ppush(co.srv.cfg.domain):lpush'/user/':shpush(actor.id)
	var uid = co:stra(32) uid:shpush(actor.id) -- dumb hack bc lazy FIXME

	var upk = lib.crypt.loadpriv(actor.key)
	var pem: lib.crypt.pemfile

	if not upk.ok then
		lib.warn("could not load user's keypair; this is a sign of a bug, a corrupt database, or a problem with mbedtls")
	else defer upk.val:free()
		if not lib.crypt.pem(true, &upk.val, &pem[0]) then
			pem[0] = 0;
			lib.warn('could not export actor certificate as PEM file; there is a bug, the database is corrupt, or there is a problem in mbedtls')
		end
	end
	var body = tpl {
		domain = co.srv.cfg.domain;
		uid = uid:finalize();
		lpid = lpid:finalize();
		handle = cs(actor.handle);
		nym = cs(actor.nym);
		desc = cs(actor.bio);
		avi = cs(actor.avatar);
		rsa = cs(&pem[0]);
		locked = 'false';
	}

	co:json(body:poolstr(&co.srv.pool))

end
return api_lp_actor
