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

modules/client/sync: Simplify stack arguments; addl cleanup.

This commit is contained in:
Jason Volk 2018-09-01 02:57:15 -07:00
parent fce5be0317
commit 96363a527d
2 changed files with 69 additions and 67 deletions

View file

@ -80,27 +80,16 @@ ircd::m::sync::method_get
ircd::resource::response
ircd::m::sync::handle_get(client &client,
const resource::request &request)
try
{
const args args
{
request
};
if(!short_poll(client, request, args))
longpoll::poll(client, request, args);
return {};
}
bool
ircd::m::sync::short_poll(client &client,
const resource::request &request,
const args &args)
try
{
shortpoll sp
{
client, request, args
client, args
};
if(sp.since > sp.current)
@ -119,12 +108,19 @@ try
384 //TODO: conf
};
return
const bool shortpolled
{
sp.delta == 0?
false:
sp.delta > max_linear_sync?
polylog::handle(client, request, sp, top):
linear::handle(client, request, sp, top);
polylog::handle(client, sp, top):
linear::handle(client, sp, top)
};
if(!shortpolled)
longpoll::poll(client, args);
return {};
}
catch(const bad_lex_cast &e)
{
@ -136,7 +132,6 @@ catch(const bad_lex_cast &e)
void
ircd::m::sync::longpoll::poll(client &client,
const resource::request &request,
const args &args)
try
{
@ -156,7 +151,7 @@ try
if(!accepted.opts->notify_clients)
continue;
if(handle(client, request, args, accepted))
if(handle(client, args, accepted))
return;
}
}
@ -182,7 +177,6 @@ catch(const ctx::timeout &e)
bool
ircd::m::sync::longpoll::handle(client &client,
const resource::request &request,
const args &args,
const m::event &event)
{
@ -194,7 +188,7 @@ ircd::m::sync::longpoll::handle(client &client,
if(room_id)
{
const m::room room{room_id};
return handle(client, request, args, event, room);
return handle(client, args, event, room);
}
return false;
@ -202,14 +196,13 @@ ircd::m::sync::longpoll::handle(client &client,
bool
ircd::m::sync::longpoll::handle(client &client,
const resource::request &request,
const args &args,
const m::event &event,
const m::room &room)
{
const m::user::id &user_id
{
request.user_id
args.request.user_id
};
if(!room.membership(user_id, "join"))
@ -222,12 +215,15 @@ ircd::m::sync::longpoll::handle(client &client,
const auto rooms
{
sync_rooms(client, request, request.user_id, room, args, event)
sync_rooms(client, user_id, room, args, event)
};
const m::user::room ur
{
m::user::id{request.user_id}
m::user::id
{
args.request.user_id
}
};
std::vector<json::value> presents;
@ -267,7 +263,6 @@ ircd::m::sync::longpoll::handle(client &client,
std::string
ircd::m::sync::longpoll::sync_rooms(client &client,
const resource::request &request,
const m::user::id &user_id,
const m::room &room,
const args &args,
@ -377,7 +372,6 @@ ircd::m::sync::longpoll::sync_room(client &client,
bool
ircd::m::sync::linear::handle(client &client,
const resource::request &request,
shortpoll &sp,
json::stack::object &object)
{
@ -407,7 +401,7 @@ ircd::m::sync::linear::handle(client &client,
json::get<"room_id"_>(event)
};
if(!room.membership(request.user_id))
if(!room.membership(sp.args.request.user_id))
return true;
auto it
@ -503,7 +497,6 @@ ircd::m::sync::linear::handle(client &client,
bool
ircd::m::sync::polylog::handle(client &client,
const resource::request &request,
shortpoll &sp,
json::stack::object &object)
try

View file

@ -14,10 +14,8 @@ namespace ircd::m::sync
struct stats;
struct shortpoll;
static bool short_poll(client &, const resource::request &, const args &);
static resource::response handle_get(client &, const resource::request &);
extern resource::method method_get;
extern const string_view description;
extern resource resource;
}
@ -25,15 +23,15 @@ namespace ircd::m::sync
namespace ircd::m::sync::longpoll
{
static std::string sync_room(client &, const m::room &, const args &, const m::event &);
static std::string sync_rooms(client &, const resource::request &, const m::user::id &, const m::room &, const args &, const m::event &);
static bool handle(client &, const resource::request &, const args &, const m::event &, const m::room &);
static bool handle(client &, const resource::request &, const args &, const m::event &);
static void poll(client &, const resource::request &, const args &);
static std::string sync_rooms(client &, const m::user::id &, const m::room &, const args &, const m::event &);
static bool handle(client &, const args &, const m::event &, const m::room &);
static bool handle(client &, const args &, const m::event &);
static void poll(client &, const args &);
}
namespace ircd::m::sync::linear
{
static bool handle(client &, const resource::request &, shortpoll &, json::stack::object &);
static bool handle(client &, shortpoll &, json::stack::object &);
}
namespace ircd::m::sync::polylog
@ -50,7 +48,7 @@ namespace ircd::m::sync::polylog
static void rooms(shortpoll &, json::stack::object &);
static void presence(shortpoll &, json::stack::object &);
static void account_data(shortpoll &, json::stack::object &);
static bool handle(client &, const resource::request &, shortpoll &, json::stack::object &);
static bool handle(client &, shortpoll &, json::stack::object &);
}
/// Argument parser for the client's /sync request
@ -60,6 +58,10 @@ struct ircd::m::sync::args
static conf::item<milliseconds> timeout_min;
static conf::item<milliseconds> timeout_default;
args(const resource::request &request)
:request{request}
{}
const resource::request &request;
string_view filter_id
@ -106,10 +108,6 @@ struct ircd::m::sync::args
// marked as being online when it uses this API. One of: ["offline"]
request.query.get("set_presence", true)
};
args(const resource::request &request)
:request{request}
{}
};
struct ircd::m::sync::stats
@ -123,10 +121,19 @@ struct ircd::m::sync::shortpoll
{
static conf::item<size_t> flush_hiwat;
ircd::client &client;
const resource::request &request;
const sync::args &args;
shortpoll(ircd::client &client,
const sync::args &args)
:client{client}
,args{args}
{}
sync::stats stats;
ircd::client &client;
const sync::args &args;
const resource::request &request
{
args.request
};
const uint64_t &since
{
@ -180,33 +187,35 @@ struct ircd::m::sync::shortpoll
false
};
unique_buffer<mutable_buffer> buf {96_KiB};
unique_buffer<mutable_buffer> buf
{
96_KiB
};
std::unique_ptr<resource::response::chunked> response;
json::stack out
{
buf, [this](const const_buffer &buf)
{
if(!committed)
return buf;
if(!response)
response = std::make_unique<resource::response::chunked>
(
client, http::OK, "application/json; charset=utf-8"
);
stats.flush_bytes += response->write(buf);
stats.flush_count++;
return buf;
},
size_t(flush_hiwat)
buf, std::bind(&shortpoll::flush, this, ph::_1), size_t(flush_hiwat)
};
shortpoll(ircd::client &client,
const resource::request &request,
const sync::args &args)
:client{client}
,request{request}
,args{args}
{}
void commit()
{
response = std::make_unique<resource::response::chunked>
(
client, http::OK, "application/json; charset=utf-8"
);
}
const_buffer flush(const const_buffer &buf)
{
if(!committed)
return buf;
if(!response)
commit();
stats.flush_bytes += response->write(buf);
stats.flush_count++;
return buf;
}
};