mirror of
https://github.com/matrix-construct/construct
synced 2025-02-18 01:30:12 +01:00
modules/m_room_message: Split highlight auth hook to module.
This commit is contained in:
parent
ef7954f994
commit
d7d3b54e5c
3 changed files with 86 additions and 74 deletions
|
@ -107,7 +107,6 @@ m_user_rooms_la_SOURCES = m_user_rooms.cc
|
|||
m_user_filter_la_SOURCES = m_user_filter.cc
|
||||
m_user_mitsein_la_SOURCES = m_user_mitsein.cc
|
||||
m_user_servers_la_SOURCES = m_user_servers.cc
|
||||
m_user_highlight_la_SOURCES = m_user_highlight.cc
|
||||
m_user_profile_la_SOURCES = m_user_profile.cc
|
||||
m_user_account_data_la_SOURCES = m_user_account_data.cc
|
||||
m_user_room_account_data_la_SOURCES = m_user_room_account_data.cc
|
||||
|
@ -149,6 +148,7 @@ m_room_third_party_invite_la_SOURCES = m_room_third_party_invite.cc
|
|||
m_room_redaction_la_SOURCES = m_room_redaction.cc
|
||||
m_room_bootstrap_la_SOURCES = m_room_bootstrap.cc
|
||||
m_room_name_la_SOURCES = m_room_name.cc
|
||||
m_user_highlight_la_SOURCES = m_user_highlight.cc m_user_highlight_auth.cc
|
||||
m_create_la_SOURCES = m_create.cc
|
||||
m_event_horizon_la_SOURCES = m_event_pretty.cc
|
||||
m_event_pretty_la_SOURCES = m_event_horizon.cc
|
||||
|
|
|
@ -10,9 +10,6 @@
|
|||
|
||||
namespace ircd::m
|
||||
{
|
||||
static void room_message_auth(const event &, room::auth::hookdata &);
|
||||
extern hookfn<room::auth::hookdata &> room_message_auth_hook;
|
||||
|
||||
static void room_message_notify(const event &, vm::eval &);
|
||||
extern hookfn<vm::eval &> room_message_notify_hook;
|
||||
}
|
||||
|
@ -63,73 +60,3 @@ ircd::m::room_message_notify(const event &event,
|
|||
size(body) > 128? "..."_sv : string_view{}
|
||||
};
|
||||
}
|
||||
|
||||
decltype(ircd::m::room_message_auth_hook)
|
||||
ircd::m::room_message_auth_hook
|
||||
{
|
||||
room_message_auth,
|
||||
{
|
||||
{ "_site", "room.auth" },
|
||||
{ "type", "m.room.message" },
|
||||
}
|
||||
};
|
||||
|
||||
void
|
||||
ircd::m::room_message_auth(const event &event,
|
||||
room::auth::hookdata &data)
|
||||
{
|
||||
using FAIL = m::room::auth::FAIL;
|
||||
using conforms = m::event::conforms;
|
||||
assert(json::get<"type"_>(event) == "m.room.message");
|
||||
|
||||
const auto &content
|
||||
{
|
||||
json::get<"content"_>(event)
|
||||
};
|
||||
|
||||
if(!user::highlight::match_at_room)
|
||||
return;
|
||||
|
||||
const json::string &body
|
||||
{
|
||||
content.get("body")
|
||||
};
|
||||
|
||||
if(likely(!startswith(body, "@room")))
|
||||
return;
|
||||
|
||||
const room::power power
|
||||
{
|
||||
data.auth_power?
|
||||
*data.auth_power : m::event{},
|
||||
*data.auth_create
|
||||
};
|
||||
|
||||
const auto &user_level
|
||||
{
|
||||
power.level_user(at<"sender"_>(event))
|
||||
};
|
||||
|
||||
int64_t required_level
|
||||
{
|
||||
room::power::default_power_level
|
||||
};
|
||||
|
||||
power.for_each("notifications", room::power::closure_bool{[&required_level]
|
||||
(const auto &name, const auto &level)
|
||||
{
|
||||
if(name != "room")
|
||||
return true;
|
||||
|
||||
required_level = level;
|
||||
return false;
|
||||
}});
|
||||
|
||||
if(user_level < required_level)
|
||||
throw FAIL
|
||||
{
|
||||
"Insufficient power level to highlight the room (have:%ld require:%ld).",
|
||||
user_level,
|
||||
required_level
|
||||
};
|
||||
}
|
||||
|
|
85
modules/m_user_highlight_auth.cc
Normal file
85
modules/m_user_highlight_auth.cc
Normal file
|
@ -0,0 +1,85 @@
|
|||
// 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
|
||||
{
|
||||
static void user_highlight_auth(const event &, room::auth::hookdata &);
|
||||
extern hookfn<room::auth::hookdata &> user_highlight_auth_hook;
|
||||
}
|
||||
|
||||
decltype(ircd::m::user_highlight_auth_hook)
|
||||
ircd::m::user_highlight_auth_hook
|
||||
{
|
||||
user_highlight_auth,
|
||||
{
|
||||
{ "_site", "room.auth" },
|
||||
{ "type", "m.room.message" },
|
||||
}
|
||||
};
|
||||
|
||||
void
|
||||
ircd::m::user_highlight_auth(const event &event,
|
||||
room::auth::hookdata &data)
|
||||
{
|
||||
using FAIL = m::room::auth::FAIL;
|
||||
using conforms = m::event::conforms;
|
||||
assert(json::get<"type"_>(event) == "m.room.message");
|
||||
|
||||
const auto &content
|
||||
{
|
||||
json::get<"content"_>(event)
|
||||
};
|
||||
|
||||
if(!user::highlight::match_at_room)
|
||||
return;
|
||||
|
||||
const json::string &body
|
||||
{
|
||||
content.get("body")
|
||||
};
|
||||
|
||||
if(likely(!startswith(body, "@room")))
|
||||
return;
|
||||
|
||||
const room::power power
|
||||
{
|
||||
data.auth_power?
|
||||
*data.auth_power : m::event{},
|
||||
*data.auth_create
|
||||
};
|
||||
|
||||
const auto &user_level
|
||||
{
|
||||
power.level_user(at<"sender"_>(event))
|
||||
};
|
||||
|
||||
int64_t required_level
|
||||
{
|
||||
room::power::default_power_level
|
||||
};
|
||||
|
||||
power.for_each("notifications", room::power::closure_bool{[&required_level]
|
||||
(const auto &name, const auto &level)
|
||||
{
|
||||
if(name != "room")
|
||||
return true;
|
||||
|
||||
required_level = level;
|
||||
return false;
|
||||
}});
|
||||
|
||||
if(user_level < required_level)
|
||||
throw FAIL
|
||||
{
|
||||
"Insufficient power level to highlight the room (have:%ld require:%ld).",
|
||||
user_level,
|
||||
required_level
|
||||
};
|
||||
}
|
Loading…
Add table
Reference in a new issue