2019-01-04 02:21:02 +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.
|
|
|
|
|
|
|
|
namespace ircd::m::sync
|
|
|
|
{
|
2019-02-24 20:59:53 +01:00
|
|
|
static bool presence_polylog(data &);
|
|
|
|
static bool presence_linear(data &);
|
2019-01-10 05:39:12 +01:00
|
|
|
|
2019-01-04 02:21:02 +01:00
|
|
|
extern item presence;
|
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:53 +02:00
|
|
|
ircd::mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Client Sync :Presence"
|
|
|
|
};
|
|
|
|
|
2019-01-04 02:21:02 +01:00
|
|
|
decltype(ircd::m::sync::presence)
|
|
|
|
ircd::m::sync::presence
|
|
|
|
{
|
|
|
|
"presence",
|
2019-01-04 23:47:01 +01:00
|
|
|
presence_polylog,
|
|
|
|
presence_linear,
|
2019-01-04 02:21:02 +01:00
|
|
|
};
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
bool
|
2019-01-04 23:47:01 +01:00
|
|
|
ircd::m::sync::presence_linear(data &data)
|
2019-01-04 02:21:02 +01:00
|
|
|
{
|
2019-02-28 03:24:12 +01:00
|
|
|
if(!data.event_idx)
|
|
|
|
return false;
|
|
|
|
|
2019-01-10 05:39:12 +01:00
|
|
|
assert(data.event);
|
2019-02-27 00:50:58 +01:00
|
|
|
const m::event &event{*data.event};
|
2019-01-10 05:39:12 +01:00
|
|
|
if(json::get<"type"_>(event) != "ircd.presence")
|
2019-02-24 20:59:53 +01:00
|
|
|
return false;
|
2019-01-10 05:39:12 +01:00
|
|
|
|
2019-02-27 00:50:58 +01:00
|
|
|
if(!my_host(json::get<"origin"_>(event)))
|
2019-02-24 20:59:53 +01:00
|
|
|
return false;
|
2019-01-10 05:39:12 +01:00
|
|
|
|
2019-03-08 22:42:54 +01:00
|
|
|
json::stack::object presence
|
|
|
|
{
|
|
|
|
*data.out, "presence"
|
|
|
|
};
|
|
|
|
|
2019-02-27 00:50:58 +01:00
|
|
|
json::stack::array array
|
|
|
|
{
|
|
|
|
*data.out, "events"
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object object
|
|
|
|
{
|
|
|
|
*data.out
|
|
|
|
};
|
|
|
|
|
2019-01-04 23:47:01 +01:00
|
|
|
// sender
|
|
|
|
json::stack::member
|
|
|
|
{
|
2019-02-27 00:50:58 +01:00
|
|
|
*data.out, "sender", unquote(at<"content"_>(event).get("user_id"))
|
2019-01-04 23:47:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// type
|
|
|
|
json::stack::member
|
|
|
|
{
|
2019-02-27 00:50:58 +01:00
|
|
|
*data.out, "type", json::value{"m.presence"}
|
2019-01-04 23:47:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// content
|
|
|
|
json::stack::member
|
|
|
|
{
|
2019-02-27 00:50:58 +01:00
|
|
|
*data.out, "content", at<"content"_>(event)
|
2019-01-04 23:47:01 +01:00
|
|
|
};
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
return true;
|
2019-01-04 23:47:01 +01:00
|
|
|
}
|
2019-01-04 02:21:02 +01:00
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
bool
|
2019-01-04 02:21:02 +01:00
|
|
|
ircd::m::sync::presence_polylog(data &data)
|
2019-01-09 00:10:06 +01:00
|
|
|
{
|
|
|
|
json::stack::array array
|
2019-01-04 02:21:02 +01:00
|
|
|
{
|
2019-02-27 00:50:58 +01:00
|
|
|
*data.out, "events"
|
2019-01-04 02:21:02 +01:00
|
|
|
};
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
bool ret{false};
|
2019-01-04 02:21:02 +01:00
|
|
|
ctx::mutex mutex;
|
2019-02-24 20:59:53 +01:00
|
|
|
const auto append_event{[&data, &array, &mutex, &ret]
|
2019-01-04 02:21:02 +01:00
|
|
|
(const json::object &event)
|
|
|
|
{
|
2019-08-16 15:29:14 +02:00
|
|
|
// Conditions to not send; we don't send for offline users
|
|
|
|
// on initial sync, unless they have a message set.
|
|
|
|
const bool skip
|
|
|
|
{
|
|
|
|
data.range.first == 0
|
|
|
|
&& unquote(event.get("presence")) == "offline"
|
|
|
|
&& !event.has("status_msg")
|
|
|
|
};
|
|
|
|
|
|
|
|
if(skip)
|
|
|
|
return;
|
|
|
|
|
2019-01-04 02:21:02 +01:00
|
|
|
// Lock the json::stack for the append operations. This mutex will only be
|
|
|
|
// contended during a json::stack flush to the client; not during database
|
|
|
|
// queries leading to this.
|
2019-03-02 21:33:32 +01:00
|
|
|
const std::lock_guard l{mutex};
|
2019-02-24 20:59:53 +01:00
|
|
|
ret = true;
|
2019-01-10 05:39:12 +01:00
|
|
|
|
2019-01-09 00:10:06 +01:00
|
|
|
json::stack::object object
|
|
|
|
{
|
|
|
|
array
|
|
|
|
};
|
2019-01-04 02:21:02 +01:00
|
|
|
|
|
|
|
// sender
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
object, "sender", unquote(event.get("user_id"))
|
|
|
|
};
|
|
|
|
|
|
|
|
// type
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
object, "type", json::value{"m.presence"}
|
|
|
|
};
|
|
|
|
|
|
|
|
// content
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
object, "content", event
|
|
|
|
};
|
|
|
|
}};
|
|
|
|
|
2019-07-13 00:18:26 +02:00
|
|
|
// Setup for concurrentization.
|
2019-07-15 21:46:19 +02:00
|
|
|
static const size_t fibers(64);
|
|
|
|
sync::pool.min(fibers);
|
|
|
|
ctx::concurrent<std::string> concurrent
|
2019-01-04 02:21:02 +01:00
|
|
|
{
|
2019-07-15 21:46:19 +02:00
|
|
|
sync::pool, [&data, &append_event](std::string user_id)
|
2019-01-09 00:10:06 +01:00
|
|
|
{
|
2019-01-27 00:24:29 +01:00
|
|
|
const event::idx event_idx
|
|
|
|
{
|
2019-07-15 21:46:19 +02:00
|
|
|
m::presence::get(std::nothrow, m::user::id{user_id})
|
2019-01-27 00:24:29 +01:00
|
|
|
};
|
|
|
|
|
2019-07-15 21:46:19 +02:00
|
|
|
if(!apropos(data, event_idx))
|
|
|
|
return;
|
|
|
|
|
|
|
|
m::get(std::nothrow, event_idx, "content", append_event);
|
2019-01-09 00:10:06 +01:00
|
|
|
}
|
2019-01-04 02:21:02 +01:00
|
|
|
};
|
|
|
|
|
2019-01-12 02:41:23 +01:00
|
|
|
// Iterate all of the users visible to our user in joined rooms.
|
2019-01-09 00:10:06 +01:00
|
|
|
const m::user::mitsein mitsein{data.user};
|
2019-07-15 21:46:19 +02:00
|
|
|
mitsein.for_each("join", [&concurrent]
|
2019-01-09 00:10:06 +01:00
|
|
|
(const m::user &user)
|
2019-01-04 02:21:02 +01:00
|
|
|
{
|
2019-07-15 21:46:19 +02:00
|
|
|
concurrent(std::string(user.user_id));
|
2019-08-17 10:59:17 +02:00
|
|
|
return true;
|
2019-01-09 00:10:06 +01:00
|
|
|
});
|
2019-02-24 20:59:53 +01:00
|
|
|
|
2019-08-11 13:09:16 +02:00
|
|
|
const ctx::uninterruptible ui;
|
2019-07-15 21:46:19 +02:00
|
|
|
concurrent.wait();
|
2019-02-24 20:59:53 +01:00
|
|
|
return ret;
|
2019-01-04 02:21:02 +01:00
|
|
|
}
|