0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-27 16:04:15 +01:00
construct/matrix/room_members.cc

225 lines
4.4 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)
{
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)
{
++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()
};
// 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);
return this->for_each(membership, host, [&closure]
(const auto &user_id, const auto &event_idx)
{
return closure(user_id);
});
}
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)
{
2019-09-27 04:28:25 +02:00
const auto &event_idx
{
2019-09-27 04:28:25 +02:00
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;
}
2019-09-27 04:28:25 +02:00
if(!closure(user_id, event_idx))
return false;
return true;
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 &closure)
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;
2019-09-27 04:28:25 +02:00
if(!closure(user_id))
return false;
}
2019-09-27 04:28:25 +02:00
return true;
}