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
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
2017-09-26 06:42:07 +02:00
|
|
|
const auto sync_description
|
|
|
|
{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.
|
|
|
|
|
|
|
|
)"};
|
|
|
|
|
2017-08-23 23:10:28 +02:00
|
|
|
resource sync_resource
|
|
|
|
{
|
2017-10-12 05:52:33 +02:00
|
|
|
"/_matrix/client/r0/sync",
|
2017-12-12 21:26:39 +01:00
|
|
|
{
|
|
|
|
sync_description
|
|
|
|
}
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2017-09-26 06:42:07 +02:00
|
|
|
void longpoll(client &client, const resource::request &request, const steady_point &timeout);
|
|
|
|
|
|
|
|
void synchronizer_worker();
|
|
|
|
ircd::context synchronizer_context
|
|
|
|
{
|
|
|
|
"synchronizer",
|
|
|
|
256_KiB,
|
|
|
|
&synchronizer_worker,
|
|
|
|
ircd::context::POST,
|
|
|
|
};
|
|
|
|
|
|
|
|
void synchronizer_timeout_worker();
|
|
|
|
ircd::context synchronizer_timeout_context
|
|
|
|
{
|
|
|
|
"synchronizer",
|
|
|
|
256_KiB,
|
|
|
|
&synchronizer_timeout_worker,
|
|
|
|
ircd::context::POST,
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto on_unload{[]
|
|
|
|
{
|
|
|
|
synchronizer_context.interrupt();
|
|
|
|
synchronizer_timeout_context.interrupt();
|
|
|
|
|
|
|
|
synchronizer_context.join();
|
|
|
|
synchronizer_timeout_context.join();
|
|
|
|
}};
|
|
|
|
|
2018-02-15 22:06:49 +01:00
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
2017-09-26 06:42:07 +02:00
|
|
|
{
|
2018-02-15 22:06:49 +01:00
|
|
|
"Client 6.2.1 :Sync",
|
2017-09-26 06:42:07 +02:00
|
|
|
nullptr,
|
|
|
|
on_unload
|
2017-09-25 03:05:42 +02:00
|
|
|
};
|
|
|
|
|
2017-09-26 06:42:07 +02:00
|
|
|
resource::response
|
2017-10-03 13:12:54 +02:00
|
|
|
initial_sync(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const string_view &filter_id,
|
|
|
|
const bool &full_state,
|
|
|
|
const string_view &set_presence);
|
2017-09-25 03:05:42 +02:00
|
|
|
|
2017-08-23 23:10:28 +02:00
|
|
|
resource::response
|
2018-02-15 22:06:49 +01:00
|
|
|
sync(client &client,
|
|
|
|
const resource::request &request)
|
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.
|
|
|
|
const auto filter_id
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2017-09-26 06:42:07 +02:00
|
|
|
request.query["filter"]
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2017-09-26 06:42:07 +02:00
|
|
|
// 6.2.1 A point in time to continue a sync from.
|
2017-08-23 23:10:28 +02:00
|
|
|
const auto since
|
|
|
|
{
|
2017-09-26 06:42:07 +02:00
|
|
|
request.query["since"]
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
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.
|
|
|
|
const bool full_state
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2017-09-26 06:42:07 +02:00
|
|
|
request.query["full_state"] == "true"
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
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"]
|
|
|
|
const string_view set_presence
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2017-09-26 06:42:07 +02:00
|
|
|
request.query["set_presence"]
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2017-10-03 13:12:54 +02:00
|
|
|
// Start a new spool for client
|
2017-09-26 06:42:07 +02:00
|
|
|
if(!since)
|
2017-10-03 13:12:54 +02:00
|
|
|
return initial_sync(client, request, filter_id, full_state, set_presence);
|
2017-08-23 23:10:28 +02:00
|
|
|
|
2017-09-26 06:42:07 +02:00
|
|
|
// The ircd.tape.head
|
2017-10-25 18:47:03 +02:00
|
|
|
int64_t sequence{0};
|
2018-02-11 22:38:41 +01:00
|
|
|
//TODO: user's room
|
|
|
|
if(!m::user::tokens.get(std::nothrow, "ircd.tape.head"_sv, request.access_token, [&sequence]
|
|
|
|
(const m::event &event)
|
2017-09-26 06:42:07 +02:00
|
|
|
{
|
|
|
|
const json::object &content
|
|
|
|
{
|
2017-10-05 01:40:02 +02:00
|
|
|
at<"content"_>(event)
|
2017-09-26 06:42:07 +02:00
|
|
|
};
|
|
|
|
|
2017-10-25 18:47:03 +02:00
|
|
|
sequence = content.at<int64_t>("sequence");
|
2017-09-26 06:42:07 +02:00
|
|
|
}))
|
|
|
|
throw m::NOT_FOUND{"since parameter invalid"};
|
|
|
|
|
|
|
|
// 6.2.1 The maximum time to poll in milliseconds before returning this request.
|
2018-02-13 00:10:35 +01:00
|
|
|
const int64_t timeout_requested
|
2017-09-26 06:42:07 +02:00
|
|
|
{
|
2018-02-13 00:10:35 +01:00
|
|
|
request.query["timeout"]? lex_cast<int64_t>(request.query.at("timeout")) : 0L
|
2018-02-12 23:44:46 +01:00
|
|
|
};
|
|
|
|
|
2017-09-26 06:42:07 +02:00
|
|
|
const auto timeout_at
|
|
|
|
{
|
2018-02-13 00:10:35 +01:00
|
|
|
now<steady_point>() +
|
|
|
|
milliseconds(timeout_requested? std::max(timeout_requested, 3000L) : 30000L)
|
2017-09-26 06:42:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
longpoll(client, request, timeout_at);
|
2017-09-25 03:05:42 +02:00
|
|
|
|
|
|
|
// This handler returns no response. As long as this handler doesn't throw
|
|
|
|
// an exception IRCd will keep the client alive.
|
2017-08-23 23:10:28 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
resource::method get_sync
|
|
|
|
{
|
|
|
|
sync_resource, "GET", sync,
|
|
|
|
{
|
|
|
|
get_sync.REQUIRES_AUTH
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-26 06:42:07 +02:00
|
|
|
/// Input
|
|
|
|
///
|
|
|
|
///
|
2017-10-03 13:12:54 +02:00
|
|
|
|
|
|
|
struct syncpoll
|
|
|
|
{
|
|
|
|
static std::list<syncpoll> polling;
|
|
|
|
static std::multimap<steady_point, decltype(polling)::iterator> pollout;
|
|
|
|
|
|
|
|
std::string user_id;
|
|
|
|
std::string since;
|
|
|
|
std::string access_token; // can get rid of this and use some session id
|
|
|
|
std::weak_ptr<ircd::client> client;
|
|
|
|
decltype(pollout)::iterator it { std::end(pollout) };
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(syncpoll::polling) syncpoll::polling {};
|
|
|
|
decltype(syncpoll::pollout) syncpoll::pollout {};
|
|
|
|
|
2017-09-26 06:42:07 +02:00
|
|
|
void
|
|
|
|
longpoll(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const steady_point &timeout)
|
2017-09-25 03:05:42 +02:00
|
|
|
{
|
2017-10-03 13:12:54 +02:00
|
|
|
static auto &polling{syncpoll::polling};
|
|
|
|
static auto &pollout{syncpoll::pollout};
|
|
|
|
|
2017-09-26 06:42:07 +02:00
|
|
|
const auto it
|
|
|
|
{
|
2017-10-03 13:12:54 +02:00
|
|
|
polling.emplace(polling.end(), syncpoll
|
|
|
|
{
|
|
|
|
std::string{request.user_id},
|
|
|
|
std::string{request.query.at("since")},
|
2018-02-11 22:38:41 +01:00
|
|
|
std::string{request.access_token}, //TODO: nope.
|
2017-10-03 13:12:54 +02:00
|
|
|
weak_from(client)
|
|
|
|
})
|
2017-09-26 06:42:07 +02:00
|
|
|
};
|
2017-09-25 03:05:42 +02:00
|
|
|
|
2017-09-26 06:42:07 +02:00
|
|
|
syncpoll &data
|
|
|
|
{
|
2017-10-03 13:12:54 +02:00
|
|
|
*it
|
2017-09-26 06:42:07 +02:00
|
|
|
};
|
|
|
|
|
2017-10-03 13:12:54 +02:00
|
|
|
data.it = pollout.emplace(timeout, it);
|
2017-09-26 06:42:07 +02:00
|
|
|
|
2017-10-03 13:12:54 +02:00
|
|
|
if(pollout.size() == 1)
|
2017-09-26 06:42:07 +02:00
|
|
|
notify(synchronizer_timeout_context);
|
2018-02-12 20:58:40 +01:00
|
|
|
|
|
|
|
client.longpoll = true;
|
2017-09-26 06:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Timeout worker stack
|
|
|
|
//
|
|
|
|
|
2017-10-03 13:12:54 +02:00
|
|
|
void synchronizer_timeout(const syncpoll &sp);
|
2017-09-26 06:42:07 +02:00
|
|
|
|
|
|
|
/// This function is the base of an ircd::context which yields until a client
|
|
|
|
/// is due to timeout. This worker reaps timed out clients from the lists.
|
|
|
|
void
|
|
|
|
synchronizer_timeout_worker()
|
|
|
|
try
|
2017-09-25 03:05:42 +02:00
|
|
|
{
|
2017-09-26 06:42:07 +02:00
|
|
|
static auto &polling{syncpoll::polling};
|
|
|
|
static auto &pollout{syncpoll::pollout};
|
2017-09-25 03:05:42 +02:00
|
|
|
|
2017-09-26 06:42:07 +02:00
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
while(!pollout.empty())
|
|
|
|
{
|
|
|
|
const auto &timeout{std::begin(pollout)->first};
|
|
|
|
const auto &iterator{std::begin(pollout)->second};
|
|
|
|
if(timeout > now<steady_point>())
|
|
|
|
{
|
|
|
|
ctx::wait_until<std::nothrow_t>(timeout);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-10-03 13:12:54 +02:00
|
|
|
const auto &data{*iterator};
|
|
|
|
synchronizer_timeout(data);
|
2017-09-26 06:42:07 +02:00
|
|
|
polling.erase(iterator);
|
|
|
|
pollout.erase(std::begin(pollout));
|
|
|
|
}
|
|
|
|
|
|
|
|
while(pollout.empty())
|
|
|
|
ctx::wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(const ircd::ctx::interrupted &e)
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2017-09-26 06:42:07 +02:00
|
|
|
ircd::log::debug("Synchronizer timeout worker interrupted");
|
|
|
|
}
|
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.
|
2017-09-25 03:05:42 +02:00
|
|
|
void
|
2017-10-03 13:12:54 +02:00
|
|
|
synchronizer_timeout(const syncpoll &sp)
|
2017-09-25 03:05:42 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
const life_guard<client> client
|
|
|
|
{
|
2017-09-26 06:42:07 +02:00
|
|
|
sp.client
|
2017-09-25 03:05:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
{
|
2017-09-26 06:42:07 +02:00
|
|
|
*client, http::REQUEST_TIMEOUT
|
2017-09-25 03:05:42 +02:00
|
|
|
};
|
2018-02-12 23:44:46 +01:00
|
|
|
|
2018-02-14 00:05:02 +01:00
|
|
|
client->longpoll = false;
|
|
|
|
client->async();
|
2017-09-25 03:05:42 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2017-09-26 06:42:07 +02:00
|
|
|
log::error("synchronizer_timeout(): %s", e.what());
|
2017-09-25 03:05:42 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 06:42:07 +02:00
|
|
|
//
|
|
|
|
// Main worker stack
|
|
|
|
//
|
2017-09-25 03:05:42 +02:00
|
|
|
|
2017-10-03 13:12:54 +02:00
|
|
|
bool update_sync(const syncpoll &data, const m::event &event, const m::room &);
|
2017-09-26 06:42:07 +02:00
|
|
|
void synchronize(const m::event &, const m::room::id &);
|
|
|
|
void synchronize(const m::event &);
|
2017-09-25 03:05:42 +02:00
|
|
|
|
|
|
|
void
|
2017-09-26 06:42:07 +02:00
|
|
|
synchronizer_worker()
|
2017-09-25 03:05:42 +02:00
|
|
|
{
|
2017-09-26 06:42:07 +02:00
|
|
|
while(1) try
|
2017-09-25 03:05:42 +02:00
|
|
|
{
|
2017-10-25 18:47:03 +02:00
|
|
|
std::unique_lock<decltype(m::vm::inserted)> lock
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2017-10-25 18:47:03 +02:00
|
|
|
m::vm::inserted
|
2017-10-03 13:12:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// reference to the event on the inserter's stack
|
2017-09-25 03:05:42 +02:00
|
|
|
const auto &event
|
|
|
|
{
|
2017-10-25 18:47:03 +02:00
|
|
|
m::vm::inserted.wait(lock)
|
2017-09-25 03:05:42 +02:00
|
|
|
};
|
|
|
|
|
2017-10-03 13:12:54 +02:00
|
|
|
if(!syncpoll::polling.empty())
|
|
|
|
synchronize(event);
|
2017-09-25 03:05:42 +02:00
|
|
|
}
|
2017-11-16 02:48:25 +01:00
|
|
|
catch(const ircd::ctx::interrupted &e)
|
|
|
|
{
|
|
|
|
ircd::log::debug("Synchronizer worker interrupted");
|
|
|
|
return;
|
|
|
|
}
|
2017-09-26 06:42:07 +02:00
|
|
|
catch(const timeout &e)
|
|
|
|
{
|
2017-10-03 13:12:54 +02:00
|
|
|
ircd::log::debug("Synchronizer worker: %s", e.what());
|
2017-09-26 06:42:07 +02:00
|
|
|
}
|
2017-11-16 02:48:25 +01:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
ircd::log::error("Synchronizer worker: %s", e.what());
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2017-10-03 13:12:54 +02:00
|
|
|
assert(0);
|
2017-09-26 06:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
synchronize(const m::event &event,
|
|
|
|
const m::room::id &room_id)
|
|
|
|
{
|
2017-10-03 13:12:54 +02:00
|
|
|
static auto &polling{syncpoll::polling};
|
|
|
|
static auto &pollout{syncpoll::pollout};
|
|
|
|
|
|
|
|
const m::room room
|
|
|
|
{
|
|
|
|
room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
for(auto it(std::begin(polling)); it != std::end(polling);)
|
|
|
|
{
|
|
|
|
const auto &data{*it};
|
|
|
|
if(!room.membership(data.user_id))
|
|
|
|
{
|
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(update_sync(data, event, room))
|
|
|
|
{
|
|
|
|
pollout.erase(data.it);
|
|
|
|
polling.erase(it++);
|
|
|
|
}
|
|
|
|
else ++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
update_sync_room(client &client,
|
|
|
|
const m::room &room,
|
|
|
|
const string_view &since,
|
|
|
|
const m::event &event)
|
|
|
|
{
|
|
|
|
std::vector<std::string> state;
|
2017-10-05 01:40:02 +02:00
|
|
|
if(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;
|
2017-10-05 01:40:02 +02:00
|
|
|
if(!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()
|
|
|
|
};
|
|
|
|
|
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",
|
|
|
|
{
|
|
|
|
{ "highlight_count", int64_t(0) },
|
|
|
|
{ "notification_count", int64_t(0) },
|
|
|
|
}},
|
|
|
|
{ "ephemeral",
|
|
|
|
{
|
|
|
|
{ "events", ephemeral_serial },
|
|
|
|
}},
|
|
|
|
{ "state",
|
|
|
|
{
|
|
|
|
{ "events", state_serial }
|
|
|
|
}},
|
|
|
|
{ "timeline",
|
|
|
|
{
|
|
|
|
{ "events", timeline_serial },
|
|
|
|
{ "prev_batch", int64_t(m::vm::current_sequence) }, //TODO: XXX
|
|
|
|
{ "limited", false }, //TODO: XXX
|
|
|
|
}},
|
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
|
2017-10-03 13:12:54 +02:00
|
|
|
update_sync(const syncpoll &data,
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2017-10-03 13:12:54 +02:00
|
|
|
const auto rooms
|
|
|
|
{
|
|
|
|
update_sync_rooms(*client, data.user_id, room, data.since, event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto presence
|
|
|
|
{
|
|
|
|
"{}"
|
|
|
|
};
|
|
|
|
|
2018-02-12 23:44:46 +01:00
|
|
|
const int64_t &next_batch
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-02-12 23:44:46 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
initial_sync_room(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const m::room &room,
|
|
|
|
const bool &full_state)
|
|
|
|
{
|
|
|
|
std::vector<std::string> state;
|
|
|
|
{
|
2018-02-11 22:38:41 +01:00
|
|
|
const m::room::state state_
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-02-11 22:38:41 +01:00
|
|
|
room
|
2017-10-03 13:12:54 +02:00
|
|
|
};
|
|
|
|
|
2018-02-11 22:38:41 +01:00
|
|
|
state_.for_each([&state](const m::event &event)
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-02-11 22:38:41 +01:00
|
|
|
state.emplace_back(json::strung(event));
|
2017-10-03 13:12:54 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto state_serial
|
|
|
|
{
|
2017-10-16 06:18:42 +02:00
|
|
|
json::strung(state.data(), state.data() + state.size())
|
2017-10-03 13:12:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<std::string> timeline;
|
|
|
|
{
|
2018-02-11 22:38:41 +01:00
|
|
|
const m::room::messages timeline_
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-02-11 22:38:41 +01:00
|
|
|
room
|
2017-10-03 13:12:54 +02:00
|
|
|
};
|
|
|
|
|
2018-02-11 22:38:41 +01:00
|
|
|
timeline_.test([&timeline](const m::event &event)
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-02-11 22:38:41 +01:00
|
|
|
timeline.emplace_back(json::strung(event));
|
|
|
|
return timeline.size() >= 10;
|
2017-10-03 13:12:54 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto timeline_serial
|
|
|
|
{
|
2017-10-16 06:18:42 +02:00
|
|
|
json::strung(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()
|
|
|
|
};
|
|
|
|
|
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",
|
|
|
|
{
|
|
|
|
{ "highlight_count", int64_t(0) },
|
|
|
|
{ "notification_count", int64_t(0) },
|
|
|
|
}},
|
|
|
|
{ "ephemeral",
|
|
|
|
{
|
|
|
|
{ "events", ephemeral_serial },
|
|
|
|
}},
|
|
|
|
{ "state",
|
|
|
|
{
|
|
|
|
{ "events", state_serial }
|
|
|
|
}},
|
|
|
|
{ "timeline",
|
|
|
|
{
|
|
|
|
{ "events", timeline_serial },
|
|
|
|
{ "prev_batch", int64_t(m::vm::current_sequence) }, //TODO: XXX
|
|
|
|
{ "limited", false }, //TODO: XXX
|
|
|
|
}},
|
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
|
|
|
|
initial_sync_rooms(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const string_view &filter_id,
|
|
|
|
const bool &full_state)
|
|
|
|
{
|
2018-02-11 22:38:41 +01:00
|
|
|
m::user user{request.user_id};
|
|
|
|
const auto user_room_id{user.room_id()};
|
|
|
|
m::room::state user_state{user_room_id};
|
2017-10-03 13:12:54 +02:00
|
|
|
|
|
|
|
std::array<std::vector<std::string>, 3> r;
|
|
|
|
std::array<std::vector<json::member>, 3> m;
|
2018-02-11 22:38:41 +01:00
|
|
|
|
|
|
|
// Get the rooms the user is a joined member in by iterating the state
|
|
|
|
// events in the user's room.
|
|
|
|
user_state.for_each("join", [&r, &m, &client, &request, &full_state]
|
|
|
|
(const m::event &event)
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-02-11 22:38:41 +01:00
|
|
|
const m::room::id &room_id{unquote(at<"state_key"_>(event))};
|
2017-10-03 13:12:54 +02:00
|
|
|
const auto i
|
|
|
|
{
|
2018-02-11 22:38:41 +01:00
|
|
|
//membership == "join"? 0:
|
|
|
|
//membership == "leave"? 1:
|
|
|
|
//membership == "invite"? 2:
|
|
|
|
//-1
|
|
|
|
0
|
2017-10-03 13:12:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
r.at(i).emplace_back(initial_sync_room(client, request, room_id, full_state));
|
|
|
|
m.at(i).emplace_back(room_id, r.at(i).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 },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
initial_sync(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const string_view &filter_id,
|
|
|
|
const bool &full_state,
|
|
|
|
const string_view &set_presence)
|
|
|
|
{
|
|
|
|
const std::string rooms
|
|
|
|
{
|
|
|
|
initial_sync_rooms(client, request, filter_id, full_state)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto presence
|
|
|
|
{
|
|
|
|
"{}"
|
|
|
|
};
|
|
|
|
|
2017-10-25 18:47:03 +02:00
|
|
|
const int64_t &next_batch
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2017-10-25 18:47:03 +02:00
|
|
|
m::vm::current_sequence
|
2017-10-03 13:12:54 +02:00
|
|
|
};
|
|
|
|
|
2018-02-11 22:38:41 +01:00
|
|
|
const auto &state_key
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-02-11 22:38:41 +01:00
|
|
|
request.access_token
|
2017-10-03 13:12:54 +02:00
|
|
|
};
|
|
|
|
|
2018-02-11 22:38:41 +01:00
|
|
|
//TODO: user's room
|
|
|
|
m::send(m::user::tokens, request.user_id, "ircd.tape.head", state_key,
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2017-11-30 19:56:18 +01:00
|
|
|
{ "sequence", next_batch }
|
2017-10-03 13:12:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return 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-25 03:05:42 +02:00
|
|
|
}
|