2018-02-12 23:45:48 +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.
|
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
2018-02-15 22:06:49 +01:00
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
2018-02-12 23:45:48 +01:00
|
|
|
{
|
2018-02-15 22:06:49 +01:00
|
|
|
"Client 11.6 :Presence"
|
2018-02-12 23:45:48 +01:00
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource
|
2018-02-15 22:06:49 +01:00
|
|
|
presence_resource
|
2018-02-12 23:45:48 +01:00
|
|
|
{
|
2018-11-06 04:14:53 +01:00
|
|
|
"/_matrix/client/r0/presence/",
|
2018-02-12 23:45:48 +01:00
|
|
|
{
|
2018-02-15 22:06:49 +01:00
|
|
|
"(11.6.2) Presence",
|
2018-02-12 23:45:48 +01:00
|
|
|
resource::DIRECTORY,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-02 16:25:37 +01:00
|
|
|
//
|
2018-04-23 22:46:37 +02:00
|
|
|
// get
|
2018-03-02 16:25:37 +01:00
|
|
|
//
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::response
|
2018-04-23 22:46:37 +02:00
|
|
|
get__presence(client &,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &);
|
2018-02-16 04:59:59 +01:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::method
|
2018-04-23 22:46:37 +02:00
|
|
|
method_get
|
2018-03-02 16:25:37 +01:00
|
|
|
{
|
2018-04-23 22:46:37 +02:00
|
|
|
presence_resource, "GET", get__presence
|
2018-03-02 16:25:37 +01:00
|
|
|
};
|
2018-02-16 04:59:59 +01:00
|
|
|
|
2018-04-23 22:46:37 +02:00
|
|
|
static resource::response
|
|
|
|
get__presence_status(client &,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &,
|
2018-04-23 22:46:37 +02:00
|
|
|
const m::user::id &);
|
|
|
|
|
2018-02-12 23:45:48 +01:00
|
|
|
resource::response
|
2018-04-23 22:46:37 +02:00
|
|
|
get__presence(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request)
|
2018-02-12 23:45:48 +01:00
|
|
|
{
|
2018-02-16 04:59:59 +01:00
|
|
|
if(request.parv.size() < 1)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
2018-04-23 22:46:37 +02:00
|
|
|
"user_id or command required"
|
2018-02-16 04:59:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
m::user::id::buf user_id
|
2018-02-12 23:45:48 +01:00
|
|
|
{
|
2018-12-07 01:41:47 +01:00
|
|
|
url::decode(user_id, request.parv[0])
|
2018-02-16 04:59:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if(request.parv.size() < 2)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
|
|
|
"command required"
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &cmd
|
|
|
|
{
|
|
|
|
request.parv[1]
|
|
|
|
};
|
|
|
|
|
|
|
|
if(cmd == "status")
|
2018-04-23 22:46:37 +02:00
|
|
|
return get__presence_status(client, request, user_id);
|
2018-02-16 04:59:59 +01:00
|
|
|
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"Presence command not found"
|
2018-02-12 23:45:48 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-02 16:25:37 +01:00
|
|
|
resource::response
|
2018-04-23 22:46:37 +02:00
|
|
|
get__presence_status(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2018-03-02 16:25:37 +01:00
|
|
|
const m::user::id &user_id)
|
2018-02-12 23:45:48 +01:00
|
|
|
{
|
2018-04-23 22:46:37 +02:00
|
|
|
const m::user user
|
2018-02-12 23:45:48 +01:00
|
|
|
{
|
2018-04-23 22:46:37 +02:00
|
|
|
user_id
|
2018-03-02 16:25:37 +01:00
|
|
|
};
|
|
|
|
|
2018-04-23 22:46:37 +02:00
|
|
|
m::presence::get(user, [&client]
|
|
|
|
(const json::object &object)
|
|
|
|
{
|
|
|
|
resource::response
|
2018-03-02 16:25:37 +01:00
|
|
|
{
|
2018-04-23 22:46:37 +02:00
|
|
|
client, object
|
2018-03-02 16:25:37 +01:00
|
|
|
};
|
2018-04-23 22:46:37 +02:00
|
|
|
});
|
2018-03-02 16:25:37 +01:00
|
|
|
|
2018-04-23 22:46:37 +02:00
|
|
|
return {}; // responded from closure or threw
|
2018-03-02 16:25:37 +01:00
|
|
|
}
|
|
|
|
|
2018-04-23 22:46:37 +02:00
|
|
|
//
|
|
|
|
// POST ?
|
|
|
|
//
|
2018-02-16 04:59:59 +01:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::response
|
2018-04-23 22:46:37 +02:00
|
|
|
post__presence(client &,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &);
|
2018-02-16 04:59:59 +01:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::method
|
2018-04-23 22:46:37 +02:00
|
|
|
method_post
|
|
|
|
{
|
|
|
|
presence_resource, "POST", post__presence,
|
|
|
|
{
|
|
|
|
method_post.REQUIRES_AUTH
|
|
|
|
}
|
|
|
|
};
|
2018-02-16 04:59:59 +01:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2018-04-23 22:46:37 +02:00
|
|
|
post__presence(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request)
|
2018-04-23 22:46:37 +02:00
|
|
|
{
|
|
|
|
if(request.parv.size() < 1)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
2018-02-16 04:59:59 +01:00
|
|
|
{
|
2018-04-23 22:46:37 +02:00
|
|
|
"command required"
|
2018-02-16 04:59:59 +01:00
|
|
|
};
|
|
|
|
|
2018-04-23 22:46:37 +02:00
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"Presence command not found"
|
|
|
|
};
|
2018-02-16 04:59:59 +01:00
|
|
|
}
|
|
|
|
|
2018-04-23 22:46:37 +02:00
|
|
|
//
|
|
|
|
// put
|
|
|
|
//
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::response
|
2018-04-23 22:46:37 +02:00
|
|
|
put__presence_status(client &,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &,
|
2018-04-23 22:46:37 +02:00
|
|
|
const m::user::id &);
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::response
|
2018-04-23 22:46:37 +02:00
|
|
|
put__presence(client &,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &);
|
2018-04-23 22:46:37 +02:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::method
|
2018-04-23 22:46:37 +02:00
|
|
|
method_put
|
|
|
|
{
|
|
|
|
presence_resource, "PUT", put__presence,
|
|
|
|
{
|
2021-02-19 15:44:27 +01:00
|
|
|
method_put.REQUIRES_AUTH |
|
|
|
|
method_put.RATE_LIMITED
|
2018-04-23 22:46:37 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2018-04-23 22:46:37 +02:00
|
|
|
put__presence(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request)
|
2018-02-16 04:59:59 +01:00
|
|
|
{
|
|
|
|
if(request.parv.size() < 1)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
2018-04-23 22:46:37 +02:00
|
|
|
"user_id required"
|
2018-02-16 04:59:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
m::user::id::buf user_id
|
|
|
|
{
|
2018-12-07 01:41:47 +01:00
|
|
|
url::decode(user_id, request.parv[0])
|
2018-02-16 04:59:59 +01:00
|
|
|
};
|
|
|
|
|
2018-04-23 22:46:37 +02:00
|
|
|
if(user_id != request.user_id)
|
|
|
|
throw m::FORBIDDEN
|
|
|
|
{
|
|
|
|
"You cannot set the presence of '%s' when you are '%s'",
|
|
|
|
user_id,
|
|
|
|
request.user_id
|
|
|
|
};
|
|
|
|
|
2018-02-16 04:59:59 +01:00
|
|
|
if(request.parv.size() < 2)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
|
|
|
"command required"
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &cmd
|
|
|
|
{
|
|
|
|
request.parv[1]
|
|
|
|
};
|
|
|
|
|
|
|
|
if(cmd == "status")
|
2018-04-23 22:46:37 +02:00
|
|
|
return put__presence_status(client, request, user_id);
|
2018-02-16 04:59:59 +01:00
|
|
|
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"Presence command not found"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2018-04-23 22:46:37 +02:00
|
|
|
put__presence_status(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2018-04-23 22:46:37 +02:00
|
|
|
const m::user::id &user_id)
|
2018-03-29 04:49:18 +02:00
|
|
|
{
|
2018-04-23 22:46:37 +02:00
|
|
|
const string_view &presence
|
2018-03-29 04:49:18 +02:00
|
|
|
{
|
2018-04-23 22:46:37 +02:00
|
|
|
unquote(request.at("presence"))
|
2018-03-29 04:49:18 +02:00
|
|
|
};
|
|
|
|
|
2018-04-23 22:46:37 +02:00
|
|
|
if(!m::presence::valid_state(presence))
|
|
|
|
throw m::UNSUPPORTED
|
2018-02-16 04:59:59 +01:00
|
|
|
{
|
2018-04-23 22:46:37 +02:00
|
|
|
"That presence state is not supported"
|
2018-02-16 04:59:59 +01:00
|
|
|
};
|
|
|
|
|
2018-04-23 22:46:37 +02:00
|
|
|
const string_view &status_msg
|
2018-02-16 04:59:59 +01:00
|
|
|
{
|
2018-04-23 22:46:37 +02:00
|
|
|
trunc(unquote(request["status_msg"]), 390)
|
2018-02-16 04:59:59 +01:00
|
|
|
};
|
|
|
|
|
2018-04-24 02:50:47 +02:00
|
|
|
const m::user user
|
|
|
|
{
|
|
|
|
request.user_id
|
|
|
|
};
|
|
|
|
|
2018-04-24 03:22:26 +02:00
|
|
|
bool modified{true};
|
|
|
|
m::presence::get(std::nothrow, user, [&modified, &presence, &status_msg]
|
2018-04-24 02:50:47 +02:00
|
|
|
(const json::object &object)
|
|
|
|
{
|
|
|
|
if(unquote(object.get("presence")) != presence)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(unquote(object.get("status_msg")) != status_msg)
|
|
|
|
return;
|
|
|
|
|
2018-04-24 03:22:26 +02:00
|
|
|
modified = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
if(!modified)
|
2019-09-29 01:12:07 +02:00
|
|
|
return m::resource::response
|
2018-04-24 02:50:47 +02:00
|
|
|
{
|
2018-04-24 03:22:26 +02:00
|
|
|
client, http::OK
|
2018-04-24 02:50:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto eid
|
|
|
|
{
|
|
|
|
m::presence::set(user, presence, status_msg)
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
return m::resource::response
|
2018-02-16 04:59:59 +01:00
|
|
|
{
|
|
|
|
client, http::OK
|
|
|
|
};
|
|
|
|
}
|