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
|
|
|
|
{
|
|
|
|
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-08-14 12:34:24 +02:00
|
|
|
|
2019-01-04 02:21:02 +01:00
|
|
|
extern item room_unread_notifications;
|
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:53 +02:00
|
|
|
ircd::mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Client Sync :Room Unread Notifications"
|
|
|
|
};
|
|
|
|
|
2019-01-04 02:21:02 +01:00
|
|
|
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,
|
2019-07-04 11:29:05 +02:00
|
|
|
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.room)
|
2019-02-27 02:34:07 +01:00
|
|
|
return false;
|
|
|
|
|
2019-08-28 11:06:05 +02:00
|
|
|
const bool is_user_room
|
|
|
|
{
|
|
|
|
data.room->room_id == data.user_room.room_id
|
|
|
|
};
|
|
|
|
|
2019-03-08 22:42:54 +01:00
|
|
|
assert(data.event);
|
2020-03-25 00:16:27 +01:00
|
|
|
const auto &type
|
|
|
|
{
|
|
|
|
json::get<"type"_>(*data.event)
|
|
|
|
};
|
|
|
|
|
2019-08-28 11:06:05 +02:00
|
|
|
const bool is_self_read
|
|
|
|
{
|
2020-03-25 00:16:27 +01:00
|
|
|
is_user_room &&
|
|
|
|
type == "ircd.read"
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool is_push_note
|
|
|
|
{
|
|
|
|
is_user_room &&
|
|
|
|
startswith(type, user::notifications::type_prefix)
|
|
|
|
};
|
|
|
|
|
|
|
|
const user::notifications::opts note_opts
|
|
|
|
{
|
|
|
|
is_push_note?
|
|
|
|
user::notifications::unmake_type(type):
|
|
|
|
user::notifications::opts{}
|
2019-08-28 11:06:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const m::room &room
|
|
|
|
{
|
2020-03-25 00:16:27 +01:00
|
|
|
is_self_read?
|
|
|
|
room::id{at<"state_key"_>(*data.event)}:
|
|
|
|
is_push_note && note_opts.room_id?
|
|
|
|
note_opts.room_id:
|
|
|
|
data.room->room_id
|
2019-08-28 11:06:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
char membuf[32];
|
|
|
|
const string_view &membership
|
|
|
|
{
|
2020-03-25 00:16:27 +01:00
|
|
|
room.room_id != data.room->room_id?
|
|
|
|
m::membership(membuf, room, data.user):
|
|
|
|
data.membership
|
2019-08-28 11:06:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(!membership)
|
2019-08-14 12:34:24 +02:00
|
|
|
return false;
|
|
|
|
|
2019-08-28 11:06:05 +02:00
|
|
|
// skips state events only until a non-state event is seen.
|
2020-03-25 00:16:27 +01:00
|
|
|
if(likely(!is_user_room))
|
2019-08-28 11:06:05 +02:00
|
|
|
if(defined(json::get<"state_key"_>(*data.event)))
|
2019-08-14 12:34:24 +02:00
|
|
|
return false;
|
|
|
|
|
2019-08-28 11:06:05 +02:00
|
|
|
// skips old events the server has backfilled in the background.
|
2020-03-25 00:16:27 +01:00
|
|
|
if(likely(!is_user_room))
|
2019-09-06 02:31:54 +02:00
|
|
|
if(room::events::viewport_size >= 0)
|
|
|
|
if(json::get<"depth"_>(*data.event) + room::events::viewport_size < data.room_depth)
|
2019-08-28 11:06:05 +02:00
|
|
|
return false;
|
|
|
|
|
2019-02-27 02:34:07 +01:00
|
|
|
m::event::id::buf last_read;
|
2019-08-28 11:06:05 +02:00
|
|
|
if(likely(!is_self_read))
|
2019-09-06 20:13:58 +02:00
|
|
|
if(!m::receipt::get(last_read, room.room_id, data.user))
|
2019-08-28 11:06:05 +02:00
|
|
|
return false;
|
2019-02-27 02:34:07 +01:00
|
|
|
|
|
|
|
const auto start_idx
|
|
|
|
{
|
2019-08-28 11:06:05 +02:00
|
|
|
last_read?
|
|
|
|
index(last_read):
|
|
|
|
!is_self_read?
|
|
|
|
index(room):
|
|
|
|
0UL
|
2019-02-27 02:34:07 +01:00
|
|
|
};
|
|
|
|
|
2019-03-08 22:42:54 +01:00
|
|
|
json::stack::object rooms
|
|
|
|
{
|
|
|
|
*data.out, "rooms"
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object membership_
|
|
|
|
{
|
2019-08-28 11:06:05 +02:00
|
|
|
*data.out, membership
|
2019-03-08 22:42:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object room_
|
|
|
|
{
|
2019-08-28 11:06:05 +02:00
|
|
|
*data.out, room.room_id
|
2019-03-08 22:42:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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)
|
|
|
|
};
|
|
|
|
|
2020-03-27 02:22:55 +01:00
|
|
|
const auto notification_count
|
|
|
|
{
|
|
|
|
start_idx && !is_self_read?
|
|
|
|
_notification_count(room, start_idx, upper_bound):
|
|
|
|
0L
|
|
|
|
};
|
|
|
|
|
2019-02-27 02:34:07 +01:00
|
|
|
json::stack::member
|
|
|
|
{
|
2020-03-27 02:22:55 +01:00
|
|
|
*data.out, "notification_count", json::value
|
2019-02-27 02:34:07 +01:00
|
|
|
{
|
2020-03-27 02:22:55 +01:00
|
|
|
notification_count
|
2019-02-27 02:34:07 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::member
|
|
|
|
{
|
2020-03-27 02:22:55 +01:00
|
|
|
*data.out, "highlight_count", json::value
|
2019-02-27 02:34:07 +01:00
|
|
|
{
|
2020-03-27 02:22:55 +01:00
|
|
|
notification_count?
|
|
|
|
_highlight_count(room, data.user, start_idx, upper_bound):
|
2019-08-28 11:06:05 +02:00
|
|
|
0L
|
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-08-28 11:06:05 +02:00
|
|
|
if(!data.membership)
|
2019-02-24 20:59:53 +01:00
|
|
|
return false;
|
2019-01-04 02:21:02 +01:00
|
|
|
|
2019-08-28 11:06:05 +02:00
|
|
|
assert(data.room);
|
|
|
|
const auto &room
|
|
|
|
{
|
|
|
|
*data.room
|
|
|
|
};
|
|
|
|
|
|
|
|
m::event::id::buf last_read_buf;
|
|
|
|
const auto last_read
|
|
|
|
{
|
2019-09-06 20:13:58 +02:00
|
|
|
m::receipt::get(last_read_buf, room.room_id, data.user)
|
2019-08-28 11:06:05 +02:00
|
|
|
};
|
|
|
|
|
2019-01-27 02:01:36 +01:00
|
|
|
const auto start_idx
|
2019-01-09 00:10:06 +01:00
|
|
|
{
|
2019-08-28 11:06:05 +02:00
|
|
|
last_read?
|
|
|
|
index(last_read):
|
|
|
|
data.membership == "join"?
|
|
|
|
room::index(room):
|
|
|
|
0UL
|
2019-01-09 00:10:06 +01:00
|
|
|
};
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
if(!apropos(data, start_idx))
|
|
|
|
return false;
|
|
|
|
|
2020-03-27 02:22:55 +01:00
|
|
|
const auto notification_count
|
|
|
|
{
|
|
|
|
_notification_count(room, start_idx, data.range.second)
|
|
|
|
};
|
|
|
|
|
2019-01-04 02:21:02 +01:00
|
|
|
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
|
|
|
{
|
2020-03-27 02:22:55 +01:00
|
|
|
notification_count
|
2019-01-04 02:21:02 +01:00
|
|
|
}
|
|
|
|
};
|
2019-02-22 18:25:00 +01:00
|
|
|
|
2020-03-19 21:10:08 +01:00
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
*data.out, "highlight_count", json::value
|
2019-06-27 10:51:47 +02:00
|
|
|
{
|
2020-03-27 02:22:55 +01:00
|
|
|
notification_count?
|
|
|
|
_highlight_count(room, data.user, start_idx, data.range.second):
|
|
|
|
0L
|
2020-03-19 21:10:08 +01:00
|
|
|
}
|
|
|
|
};
|
2019-06-27 10:51:47 +02: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)
|
|
|
|
{
|
2019-09-24 06:48:29 +02:00
|
|
|
const event::idx_range range
|
|
|
|
{
|
2019-10-10 22:33:43 +02:00
|
|
|
std::minmax(a, b)
|
2019-09-24 06:48:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return room::events::count(room, range);
|
2019-01-04 02:21:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
long
|
2019-06-27 10:18:23 +02:00
|
|
|
ircd::m::sync::_highlight_count(const room &room,
|
|
|
|
const user &user,
|
2019-01-04 02:21:02 +01:00
|
|
|
const event::idx &a,
|
|
|
|
const event::idx &b)
|
|
|
|
{
|
2019-09-24 06:48:29 +02:00
|
|
|
const event::idx_range range
|
2019-01-04 02:21:02 +01:00
|
|
|
{
|
2019-10-10 22:33:43 +02:00
|
|
|
std::minmax(a, b)
|
2019-01-04 02:21:02 +01:00
|
|
|
};
|
|
|
|
|
2020-03-25 00:16:27 +01:00
|
|
|
const user::notifications notifications
|
|
|
|
{
|
|
|
|
user
|
|
|
|
};
|
|
|
|
|
|
|
|
user::notifications::opts opts;
|
|
|
|
opts.room_id = room.room_id;
|
|
|
|
opts.only = "highlight";
|
2020-03-26 19:29:01 +01:00
|
|
|
opts.from = range.second;
|
|
|
|
opts.to = range.first;
|
2020-03-25 00:16:27 +01:00
|
|
|
const auto ret
|
|
|
|
{
|
|
|
|
notifications.count(opts)
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
2019-01-04 02:21:02 +01:00
|
|
|
}
|