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
|
|
|
|
{
|
|
|
|
user_room.get(type, ruleid)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::event::id::buf event_id
|
|
|
|
{
|
|
|
|
m::event_id(event_idx, std::nothrow)
|
|
|
|
};
|
|
|
|
|
|
|
|
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-19 23:33:31 +01:00
|
|
|
// Present the default rules to the closure.
|
|
|
|
if(likely(scope == "global"))
|
|
|
|
{
|
|
|
|
const auto &rules
|
|
|
|
{
|
|
|
|
push::rules::defaults.get<json::array>(kind)
|
|
|
|
};
|
|
|
|
|
|
|
|
for(const json::object &rule : rules)
|
|
|
|
{
|
|
|
|
const json::string &_ruleid
|
|
|
|
{
|
|
|
|
rule["rule_id"]
|
|
|
|
};
|
|
|
|
|
|
|
|
if(_ruleid == ruleid)
|
|
|
|
{
|
|
|
|
closure(path, rule);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-18 21:59:33 +01:00
|
|
|
user_room.get(type, ruleid)
|
2020-03-18 05:57:44 +01:00
|
|
|
};
|
|
|
|
|
2020-03-18 21:59:33 +01:00
|
|
|
return m::get(std::nothrow, event_idx, "content", [&path, &closure]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
closure(path, content);
|
|
|
|
});
|
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)
|
|
|
|
return get(std::nothrow, path, closure);
|
|
|
|
|
|
|
|
// Present the default rules to the closure; note that the for_each
|
|
|
|
// convention dictates if the user's closure returns false we'll never
|
|
|
|
// get past this branch to iterate the default rules.
|
|
|
|
if(!scope || scope == "global")
|
|
|
|
{
|
|
|
|
for(const auto &_kind : json::keys<decltype(push::rules::defaults)>())
|
|
|
|
{
|
|
|
|
if(kind && kind != _kind)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for(const json::object &rule : push::rules::defaults.at<json::array>(_kind))
|
|
|
|
{
|
|
|
|
const push::path path
|
|
|
|
{
|
|
|
|
"global", _kind, json::string{rule["rule_id"]}
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!closure(path, rule))
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
const user::room user_room
|
|
|
|
{
|
|
|
|
user
|
|
|
|
};
|
|
|
|
|
|
|
|
const room::state state
|
|
|
|
{
|
2020-03-18 21:59:33 +01:00
|
|
|
user_room
|
2020-03-18 05:57:44 +01:00
|
|
|
};
|
|
|
|
|
2020-03-18 21:59:33 +01:00
|
|
|
return state.for_each(type, room::state::closure_bool{[&closure]
|
|
|
|
(const string_view &type, const string_view &state_key, const m::event::idx &event_idx)
|
|
|
|
{
|
|
|
|
const auto path
|
|
|
|
{
|
|
|
|
push::make_path(type, state_key)
|
|
|
|
};
|
|
|
|
|
|
|
|
return m::query<bool>(std::nothrow, event_idx, "content", true, [&path, &closure]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
return closure(path, content);
|
|
|
|
});
|
|
|
|
}});
|
2020-03-18 05:57:44 +01:00
|
|
|
}
|