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.
|
|
|
|
|
|
|
|
ircd::mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Client Sync :Room Unread Notifications"
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace ircd::m::sync
|
|
|
|
{
|
|
|
|
static long _notification_count(const room &, const event::idx &a, const event::idx &b);
|
|
|
|
static long _highlight_count(const room &, const user &u, const event::idx &a, const event::idx &b);
|
2019-02-24 20:59:53 +01:00
|
|
|
static bool room_unread_notifications_polylog(data &);
|
|
|
|
static bool room_unread_notifications_linear(data &);
|
2019-01-04 02:21:02 +01:00
|
|
|
extern item room_unread_notifications;
|
|
|
|
}
|
|
|
|
|
|
|
|
decltype(ircd::m::sync::room_unread_notifications)
|
|
|
|
ircd::m::sync::room_unread_notifications
|
|
|
|
{
|
2019-01-09 00:10:06 +01:00
|
|
|
"rooms.unread_notifications",
|
2019-01-04 23:47:01 +01:00
|
|
|
room_unread_notifications_polylog,
|
|
|
|
room_unread_notifications_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::room_unread_notifications_linear(data &data)
|
|
|
|
{
|
2019-02-28 03:24:12 +01:00
|
|
|
if(!data.event_idx)
|
|
|
|
return false;
|
|
|
|
|
2019-03-08 22:42:54 +01:00
|
|
|
if(!data.membership)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!data.room)
|
2019-02-27 02:34:07 +01:00
|
|
|
return false;
|
|
|
|
|
2019-03-08 22:42:54 +01:00
|
|
|
assert(data.event);
|
2019-02-27 02:34:07 +01:00
|
|
|
const auto &room{*data.room};
|
|
|
|
m::event::id::buf last_read;
|
|
|
|
if(!m::receipt::read(last_read, room.room_id, data.user))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const auto start_idx
|
|
|
|
{
|
|
|
|
index(last_read)
|
|
|
|
};
|
|
|
|
|
2019-03-08 22:42:54 +01:00
|
|
|
json::stack::object rooms
|
|
|
|
{
|
|
|
|
*data.out, "rooms"
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object membership_
|
|
|
|
{
|
|
|
|
*data.out, data.membership
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object room_
|
|
|
|
{
|
|
|
|
*data.out, data.room->room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object unread_notifications
|
|
|
|
{
|
|
|
|
*data.out, "unread_notifications"
|
|
|
|
};
|
|
|
|
|
2019-03-28 07:58:33 +01:00
|
|
|
const event::idx upper_bound
|
|
|
|
{
|
|
|
|
std::max(data.range.second, data.event_idx + 1)
|
|
|
|
};
|
|
|
|
|
2019-02-27 02:34:07 +01:00
|
|
|
// highlight_count
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
*data.out, "highlight_count", json::value
|
|
|
|
{
|
2019-03-28 07:58:33 +01:00
|
|
|
_highlight_count(room, data.user, start_idx, upper_bound)
|
2019-02-27 02:34:07 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// notification_count
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
*data.out, "notification_count", json::value
|
|
|
|
{
|
2019-03-28 07:58:33 +01:00
|
|
|
_notification_count(room, start_idx, upper_bound)
|
2019-02-27 02:34:07 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
2019-01-04 23:47:01 +01:00
|
|
|
}
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
bool
|
2019-01-04 02:21:02 +01:00
|
|
|
ircd::m::sync::room_unread_notifications_polylog(data &data)
|
|
|
|
{
|
2019-02-19 23:49:50 +01:00
|
|
|
const auto &room{*data.room};
|
2019-01-04 02:21:02 +01:00
|
|
|
m::event::id::buf last_read;
|
|
|
|
if(!m::receipt::read(last_read, room.room_id, data.user))
|
2019-02-24 20:59:53 +01:00
|
|
|
return false;
|
2019-01-04 02:21:02 +01:00
|
|
|
|
2019-01-27 02:01:36 +01:00
|
|
|
const auto start_idx
|
2019-01-09 00:10:06 +01:00
|
|
|
{
|
2019-01-27 02:01:36 +01:00
|
|
|
index(last_read)
|
2019-01-09 00:10:06 +01:00
|
|
|
};
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
if(!apropos(data, start_idx))
|
|
|
|
return false;
|
|
|
|
|
2019-01-04 02:21:02 +01:00
|
|
|
// highlight_count
|
|
|
|
json::stack::member
|
|
|
|
{
|
2019-02-27 00:50:58 +01:00
|
|
|
*data.out, "highlight_count", json::value
|
2019-01-04 02:21:02 +01:00
|
|
|
{
|
2019-01-10 22:19:07 +01:00
|
|
|
_highlight_count(room, data.user, start_idx, data.range.second)
|
2019-01-04 02:21:02 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// notification_count
|
|
|
|
json::stack::member
|
|
|
|
{
|
2019-02-27 00:50:58 +01:00
|
|
|
*data.out, "notification_count", json::value
|
2019-01-04 02:21:02 +01:00
|
|
|
{
|
2019-01-10 22:19:07 +01:00
|
|
|
_notification_count(room, start_idx, data.range.second)
|
2019-01-04 02:21:02 +01:00
|
|
|
}
|
|
|
|
};
|
2019-02-22 18:25:00 +01:00
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
return true;
|
2019-01-04 02:21:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
long
|
|
|
|
ircd::m::sync::_notification_count(const room &room,
|
|
|
|
const event::idx &a,
|
|
|
|
const event::idx &b)
|
|
|
|
{
|
|
|
|
return m::count_since(room, a, a < b? b : a);
|
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
|
|
|
};
|
|
|
|
|
|
|
|
return count(u, r, a, a < b? b : a);
|
|
|
|
}
|