2018-02-04 03:22:01 +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.
|
2017-12-12 21:33:14 +01:00
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
2018-02-15 22:06:49 +01:00
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
2018-02-15 22:06:49 +01:00
|
|
|
"Client 7.2 :Room aliases"
|
|
|
|
};
|
|
|
|
|
|
|
|
resource
|
|
|
|
directory_room_resource
|
|
|
|
{
|
|
|
|
"/_matrix/client/r0/directory/room/",
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
|
|
|
"(7.2) Room aliases",
|
|
|
|
directory_room_resource.DIRECTORY
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-31 03:10:36 +02:00
|
|
|
static resource::response
|
|
|
|
get__directory_room(client &client,
|
|
|
|
const resource::request &request);
|
2018-02-26 13:55:27 +01:00
|
|
|
|
2019-03-31 03:10:36 +02:00
|
|
|
resource::method
|
|
|
|
directory_room_get
|
|
|
|
{
|
|
|
|
directory_room_resource, "GET", get__directory_room
|
|
|
|
};
|
2018-03-08 20:39:09 +01:00
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
resource::response
|
2018-02-22 01:32:53 +01:00
|
|
|
get__directory_room(client &client,
|
|
|
|
const resource::request &request)
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
|
|
|
m::room::alias::buf room_alias
|
|
|
|
{
|
2018-12-07 01:41:47 +01:00
|
|
|
url::decode(room_alias, request.parv[0])
|
2017-12-12 21:33:14 +01:00
|
|
|
};
|
|
|
|
|
2019-03-31 03:10:36 +02:00
|
|
|
const m::room::id::buf room_id
|
2018-02-25 09:09:42 +01:00
|
|
|
{
|
2019-03-31 03:10:36 +02:00
|
|
|
m::room_id(room_alias)
|
2018-02-25 09:09:42 +01:00
|
|
|
};
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
return resource::response
|
|
|
|
{
|
2018-02-26 13:55:27 +01:00
|
|
|
client, json::members
|
|
|
|
{
|
|
|
|
{ "room_id", room_id }
|
|
|
|
}
|
2017-12-12 21:33:14 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-31 03:10:36 +02:00
|
|
|
static resource::response
|
|
|
|
put__directory_room(client &client,
|
|
|
|
const resource::request &request);
|
|
|
|
|
2018-02-15 22:06:49 +01:00
|
|
|
resource::method
|
2019-03-31 03:10:36 +02:00
|
|
|
directory_room_put
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
2019-08-14 07:09:25 +02:00
|
|
|
directory_room_resource, "PUT", put__directory_room,
|
|
|
|
{
|
|
|
|
directory_room_put.REQUIRES_AUTH
|
|
|
|
}
|
2018-02-22 01:32:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
put__directory_room(client &client,
|
|
|
|
const resource::request &request)
|
|
|
|
{
|
2019-03-31 03:29:58 +02:00
|
|
|
if(request.parv.size() < 1)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
|
|
|
"Room alias path parameter missing"
|
|
|
|
};
|
|
|
|
|
2018-02-22 01:32:53 +01:00
|
|
|
m::room::alias::buf room_alias
|
|
|
|
{
|
2018-12-07 01:41:47 +01:00
|
|
|
url::decode(room_alias, request.parv[0])
|
2018-02-22 01:32:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::id &room_id
|
|
|
|
{
|
|
|
|
unquote(request.at("room_id"))
|
|
|
|
};
|
|
|
|
|
2019-03-31 03:29:58 +02:00
|
|
|
if(!exists(room_id))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"Room %s is not found here.",
|
|
|
|
string_view{room_id}
|
|
|
|
};
|
|
|
|
|
2019-08-14 06:59:09 +02:00
|
|
|
const unique_mutable_buffer buf
|
2019-03-31 03:29:58 +02:00
|
|
|
{
|
2019-08-14 06:59:09 +02:00
|
|
|
48_KiB
|
2019-03-31 03:29:58 +02:00
|
|
|
};
|
|
|
|
|
2019-08-14 06:59:09 +02:00
|
|
|
// Generate the new content of the m.room.aliases
|
2019-03-31 03:29:58 +02:00
|
|
|
json::stack out{buf};
|
|
|
|
{
|
2019-08-14 06:59:09 +02:00
|
|
|
json::stack::object content
|
|
|
|
{
|
|
|
|
out
|
|
|
|
};
|
2019-03-31 03:29:58 +02:00
|
|
|
|
2019-08-14 06:59:09 +02:00
|
|
|
json::stack::array array
|
|
|
|
{
|
|
|
|
content, "aliases"
|
|
|
|
};
|
2019-03-31 03:29:58 +02:00
|
|
|
|
2019-08-14 06:59:09 +02:00
|
|
|
// Iterate existing aliases for host
|
|
|
|
const m::room::aliases aliases{room_id};
|
|
|
|
aliases.for_each(room_alias.host(), [&room_alias, &array]
|
|
|
|
(const m::room::alias &existing_alias)
|
|
|
|
{
|
|
|
|
// Check for duplicate
|
|
|
|
if(iequals(existing_alias, room_alias))
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::CONFLICT, "M_EXISTS",
|
|
|
|
"Room alias %s already exists",
|
|
|
|
string_view{room_alias},
|
|
|
|
};
|
|
|
|
|
|
|
|
// Add existing alias
|
|
|
|
array.append(existing_alias);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
//Add new alias
|
|
|
|
array.append(room_alias);
|
|
|
|
}
|
2019-03-31 03:29:58 +02:00
|
|
|
|
2019-08-14 06:59:09 +02:00
|
|
|
// Commit the new event
|
|
|
|
// TODO: ABA with another aliases event.
|
2019-03-31 03:29:58 +02:00
|
|
|
const auto eid
|
|
|
|
{
|
|
|
|
m::send(room_id, request.user_id, "m.room.aliases", room_alias.host(), json::object
|
|
|
|
{
|
|
|
|
out.completed()
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2018-02-22 01:32:53 +01:00
|
|
|
return resource::response
|
|
|
|
{
|
2019-03-31 03:29:58 +02:00
|
|
|
client, json::members
|
|
|
|
{
|
|
|
|
{ "event_id", eid }
|
|
|
|
}
|
2018-02-22 01:32:53 +01:00
|
|
|
};
|
|
|
|
}
|
2019-08-14 07:09:42 +02:00
|
|
|
|
|
|
|
static resource::response
|
|
|
|
delete__directory_room(client &client,
|
|
|
|
const resource::request &request);
|
|
|
|
|
|
|
|
resource::method
|
|
|
|
directory_room_delete
|
|
|
|
{
|
|
|
|
directory_room_resource, "DELETE", delete__directory_room,
|
|
|
|
{
|
|
|
|
directory_room_delete.REQUIRES_AUTH
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
delete__directory_room(client &client,
|
|
|
|
const resource::request &request)
|
|
|
|
{
|
|
|
|
if(request.parv.size() < 1)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
|
|
|
"Room alias path parameter missing"
|
|
|
|
};
|
|
|
|
|
|
|
|
m::room::alias::buf room_alias
|
|
|
|
{
|
|
|
|
url::decode(room_alias, request.parv[0])
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::id::buf room_id
|
|
|
|
{
|
|
|
|
m::room_id(room_alias)
|
|
|
|
};
|
|
|
|
|
|
|
|
const unique_mutable_buffer buf
|
|
|
|
{
|
|
|
|
48_KiB
|
|
|
|
};
|
|
|
|
|
|
|
|
// Generate the new content of the m.room.aliases
|
|
|
|
json::stack out{buf};
|
|
|
|
bool removed {false};
|
|
|
|
{
|
|
|
|
json::stack::object content
|
|
|
|
{
|
|
|
|
out
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::array array
|
|
|
|
{
|
|
|
|
content, "aliases"
|
|
|
|
};
|
|
|
|
|
|
|
|
// Iterate existing aliases for host
|
|
|
|
const m::room::aliases aliases{room_id};
|
|
|
|
aliases.for_each(room_alias.host(), [&room_alias, &array, &removed]
|
|
|
|
(const m::room::alias &existing_alias)
|
|
|
|
{
|
|
|
|
// Check for existing
|
|
|
|
if(!iequals(existing_alias, room_alias))
|
|
|
|
array.append(existing_alias);
|
|
|
|
else
|
|
|
|
removed = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!removed)
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, http::OK
|
|
|
|
};
|
|
|
|
|
|
|
|
// Commit the new event
|
|
|
|
// TODO: ABA with another aliases event.
|
|
|
|
const auto eid
|
|
|
|
{
|
|
|
|
m::send(room_id, request.user_id, "m.room.aliases", room_alias.host(), json::object
|
|
|
|
{
|
|
|
|
out.completed()
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, json::members
|
|
|
|
{
|
|
|
|
{ "event_id", eid }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|