// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2019 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.

#include "rooms.h"

using namespace ircd;

m::resource::response
get__event(client &client,
           const m::resource::request &request,
           const m::room::id &room_id)
{
	if(request.parv.size() < 3)
		throw m::NEED_MORE_PARAMS
		{
			"event_id path parameter required"
		};

	m::event::id::buf event_id
	{
		url::decode(event_id, request.parv[2])
	};

	const m::room room
	{
		room_id, event_id
	};

	if(!visible(room, request.user_id))
		throw m::ACCESS_DENIED
		{
			"You are not permitted to view the room at this event"
		};

	m::event::fetch::opts fopts;
	fopts.query_json_force = true;
	const m::event::fetch event
	{
		event_id, fopts
	};

	const unique_mutable_buffer buf
	{
		m::event::MAX_SIZE
	};

	json::stack out{buf};
	{
		json::stack::object top{out};
		m::event::append
		{
			top, event,
			{
				.event_idx = &event.event_idx,
				.user_id = &request.user_id,
				.query_prev_state = false,
				.query_redacted = false,
				.query_visible = false,
			}
		};
	};

	return m::resource::response
	{
		client, json::object
		{
			out.completed()
		}
	};
}
