0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-26 15:33:54 +01:00

modules/client/presence: Various cleanup/reorg.

This commit is contained in:
Jason Volk 2018-04-23 13:46:37 -07:00
parent 5b8b35a7c6
commit f9e449dbf5

View file

@ -26,21 +26,220 @@ presence_resource
}
};
//
// get
//
static resource::response
get__presence(client &,
const resource::request &);
resource::method
method_get
{
presence_resource, "GET", get__presence
};
static resource::response
get__presence_status(client &,
const resource::request &,
const m::user::id &);
static resource::response
get__presence_list(client &,
const resource::request &);
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::response
get__presence_status(client &client,
const resource::request &request,
const m::user::id &user_id)
{
const m::user user
{
user_id
};
m::presence::get(user, [&client]
(const json::object &object)
{
resource::response
{
client, object
};
});
return {}; // responded from closure or threw
}
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()
}
};
}
extern "C" bool
m_presence_get(const std::nothrow_t,
const m::user &user,
const m::presence::closure &closure)
{
const m::user::room user_room
{
user
};
return user_room.get(std::nothrow, "m.presence", "", [&closure]
(const m::event &event)
{
const auto &content
{
at<"content"_>(event)
};
closure(content);
});
}
//
// POST ?
//
static resource::response
post__presence(client &,
const resource::request &);
resource::method
method_post
{
presence_resource, "POST", post__presence,
{
method_post.REQUIRES_AUTH
}
};
static resource::response
post__presence_list(client &,
const resource::request &);
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::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
};
}
//
// put
//
extern "C" m::event::id::buf
commit__m_presence(const m::presence &content);
static resource::response
put__presence_status(client &,
const resource::request &,
const m::user::id &);
static resource::response
put__presence_status(client &client,
const resource::request &request,
const m::user::id &user_id);
static resource::response
put__presence(client &client,
const resource::request &request);
put__presence(client &,
const resource::request &);
resource::method
method_put
@ -143,230 +342,3 @@ commit__m_presence(const m::presence &content)
//TODO: ABA
return send(user_room, user.user_id, "m.presence", "", json::strung{content});
}
//
// get
//
extern "C" bool
m_presence_get(const std::nothrow_t,
const m::user &,
const m::presence::closure &);
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
};
bool
m_presence_get(const std::nothrow_t,
const m::user &user,
const m::presence::closure &closure)
{
const m::user::room user_room
{
user
};
return user_room.get(std::nothrow, "m.presence", "", [&closure]
(const m::event &event)
{
const auto &content
{
at<"content"_>(event)
};
closure(content);
});
}
//
// POST ?
//
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
}
};