2018-02-14 21:23:20 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 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;
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
get__members(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const m::room::id &room_id)
|
|
|
|
{
|
2019-07-07 10:30:20 +02:00
|
|
|
char membershipbuf[32];
|
|
|
|
const auto &membership
|
|
|
|
{
|
|
|
|
url::decode(membershipbuf, request.query["membership"])
|
|
|
|
};
|
|
|
|
|
|
|
|
char not_membershipbuf[32];
|
|
|
|
const auto ¬_membership
|
|
|
|
{
|
|
|
|
url::decode(not_membershipbuf, request.query["not_membership"])
|
|
|
|
};
|
|
|
|
|
|
|
|
char atbuf[48];
|
|
|
|
const string_view at
|
|
|
|
{
|
|
|
|
url::decode(atbuf, request.query["at"])
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto event_idx
|
|
|
|
{
|
|
|
|
// at is a /sync since token we gave the client. This is simply
|
|
|
|
// an event_idx sequence integer, except during phased-polylog sync
|
|
|
|
// when this is a negative integer. If this is phased sync, we can
|
|
|
|
// parse this token for the snapshot integer.
|
|
|
|
at && !startswith(at, '-')?
|
|
|
|
lex_cast<m::event::idx>(at):
|
|
|
|
startswith(at, '-')?
|
|
|
|
lex_cast<m::event::idx>(split(at, '_').second):
|
|
|
|
m::event::idx{0}
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::event::id::buf event_id
|
|
|
|
{
|
|
|
|
event_idx && event_idx <= m::vm::sequence::retired?
|
|
|
|
m::event_id(event_idx, std::nothrow):
|
|
|
|
m::event::id::buf{}
|
|
|
|
};
|
|
|
|
|
2019-03-14 21:55:44 +01:00
|
|
|
const m::room room
|
2018-02-14 21:23:20 +01:00
|
|
|
{
|
2019-07-07 10:30:20 +02:00
|
|
|
room_id, event_id
|
2018-02-14 21:23:20 +01:00
|
|
|
};
|
|
|
|
|
2019-07-07 10:30:20 +02:00
|
|
|
if(!event_id && !exists(room))
|
2019-03-14 21:55:44 +01:00
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"Room %s does not exist.",
|
|
|
|
string_view{room_id}
|
|
|
|
};
|
|
|
|
|
2019-08-14 10:01:46 +02:00
|
|
|
if(!visible(room, request.user_id))
|
2019-03-14 21:55:44 +01:00
|
|
|
throw m::ACCESS_DENIED
|
|
|
|
{
|
|
|
|
"You do not have permission to view %s members.",
|
|
|
|
string_view{room_id}
|
|
|
|
};
|
2018-02-14 21:23:20 +01:00
|
|
|
|
2019-03-14 21:55:44 +01:00
|
|
|
resource::response::chunked response
|
2018-02-14 21:23:20 +01:00
|
|
|
{
|
2019-03-14 21:55:44 +01:00
|
|
|
client, http::OK
|
|
|
|
};
|
2018-02-14 21:23:20 +01:00
|
|
|
|
2019-03-14 21:55:44 +01:00
|
|
|
json::stack out
|
2018-02-14 21:23:20 +01:00
|
|
|
{
|
2019-03-14 21:55:44 +01:00
|
|
|
response.buf, response.flusher()
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object top
|
|
|
|
{
|
|
|
|
out
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::array chunk
|
|
|
|
{
|
|
|
|
top, "chunk"
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::members members
|
|
|
|
{
|
|
|
|
room
|
2018-02-14 21:23:20 +01:00
|
|
|
};
|
2019-03-14 21:55:44 +01:00
|
|
|
|
2019-08-25 05:37:37 +02:00
|
|
|
members.for_each(membership, [¬_membership]
|
|
|
|
(const m::user::id &member, const m::event::idx &event_idx)
|
|
|
|
{
|
|
|
|
if(m::room::members::membership(event_idx, not_membership))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
m::prefetch(event_idx);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2019-07-24 04:31:40 +02:00
|
|
|
members.for_each(membership, [&request, &chunk, ¬_membership]
|
|
|
|
(const m::user::id &member, const m::event::idx &event_idx)
|
2019-03-14 21:55:44 +01:00
|
|
|
{
|
2019-07-24 04:31:40 +02:00
|
|
|
if(m::room::members::membership(event_idx, not_membership))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const m::event::fetch event
|
|
|
|
{
|
|
|
|
event_idx, std::nothrow
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!event.valid)
|
2019-07-07 10:30:20 +02:00
|
|
|
return true;
|
|
|
|
|
2019-03-14 21:55:44 +01:00
|
|
|
chunk.append(event);
|
2019-07-07 10:30:20 +02:00
|
|
|
return true;
|
2019-07-24 04:31:40 +02:00
|
|
|
});
|
2019-03-14 21:55:44 +01:00
|
|
|
|
2019-06-24 07:09:41 +02:00
|
|
|
return std::move(response);
|
2018-02-14 21:23:20 +01:00
|
|
|
}
|
2018-02-16 21:36:30 +01:00
|
|
|
|
|
|
|
resource::response
|
|
|
|
get__joined_members(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const m::room::id &room_id)
|
|
|
|
{
|
2019-03-14 21:55:44 +01:00
|
|
|
const m::room room
|
2018-02-16 21:36:30 +01:00
|
|
|
{
|
|
|
|
room_id
|
|
|
|
};
|
|
|
|
|
2019-03-14 21:55:44 +01:00
|
|
|
if(!exists(room))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"Room %s does not exist.",
|
|
|
|
string_view{room_id}
|
|
|
|
};
|
2018-02-16 21:36:30 +01:00
|
|
|
|
2019-08-14 10:01:46 +02:00
|
|
|
if(!visible(room, request.user_id))
|
2019-03-14 21:55:44 +01:00
|
|
|
throw m::ACCESS_DENIED
|
2018-02-16 21:36:30 +01:00
|
|
|
{
|
2019-03-14 21:55:44 +01:00
|
|
|
"You do not have permission to view %s joined members.",
|
|
|
|
string_view{room_id}
|
|
|
|
};
|
2018-02-16 21:36:30 +01:00
|
|
|
|
2019-03-14 21:55:44 +01:00
|
|
|
resource::response::chunked response
|
2018-02-16 21:36:30 +01:00
|
|
|
{
|
2019-03-14 21:55:44 +01:00
|
|
|
client, http::OK
|
2018-02-16 21:36:30 +01:00
|
|
|
};
|
|
|
|
|
2019-03-14 21:55:44 +01:00
|
|
|
json::stack out
|
2018-02-16 21:36:30 +01:00
|
|
|
{
|
2019-03-14 21:55:44 +01:00
|
|
|
response.buf, response.flusher()
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object top
|
|
|
|
{
|
|
|
|
out
|
2018-02-16 21:36:30 +01:00
|
|
|
};
|
2019-03-14 21:55:44 +01:00
|
|
|
|
|
|
|
json::stack::object joined
|
|
|
|
{
|
|
|
|
top, "joined"
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::members members
|
|
|
|
{
|
|
|
|
room
|
|
|
|
};
|
|
|
|
|
2019-08-25 05:37:37 +02:00
|
|
|
members.for_each("join", []
|
|
|
|
(const m::user::id &user_id, const m::event::idx &event_idx)
|
|
|
|
{
|
|
|
|
m::prefetch(event_idx);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2019-07-24 04:31:40 +02:00
|
|
|
members.for_each("join", [&joined, &room]
|
|
|
|
(const m::user::id &user_id, const m::event::idx &event_idx)
|
2019-03-14 21:55:44 +01:00
|
|
|
{
|
|
|
|
json::stack::object room_member
|
|
|
|
{
|
|
|
|
joined, user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
m::get(std::nothrow, event_idx, "content", [&room_member]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
for(const auto &[key, val] : content)
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
room_member, key, val
|
|
|
|
};
|
|
|
|
});
|
2019-07-24 04:31:40 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
2019-03-14 21:55:44 +01:00
|
|
|
|
2019-06-24 07:09:41 +02:00
|
|
|
return std::move(response);
|
2018-02-16 21:36:30 +01:00
|
|
|
}
|