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-09-01 09:51:40 +02:00
|
|
|
#include "sync.h"
|
2017-08-23 23:10:28 +02:00
|
|
|
|
2018-09-01 09:51:40 +02:00
|
|
|
ircd::mapi::header
|
2018-04-11 00:20:47 +02:00
|
|
|
IRCD_MODULE
|
|
|
|
{
|
2018-04-22 08:50:16 +02:00
|
|
|
"Client 6.2.1 :Sync"
|
2018-04-11 00:20:47 +02:00
|
|
|
};
|
|
|
|
|
2018-10-18 16:44:20 +02:00
|
|
|
decltype(ircd::m::sync::log)
|
|
|
|
ircd::m::sync::log
|
|
|
|
{
|
|
|
|
"sync", 's'
|
|
|
|
};
|
|
|
|
|
2018-09-01 09:51:40 +02:00
|
|
|
decltype(ircd::m::sync::resource)
|
|
|
|
ircd::m::sync::resource
|
2018-04-11 00:20:47 +02:00
|
|
|
{
|
|
|
|
"/_matrix/client/r0/sync",
|
|
|
|
{
|
2018-09-01 09:51:40 +02:00
|
|
|
description
|
2018-04-11 00:20:47 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-01 09:51:40 +02:00
|
|
|
decltype(ircd::m::sync::description)
|
|
|
|
ircd::m::sync::description
|
|
|
|
{R"(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-09-01 09:51:40 +02:00
|
|
|
ircd::conf::item<ircd::milliseconds>
|
|
|
|
ircd::m::sync::args::timeout_max
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2018-09-01 09:51:40 +02:00
|
|
|
{ "name", "ircd.client.sync.timeout.max" },
|
|
|
|
{ "default", 15 * 1000L },
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2018-09-01 09:51:40 +02:00
|
|
|
ircd::conf::item<ircd::milliseconds>
|
|
|
|
ircd::m::sync::args::timeout_min
|
2017-09-26 06:42:07 +02:00
|
|
|
{
|
2018-09-01 09:51:40 +02:00
|
|
|
{ "name", "ircd.client.sync.timeout.min" },
|
|
|
|
{ "default", 5 * 1000L },
|
2018-04-11 00:20:47 +02:00
|
|
|
};
|
2017-09-26 06:42:07 +02:00
|
|
|
|
2018-09-01 09:51:40 +02:00
|
|
|
ircd::conf::item<ircd::milliseconds>
|
|
|
|
ircd::m::sync::args::timeout_default
|
2017-09-26 06:42:07 +02:00
|
|
|
{
|
2018-09-01 09:51:40 +02:00
|
|
|
{ "name", "ircd.client.sync.timeout.default" },
|
|
|
|
{ "default", 10 * 1000L },
|
2017-09-25 03:05:42 +02:00
|
|
|
};
|
|
|
|
|
2018-09-01 09:51:40 +02:00
|
|
|
ircd::conf::item<size_t>
|
|
|
|
ircd::m::sync::shortpoll::flush_hiwat
|
2018-04-11 00:20:47 +02:00
|
|
|
{
|
2018-09-01 09:51:40 +02:00
|
|
|
{ "name", "ircd.client.sync.flush.hiwat" },
|
|
|
|
{ "default", long(24_KiB) },
|
2018-04-11 00:20:47 +02:00
|
|
|
};
|
2017-09-25 03:05:42 +02:00
|
|
|
|
2018-09-01 09:51:40 +02:00
|
|
|
//
|
|
|
|
// GET sync
|
|
|
|
//
|
|
|
|
|
|
|
|
decltype(ircd::m::sync::method_get)
|
|
|
|
ircd::m::sync::method_get
|
2018-05-14 04:23:23 +02:00
|
|
|
{
|
2018-09-01 09:51:40 +02:00
|
|
|
resource, "GET", handle_get,
|
|
|
|
{
|
|
|
|
method_get.REQUIRES_AUTH,
|
|
|
|
-1s,
|
|
|
|
}
|
2018-05-14 04:23:23 +02:00
|
|
|
};
|
|
|
|
|
2018-09-01 09:51:40 +02:00
|
|
|
ircd::resource::response
|
|
|
|
ircd::m::sync::handle_get(client &client,
|
|
|
|
const resource::request &request)
|
2018-09-01 11:57:15 +02:00
|
|
|
try
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2018-09-01 09:51:40 +02:00
|
|
|
const args args
|
|
|
|
{
|
|
|
|
request
|
|
|
|
};
|
2018-04-11 00:20:47 +02:00
|
|
|
|
2018-04-22 08:50:16 +02:00
|
|
|
shortpoll sp
|
|
|
|
{
|
2018-09-01 11:57:15 +02:00
|
|
|
client, args
|
2018-04-22 08:50:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(sp.since > sp.current)
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"Since parameter is in the future..."
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object top
|
|
|
|
{
|
|
|
|
sp.out
|
|
|
|
};
|
|
|
|
|
2018-11-05 03:11:37 +01:00
|
|
|
const size_t linear_delta_max
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
2018-11-05 03:11:37 +01:00
|
|
|
linear::delta_max
|
2018-04-22 08:50:16 +02:00
|
|
|
};
|
|
|
|
|
2018-09-01 11:57:15 +02:00
|
|
|
const bool shortpolled
|
|
|
|
{
|
2018-04-22 08:50:16 +02:00
|
|
|
sp.delta == 0?
|
|
|
|
false:
|
2018-11-05 03:11:37 +01:00
|
|
|
sp.delta > linear_delta_max?
|
2018-09-01 11:57:15 +02:00
|
|
|
polylog::handle(client, sp, top):
|
|
|
|
linear::handle(client, sp, top)
|
|
|
|
};
|
|
|
|
|
2018-12-02 02:23:42 +01:00
|
|
|
// When shortpoll was successful, do nothing else.
|
|
|
|
if(shortpolled)
|
|
|
|
return {};
|
2018-09-01 11:57:15 +02:00
|
|
|
|
2018-12-02 02:23:42 +01:00
|
|
|
// When longpoll was successful, do nothing else.
|
|
|
|
if(longpoll::poll(client, args))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
// A user-timeout occurred. According to the spec we return a
|
|
|
|
// 200 with empty fields rather than a 408.
|
|
|
|
const json::value next_batch
|
|
|
|
{
|
|
|
|
lex_cast(m::vm::current_sequence), json::STRING
|
|
|
|
};
|
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, json::members
|
|
|
|
{
|
|
|
|
{ "next_batch", next_batch },
|
|
|
|
{ "rooms", json::object{} },
|
|
|
|
{ "presence", json::object{} },
|
|
|
|
}
|
|
|
|
};
|
2018-04-22 08:50:16 +02:00
|
|
|
}
|
|
|
|
catch(const bad_lex_cast &e)
|
|
|
|
{
|
|
|
|
throw m::BAD_REQUEST
|
2018-04-17 02:57:41 +02:00
|
|
|
{
|
2018-04-22 08:50:16 +02:00
|
|
|
"Since parameter invalid :%s", e.what()
|
2018-04-17 02:57:41 +02:00
|
|
|
};
|
2018-04-22 08:50:16 +02:00
|
|
|
}
|
2018-04-17 02:57:41 +02:00
|
|
|
|
2018-10-07 07:17:46 +02:00
|
|
|
//
|
|
|
|
// longpoll
|
|
|
|
//
|
|
|
|
|
|
|
|
decltype(ircd::m::sync::longpoll::notified)
|
|
|
|
ircd::m::sync::longpoll::notified
|
|
|
|
{
|
|
|
|
handle_notify,
|
|
|
|
{
|
|
|
|
{ "_site", "vm.notify" },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::sync::longpoll::handle_notify(const m::event &event,
|
|
|
|
m::vm::eval &eval)
|
|
|
|
{
|
|
|
|
assert(eval.opts);
|
|
|
|
if(!eval.opts->notify_clients)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!polling)
|
2018-11-16 03:36:26 +01:00
|
|
|
{
|
|
|
|
queue.clear();
|
2018-10-07 07:17:46 +02:00
|
|
|
return;
|
2018-11-16 03:36:26 +01:00
|
|
|
}
|
2018-10-07 07:17:46 +02:00
|
|
|
|
|
|
|
queue.emplace_back(eval);
|
|
|
|
dock.notify_all();
|
|
|
|
}
|
|
|
|
|
2018-12-02 02:23:42 +01:00
|
|
|
bool
|
2018-09-01 09:51:40 +02:00
|
|
|
ircd::m::sync::longpoll::poll(client &client,
|
|
|
|
const args &args)
|
2017-09-25 03:05:42 +02:00
|
|
|
{
|
2018-10-07 07:17:46 +02:00
|
|
|
++polling;
|
|
|
|
const unwind unpoll{[]
|
2017-09-26 06:42:07 +02:00
|
|
|
{
|
2018-10-07 07:17:46 +02:00
|
|
|
--polling;
|
|
|
|
}};
|
2017-09-25 03:05:42 +02:00
|
|
|
|
2018-04-22 08:50:16 +02:00
|
|
|
while(1)
|
2017-09-26 06:42:07 +02:00
|
|
|
{
|
2018-11-16 03:36:26 +01:00
|
|
|
if(!dock.wait_until(args.timesout))
|
2018-12-02 02:23:42 +01:00
|
|
|
return false;
|
2018-11-16 03:36:26 +01:00
|
|
|
|
2018-10-07 07:17:46 +02:00
|
|
|
if(queue.empty())
|
2018-05-28 04:56:04 +02:00
|
|
|
continue;
|
|
|
|
|
2018-10-07 07:17:46 +02:00
|
|
|
const auto &a(queue.front());
|
|
|
|
const unwind pop{[]
|
|
|
|
{
|
2018-11-16 03:36:26 +01:00
|
|
|
if(polling <= 1)
|
|
|
|
queue.pop_front();
|
2018-10-07 07:17:46 +02:00
|
|
|
}};
|
|
|
|
|
|
|
|
if(handle(client, args, a))
|
2018-12-02 02:23:42 +01:00
|
|
|
return true;
|
2018-04-11 00:20:47 +02:00
|
|
|
}
|
|
|
|
}
|
2017-09-25 03:05:42 +02:00
|
|
|
|
2018-04-22 08:50:16 +02:00
|
|
|
bool
|
2018-09-01 09:51:40 +02:00
|
|
|
ircd::m::sync::longpoll::handle(client &client,
|
|
|
|
const args &args,
|
2018-10-07 07:17:46 +02:00
|
|
|
const accepted &event)
|
2017-09-26 06:42:07 +02:00
|
|
|
{
|
|
|
|
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)
|
2018-09-01 09:51:40 +02:00
|
|
|
{
|
|
|
|
const m::room room{room_id};
|
2018-09-01 11:57:15 +02:00
|
|
|
return handle(client, args, event, room);
|
2018-09-01 09:51:40 +02:00
|
|
|
}
|
2018-04-22 08:50:16 +02:00
|
|
|
|
|
|
|
return false;
|
2017-09-26 06:42:07 +02:00
|
|
|
}
|
|
|
|
|
2018-04-22 08:50:16 +02:00
|
|
|
bool
|
2018-09-01 09:51:40 +02:00
|
|
|
ircd::m::sync::longpoll::handle(client &client,
|
|
|
|
const args &args,
|
2018-10-07 07:17:46 +02:00
|
|
|
const accepted &event,
|
2018-09-01 09:51:40 +02:00
|
|
|
const m::room &room)
|
2017-09-26 06:42:07 +02:00
|
|
|
{
|
2018-04-22 08:50:16 +02:00
|
|
|
const m::user::id &user_id
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-09-01 11:57:15 +02:00
|
|
|
args.request.user_id
|
2018-04-22 08:50:16 +02:00
|
|
|
};
|
2017-10-03 13:12:54 +02:00
|
|
|
|
2018-04-22 08:50:16 +02:00
|
|
|
if(!room.membership(user_id, "join"))
|
|
|
|
return false;
|
2018-04-11 00:20:47 +02:00
|
|
|
|
2018-09-01 09:51:40 +02:00
|
|
|
const auto rooms
|
|
|
|
{
|
2018-09-01 11:57:15 +02:00
|
|
|
sync_rooms(client, user_id, room, args, event)
|
2018-09-01 09:51:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const m::user::room ur
|
|
|
|
{
|
2018-09-01 11:57:15 +02:00
|
|
|
m::user::id
|
|
|
|
{
|
|
|
|
args.request.user_id
|
|
|
|
}
|
2018-09-01 09:51:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<json::value> presents;
|
|
|
|
ur.get(std::nothrow, "ircd.presence", [&]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
const auto &content
|
|
|
|
{
|
|
|
|
at<"content"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
presents.emplace_back(event);
|
|
|
|
});
|
|
|
|
|
|
|
|
const json::members presence
|
|
|
|
{
|
|
|
|
{ "events", json::value { presents.data(), presents.size() } },
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &next_batch
|
|
|
|
{
|
|
|
|
int64_t(m::vm::current_sequence)
|
|
|
|
};
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
{
|
|
|
|
client, json::members
|
|
|
|
{
|
|
|
|
{ "next_batch", json::value { lex_cast(next_batch), json::STRING } },
|
|
|
|
{ "rooms", rooms },
|
|
|
|
{ "presence", presence },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-22 08:50:16 +02:00
|
|
|
return true;
|
2017-10-03 13:12:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
2018-09-01 09:51:40 +02:00
|
|
|
ircd::m::sync::longpoll::sync_rooms(client &client,
|
|
|
|
const m::user::id &user_id,
|
|
|
|
const m::room &room,
|
|
|
|
const args &args,
|
2018-10-07 07:17:46 +02:00
|
|
|
const accepted &event)
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-09-13 10:45:12 +02:00
|
|
|
std::vector<std::string> r;
|
|
|
|
std::vector<json::member> m;
|
|
|
|
|
|
|
|
thread_local char membership_buf[64];
|
|
|
|
const auto membership
|
|
|
|
{
|
|
|
|
room.membership(membership_buf, user_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
r.emplace_back(sync_room(client, room, args, event));
|
|
|
|
m.emplace_back(room.room_id, r.back());
|
|
|
|
|
|
|
|
const json::strung body
|
|
|
|
{
|
|
|
|
m.data(), m.data() + m.size()
|
|
|
|
};
|
|
|
|
|
|
|
|
return json::strung{json::members
|
|
|
|
{
|
|
|
|
{ membership, body }
|
|
|
|
}};
|
2018-09-01 09:51:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::m::sync::longpoll::sync_room(client &client,
|
|
|
|
const m::room &room,
|
|
|
|
const args &args,
|
2018-10-07 07:17:46 +02:00
|
|
|
const accepted &accepted)
|
2018-09-01 09:51:40 +02:00
|
|
|
{
|
|
|
|
const auto &since
|
|
|
|
{
|
|
|
|
args.since
|
|
|
|
};
|
|
|
|
|
2018-09-06 04:32:14 +02:00
|
|
|
const m::event &event{accepted};
|
2017-10-03 13:12:54 +02:00
|
|
|
std::vector<std::string> timeline;
|
2018-09-07 08:08:20 +02:00
|
|
|
if(defined(json::get<"event_id"_>(event)))
|
2018-09-06 04:32:14 +02:00
|
|
|
{
|
|
|
|
json::strung strung(event);
|
2018-10-07 07:17:46 +02:00
|
|
|
if(!!accepted.client_txnid)
|
2018-09-06 04:32:14 +02:00
|
|
|
strung = json::insert(strung, json::member
|
|
|
|
{
|
|
|
|
"unsigned", json::members
|
|
|
|
{
|
2018-10-07 07:17:46 +02:00
|
|
|
{ "transaction_id", accepted.client_txnid }
|
2018-09-06 04:32:14 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
timeline.emplace_back(std::move(strung));
|
|
|
|
}
|
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;
|
2018-04-22 08:50:16 +02:00
|
|
|
if(json::get<"type"_>(event) == "m.typing") //TODO: X
|
|
|
|
ephemeral.emplace_back(json::strung{event});
|
|
|
|
|
|
|
|
if(json::get<"type"_>(event) == "m.receipt") //TODO: X
|
|
|
|
ephemeral.emplace_back(json::strung(event));
|
|
|
|
|
2018-02-12 23:44:46 +01:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2018-10-14 12:17:21 +02:00
|
|
|
m::event::id::buf last_read_buf;
|
|
|
|
const auto &last_read
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
2018-10-14 12:17:21 +02:00
|
|
|
m::receipt::read(last_read_buf, room.room_id, args.request.user_id)
|
2018-09-07 15:25:40 +02:00
|
|
|
};
|
|
|
|
|
2018-09-14 15:21:19 +02:00
|
|
|
const auto last_read_idx
|
2018-09-07 15:25:40 +02:00
|
|
|
{
|
|
|
|
last_read && json::get<"event_id"_>(event)?
|
2018-09-14 15:21:19 +02:00
|
|
|
index(last_read):
|
|
|
|
0UL
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto current_idx
|
|
|
|
{
|
|
|
|
last_read_idx?
|
|
|
|
index(at<"event_id"_>(event)):
|
2018-09-07 15:25:40 +02:00
|
|
|
0UL
|
2018-04-22 08:50:16 +02:00
|
|
|
};
|
|
|
|
|
2018-09-14 15:21:19 +02:00
|
|
|
const auto notes
|
|
|
|
{
|
|
|
|
last_read_idx?
|
|
|
|
notification_count(room, last_read_idx, current_idx):
|
|
|
|
json::undefined_number
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto highlights
|
|
|
|
{
|
|
|
|
last_read_idx?
|
|
|
|
highlight_count(room, args.request.user_id, last_read_idx, current_idx):
|
|
|
|
json::undefined_number
|
|
|
|
};
|
|
|
|
|
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-09-14 15:21:19 +02:00
|
|
|
{ "highlight_count", highlights },
|
|
|
|
{ "notification_count", notes },
|
2018-02-12 23:44:46 +01:00
|
|
|
}},
|
|
|
|
{ "ephemeral",
|
|
|
|
{
|
|
|
|
{ "events", ephemeral_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
|
|
|
}
|
|
|
|
|
2018-10-07 07:17:46 +02:00
|
|
|
//
|
|
|
|
// linear
|
|
|
|
//
|
|
|
|
|
2018-11-05 03:11:37 +01:00
|
|
|
decltype(ircd::m::sync::linear::delta_max)
|
|
|
|
ircd::m::sync::linear::delta_max
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.sync.linear.delta.max" },
|
|
|
|
{ "default", 1024 },
|
|
|
|
};
|
|
|
|
|
2018-09-01 09:51:40 +02:00
|
|
|
bool
|
|
|
|
ircd::m::sync::linear::handle(client &client,
|
|
|
|
shortpoll &sp,
|
|
|
|
json::stack::object &object)
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-09-01 09:51:40 +02:00
|
|
|
uint64_t since
|
|
|
|
{
|
|
|
|
sp.since
|
|
|
|
};
|
2017-10-03 13:12:54 +02:00
|
|
|
|
2018-09-01 09:51:40 +02:00
|
|
|
std::map<std::string, std::vector<std::string>, std::less<>> r;
|
|
|
|
|
|
|
|
bool limited{false};
|
|
|
|
m::events::for_each(since, [&]
|
|
|
|
(const uint64_t &sequence, const m::event &event)
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-09-01 09:51:40 +02:00
|
|
|
if(!r.empty() && (since - sp.since > 128))
|
|
|
|
{
|
|
|
|
limited = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
since = sequence;
|
|
|
|
if(!json::get<"room_id"_>(event))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const m::room room
|
|
|
|
{
|
|
|
|
json::get<"room_id"_>(event)
|
|
|
|
};
|
|
|
|
|
2018-09-01 11:57:15 +02:00
|
|
|
if(!room.membership(sp.args.request.user_id))
|
2018-09-01 09:51:40 +02:00
|
|
|
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;
|
2017-10-03 13:12:54 +02:00
|
|
|
});
|
2017-09-26 06:42:07 +02:00
|
|
|
|
2018-09-01 09:51:40 +02:00
|
|
|
if(r.empty())
|
|
|
|
return false;
|
2017-10-03 13:12:54 +02:00
|
|
|
|
2018-09-13 10:45:12 +02:00
|
|
|
std::vector<json::member> events[3];
|
2018-04-11 00:20:47 +02:00
|
|
|
|
2018-09-01 09:51:40 +02:00
|
|
|
for(auto &p : r)
|
2018-04-11 00:20:47 +02:00
|
|
|
{
|
2018-09-14 15:21:19 +02:00
|
|
|
const m::room::id &room_id{p.first};
|
2018-09-01 09:51:40 +02:00
|
|
|
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()};
|
|
|
|
|
2018-10-14 12:17:21 +02:00
|
|
|
m::event::id::buf last_read_buf;
|
|
|
|
const auto &last_read
|
2018-09-07 15:25:40 +02:00
|
|
|
{
|
2018-10-14 12:17:21 +02:00
|
|
|
m::receipt::read(last_read_buf, room_id, sp.user)
|
2018-09-07 15:25:40 +02:00
|
|
|
};
|
|
|
|
|
2018-09-14 15:21:19 +02:00
|
|
|
const auto last_read_idx
|
2018-09-07 15:25:40 +02:00
|
|
|
{
|
|
|
|
last_read?
|
2018-09-14 15:21:19 +02:00
|
|
|
index(last_read):
|
2018-09-07 15:25:40 +02:00
|
|
|
0UL
|
|
|
|
};
|
|
|
|
|
2018-09-14 15:21:19 +02:00
|
|
|
const auto notes
|
|
|
|
{
|
|
|
|
last_read_idx?
|
|
|
|
notification_count(room_id, last_read_idx, sp.current):
|
|
|
|
json::undefined_number
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto highlights
|
|
|
|
{
|
|
|
|
last_read_idx?
|
|
|
|
highlight_count(room_id, sp.user, last_read_idx, sp.current):
|
|
|
|
json::undefined_number
|
|
|
|
};
|
|
|
|
|
2018-09-01 09:51:40 +02:00
|
|
|
const string_view prev_batch
|
2018-04-11 00:20:47 +02:00
|
|
|
{
|
2018-09-01 09:51:40 +02:00
|
|
|
!timeline.empty()?
|
|
|
|
unquote(json::object{timeline.front()}.at("event_id")):
|
|
|
|
string_view{}
|
2018-04-11 00:20:47 +02:00
|
|
|
};
|
|
|
|
|
2018-09-01 09:51:40 +02:00
|
|
|
const json::members body
|
|
|
|
{
|
|
|
|
{ "ephemeral",
|
|
|
|
{
|
|
|
|
{ "events", ephemeral_serial },
|
|
|
|
}},
|
|
|
|
{ "state",
|
|
|
|
{
|
|
|
|
{ "events", state_serial }
|
|
|
|
}},
|
|
|
|
{ "timeline",
|
|
|
|
{
|
|
|
|
{ "events", timeline_serial },
|
|
|
|
{ "prev_batch", prev_batch },
|
|
|
|
{ "limited", limited },
|
|
|
|
}},
|
2018-09-07 15:25:40 +02:00
|
|
|
{ "unread_notifications",
|
|
|
|
{
|
2018-09-14 15:21:19 +02:00
|
|
|
{ "highlight_count", highlights },
|
|
|
|
{ "notification_count", notes },
|
2018-09-07 15:25:40 +02:00
|
|
|
}},
|
2018-09-01 09:51:40 +02:00
|
|
|
};
|
2018-04-11 00:20:47 +02:00
|
|
|
|
2018-09-13 10:45:12 +02:00
|
|
|
thread_local char membership_buf[64];
|
|
|
|
const auto membership{m::room{room_id}.membership(membership_buf, sp.user)};
|
|
|
|
const int ep
|
|
|
|
{
|
|
|
|
membership == "join"? 0:
|
|
|
|
membership == "leave"? 1:
|
|
|
|
membership == "invite"? 2:
|
|
|
|
1 // default to leave (catches "ban" for now)
|
|
|
|
};
|
|
|
|
|
|
|
|
events[ep].emplace_back(room_id, body);
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::value joinsv
|
|
|
|
{
|
|
|
|
events[0].data(), events[0].size()
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::value leavesv
|
|
|
|
{
|
|
|
|
events[1].data(), events[1].size()
|
2018-09-01 09:51:40 +02:00
|
|
|
};
|
|
|
|
|
2018-09-13 10:45:12 +02:00
|
|
|
const json::value invitesv
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-09-13 10:45:12 +02:00
|
|
|
events[2].data(), events[2].size()
|
2017-10-03 13:12:54 +02:00
|
|
|
};
|
|
|
|
|
2018-09-01 09:51:40 +02:00
|
|
|
const json::members rooms
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-09-13 10:45:12 +02:00
|
|
|
{ "join", joinsv },
|
|
|
|
{ "leave", leavesv },
|
|
|
|
{ "invite", invitesv },
|
2017-10-03 13:12:54 +02:00
|
|
|
};
|
|
|
|
|
2018-09-01 09:51:40 +02:00
|
|
|
resource::response
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-04-22 08:50:16 +02:00
|
|
|
client, json::members
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-09-01 09:51:40 +02:00
|
|
|
{ "next_batch", json::value { lex_cast(int64_t(since)), json::STRING } },
|
|
|
|
{ "rooms", rooms },
|
|
|
|
{ "presence", json::object{} },
|
2017-10-03 13:12:54 +02:00
|
|
|
}
|
|
|
|
};
|
2018-04-22 08:50:16 +02:00
|
|
|
|
2018-09-01 09:51:40 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-04-22 08:50:16 +02:00
|
|
|
|
2018-10-07 07:17:46 +02:00
|
|
|
//
|
|
|
|
// polylog
|
|
|
|
//
|
|
|
|
|
2018-10-16 09:06:44 +02:00
|
|
|
ircd::conf::item<bool>
|
|
|
|
ircd::m::sync::polylog::prefetch_state
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.sync.polylog.prefetch.state" },
|
|
|
|
{ "default", true },
|
|
|
|
};
|
|
|
|
|
|
|
|
ircd::conf::item<bool>
|
|
|
|
ircd::m::sync::polylog::prefetch_timeline
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.sync.polylog.prefetch.timeline" },
|
|
|
|
{ "default", true },
|
|
|
|
};
|
|
|
|
|
2018-04-22 08:50:16 +02:00
|
|
|
bool
|
2018-09-01 09:51:40 +02:00
|
|
|
ircd::m::sync::polylog::handle(client &client,
|
|
|
|
shortpoll &sp,
|
|
|
|
json::stack::object &object)
|
2018-05-14 04:24:33 +02:00
|
|
|
try
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
2018-10-18 16:44:20 +02:00
|
|
|
// Generate individual stats for sections
|
|
|
|
thread_local char iecbuf[64], rembuf[128];
|
|
|
|
sync::stats stats{sp.stats};
|
|
|
|
stats.timer = timer{};
|
|
|
|
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
|
|
|
json::stack::member member{object, "rooms"};
|
|
|
|
json::stack::object object{member};
|
2018-09-01 09:51:40 +02:00
|
|
|
rooms(sp, object);
|
2018-04-22 08:50:16 +02:00
|
|
|
}
|
|
|
|
|
2018-10-18 16:44:20 +02:00
|
|
|
#ifdef RB_DEBUG
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "polylog %s %s rooms %s wc:%zu in %lu$ms",
|
|
|
|
string(rembuf, ircd::remote(sp.client)),
|
|
|
|
string_view{sp.request.user_id},
|
|
|
|
pretty(iecbuf, iec(sp.stats.flush_bytes - stats.flush_bytes)),
|
|
|
|
sp.stats.flush_count - stats.flush_count,
|
|
|
|
stats.timer.at<milliseconds>().count()
|
|
|
|
};
|
|
|
|
stats = sync::stats{sp.stats};
|
|
|
|
stats.timer = timer{};
|
|
|
|
#endif
|
|
|
|
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
|
|
|
json::stack::member member{object, "presence"};
|
|
|
|
json::stack::object object{member};
|
2018-09-01 09:51:40 +02:00
|
|
|
presence(sp, object);
|
2018-04-22 08:50:16 +02:00
|
|
|
}
|
|
|
|
|
2018-10-18 16:44:20 +02:00
|
|
|
#ifdef RB_DEBUG
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "polylog %s %s presence %s wc:%zu in %lu$ms",
|
|
|
|
string(rembuf, ircd::remote(sp.client)),
|
|
|
|
string_view{sp.request.user_id},
|
|
|
|
pretty(iecbuf, iec(sp.stats.flush_bytes - stats.flush_bytes)),
|
|
|
|
sp.stats.flush_count - stats.flush_count,
|
|
|
|
stats.timer.at<milliseconds>().count()
|
|
|
|
};
|
|
|
|
stats = sync::stats{sp.stats};
|
|
|
|
stats.timer = timer{};
|
|
|
|
#endif
|
|
|
|
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
|
|
|
json::stack::member member{object, "account_data"};
|
|
|
|
json::stack::object object{member};
|
2018-09-01 09:51:40 +02:00
|
|
|
account_data(sp, object);
|
2018-04-22 08:50:16 +02:00
|
|
|
}
|
|
|
|
|
2018-10-18 16:44:20 +02:00
|
|
|
#ifdef RB_DEBUG
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "polylog %s %s account_data %s wc:%zu in %lu$ms",
|
|
|
|
string(rembuf, ircd::remote(sp.client)),
|
|
|
|
string_view{sp.request.user_id},
|
|
|
|
pretty(iecbuf, iec(sp.stats.flush_bytes - stats.flush_bytes)),
|
|
|
|
sp.stats.flush_count - stats.flush_count,
|
|
|
|
stats.timer.at<milliseconds>().count()
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
|
|
|
json::stack::member member
|
|
|
|
{
|
2018-05-18 04:31:38 +02:00
|
|
|
object, "next_batch", json::value(lex_cast(int64_t(sp.current)), json::STRING)
|
2018-04-22 08:50:16 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-10-21 09:24:26 +02:00
|
|
|
log::info
|
2018-10-18 16:44:20 +02:00
|
|
|
{
|
|
|
|
log, "polylog %s %s %s wc:%zu in %lu$ms",
|
|
|
|
string(rembuf, ircd::remote(sp.client)),
|
|
|
|
string_view{sp.request.user_id},
|
|
|
|
pretty(iecbuf, iec(sp.stats.flush_bytes)),
|
|
|
|
sp.stats.flush_count,
|
|
|
|
sp.stats.timer.at<milliseconds>().count()
|
|
|
|
};
|
2018-09-01 09:51:40 +02:00
|
|
|
|
2018-04-22 08:50:16 +02:00
|
|
|
return sp.committed;
|
|
|
|
}
|
2018-05-14 04:24:33 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2018-10-18 16:44:20 +02:00
|
|
|
log, "polylog sync FAILED %lu to %lu (vm @ %zu) :%s"
|
2018-05-14 04:24:33 +02:00
|
|
|
,sp.since
|
|
|
|
,sp.current
|
|
|
|
,m::vm::current_sequence
|
|
|
|
,e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2018-04-22 08:50:16 +02:00
|
|
|
|
|
|
|
void
|
2018-09-01 09:51:40 +02:00
|
|
|
ircd::m::sync::polylog::presence(shortpoll &sp,
|
|
|
|
json::stack::object &out)
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
|
|
|
json::stack::member member{out, "events"};
|
|
|
|
json::stack::array array{member};
|
2018-09-01 09:51:40 +02:00
|
|
|
|
2018-04-22 08:50:16 +02:00
|
|
|
const m::user::mitsein mitsein
|
|
|
|
{
|
|
|
|
sp.user
|
|
|
|
};
|
|
|
|
|
|
|
|
mitsein.for_each("join", [&sp, &array]
|
|
|
|
(const m::user &user)
|
|
|
|
{
|
|
|
|
const m::user::room user_room{user};
|
2018-05-18 04:32:26 +02:00
|
|
|
if(head_idx(std::nothrow, user_room) <= sp.since)
|
2018-04-22 08:50:16 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
//TODO: can't check event_idx cuz only closed presence content
|
|
|
|
m::presence::get(std::nothrow, user, [&sp, &array]
|
|
|
|
(const json::object &event)
|
|
|
|
{
|
|
|
|
json::stack::object object{array};
|
|
|
|
|
|
|
|
// sender
|
|
|
|
{
|
|
|
|
json::stack::member member
|
|
|
|
{
|
|
|
|
object, "sender", unquote(event.get("user_id"))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// type
|
|
|
|
{
|
|
|
|
json::stack::member member
|
|
|
|
{
|
|
|
|
object, "type", json::value{"m.presence"}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// content
|
|
|
|
{
|
|
|
|
json::stack::member member
|
|
|
|
{
|
|
|
|
object, "content", event
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-09-26 06:42:07 +02:00
|
|
|
}
|
2018-04-22 08:50:16 +02:00
|
|
|
|
|
|
|
void
|
2018-09-01 09:51:40 +02:00
|
|
|
ircd::m::sync::polylog::account_data(shortpoll &sp,
|
|
|
|
json::stack::object &out)
|
2018-09-23 00:18:56 +02:00
|
|
|
try
|
2017-09-26 06:42:07 +02:00
|
|
|
{
|
2018-04-22 08:50:16 +02:00
|
|
|
json::stack::member member{out, "events"};
|
|
|
|
json::stack::array array{member};
|
|
|
|
const m::room::state state
|
|
|
|
{
|
|
|
|
sp.user_room
|
|
|
|
};
|
|
|
|
|
|
|
|
state.for_each("ircd.account_data", [&sp, &array]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
const auto &event_idx
|
|
|
|
{
|
2018-05-18 04:32:26 +02:00
|
|
|
index(event, std::nothrow)
|
2018-04-22 08:50:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(event_idx < sp.since || event_idx >= sp.current)
|
|
|
|
return;
|
|
|
|
|
|
|
|
json::stack::object object{array};
|
|
|
|
|
|
|
|
// type
|
|
|
|
{
|
|
|
|
json::stack::member member
|
|
|
|
{
|
|
|
|
object, "type", at<"state_key"_>(event)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// content
|
|
|
|
{
|
|
|
|
json::stack::member member
|
|
|
|
{
|
|
|
|
object, "content", at<"content"_>(event)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-09-23 00:18:56 +02:00
|
|
|
catch(const json::not_found &e)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
2018-10-18 16:44:20 +02:00
|
|
|
log, "polylog sync account data error %lu to %lu (vm @ %zu) :%s"
|
2018-09-23 00:18:56 +02:00
|
|
|
,sp.since
|
|
|
|
,sp.current
|
|
|
|
,m::vm::current_sequence
|
|
|
|
,e.what()
|
|
|
|
};
|
|
|
|
}
|
2018-04-22 08:50:16 +02:00
|
|
|
|
|
|
|
void
|
2018-09-01 09:51:40 +02:00
|
|
|
ircd::m::sync::polylog::rooms(shortpoll &sp,
|
|
|
|
json::stack::object &object)
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
2018-10-14 12:17:21 +02:00
|
|
|
sync_rooms(sp, object, "invite");
|
|
|
|
sync_rooms(sp, object, "join");
|
|
|
|
sync_rooms(sp, object, "leave");
|
|
|
|
sync_rooms(sp, object, "ban");
|
2018-04-22 08:50:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-10-14 12:17:21 +02:00
|
|
|
ircd::m::sync::polylog::sync_rooms(shortpoll &sp,
|
|
|
|
json::stack::object &out,
|
|
|
|
const string_view &membership)
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
2018-10-14 12:17:21 +02:00
|
|
|
json::stack::member rooms_member{out, membership};
|
|
|
|
json::stack::object rooms_object{rooms_member};
|
|
|
|
sp.rooms.for_each(membership, [&sp, &rooms_object]
|
|
|
|
(const m::room &room, const string_view &membership)
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
2018-05-18 04:32:26 +02:00
|
|
|
if(head_idx(std::nothrow, room) <= sp.since)
|
2018-04-22 08:50:16 +02:00
|
|
|
return;
|
|
|
|
|
2018-10-18 16:44:20 +02:00
|
|
|
// Generate individual stats for this room's sync
|
|
|
|
#ifdef RB_DEBUG
|
|
|
|
sync::stats stats{sp.stats};
|
|
|
|
stats.timer = timer{};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// This scope ensures the object destructs and flushes before
|
|
|
|
// the log message tallying the stats for this room below.
|
|
|
|
{
|
|
|
|
json::stack::member member{rooms_object, room.room_id};
|
|
|
|
json::stack::object object{member};
|
|
|
|
sync_room(sp, object, room, membership);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef RB_DEBUG
|
|
|
|
thread_local char iecbuf[64], rembuf[128];
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "polylog %s %s %s %s wc:%zu in %lu$ms",
|
|
|
|
string(rembuf, ircd::remote(sp.client)),
|
|
|
|
string_view{sp.request.user_id},
|
|
|
|
string_view{room.room_id},
|
|
|
|
pretty(iecbuf, iec(sp.stats.flush_bytes - stats.flush_bytes)),
|
|
|
|
sp.stats.flush_count - stats.flush_count,
|
|
|
|
stats.timer.at<milliseconds>().count()
|
|
|
|
};
|
|
|
|
#endif
|
2018-04-22 08:50:16 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-09-01 09:51:40 +02:00
|
|
|
ircd::m::sync::polylog::sync_room(shortpoll &sp,
|
|
|
|
json::stack::object &out,
|
|
|
|
const m::room &room,
|
|
|
|
const string_view &membership)
|
|
|
|
try
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
|
|
|
// timeline
|
|
|
|
{
|
|
|
|
json::stack::member member{out, "timeline"};
|
|
|
|
json::stack::object object{member};
|
2018-09-01 09:51:40 +02:00
|
|
|
room_timeline(sp, object, room);
|
2018-04-22 08:50:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// state
|
|
|
|
{
|
|
|
|
json::stack::member member
|
|
|
|
{
|
|
|
|
out, membership != "invite"?
|
|
|
|
"state":
|
|
|
|
"invite_state"
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object object{member};
|
2018-09-01 09:51:40 +02:00
|
|
|
room_state(sp, object, room);
|
2018-04-22 08:50:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ephemeral
|
|
|
|
{
|
|
|
|
json::stack::member member{out, "ephemeral"};
|
|
|
|
json::stack::object object{member};
|
2018-09-01 09:51:40 +02:00
|
|
|
room_ephemeral(sp, object, room);
|
2018-04-22 08:50:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// account_data
|
|
|
|
{
|
|
|
|
json::stack::member member{out, "account_data"};
|
|
|
|
json::stack::object object{member};
|
2018-09-01 09:51:40 +02:00
|
|
|
room_account_data(sp, object, room);
|
2018-04-22 08:50:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// unread_notifications
|
|
|
|
{
|
|
|
|
json::stack::member member{out, "unread_notifications"};
|
|
|
|
json::stack::object object{member};
|
2018-09-01 09:51:40 +02:00
|
|
|
room_unread_notifications(sp, object, room);
|
2018-04-22 08:50:16 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-01 09:51:40 +02:00
|
|
|
catch(const json::not_found &e)
|
|
|
|
{
|
2018-10-14 12:17:21 +02:00
|
|
|
log::critical
|
2018-09-01 09:51:40 +02:00
|
|
|
{
|
2018-10-18 16:44:20 +02:00
|
|
|
log, "polylog sync room %s error %lu to %lu (vm @ %zu) :%s"
|
2018-09-01 09:51:40 +02:00
|
|
|
,string_view{room.room_id}
|
|
|
|
,sp.since
|
|
|
|
,sp.current
|
|
|
|
,m::vm::current_sequence
|
|
|
|
,e.what()
|
|
|
|
};
|
|
|
|
}
|
2018-04-22 08:50:16 +02:00
|
|
|
|
|
|
|
void
|
2018-09-01 09:51:40 +02:00
|
|
|
ircd::m::sync::polylog::room_state(shortpoll &sp,
|
|
|
|
json::stack::object &out,
|
|
|
|
const m::room &room)
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
2018-05-22 10:13:38 +02:00
|
|
|
static const m::event::fetch::opts fopts
|
|
|
|
{
|
|
|
|
m::event::keys::include
|
|
|
|
{
|
|
|
|
"content",
|
|
|
|
"depth",
|
|
|
|
"event_id",
|
|
|
|
"origin_server_ts",
|
|
|
|
"redacts",
|
|
|
|
"room_id",
|
|
|
|
"sender",
|
|
|
|
"state_key",
|
|
|
|
"type",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-04-22 08:50:16 +02:00
|
|
|
json::stack::member member
|
|
|
|
{
|
|
|
|
out, "events"
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::array array
|
|
|
|
{
|
|
|
|
member
|
|
|
|
};
|
|
|
|
|
2018-05-22 10:13:38 +02:00
|
|
|
m::room::state state
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
2018-12-19 23:57:44 +01:00
|
|
|
room
|
2018-04-22 08:50:16 +02:00
|
|
|
};
|
|
|
|
|
2018-10-16 09:06:44 +02:00
|
|
|
if(bool(prefetch_state))
|
2018-10-23 18:54:45 +02:00
|
|
|
state.prefetch(sp.since, sp.current);
|
2018-09-01 14:10:28 +02:00
|
|
|
|
2018-12-19 23:57:44 +01:00
|
|
|
state.for_each([&sp, &array]
|
|
|
|
(const m::event::idx &event_idx)
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
2018-12-19 23:57:44 +01:00
|
|
|
if(event_idx < sp.since || event_idx >= sp.current)
|
2018-04-22 08:50:16 +02:00
|
|
|
return;
|
|
|
|
|
2018-12-19 23:57:44 +01:00
|
|
|
const event::fetch event
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
2018-12-19 23:57:44 +01:00
|
|
|
event_idx, std::nothrow, &fopts
|
2018-04-22 08:50:16 +02:00
|
|
|
};
|
|
|
|
|
2018-12-19 23:57:44 +01:00
|
|
|
if(!event.valid || at<"depth"_>(event) >= int64_t(sp.state_at))
|
2018-04-22 08:50:16 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
array.append(event);
|
|
|
|
sp.committed = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-09-01 09:51:40 +02:00
|
|
|
ircd::m::sync::polylog::room_timeline(shortpoll &sp,
|
|
|
|
json::stack::object &out,
|
|
|
|
const m::room &room)
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
|
|
|
// events
|
|
|
|
bool limited{false};
|
|
|
|
m::event::id::buf prev;
|
|
|
|
{
|
|
|
|
json::stack::member member{out, "events"};
|
|
|
|
json::stack::array array{member};
|
2018-09-01 09:51:40 +02:00
|
|
|
prev = room_timeline_events(sp, array, room, limited);
|
2018-04-22 08:50:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// prev_batch
|
|
|
|
{
|
|
|
|
json::stack::member member
|
|
|
|
{
|
|
|
|
out, "prev_batch", string_view{prev}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// limited
|
|
|
|
{
|
|
|
|
json::stack::member member
|
|
|
|
{
|
|
|
|
out, "limited", json::value{limited}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-01 09:51:40 +02:00
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::sync::polylog::room_timeline_events(shortpoll &sp,
|
|
|
|
json::stack::array &out,
|
|
|
|
const m::room &room,
|
|
|
|
bool &limited)
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
2018-05-22 10:13:38 +02:00
|
|
|
static const m::event::fetch::opts fopts
|
|
|
|
{
|
|
|
|
m::event::keys::include
|
|
|
|
{
|
|
|
|
"content",
|
|
|
|
"depth",
|
|
|
|
"event_id",
|
|
|
|
"origin_server_ts",
|
|
|
|
"prev_events",
|
|
|
|
"redacts",
|
|
|
|
"room_id",
|
|
|
|
"sender",
|
|
|
|
"state_key",
|
|
|
|
"type",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-04-22 08:50:16 +02:00
|
|
|
// messages seeks to the newest event, but the client wants the oldest
|
|
|
|
// event first so we seek down first and then iterate back up. Due to
|
|
|
|
// an issue with rocksdb's prefix-iteration this iterator becomes
|
|
|
|
// toxic as soon as it becomes invalid. As a result we have to copy the
|
|
|
|
// event_id on the way down in case of renewing the iterator for the
|
|
|
|
// way back. This is not a big deal but rocksdb should fix their shit.
|
|
|
|
ssize_t i(0);
|
|
|
|
m::event::id::buf event_id;
|
2018-05-22 10:13:38 +02:00
|
|
|
m::room::messages it
|
|
|
|
{
|
|
|
|
room, &fopts
|
|
|
|
};
|
|
|
|
|
2018-04-22 08:50:16 +02:00
|
|
|
for(; it && i < 10; --it, ++i)
|
|
|
|
{
|
|
|
|
event_id = it.event_id();
|
|
|
|
if(it.event_idx() < sp.since)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(it.event_idx() >= sp.current)
|
|
|
|
break;
|
2018-09-01 14:10:28 +02:00
|
|
|
|
2018-10-16 09:06:44 +02:00
|
|
|
if(bool(prefetch_timeline))
|
|
|
|
m::prefetch(it.event_idx(), fopts);
|
2018-04-22 08:50:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
limited = i >= 10;
|
|
|
|
sp.committed |= i > 0;
|
|
|
|
|
|
|
|
if(i > 0 && !it)
|
|
|
|
it.seek(event_id);
|
|
|
|
|
|
|
|
if(i > 0 && it)
|
|
|
|
{
|
|
|
|
const m::event &event{*it};
|
|
|
|
sp.state_at = at<"depth"_>(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(i > 0)
|
|
|
|
for(; it && i > -1; ++it, --i)
|
|
|
|
out.append(*it);
|
|
|
|
|
|
|
|
return event_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-09-01 09:51:40 +02:00
|
|
|
ircd::m::sync::polylog::room_ephemeral(shortpoll &sp,
|
|
|
|
json::stack::object &out,
|
|
|
|
const m::room &room)
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
|
|
|
{
|
|
|
|
json::stack::member member{out, "events"};
|
|
|
|
json::stack::array array{member};
|
2018-09-01 09:51:40 +02:00
|
|
|
room_ephemeral_events(sp, array, room);
|
2018-04-22 08:50:16 +02:00
|
|
|
}
|
2017-10-03 13:12:54 +02:00
|
|
|
}
|
|
|
|
|
2018-04-22 08:50:16 +02:00
|
|
|
void
|
2018-09-01 09:51:40 +02:00
|
|
|
ircd::m::sync::polylog::room_ephemeral_events(shortpoll &sp,
|
|
|
|
json::stack::array &out,
|
|
|
|
const m::room &room)
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
|
|
|
const m::room::members members{room};
|
|
|
|
members.for_each("join", m::room::members::closure{[&]
|
|
|
|
(const m::user &user)
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-05-22 10:13:38 +02:00
|
|
|
static const m::event::fetch::opts fopts
|
|
|
|
{
|
|
|
|
m::event::keys::include
|
|
|
|
{
|
|
|
|
"event_id",
|
|
|
|
"content",
|
|
|
|
"sender",
|
2018-09-01 09:51:40 +02:00
|
|
|
},
|
2018-05-22 10:13:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
m::user::room user_room{user};
|
|
|
|
user_room.fopts = &fopts;
|
|
|
|
|
2018-05-18 04:32:26 +02:00
|
|
|
if(head_idx(std::nothrow, user_room) <= sp.since)
|
2018-04-22 08:50:16 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
user_room.get(std::nothrow, "ircd.read", room.room_id, [&]
|
|
|
|
(const m::event &event)
|
2017-10-03 13:12:54 +02:00
|
|
|
{
|
2018-04-22 08:50:16 +02:00
|
|
|
const auto &event_idx
|
|
|
|
{
|
2018-05-18 04:32:26 +02:00
|
|
|
index(event, std::nothrow)
|
2018-04-22 08:50:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(event_idx < sp.since || event_idx >= sp.current)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sp.committed = true;
|
|
|
|
json::stack::object object{out};
|
|
|
|
|
|
|
|
// type
|
|
|
|
{
|
|
|
|
json::stack::member member
|
|
|
|
{
|
|
|
|
object, "type", "m.receipt"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// content
|
2018-04-11 00:20:47 +02:00
|
|
|
{
|
2018-04-22 08:50:16 +02:00
|
|
|
const json::object data
|
|
|
|
{
|
|
|
|
at<"content"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
thread_local char buf[1024];
|
|
|
|
const json::members reformat
|
2018-04-11 00:20:47 +02:00
|
|
|
{
|
2018-04-22 08:50:16 +02:00
|
|
|
{ unquote(data.at("event_id")),
|
2018-04-11 00:20:47 +02:00
|
|
|
{
|
2018-04-22 08:50:16 +02:00
|
|
|
{ "m.read",
|
|
|
|
{
|
|
|
|
{ at<"sender"_>(event),
|
|
|
|
{
|
|
|
|
{ "ts", data.at("ts") }
|
2018-04-11 00:20:47 +02:00
|
|
|
}}
|
2018-04-22 08:50:16 +02:00
|
|
|
}}
|
|
|
|
}}
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::member member
|
|
|
|
{
|
|
|
|
object, "content", json::stringify(mutable_buffer{buf}, reformat)
|
|
|
|
};
|
2018-04-11 00:20:47 +02:00
|
|
|
}
|
2018-04-22 08:50:16 +02:00
|
|
|
});
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-09-01 09:51:40 +02:00
|
|
|
ircd::m::sync::polylog::room_account_data(shortpoll &sp,
|
|
|
|
json::stack::object &out,
|
|
|
|
const m::room &room)
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
2018-06-20 05:52:08 +02:00
|
|
|
json::stack::member member{out, "events"};
|
|
|
|
json::stack::array array{member};
|
|
|
|
const m::room::state state
|
|
|
|
{
|
|
|
|
sp.user_room
|
|
|
|
};
|
|
|
|
|
|
|
|
char typebuf[288]; //TODO: room_account_data_typebuf_size
|
|
|
|
const auto type
|
|
|
|
{
|
|
|
|
m::user::_account_data_type(typebuf, room.room_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
state.for_each(type, [&sp, &array]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
const auto &event_idx
|
|
|
|
{
|
|
|
|
index(event, std::nothrow)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(event_idx < sp.since || event_idx >= sp.current)
|
|
|
|
return;
|
2018-04-22 08:50:16 +02:00
|
|
|
|
2018-06-20 05:52:08 +02:00
|
|
|
json::stack::object object{array};
|
|
|
|
|
|
|
|
// type
|
|
|
|
{
|
|
|
|
json::stack::member member
|
|
|
|
{
|
|
|
|
object, "type", at<"state_key"_>(event)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// content
|
|
|
|
{
|
|
|
|
json::stack::member member
|
|
|
|
{
|
|
|
|
object, "content", at<"content"_>(event)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
2018-04-22 08:50:16 +02:00
|
|
|
}
|
2017-10-03 13:12:54 +02:00
|
|
|
|
2018-04-22 08:50:16 +02:00
|
|
|
void
|
2018-09-01 09:51:40 +02:00
|
|
|
ircd::m::sync::polylog::room_unread_notifications(shortpoll &sp,
|
|
|
|
json::stack::object &out,
|
|
|
|
const m::room &room)
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
2018-10-14 12:17:21 +02:00
|
|
|
m::event::id::buf last_read;
|
|
|
|
if(!m::receipt::read(last_read, room.room_id, sp.user))
|
2018-09-14 15:21:19 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
const auto last_read_idx
|
|
|
|
{
|
|
|
|
index(last_read)
|
|
|
|
};
|
|
|
|
|
2018-04-22 08:50:16 +02:00
|
|
|
// highlight_count
|
2018-09-07 15:25:40 +02:00
|
|
|
json::stack::member
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
2018-09-14 15:21:19 +02:00
|
|
|
out, "highlight_count", json::value
|
|
|
|
{
|
|
|
|
highlight_count(room, sp.user, last_read_idx, sp.current)
|
|
|
|
}
|
2018-09-07 15:25:40 +02:00
|
|
|
};
|
2018-04-22 08:50:16 +02:00
|
|
|
|
|
|
|
// notification_count
|
2018-09-07 15:25:40 +02:00
|
|
|
json::stack::member
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
2018-09-07 15:25:40 +02:00
|
|
|
out, "notification_count", json::value
|
2018-04-22 08:50:16 +02:00
|
|
|
{
|
2018-09-14 15:21:19 +02:00
|
|
|
notification_count(room, last_read_idx, sp.current)
|
2018-09-07 15:25:40 +02:00
|
|
|
}
|
|
|
|
};
|
2018-04-22 08:50:16 +02:00
|
|
|
}
|
2018-09-14 15:21:19 +02:00
|
|
|
|
|
|
|
long
|
|
|
|
ircd::m::sync::highlight_count(const room &r,
|
|
|
|
const user &u,
|
|
|
|
const event::idx &a,
|
|
|
|
const event::idx &b)
|
|
|
|
{
|
|
|
|
using proto = size_t (const user &, const room &, const event::idx &, const event::idx &);
|
|
|
|
|
|
|
|
static mods::import<proto> count
|
|
|
|
{
|
|
|
|
"m_user", "highlighted_count__between"
|
|
|
|
};
|
|
|
|
|
2018-09-27 09:59:17 +02:00
|
|
|
return count(u, r, a, a < b? b : a);
|
2018-09-14 15:21:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
long
|
|
|
|
ircd::m::sync::notification_count(const room &room,
|
|
|
|
const event::idx &a,
|
|
|
|
const event::idx &b)
|
|
|
|
{
|
2018-09-27 09:59:17 +02:00
|
|
|
return m::count_since(room, a, a < b? b : a);
|
2018-09-14 15:21:19 +02:00
|
|
|
}
|