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
|
|
|
};
|
|
|
|
|
2018-02-15 22:06:49 +01:00
|
|
|
ircd::resource
|
|
|
|
presence_resource
|
2018-02-12 23:45:48 +01:00
|
|
|
{
|
|
|
|
"/_matrix/client/r0/presence/", resource::opts
|
|
|
|
{
|
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
|
|
|
//
|
|
|
|
// put
|
|
|
|
//
|
|
|
|
|
2018-02-16 04:59:59 +01:00
|
|
|
static resource::response
|
|
|
|
put__presence_status(client &client,
|
|
|
|
const resource::request &request,
|
2018-03-02 16:25:37 +01:00
|
|
|
const m::user::id &user_id);
|
2018-02-16 04:59:59 +01:00
|
|
|
|
2018-03-02 16:25:37 +01:00
|
|
|
static resource::response
|
|
|
|
put__presence(client &client,
|
|
|
|
const resource::request &request);
|
2018-02-16 04:59:59 +01:00
|
|
|
|
2018-03-02 16:25:37 +01:00
|
|
|
resource::method
|
|
|
|
method_put
|
|
|
|
{
|
|
|
|
presence_resource, "PUT", put__presence,
|
2018-02-16 04:59:59 +01:00
|
|
|
{
|
2018-03-02 16:25:37 +01:00
|
|
|
method_put.REQUIRES_AUTH
|
|
|
|
}
|
|
|
|
};
|
2018-02-16 04:59:59 +01:00
|
|
|
|
2018-02-12 23:45:48 +01:00
|
|
|
resource::response
|
2018-02-15 22:06:49 +01:00
|
|
|
put__presence(client &client,
|
|
|
|
const 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
|
|
|
|
{
|
|
|
|
"user_id required"
|
|
|
|
};
|
|
|
|
|
|
|
|
m::user::id::buf user_id
|
2018-02-12 23:45:48 +01:00
|
|
|
{
|
2018-02-16 04:59:59 +01:00
|
|
|
url::decode(request.parv[0], user_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
if(request.parv.size() < 2)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
|
|
|
"command required"
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &cmd
|
|
|
|
{
|
|
|
|
request.parv[1]
|
|
|
|
};
|
|
|
|
|
|
|
|
if(cmd == "status")
|
|
|
|
return put__presence_status(client, request, user_id);
|
|
|
|
|
|
|
|
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
|
|
|
|
put__presence_status(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const m::user::id &user_id)
|
2018-02-12 23:45:48 +01:00
|
|
|
{
|
2018-03-02 16:25:37 +01:00
|
|
|
const string_view &presence
|
2018-02-12 23:45:48 +01:00
|
|
|
{
|
2018-03-02 16:25:37 +01:00
|
|
|
unquote(request.at("presence"))
|
|
|
|
};
|
|
|
|
|
2018-03-04 16:15:50 +01:00
|
|
|
if(!m::presence::valid_state(presence))
|
2018-03-02 16:25:37 +01:00
|
|
|
throw m::UNSUPPORTED
|
|
|
|
{
|
|
|
|
"That presence state is not supported"
|
|
|
|
};
|
|
|
|
|
2018-03-04 16:15:50 +01:00
|
|
|
const string_view &status_msg
|
2018-03-02 16:25:37 +01:00
|
|
|
{
|
2018-03-04 16:15:50 +01:00
|
|
|
trunc(unquote(request["status_msg"]), 390)
|
2018-03-02 16:25:37 +01:00
|
|
|
};
|
|
|
|
|
2018-03-04 16:15:50 +01:00
|
|
|
const m::user user{request.user_id};
|
|
|
|
m::presence::set(user, presence, status_msg);
|
|
|
|
return resource::response
|
2018-03-02 16:25:37 +01:00
|
|
|
{
|
2018-03-04 16:15:50 +01:00
|
|
|
client, http::OK
|
2018-03-02 16:25:37 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// get
|
|
|
|
//
|
2018-02-16 04:59:59 +01:00
|
|
|
|
|
|
|
static resource::response
|
|
|
|
get__presence_status(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const m::user::id &user_id)
|
|
|
|
{
|
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
user_room.get("m.presence", [&client]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
const auto &content
|
|
|
|
{
|
|
|
|
at<"content"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
json::iov response;
|
|
|
|
const json::iov::push _presence
|
|
|
|
{
|
|
|
|
response, { "presence", content.at("presence") }
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::iov::set_if _status_msg
|
|
|
|
{
|
|
|
|
response, content.has("status_msg"),
|
|
|
|
{
|
|
|
|
"status_msg", content.at("status_msg")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//TODO: better last_active_ago
|
|
|
|
const auto last_active_ago
|
|
|
|
{
|
|
|
|
ircd::now<milliseconds>() - milliseconds(at<"origin_server_ts"_>(event))
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::iov::push _last_active_ago
|
|
|
|
{
|
|
|
|
response, { "last_active_ago", last_active_ago.count() }
|
|
|
|
};
|
|
|
|
|
|
|
|
//TODO: better currently_active
|
|
|
|
const bool currently_active
|
|
|
|
{
|
|
|
|
content.at("presence") == "online"
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::iov::push _currently_active
|
|
|
|
{
|
|
|
|
response, { "currently_active", currently_active }
|
|
|
|
};
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
{
|
|
|
|
client, response
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return {}; // responded from closure
|
|
|
|
}
|
|
|
|
|
|
|
|
static resource::response
|
|
|
|
get__presence_list(client &client,
|
|
|
|
const resource::request &request)
|
|
|
|
{
|
|
|
|
if(request.parv.size() < 2)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
|
|
|
"user_id required"
|
|
|
|
};
|
|
|
|
|
|
|
|
m::user::id::buf user_id
|
|
|
|
{
|
|
|
|
url::decode(request.parv[1], user_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
//TODO: reuse composition from /status
|
|
|
|
std::vector<json::value> list;
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, json::value
|
|
|
|
{
|
|
|
|
list.data(), list.size()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
get__presence(client &client,
|
|
|
|
const resource::request &request)
|
|
|
|
{
|
|
|
|
if(request.parv.size() < 1)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
|
|
|
"user_id or command required"
|
|
|
|
};
|
|
|
|
|
|
|
|
if(request.parv[0] == "list")
|
|
|
|
return get__presence_list(client, request);
|
|
|
|
|
|
|
|
m::user::id::buf user_id
|
|
|
|
{
|
|
|
|
url::decode(request.parv[0], user_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(request.parv.size() < 2)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
|
|
|
"command required"
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &cmd
|
|
|
|
{
|
|
|
|
request.parv[1]
|
|
|
|
};
|
|
|
|
|
|
|
|
if(cmd == "status")
|
|
|
|
return get__presence_status(client, request, user_id);
|
|
|
|
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"Presence command not found"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
resource::method
|
|
|
|
method_get
|
|
|
|
{
|
|
|
|
presence_resource, "GET", get__presence
|
|
|
|
};
|
|
|
|
|
|
|
|
static resource::response
|
|
|
|
post__presence_list(client &client,
|
|
|
|
const resource::request &request)
|
|
|
|
{
|
|
|
|
if(request.parv.size() < 2)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
|
|
|
"user_id required"
|
|
|
|
};
|
|
|
|
|
|
|
|
m::user::id::buf user_id
|
|
|
|
{
|
|
|
|
url::decode(request.parv[1], user_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, http::OK
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
post__presence(client &client,
|
|
|
|
const resource::request &request)
|
|
|
|
{
|
|
|
|
if(request.parv.size() < 1)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
|
|
|
"command required"
|
|
|
|
};
|
|
|
|
|
|
|
|
if(request.parv[0] == "list")
|
|
|
|
return get__presence_list(client, request);
|
|
|
|
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"Presence command not found"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
resource::method
|
|
|
|
method_post
|
|
|
|
{
|
|
|
|
presence_resource, "POST", post__presence,
|
|
|
|
{
|
|
|
|
method_post.REQUIRES_AUTH
|
|
|
|
}
|
|
|
|
};
|