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-08-23 23:10:28 +02:00
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
#include "sync.int.h"
|
2017-08-23 23:10:28 +02:00
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Client 6.2.1 :Sync",
|
|
|
|
nullptr,
|
|
|
|
on_unload
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
on_unload()
|
|
|
|
{
|
|
|
|
synchronizer.interrupt();
|
|
|
|
synchronizer.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// sync resource
|
|
|
|
//
|
|
|
|
|
|
|
|
resource
|
|
|
|
sync_resource
|
|
|
|
{
|
|
|
|
"/_matrix/client/r0/sync",
|
|
|
|
{
|
|
|
|
sync_description
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view
|
|
|
|
sync_description
|
2017-09-26 06:42:07 +02:00
|
|
|
{R"(
|
|
|
|
|
2018-02-15 22:06:49 +01:00
|
|
|
6.2.1
|
2017-09-26 06:42:07 +02:00
|
|
|
|
|
|
|
Synchronise the client's state with the latest state on the server. Clients
|
|
|
|
use this API when they first log in to get an initial snapshot of the state
|
|
|
|
on the server, and then continue to call this API to get incremental deltas
|
|
|
|
to the state, and to receive new messages.
|
|
|
|
|
|
|
|
)"};
|
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
//
|
|
|
|
// GET sync
|
|
|
|
//
|
|
|
|
|
|
|
|
resource::method
|
|
|
|
get_sync
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
sync_resource, "GET", sync,
|
2017-12-12 21:26:39 +01:00
|
|
|
{
|
2018-04-16 01:42:13 +02:00
|
|
|
get_sync.REQUIRES_AUTH,
|
|
|
|
-1s, // TODO: this is only -1 b/c initialsyncs
|
2017-12-12 21:26:39 +01:00
|
|
|
}
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
m::import<resource::response (client &, const resource::request &)>
|
|
|
|
initialsync
|
2017-09-26 06:42:07 +02:00
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
"client_initialsync", "initialsync"
|
2017-09-26 06:42:07 +02:00
|
|
|
};
|
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
resource::response
|
|
|
|
sync(client &client,
|
|
|
|
const resource::request &request)
|
2017-09-26 06:42:07 +02:00
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
const syncargs args
|
|
|
|
{
|
|
|
|
request
|
|
|
|
};
|
2017-09-26 06:42:07 +02:00
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
return args.since?
|
|
|
|
since_sync(client, request, args):
|
|
|
|
initialsync(client, request);
|
|
|
|
}
|
2017-09-26 06:42:07 +02:00
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
conf::item<milliseconds>
|
|
|
|
sync_timeout_max
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.sync.timeout.max" },
|
|
|
|
{ "default", 305 * 1000L },
|
|
|
|
};
|
2017-09-26 06:42:07 +02:00
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
conf::item<milliseconds>
|
|
|
|
sync_timeout_min
|
2017-09-26 06:42:07 +02:00
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
{ "name", "ircd.client.sync.timeout.min" },
|
|
|
|
{ "default", 15 * 1000L },
|
2017-09-25 03:05:42 +02:00
|
|
|
};
|
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
conf::item<milliseconds>
|
|
|
|
sync_timeout_default
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.sync.timeout.default" },
|
|
|
|
{ "default", 180 * 1000L },
|
|
|
|
};
|
2017-09-25 03:05:42 +02:00
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
syncargs::syncargs(const resource::request &request)
|
|
|
|
:filter_id
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2017-09-26 06:42:07 +02:00
|
|
|
// 6.2.1 The ID of a filter created using the filter API or a filter JSON object
|
|
|
|
// encoded as a string. The server will detect whether it is an ID or a JSON object
|
|
|
|
// by whether the first character is a "{" open brace. Passing the JSON inline is best
|
|
|
|
// suited to one off requests. Creating a filter using the filter API is recommended
|
|
|
|
// for clients that reuse the same filter multiple times, for example in long poll requests.
|
2018-04-11 00:20:47 +02:00
|
|
|
request.query["filter"]
|
|
|
|
}
|
|
|
|
,since
|
|
|
|
{
|
2017-09-26 06:42:07 +02:00
|
|
|
// 6.2.1 A point in time to continue a sync from.
|
2018-04-11 00:20:47 +02:00
|
|
|
request.query["since"]
|
|
|
|
}
|
|
|
|
,timesout{[&request]
|
|
|
|
{
|
|
|
|
// 6.2.1 The maximum time to poll in milliseconds before returning this request.
|
|
|
|
auto ret(request.query.get("timeout", milliseconds(sync_timeout_default)));
|
|
|
|
ret = std::min(ret, milliseconds(sync_timeout_max));
|
|
|
|
ret = std::max(ret, milliseconds(sync_timeout_min));
|
|
|
|
return now<steady_point>() + ret;
|
|
|
|
}()}
|
|
|
|
,full_state
|
|
|
|
{
|
2017-09-26 06:42:07 +02:00
|
|
|
// 6.2.1 Controls whether to include the full state for all rooms the user is a member of.
|
|
|
|
// If this is set to true, then all state events will be returned, even if since is non-empty.
|
|
|
|
// The timeline will still be limited by the since parameter. In this case, the timeout
|
|
|
|
// parameter will be ignored and the query will return immediately, possibly with an
|
|
|
|
// empty timeline. If false, and since is non-empty, only state which has changed since
|
|
|
|
// the point indicated by since will be returned. By default, this is false.
|
2018-04-11 00:20:47 +02:00
|
|
|
request.query.get("full_state", false)
|
|
|
|
}
|
|
|
|
,set_presence
|
|
|
|
{
|
2017-09-26 06:42:07 +02:00
|
|
|
// 6.2.1 Controls whether the client is automatically marked as online by polling this API.
|
|
|
|
// If this parameter is omitted then the client is automatically marked as online when it
|
|
|
|
// uses this API. Otherwise if the parameter is set to "offline" then the client is not
|
|
|
|
// marked as being online when it uses this API. One of: ["offline"]
|
2018-04-11 00:20:47 +02:00
|
|
|
request.query.get("set_presence", true)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
since_sync(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const syncargs &args)
|
|
|
|
{
|
|
|
|
const m::user::room user_room
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
request.user_id
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
// Can dump pending?
|
2018-04-17 02:57:41 +02:00
|
|
|
if(shortpoll_sync(client, request, args))
|
|
|
|
return {};
|
2018-04-11 00:20:47 +02:00
|
|
|
|
|
|
|
// Can't dump pending
|
|
|
|
return longpoll_sync(client, request, args);
|
|
|
|
}
|
|
|
|
|
2018-04-17 02:57:41 +02:00
|
|
|
bool
|
|
|
|
shortpoll_sync(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const syncargs &args)
|
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
try
|
|
|
|
{
|
2018-04-17 02:57:41 +02:00
|
|
|
const uint64_t _since
|
|
|
|
{
|
|
|
|
lex_cast<uint64_t>(args.since)
|
|
|
|
};
|
|
|
|
|
|
|
|
uint64_t since
|
|
|
|
{
|
|
|
|
_since
|
|
|
|
};
|
|
|
|
|
|
|
|
std::map<std::string, std::vector<std::string>, std::less<>> r;
|
2017-08-23 23:10:28 +02:00
|
|
|
|
2018-04-18 09:33:38 +02:00
|
|
|
bool limited{false};
|
|
|
|
m::events::for_each(since, [&]
|
2018-04-17 02:57:41 +02:00
|
|
|
(const uint64_t &sequence, const m::event &event)
|
2017-09-26 06:42:07 +02:00
|
|
|
{
|
2018-04-17 02:57:41 +02:00
|
|
|
if(!r.empty() && (since - _since > 128))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
since = sequence;
|
|
|
|
if(!json::get<"room_id"_>(event))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const m::room room
|
2017-09-26 06:42:07 +02:00
|
|
|
{
|
2018-04-17 02:57:41 +02:00
|
|
|
json::get<"room_id"_>(event)
|
2017-09-26 06:42:07 +02:00
|
|
|
};
|
|
|
|
|
2018-04-17 02:57:41 +02:00
|
|
|
if(!room.membership(request.user_id))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto it
|
|
|
|
{
|
|
|
|
r.lower_bound(room.room_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(it == end(r) || it->first != room.room_id)
|
|
|
|
it = r.emplace_hint(it, std::string{room.room_id}, std::vector<std::string>{});
|
|
|
|
|
|
|
|
it->second.emplace_back(json::strung{event});
|
|
|
|
return true;
|
2018-04-11 00:20:47 +02:00
|
|
|
});
|
2017-09-26 06:42:07 +02:00
|
|
|
|
2018-04-17 02:57:41 +02:00
|
|
|
if(r.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::vector<json::member> joins;
|
|
|
|
|
|
|
|
for(auto &p : r)
|
|
|
|
{
|
|
|
|
const auto &room_id{p.first};
|
|
|
|
auto &vec{p.second};
|
|
|
|
|
|
|
|
std::vector<std::string> timeline;
|
|
|
|
std::vector<std::string> state;
|
|
|
|
std::vector<std::string> ephemeral;
|
|
|
|
|
|
|
|
for(std::string &event : vec)
|
|
|
|
if(json::object{event}.has("state_key"))
|
|
|
|
state.emplace_back(std::move(event));
|
|
|
|
else if(!json::object{event}.has("prev_events"))
|
|
|
|
ephemeral.emplace_back(std::move(event));
|
|
|
|
else
|
|
|
|
timeline.emplace_back(std::move(event));
|
|
|
|
|
|
|
|
const json::strung timeline_serial{timeline.data(), timeline.data() + timeline.size()};
|
|
|
|
const json::strung state_serial{state.data(), state.data() + state.size()};
|
|
|
|
const json::strung ephemeral_serial{ephemeral.data(), ephemeral.data() + ephemeral.size()};
|
|
|
|
|
|
|
|
const string_view prev_batch
|
|
|
|
{
|
|
|
|
!timeline.empty()?
|
|
|
|
unquote(json::object{timeline.front()}.at("event_id")):
|
|
|
|
string_view{}
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::members body
|
|
|
|
{
|
|
|
|
{ "ephemeral",
|
|
|
|
{
|
|
|
|
{ "events", ephemeral_serial },
|
|
|
|
}},
|
|
|
|
{ "state",
|
|
|
|
{
|
|
|
|
{ "events", state_serial }
|
|
|
|
}},
|
|
|
|
{ "timeline",
|
|
|
|
{
|
|
|
|
{ "events", timeline_serial },
|
|
|
|
{ "prev_batch", prev_batch },
|
|
|
|
{ "limited", false },
|
|
|
|
}},
|
|
|
|
};
|
|
|
|
|
|
|
|
joins.emplace_back(room_id, body);
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::value join
|
|
|
|
{
|
|
|
|
joins.data(), joins.size()
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::members rooms
|
|
|
|
{
|
|
|
|
{ "join", join },
|
|
|
|
{ "leave", json::object{} },
|
|
|
|
{ "invite", json::object{} },
|
|
|
|
};
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
{
|
|
|
|
client, json::members
|
|
|
|
{
|
|
|
|
{ "next_batch", int64_t(since) },
|
|
|
|
{ "rooms", rooms },
|
|
|
|
{ "presence", json::object{} },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
2018-04-11 00:20:47 +02:00
|
|
|
}
|
2018-04-17 02:57:41 +02:00
|
|
|
catch(const bad_lex_cast &e)
|
2018-04-11 00:20:47 +02:00
|
|
|
{
|
2018-04-17 02:57:41 +02:00
|
|
|
throw m::BAD_REQUEST
|
2017-09-26 06:42:07 +02:00
|
|
|
{
|
2018-04-17 02:57:41 +02:00
|
|
|
"Since parameter invalid :%s", e.what()
|
2018-02-12 23:44:46 +01:00
|
|
|
};
|
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
}
|
2017-09-26 06:42:07 +02:00
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
resource::response
|
|
|
|
longpoll_sync(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const syncargs &args)
|
|
|
|
{
|
|
|
|
auto it(begin(polling));
|
|
|
|
for(; it != end(polling); ++it)
|
|
|
|
if(it->timesout > args.timesout)
|
|
|
|
break;
|
2017-09-25 03:05:42 +02:00
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
it = polling.emplace(it, client, request, args);
|
|
|
|
add(*it);
|
|
|
|
client.longpoll = true;
|
|
|
|
synchronizer_dock.notify_one();
|
2017-08-23 23:10:28 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
longpoll::longpoll(ircd::client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const syncargs &args)
|
|
|
|
:client{weak_from(client)}
|
|
|
|
,timesout{args.timesout}
|
|
|
|
,user_id{request.user_id}
|
|
|
|
,since{args.since}
|
|
|
|
,access_token{request.access_token}
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
}
|
2017-08-23 23:10:28 +02:00
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
//
|
|
|
|
// Synchronizer worker
|
|
|
|
//
|
2017-10-03 13:12:54 +02:00
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
ircd::context
|
|
|
|
synchronizer
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
"synchronizer", 1_MiB, worker, context::POST
|
2017-10-03 13:12:54 +02:00
|
|
|
};
|
|
|
|
|
2017-09-26 06:42:07 +02:00
|
|
|
void
|
2018-04-11 00:20:47 +02:00
|
|
|
worker()
|
|
|
|
try
|
2017-09-25 03:05:42 +02:00
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
std::unique_lock<decltype(m::vm::accept)> lock
|
2017-09-26 06:42:07 +02:00
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
m::vm::accept
|
2017-09-26 06:42:07 +02:00
|
|
|
};
|
2017-09-25 03:05:42 +02:00
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
while(1) try
|
2017-09-26 06:42:07 +02:00
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
auto &accepted
|
|
|
|
{
|
|
|
|
//m::vm::accept.wait_for(lock, milliseconds(sync_timeout_min))
|
|
|
|
m::vm::accept.wait(lock)
|
|
|
|
};
|
2017-09-26 06:42:07 +02:00
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
synchronize(accepted);
|
2017-09-26 06:42:07 +02:00
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
// Afterward we reap clients with errors or timed out.
|
|
|
|
errored_check();
|
|
|
|
timeout_check();
|
|
|
|
}
|
|
|
|
catch(const timeout &)
|
|
|
|
{
|
|
|
|
const ctx::exception_handler eh;
|
2018-02-12 20:58:40 +01:00
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
// We land here after sitting in synchronize() for too long without an
|
|
|
|
// event being seen. Because clients are polling we have to run a reap.
|
|
|
|
timeout_check();
|
|
|
|
}
|
|
|
|
catch(const ctx::interrupted &)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"Synchronizer worker: %s", e.what()
|
|
|
|
};
|
|
|
|
}
|
2017-09-26 06:42:07 +02:00
|
|
|
}
|
2018-04-11 00:20:47 +02:00
|
|
|
catch(const ctx::interrupted &e)
|
|
|
|
{
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
"Synchronizer worker interrupted"
|
|
|
|
};
|
2017-09-26 06:42:07 +02:00
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-09-26 06:42:07 +02:00
|
|
|
|
|
|
|
void
|
2018-04-11 00:20:47 +02:00
|
|
|
errored_check()
|
2017-09-25 03:05:42 +02:00
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
auto it(begin(polling));
|
|
|
|
while(it != end(polling))
|
2017-09-26 06:42:07 +02:00
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
auto &sp(*it);
|
|
|
|
if(sp.client.expired())
|
2017-09-26 06:42:07 +02:00
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
del(sp);
|
|
|
|
it = polling.erase(it);
|
2017-09-26 06:42:07 +02:00
|
|
|
}
|
2018-04-11 00:20:47 +02:00
|
|
|
else ++it;
|
2017-09-26 06:42:07 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-11 00:20:47 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
timeout_check()
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
auto it(begin(polling));
|
|
|
|
if(it == end(polling))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto now{ircd::now<steady_point>()}; do
|
|
|
|
{
|
|
|
|
auto &sp(*it);
|
|
|
|
if(sp.timesout > now)
|
|
|
|
return;
|
|
|
|
|
|
|
|
del(sp);
|
|
|
|
if(!sp.client.expired())
|
|
|
|
timedout(sp.client);
|
|
|
|
|
|
|
|
it = polling.erase(it);
|
|
|
|
}
|
|
|
|
while(it != end(polling));
|
2017-09-26 06:42:07 +02:00
|
|
|
}
|
2017-09-25 03:05:42 +02:00
|
|
|
|
2017-09-26 06:42:07 +02:00
|
|
|
///
|
|
|
|
/// TODO: The http error response should not yield this context. If the sendq
|
|
|
|
/// TODO: is backed up the client should be dc'ed.
|
2018-04-11 00:20:47 +02:00
|
|
|
bool
|
|
|
|
timedout(const std::weak_ptr<client> &wp)
|
2017-09-25 03:05:42 +02:00
|
|
|
try
|
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
const life_guard<client> client{wp};
|
|
|
|
client->longpoll = false;
|
2017-09-25 03:05:42 +02:00
|
|
|
|
2018-04-17 02:57:41 +02:00
|
|
|
const auto &next_batch
|
|
|
|
{
|
|
|
|
int64_t(m::vm::current_sequence)
|
|
|
|
};
|
|
|
|
|
2017-09-25 03:05:42 +02:00
|
|
|
resource::response
|
|
|
|
{
|
2018-04-17 02:57:41 +02:00
|
|
|
*client, json::members
|
|
|
|
{
|
|
|
|
{ "next_batch", next_batch },
|
|
|
|
{ "rooms", json::object{} },
|
|
|
|
{ "presence", json::object{} },
|
|
|
|
}
|
2017-09-25 03:05:42 +02:00
|
|
|
};
|
2018-02-12 23:44:46 +01:00
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
return client->async();
|
|
|
|
}
|
|
|
|
catch(const std::bad_weak_ptr &)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch(const ctx::interrupted &e)
|
|
|
|
{
|
|
|
|
throw;
|
2017-09-25 03:05:42 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"synchronizer_timeout(): %s", e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
2017-09-25 03:05:42 +02:00
|
|
|
}
|
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
void
|
|
|
|
add(longpoll &sp)
|
|
|
|
{
|
2017-09-25 03:05:42 +02:00
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
}
|
2017-09-25 03:05:42 +02:00
|
|
|
|
|
|
|
void
|
2018-04-11 00:20:47 +02:00
|
|
|
del(longpoll &sp)
|
2017-09-25 03:05:42 +02:00
|
|
|
{
|
2017-10-03 13:12:54 +02:00
|
|
|
|
2017-09-26 06:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
synchronize(const m::event &event)
|
|
|
|
{
|
|
|
|
const auto &room_id
|
|
|
|
{
|
2017-10-05 01:40:02 +02:00
|
|
|
json::get<"room_id"_>(event)
|
2017-09-26 06:42:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(room_id)
|
|
|
|
{
|
|
|
|
synchronize(event, room_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
synchronize(const m::event &event,
|
|
|
|
const m::room::id &room_id)
|
|
|
|
{
|
2017-10-03 13:12:54 +02:00
|
|
|
const m::room room
|
|
|
|
{
|
|
|
|
room_id
|
|
|
|
};
|
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
auto it(std::begin(polling));
|
|
|
|
while(it != std::end(polling))
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
auto &data{*it};
|
2017-10-03 13:12:54 +02:00
|
|
|
if(!room.membership(data.user_id))
|
|
|
|
{
|
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
if(!update_sync(data, event, room))
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
++it;
|
|
|
|
continue;
|
2017-10-03 13:12:54 +02:00
|
|
|
}
|
2018-04-11 00:20:47 +02:00
|
|
|
|
|
|
|
it = polling.erase(it);
|
2017-10-03 13:12:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
update_sync_room(client &client,
|
|
|
|
const m::room &room,
|
|
|
|
const string_view &since,
|
|
|
|
const m::event &event)
|
|
|
|
{
|
|
|
|
std::vector<std::string> state;
|
2018-04-11 00:20:47 +02:00
|
|
|
if(defined(json::get<"event_id"_>(event)) && defined(json::get<"state_key"_>(event)))
|
2017-10-16 06:18:42 +02:00
|
|
|
state.emplace_back(json::strung(event));
|
2017-10-03 13:12:54 +02:00
|
|
|
|
2018-02-11 22:38:41 +01:00
|
|
|
const json::strung state_serial
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-02-11 22:38:41 +01:00
|
|
|
state.data(), state.data() + state.size()
|
2017-10-03 13:12:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<std::string> timeline;
|
2018-04-11 00:20:47 +02:00
|
|
|
if(defined(json::get<"event_id"_>(event)) && !defined(json::get<"state_key"_>(event)))
|
2017-10-16 06:18:42 +02:00
|
|
|
timeline.emplace_back(json::strung(event));
|
2017-10-03 13:12:54 +02:00
|
|
|
|
2018-02-11 22:38:41 +01:00
|
|
|
const json::strung timeline_serial
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-02-11 22:38:41 +01:00
|
|
|
timeline.data(), timeline.data() + timeline.size()
|
2017-10-03 13:12:54 +02:00
|
|
|
};
|
|
|
|
|
2018-02-12 23:44:46 +01:00
|
|
|
std::vector<std::string> ephemeral;
|
|
|
|
const json::strung ephemeral_serial
|
|
|
|
{
|
|
|
|
ephemeral.data(), ephemeral.data() + ephemeral.size()
|
|
|
|
};
|
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
const auto &prev_batch
|
|
|
|
{
|
|
|
|
!timeline.empty()?
|
|
|
|
unquote(json::object{timeline.front()}.get("event_id")):
|
|
|
|
string_view{}
|
|
|
|
};
|
|
|
|
|
|
|
|
//TODO: XXX
|
|
|
|
const bool limited
|
|
|
|
{
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
2017-10-03 13:12:54 +02:00
|
|
|
const json::members body
|
|
|
|
{
|
2018-02-12 23:44:46 +01:00
|
|
|
{ "account_data", json::members{} },
|
|
|
|
{ "unread_notifications",
|
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
{ "highlight_count", int64_t(0) },
|
2018-02-12 23:44:46 +01:00
|
|
|
{ "notification_count", int64_t(0) },
|
|
|
|
}},
|
|
|
|
{ "ephemeral",
|
|
|
|
{
|
|
|
|
{ "events", ephemeral_serial },
|
|
|
|
}},
|
|
|
|
{ "state",
|
|
|
|
{
|
|
|
|
{ "events", state_serial }
|
|
|
|
}},
|
|
|
|
{ "timeline",
|
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
{ "events", timeline_serial },
|
|
|
|
{ "prev_batch", prev_batch },
|
|
|
|
{ "limited", limited },
|
2018-02-12 23:44:46 +01:00
|
|
|
}},
|
2017-10-03 13:12:54 +02:00
|
|
|
};
|
|
|
|
|
2017-10-16 06:18:42 +02:00
|
|
|
return json::strung(body);
|
2017-10-03 13:12:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
update_sync_rooms(client &client,
|
|
|
|
const m::user::id &user_id,
|
|
|
|
const m::room &room,
|
|
|
|
const string_view &since,
|
|
|
|
const m::event &event)
|
|
|
|
{
|
|
|
|
std::vector<std::string> r[3];
|
|
|
|
std::vector<json::member> m[3];
|
|
|
|
r[0].emplace_back(update_sync_room(client, room, since, event));
|
|
|
|
m[0].emplace_back(room.room_id, r[0].back());
|
|
|
|
|
2017-10-16 06:18:42 +02:00
|
|
|
const std::string join{json::strung(m[0].data(), m[0].data() + m[0].size())};
|
|
|
|
const std::string leave{json::strung(m[1].data(), m[1].data() + m[1].size())};
|
|
|
|
const std::string invite{json::strung(m[2].data(), m[2].data() + m[2].size())};
|
|
|
|
return json::strung(json::members
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
|
|
|
{ "join", join },
|
|
|
|
{ "leave", leave },
|
|
|
|
{ "invite", invite },
|
|
|
|
});
|
2017-09-26 06:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-04-11 00:20:47 +02:00
|
|
|
update_sync(const longpoll &data,
|
2017-10-03 13:12:54 +02:00
|
|
|
const m::event &event,
|
|
|
|
const m::room &room)
|
2017-09-26 06:42:07 +02:00
|
|
|
try
|
|
|
|
{
|
2017-10-03 13:12:54 +02:00
|
|
|
const life_guard<client> client
|
2017-09-26 06:42:07 +02:00
|
|
|
{
|
2017-10-03 13:12:54 +02:00
|
|
|
data.client
|
2017-09-26 06:42:07 +02:00
|
|
|
};
|
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
if(!client->longpoll)
|
|
|
|
return true;
|
|
|
|
|
2017-10-03 13:12:54 +02:00
|
|
|
const auto rooms
|
|
|
|
{
|
|
|
|
update_sync_rooms(*client, data.user_id, room, data.since, event)
|
|
|
|
};
|
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
const m::user::room ur
|
|
|
|
{
|
|
|
|
m::user::id{data.user_id}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<json::value> presents;
|
|
|
|
ur.get(std::nothrow, "m.presence", [&]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
const auto &content
|
|
|
|
{
|
|
|
|
at<"content"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
presents.emplace_back(event);
|
|
|
|
});
|
|
|
|
|
|
|
|
const json::members presence
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
{ "events", json::value { presents.data(), presents.size() } },
|
2017-10-03 13:12:54 +02:00
|
|
|
};
|
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
const auto &next_batch
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
int64_t(m::vm::current_sequence)
|
2017-10-03 13:12:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
{
|
|
|
|
*client, json::members
|
|
|
|
{
|
|
|
|
{ "next_batch", next_batch },
|
|
|
|
{ "rooms", rooms },
|
2018-02-12 23:44:46 +01:00
|
|
|
{ "presence", presence },
|
2017-10-03 13:12:54 +02:00
|
|
|
}
|
|
|
|
};
|
2017-09-26 06:42:07 +02:00
|
|
|
|
2018-02-12 20:58:40 +01:00
|
|
|
client->longpoll = false;
|
|
|
|
client->async();
|
2017-09-26 06:42:07 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-10-03 13:12:54 +02:00
|
|
|
catch(const std::bad_weak_ptr &e)
|
2017-09-26 06:42:07 +02:00
|
|
|
{
|
2017-10-03 13:12:54 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
/*
|
|
|
|
if(!defined(json::get<"event_id"_>(event)))
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
thread_local char foo[64_KiB];
|
|
|
|
mutable_buffer buf{foo};
|
|
|
|
m::event event_(event);
|
|
|
|
if(at<"type"_>(event) == "m.receipt")
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-04-11 00:20:47 +02:00
|
|
|
const auto &content{at<"content"_>(event)};
|
|
|
|
for(const auto &room : content)
|
|
|
|
{
|
|
|
|
const auto &room_id{unquote(room.first)};
|
|
|
|
const json::object &object{room.second};
|
|
|
|
for(const auto &ev : object)
|
|
|
|
{
|
|
|
|
const auto &type{unquote(ev.first)};
|
|
|
|
const json::object &users{ev.second};
|
|
|
|
for(const auto &user : users)
|
|
|
|
{
|
|
|
|
const auto &user_id{unquote(user.first)};
|
|
|
|
const json::object datas{user.second};
|
|
|
|
const json::object data{datas.get("data")};
|
|
|
|
const auto ts{data.get("ts")};
|
|
|
|
const auto event_id{unquote(data.get("event_id"))};
|
|
|
|
json::get<"content"_>(event_) = json::stringify(buf, json::members
|
|
|
|
{{
|
|
|
|
event_id, json::members
|
|
|
|
{{
|
|
|
|
type, json::members
|
|
|
|
{{
|
|
|
|
user_id, json::members
|
|
|
|
{{
|
|
|
|
"ts", ts
|
|
|
|
}}
|
|
|
|
}}
|
|
|
|
}}
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-03 13:12:54 +02:00
|
|
|
|
2018-04-11 00:20:47 +02:00
|
|
|
ephemeral.emplace_back(json::strung(event_));
|
2017-10-03 13:12:54 +02:00
|
|
|
}
|
2018-04-11 00:20:47 +02:00
|
|
|
*/
|