0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-04 20:52:45 +01:00
construct/matrix/room_members.cc

231 lines
4.6 KiB
C++
Raw Normal View History

// 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.
2019-09-27 04:28:25 +02:00
bool
ircd::m::room::members::empty()
const
{
2019-09-27 04:28:25 +02:00
return empty(string_view{}, string_view{});
}
2019-09-27 04:28:25 +02:00
bool
ircd::m::room::members::empty(const string_view &membership)
const
{
2019-09-27 04:28:25 +02:00
return empty(membership, string_view{});
}
2019-09-27 04:28:25 +02:00
bool
ircd::m::room::members::empty(const string_view &membership,
const string_view &host)
const
{
2019-09-27 04:28:25 +02:00
return for_each(membership, host, closure{[]
(const user::id &user_id) noexcept
{
2019-09-27 04:28:25 +02:00
return false;
}});
}
size_t
2019-09-27 04:28:25 +02:00
ircd::m::room::members::count()
const
{
2019-09-27 04:28:25 +02:00
return count(string_view{}, string_view{});
}
size_t
2019-09-27 04:28:25 +02:00
ircd::m::room::members::count(const string_view &membership)
const
{
2019-09-27 04:28:25 +02:00
return count(membership, string_view{});
}
size_t
2019-09-27 04:28:25 +02:00
ircd::m::room::members::count(const string_view &membership,
const string_view &host)
const
{
size_t ret{0};
2019-09-27 04:28:25 +02:00
for_each(membership, host, closure{[&ret]
(const user::id &user_id) noexcept
{
++ret;
2019-09-27 04:28:25 +02:00
return true;
}});
return ret;
}
bool
2019-09-27 04:28:25 +02:00
ircd::m::room::members::for_each(const closure &closure)
const
{
2019-09-27 04:28:25 +02:00
return for_each(string_view{}, closure);
}
bool
2019-09-27 04:28:25 +02:00
ircd::m::room::members::for_each(const closure_idx &closure)
const
{
2019-09-27 04:28:25 +02:00
return for_each(string_view{}, closure);
}
bool
2019-09-27 04:28:25 +02:00
ircd::m::room::members::for_each(const string_view &membership,
const closure &closure)
const
{
2019-09-27 04:28:25 +02:00
return for_each(membership, string_view{}, closure);
}
2019-09-27 04:28:25 +02:00
bool
ircd::m::room::members::for_each(const string_view &membership,
const closure_idx &closure)
const
{
2019-09-27 04:28:25 +02:00
return for_each(membership, string_view{}, closure);
}
bool
2019-09-27 04:28:25 +02:00
ircd::m::room::members::for_each(const string_view &membership,
const string_view &host,
const closure &closure)
const
{
2019-09-27 04:28:25 +02:00
const m::room::state state
{
room
};
2019-09-27 04:28:25 +02:00
const bool present
{
2019-09-27 04:28:25 +02:00
state.present()
};
const closure_idx _closure
{
[&closure](const auto &user_id, const auto &event_idx)
{
return closure(user_id);
}
};
2019-09-27 04:28:25 +02:00
// joined members optimization. Only possible when seeking
// membership="join" on the present state of the room.
if(membership == "join" && present)
return for_each_join_present(host, _closure);
2019-09-27 04:28:25 +02:00
return this->for_each(membership, host, _closure);
}
bool
2019-09-27 04:28:25 +02:00
ircd::m::room::members::for_each(const string_view &membership,
const string_view &host,
const closure_idx &closure)
const
{
2019-09-27 04:28:25 +02:00
const m::room::state state
{
2019-09-27 04:28:25 +02:00
room
};
2019-09-27 04:28:25 +02:00
const bool present
{
state.present()
};
2019-09-27 04:28:25 +02:00
// joined members optimization. Only possible when seeking
// membership="join" on the present state of the room.
if(membership == "join" && present)
return for_each_join_present(host, [&closure, &state]
(const id::user &user_id, event::idx event_idx)
{
if(!event_idx)
event_idx = state.get(std::nothrow, "m.room.member", user_id);
2019-09-27 04:28:25 +02:00
if(unlikely(!event_idx))
{
2019-09-27 04:28:25 +02:00
log::error
{
log, "Failed member:%s event_idx:%lu in room_joined of %s",
string_view{user_id},
event_idx,
string_view{state.room_id},
};
2019-09-27 04:28:25 +02:00
return true;
}
return closure(user_id, event_idx);
2019-09-27 04:28:25 +02:00
});
2019-09-27 04:28:25 +02:00
return state.for_each("m.room.member", [this, &host, &membership, &closure]
(const string_view &type, const string_view &state_key, const event::idx &event_idx)
{
const m::user::id &user_id
{
state_key
};
2019-09-27 04:28:25 +02:00
if(host && user_id.host() != host)
return true;
2019-09-27 04:28:25 +02:00
return !membership || m::membership(event_idx, membership)?
closure(user_id, event_idx):
true;
});
}
bool
ircd::m::room::members::for_each_join_present(const string_view &host,
const closure_idx &closure)
2019-09-27 04:28:25 +02:00
const
{
2019-09-27 04:28:25 +02:00
db::domain &index
{
2019-09-27 04:28:25 +02:00
dbs::room_joined
};
2019-09-27 04:28:25 +02:00
char keybuf[dbs::ROOM_JOINED_KEY_MAX_SIZE];
const string_view &key
{
2019-09-27 04:28:25 +02:00
dbs::room_joined_key(keybuf, room.room_id, host)
};
2019-09-27 04:28:25 +02:00
auto it
{
2019-09-27 04:28:25 +02:00
index.begin(key)
};
2019-09-27 04:28:25 +02:00
for(; bool(it); ++it)
{
const auto &[origin, user_id]
{
2019-09-27 04:28:25 +02:00
dbs::room_joined_key(it->first)
};
2019-09-27 04:28:25 +02:00
if(host && origin != host)
break;
const event::idx event_idx
{
it->second.size() >= sizeof(event::idx)?
event::idx(byte_view<event::idx>(it->second)):
0UL
};
if(!closure(user_id, event_idx))
2019-09-27 04:28:25 +02:00
return false;
}
2019-09-27 04:28:25 +02:00
return true;
}