parsav  outbox.t at [a4e71fdfda]

File api/lp/outbox.t artifact 748fc8c886 part of check-in a4e71fdfda


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

local terra 
lp_outbox(co: &lib.srv.convo, uri: pstr, here: lib.mem.ptr(lib.str.ref))
	var path = lib.str.qesc(&co.srv.pool,uri,false)
	var json = co:stra(512)
	json:lpush '{"@context": "https://':ppush(co.srv.cfg.domain):lpush'/s/litepub.jsonld","id":"https://'
	    :ppush(co.srv.cfg.domain):ppush(path)
		:lpush '"'
	var at = co:pgetv('at')
	lib.dbg('api path ',
	{here(0).ptr,here(0).ct}, ' / ',
	{here(1).ptr,here(1).ct})
	json:lpush',"current":"https://':ppush(co.srv.cfg.domain):ppush(path):lpush'?at=top"'
	if not at then
		json:lpush',"type":"OrderedCollection","first":"https://':ppush(co.srv.cfg.domain):ppush(path):lpush'?at=top"'
	else
		if here(0):cmp 'user' and here.ct > 1 then
			var uid, uidok = lib.math.shorthand.parse(here(1).ptr, here(1).ct)
			if not uidok then goto e404 end
			var user = co.srv:actor_fetch_uid(uid)
			if not user then goto e404 end
			var time: lib.store.timepoint
			if at:cmp('top') then
				time = lib.osclock.time(nil)
			else
				var tp, ok = lib.math.decparse(at)
				if ok then time = tp end
			end
			var posts = co.srv:post_enum_author_uid(uid, lib.store.range {
				mode = 1; -- time -> idx
				from_time = time;
				to_idx = 65;
			})
			var oldest = time
			json:lpush',"partOf":"https://':ppush(co.srv.cfg.domain):ppush(path)
			    :lpush'","type":"CollectionPage","orderedItems":['
			if posts.sz > 0 then defer posts:free()
				for i=0, lib.math.smallest(posts.sz,64) do
					if i~=0 then json:lpush',' end
					json:ppush(lib.api.lp.tweet(co,posts(i).ptr,true))
					oldest = lib.math.smallest(posts(i)().posted, oldest)
					posts(i):free()
				end
			end
			json:lpush'],"totalItems":':ipush(posts.sz)
			if oldest ~= time and oldest > 0 and posts.sz > 64 then
				json:lpush',"next":"https://':ppush(co.srv.cfg.domain):ppush(path)
				    :lpush'?at=':ipush(oldest-1):lpush'"'
			end

		else goto e404 end -- TODO
	end
	json:lpush[[}]]
	co:json(json:finalize())
	do return end
	::e404:: do co:fail(404) return end
end

return lp_outbox