2020-03-18 05:57:44 +01:00
|
|
|
// The Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) The Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2020 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.
|
|
|
|
|
2020-03-18 21:59:33 +01:00
|
|
|
bool
|
|
|
|
ircd::m::user::pushrules::del(const path &path)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto &[scope, kind, ruleid]
|
|
|
|
{
|
|
|
|
path
|
|
|
|
};
|
|
|
|
|
|
|
|
char typebuf[event::TYPE_MAX_SIZE];
|
|
|
|
const string_view &type
|
|
|
|
{
|
|
|
|
push::make_type(typebuf, path)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
user
|
|
|
|
};
|
|
|
|
|
|
|
|
const event::idx &event_idx
|
|
|
|
{
|
2020-04-02 00:59:02 +02:00
|
|
|
user_room.get(std::nothrow, type, ruleid)
|
2020-03-18 21:59:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const m::event::id::buf event_id
|
|
|
|
{
|
2020-04-02 00:42:33 +02:00
|
|
|
m::event_id(std::nothrow, event_idx)
|
2020-03-18 21:59:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if(!event_id)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const auto redact_id
|
|
|
|
{
|
|
|
|
m::redact(user_room, user, event_id, "deleted")
|
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-03-18 05:57:44 +01:00
|
|
|
ircd::m::user::pushrules::set(const path &path,
|
2020-03-18 21:59:33 +01:00
|
|
|
const json::object &content)
|
2020-03-18 05:57:44 +01:00
|
|
|
const
|
|
|
|
{
|
2020-03-18 21:59:33 +01:00
|
|
|
const auto &[scope, kind, ruleid]
|
|
|
|
{
|
|
|
|
path
|
|
|
|
};
|
|
|
|
|
|
|
|
const push::rule rule
|
|
|
|
{
|
|
|
|
content
|
|
|
|
};
|
|
|
|
|
|
|
|
char typebuf[event::TYPE_MAX_SIZE];
|
|
|
|
const string_view &type
|
|
|
|
{
|
|
|
|
push::make_type(typebuf, path)
|
|
|
|
};
|
|
|
|
|
2020-03-18 05:57:44 +01:00
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
user
|
|
|
|
};
|
|
|
|
|
2020-03-18 21:59:33 +01:00
|
|
|
const auto rule_event_id
|
|
|
|
{
|
|
|
|
m::send(user_room, user, type, ruleid, content)
|
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
2020-03-18 05:57:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::user::pushrules::get(const path &path,
|
|
|
|
const closure &closure)
|
|
|
|
const
|
|
|
|
{
|
2020-03-18 21:59:33 +01:00
|
|
|
const auto &[scope, kind, ruleid]
|
|
|
|
{
|
|
|
|
path
|
|
|
|
};
|
|
|
|
|
2020-03-18 05:57:44 +01:00
|
|
|
if(!get(std::nothrow, path, closure))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"push rule (%s,%s,%s) for user %s not found",
|
2020-03-18 21:59:33 +01:00
|
|
|
scope,
|
|
|
|
kind,
|
|
|
|
ruleid,
|
2020-03-18 05:57:44 +01:00
|
|
|
string_view{user.user_id}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::user::pushrules::get(std::nothrow_t,
|
|
|
|
const path &path,
|
|
|
|
const closure &closure)
|
|
|
|
const
|
|
|
|
{
|
2020-03-18 21:59:33 +01:00
|
|
|
const auto &[scope, kind, ruleid]
|
2020-03-18 05:57:44 +01:00
|
|
|
{
|
2020-03-18 21:59:33 +01:00
|
|
|
path
|
2020-03-18 05:57:44 +01:00
|
|
|
};
|
|
|
|
|
2020-03-18 21:59:33 +01:00
|
|
|
char typebuf[event::TYPE_MAX_SIZE];
|
|
|
|
const string_view &type
|
2020-03-18 05:57:44 +01:00
|
|
|
{
|
2020-03-18 21:59:33 +01:00
|
|
|
push::make_type(typebuf, path)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
user
|
2020-03-18 05:57:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const event::idx &event_idx
|
|
|
|
{
|
2020-03-22 21:55:29 +01:00
|
|
|
user_room.get(std::nothrow, type, ruleid)
|
2020-03-18 05:57:44 +01:00
|
|
|
};
|
|
|
|
|
2020-03-25 05:31:00 +01:00
|
|
|
const bool user_rule_found
|
2020-03-18 21:59:33 +01:00
|
|
|
{
|
2020-03-25 05:31:00 +01:00
|
|
|
m::get(std::nothrow, event_idx, "content", [&path, &closure, &event_idx]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
closure(event_idx, path, content);
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
// Allow user-set rule found with the same id as a server-default to
|
|
|
|
// always take priority.
|
|
|
|
if(user_rule_found)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Present the default rule to the closure.
|
|
|
|
if(likely(scope == "global"))
|
|
|
|
{
|
|
|
|
const auto &rules
|
|
|
|
{
|
|
|
|
push::rules::defaults.get<json::array>(kind)
|
|
|
|
};
|
|
|
|
|
2021-02-25 23:33:52 +01:00
|
|
|
for(const json::object rule : rules)
|
2020-03-25 05:31:00 +01:00
|
|
|
{
|
2021-02-25 23:33:52 +01:00
|
|
|
const json::string _ruleid
|
2020-03-25 05:31:00 +01:00
|
|
|
{
|
|
|
|
rule["rule_id"]
|
|
|
|
};
|
|
|
|
|
|
|
|
if(_ruleid != ruleid)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
closure(0UL, path, rule);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing found
|
|
|
|
return false;
|
2020-03-18 05:57:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::user::pushrules::for_each(const closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
2020-03-18 21:59:33 +01:00
|
|
|
return for_each(path{}, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::user::pushrules::for_each(const path &path,
|
|
|
|
const closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto &[scope, kind, ruleid]
|
2020-03-18 05:57:44 +01:00
|
|
|
{
|
2020-03-18 21:59:33 +01:00
|
|
|
path
|
|
|
|
};
|
|
|
|
|
2020-03-19 23:33:31 +01:00
|
|
|
// If the path contains a ruleid there's at most one item to iterate...
|
|
|
|
if(ruleid)
|
2020-03-22 21:55:29 +01:00
|
|
|
{
|
|
|
|
get(std::nothrow, path, closure);
|
|
|
|
return true;
|
|
|
|
}
|
2020-03-19 23:33:31 +01:00
|
|
|
|
2020-03-25 05:31:00 +01:00
|
|
|
const user::room user_room
|
|
|
|
{
|
|
|
|
user
|
|
|
|
};
|
|
|
|
|
|
|
|
const room::state state
|
|
|
|
{
|
|
|
|
user_room
|
|
|
|
};
|
|
|
|
|
|
|
|
// Present the default rules to the closure
|
2020-03-19 23:33:31 +01:00
|
|
|
if(!scope || scope == "global")
|
|
|
|
{
|
|
|
|
for(const auto &_kind : json::keys<decltype(push::rules::defaults)>())
|
|
|
|
{
|
|
|
|
if(kind && kind != _kind)
|
|
|
|
continue;
|
|
|
|
|
2021-02-25 23:33:52 +01:00
|
|
|
for(const json::object rule : push::rules::defaults.at<json::array>(_kind))
|
2020-03-19 23:33:31 +01:00
|
|
|
{
|
2021-02-25 23:33:52 +01:00
|
|
|
const json::string _ruleid
|
2020-03-25 05:31:00 +01:00
|
|
|
{
|
|
|
|
rule["rule_id"]
|
|
|
|
};
|
|
|
|
|
2020-03-19 23:33:31 +01:00
|
|
|
const push::path path
|
|
|
|
{
|
2020-03-25 05:31:00 +01:00
|
|
|
"global", _kind, _ruleid
|
2020-03-19 23:33:31 +01:00
|
|
|
};
|
|
|
|
|
2020-03-25 05:31:00 +01:00
|
|
|
char typebuf[event::TYPE_MAX_SIZE];
|
|
|
|
const string_view &type
|
|
|
|
{
|
|
|
|
push::make_type(typebuf, path)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check if the user set a rule with the same path/id as a
|
|
|
|
// server-default rule; if so, we want their rule to take
|
|
|
|
// priority over the this server-default below.
|
|
|
|
if(state.has(type, _ruleid))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Present the server-default rule to the iteration.
|
2020-03-23 20:52:55 +01:00
|
|
|
if(!closure(0UL, path, rule))
|
2020-03-19 23:33:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 21:59:33 +01:00
|
|
|
char typebuf[event::TYPE_MAX_SIZE];
|
|
|
|
const room::state::type_prefix type
|
|
|
|
{
|
|
|
|
push::make_type(typebuf, path)
|
2020-03-18 05:57:44 +01:00
|
|
|
};
|
|
|
|
|
2020-03-25 05:31:00 +01:00
|
|
|
return state.for_each(type, [&closure]
|
2020-03-18 21:59:33 +01:00
|
|
|
(const string_view &type, const string_view &state_key, const m::event::idx &event_idx)
|
|
|
|
{
|
|
|
|
const auto path
|
|
|
|
{
|
|
|
|
push::make_path(type, state_key)
|
|
|
|
};
|
|
|
|
|
2020-03-23 20:52:55 +01:00
|
|
|
return m::query<bool>(std::nothrow, event_idx, "content", true, [&]
|
2020-03-18 21:59:33 +01:00
|
|
|
(const json::object &content)
|
|
|
|
{
|
2020-03-23 20:52:55 +01:00
|
|
|
return closure(event_idx, path, content);
|
2020-03-18 21:59:33 +01:00
|
|
|
});
|
2020-03-25 05:31:00 +01:00
|
|
|
});
|
2020-03-18 05:57:44 +01:00
|
|
|
}
|