mirror of
https://github.com/matrix-construct/construct
synced 2024-08-27 03:45:16 +02:00
193 lines
3.7 KiB
C++
193 lines
3.7 KiB
C++
|
// Matrix Construct
|
||
|
//
|
||
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
||
|
// Copyright (C) 2016-2019 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.
|
||
|
|
||
|
ircd::mapi::header
|
||
|
IRCD_MODULE
|
||
|
{
|
||
|
"Matrix user filter."
|
||
|
};
|
||
|
|
||
|
ircd::string_view
|
||
|
IRCD_MODULE_EXPORT
|
||
|
ircd::m::user::filter::set(const mutable_buffer &buf,
|
||
|
const json::object &val)
|
||
|
const
|
||
|
{
|
||
|
return set(buf, user, val);
|
||
|
}
|
||
|
|
||
|
ircd::string_view
|
||
|
IRCD_MODULE_EXPORT
|
||
|
ircd::m::user::filter::set(const mutable_buffer &idbuf,
|
||
|
const m::user &user,
|
||
|
const json::object &filter)
|
||
|
{
|
||
|
const user::room user_room
|
||
|
{
|
||
|
user
|
||
|
};
|
||
|
|
||
|
const sha256::buf hash
|
||
|
{
|
||
|
sha256{filter}
|
||
|
};
|
||
|
|
||
|
const string_view filter_id
|
||
|
{
|
||
|
b64tob64url(idbuf, b64encode_unpadded(idbuf, hash))
|
||
|
};
|
||
|
|
||
|
//TODO: ABA
|
||
|
if(user_room.has("ircd.filter", filter_id))
|
||
|
return {};
|
||
|
|
||
|
//TODO: ABA
|
||
|
send(user_room, user.user_id, "ircd.filter", filter_id, filter);
|
||
|
return filter_id;
|
||
|
}
|
||
|
|
||
|
std::string
|
||
|
IRCD_MODULE_EXPORT
|
||
|
ircd::m::user::filter::get(const string_view &id)
|
||
|
const
|
||
|
{
|
||
|
std::string ret;
|
||
|
get(std::nothrow, id, [&ret]
|
||
|
(const string_view &id, const json::object &val)
|
||
|
{
|
||
|
ret = val;
|
||
|
});
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
ircd::json::object
|
||
|
IRCD_MODULE_EXPORT
|
||
|
ircd::m::user::filter::get(const mutable_buffer &out,
|
||
|
const string_view &id)
|
||
|
const
|
||
|
{
|
||
|
json::object ret;
|
||
|
get(std::nothrow, id, [&out, &ret]
|
||
|
(const string_view &id, const json::object &val)
|
||
|
{
|
||
|
ret = string_view { data(out), copy(out, val) };
|
||
|
});
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
void
|
||
|
IRCD_MODULE_EXPORT
|
||
|
ircd::m::user::filter::get(const string_view &id,
|
||
|
const closure &closure)
|
||
|
const
|
||
|
{
|
||
|
if(!get(std::nothrow, user, id, closure))
|
||
|
throw m::NOT_FOUND
|
||
|
{
|
||
|
"filter id '%s' for user %s not found",
|
||
|
id,
|
||
|
string_view{user.user_id}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
bool
|
||
|
IRCD_MODULE_EXPORT
|
||
|
ircd::m::user::filter::get(std::nothrow_t,
|
||
|
const string_view &id,
|
||
|
const closure &closure)
|
||
|
const
|
||
|
{
|
||
|
return get(std::nothrow, user, id, closure);
|
||
|
}
|
||
|
|
||
|
bool
|
||
|
IRCD_MODULE_EXPORT
|
||
|
ircd::m::user::filter::for_each(const closure_bool &closure)
|
||
|
const
|
||
|
{
|
||
|
return for_each(user, closure);
|
||
|
}
|
||
|
|
||
|
bool
|
||
|
IRCD_MODULE_EXPORT
|
||
|
ircd::m::user::filter::get(std::nothrow_t,
|
||
|
const m::user &user,
|
||
|
const string_view &filter_id,
|
||
|
const closure &closure)
|
||
|
{
|
||
|
static const event::fetch::opts fopts
|
||
|
{
|
||
|
event::keys::include {"content"}
|
||
|
};
|
||
|
|
||
|
const user::room user_room
|
||
|
{
|
||
|
user, nullptr, &fopts
|
||
|
};
|
||
|
|
||
|
return user_room.get(std::nothrow, "ircd.filter", filter_id, [&filter_id, &closure]
|
||
|
(const m::event &event)
|
||
|
{
|
||
|
const json::object &content
|
||
|
{
|
||
|
at<"content"_>(event)
|
||
|
};
|
||
|
|
||
|
closure(filter_id, content);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
bool
|
||
|
IRCD_MODULE_EXPORT
|
||
|
ircd::m::user::filter::for_each(const m::user &user,
|
||
|
const closure_bool &closure)
|
||
|
{
|
||
|
const user::room user_room
|
||
|
{
|
||
|
user
|
||
|
};
|
||
|
|
||
|
const room::state state
|
||
|
{
|
||
|
user_room
|
||
|
};
|
||
|
|
||
|
return state.for_each("ircd.filter", event::closure_idx_bool{[&closure]
|
||
|
(const m::event::idx &event_idx)
|
||
|
{
|
||
|
static const event::fetch::opts fopts
|
||
|
{
|
||
|
event::keys::include {"state_key", "content"}
|
||
|
};
|
||
|
|
||
|
const event::fetch event
|
||
|
{
|
||
|
event_idx, std::nothrow, fopts
|
||
|
};
|
||
|
|
||
|
if(!event.valid)
|
||
|
return true;
|
||
|
|
||
|
const auto &filter_id
|
||
|
{
|
||
|
at<"state_key"_>(event)
|
||
|
};
|
||
|
|
||
|
const json::object &content
|
||
|
{
|
||
|
at<"content"_>(event)
|
||
|
};
|
||
|
|
||
|
return closure(filter_id, content);
|
||
|
}});
|
||
|
}
|