0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-09-28 11:48:54 +02:00

ircd:Ⓜ️:user: Add pushers interface.

This commit is contained in:
Jason Volk 2020-03-21 11:14:21 -07:00
parent 245195488c
commit 63be2183f9
4 changed files with 190 additions and 0 deletions

View file

@ -0,0 +1,35 @@
// 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.
#pragma once
#define HAVE_IRCD_M_USER_PUSHERS_H
struct ircd::m::user::pushers
{
using closure_bool = std::function<bool (const string_view &, const json::object &)>;
using closure = std::function<void (const string_view &, const json::object &)>;
m::user user;
public:
bool for_each(const closure_bool &) const;
bool get(std::nothrow_t, const string_view &key, const closure &) const;
void get(const string_view &key, const closure &) const;
bool set(const json::object &value) const;
bool del(const string_view &key) const;
pushers(const m::user &user) noexcept;
};
inline
ircd::m::user::pushers::pushers(const m::user &user)
noexcept
:user{user}
{}

View file

@ -43,6 +43,7 @@ struct ircd::m::user
struct ignores;
struct registar;
struct pushrules;
struct pushers;
using id = m::id::user;
using closure = std::function<void (const user &)>;
@ -92,3 +93,4 @@ const
#include "ignores.h"
#include "register.h"
#include "pushrules.h"
#include "pushers.h"

View file

@ -96,6 +96,7 @@ libircd_matrix_la_SOURCES += user_rooms.cc
libircd_matrix_la_SOURCES += user_filter.cc
libircd_matrix_la_SOURCES += user_mitsein.cc
libircd_matrix_la_SOURCES += user_profile.cc
libircd_matrix_la_SOURCES += user_pushers.cc
libircd_matrix_la_SOURCES += user_pushrules.cc
libircd_matrix_la_SOURCES += user_register.cc
libircd_matrix_la_SOURCES += user_room_account_data.cc

152
matrix/user_pushers.cc Normal file
View file

@ -0,0 +1,152 @@
// 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.
bool
ircd::m::user::pushers::del(const string_view &key)
const
{
const m::user::room user_room
{
user
};
static const auto &type
{
push::pusher::type_prefix
};
const event::idx &event_idx
{
user_room.get(type, key)
};
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
ircd::m::user::pushers::set(const json::object &content)
const
{
const json::string &key
{
content.at("pushkey")
};
const json::string &kind
{
content.at("kind")
};
if(kind == "null")
return del(key);
const m::user::room user_room
{
user
};
static const auto &type
{
push::pusher::type_prefix
};
const auto pusher_event_id
{
m::send(user_room, user, type, key, content)
};
return true;
}
void
ircd::m::user::pushers::get(const string_view &key,
const closure &closure)
const
{
if(!get(std::nothrow, key, closure))
throw m::NOT_FOUND
{
"pusher '%s' for user %s not found",
key,
string_view{user.user_id}
};
}
bool
ircd::m::user::pushers::get(std::nothrow_t,
const string_view &key,
const closure &closure)
const
{
const m::user::room user_room
{
user
};
static const auto &type
{
push::pusher::type_prefix
};
const event::idx &event_idx
{
user_room.get(type, key)
};
return m::get(std::nothrow, event_idx, "content", [&key, &closure]
(const json::object &content)
{
closure(key, content);
});
}
bool
ircd::m::user::pushers::for_each(const closure_bool &closure)
const
{
const user::room user_room
{
user
};
const room::state state
{
user_room
};
static const auto &type
{
push::pusher::type_prefix
};
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)
{
assert(type == _type);
return m::query<bool>(std::nothrow, event_idx, "content", true, [&state_key, &closure]
(const json::object &content)
{
return closure(state_key, content);
});
}});
}