2018-02-14 21:42:25 +01:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
#include "user.h"
|
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
put__account_data(client &client,
|
|
|
|
const resource::request &request,
|
2018-04-09 04:12:22 +02:00
|
|
|
const m::user &user)
|
2018-02-14 21:42:25 +01:00
|
|
|
{
|
2018-04-09 04:12:22 +02:00
|
|
|
if(request.parv.size() < 3)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
|
|
|
"type path parameter required"
|
|
|
|
};
|
|
|
|
|
2019-02-18 21:25:00 +01:00
|
|
|
char typebuf[m::event::TYPE_MAX_SIZE];
|
2018-04-09 04:12:22 +02:00
|
|
|
const string_view type
|
|
|
|
{
|
2018-12-07 01:41:47 +01:00
|
|
|
url::decode(typebuf, request.parv[2])
|
2018-04-09 04:12:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const json::object value
|
|
|
|
{
|
|
|
|
request
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto event_id
|
|
|
|
{
|
2019-02-21 20:53:59 +01:00
|
|
|
m::user::account_data{user}.set(type, value)
|
2018-04-09 04:12:22 +02:00
|
|
|
};
|
|
|
|
|
2018-02-14 21:42:25 +01:00
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, http::OK
|
|
|
|
};
|
|
|
|
}
|
2018-04-09 04:12:22 +02:00
|
|
|
|
|
|
|
resource::response
|
|
|
|
get__account_data(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const m::user &user)
|
|
|
|
{
|
|
|
|
if(request.parv.size() < 3)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
|
|
|
"type path parameter required"
|
|
|
|
};
|
|
|
|
|
2019-02-18 21:25:00 +01:00
|
|
|
char typebuf[m::event::TYPE_MAX_SIZE];
|
2018-04-09 04:12:22 +02:00
|
|
|
const string_view type
|
|
|
|
{
|
2018-12-07 01:41:47 +01:00
|
|
|
url::decode(typebuf, request.parv[2])
|
2018-04-09 04:12:22 +02:00
|
|
|
};
|
|
|
|
|
2019-02-21 20:53:59 +01:00
|
|
|
m::user::account_data{user}.get(type, [&client]
|
|
|
|
(const string_view &type, const json::object &value)
|
2018-04-09 04:12:22 +02:00
|
|
|
{
|
|
|
|
resource::response
|
|
|
|
{
|
|
|
|
client, value
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return {}; // responded from closure
|
|
|
|
}
|
|
|
|
|
2019-02-21 20:53:59 +01:00
|
|
|
ircd::m::event::id::buf
|
|
|
|
IRCD_MODULE_EXPORT
|
2019-07-14 00:17:23 +02:00
|
|
|
ircd::m::user::account_data::set(const string_view &type,
|
2019-02-21 20:53:59 +01:00
|
|
|
const json::object &value)
|
2019-07-14 00:17:23 +02:00
|
|
|
const
|
2018-04-09 04:12:22 +02:00
|
|
|
{
|
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
user
|
|
|
|
};
|
|
|
|
|
2019-02-21 20:53:59 +01:00
|
|
|
return send(user_room, user, "ircd.account_data", type, value);
|
|
|
|
}
|
|
|
|
|
2019-07-14 00:17:23 +02:00
|
|
|
ircd::json::object
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::user::account_data::get(const mutable_buffer &out,
|
|
|
|
const string_view &type)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
json::object ret;
|
|
|
|
get(std::nothrow, type, [&out, &ret]
|
|
|
|
(const string_view &type, const json::object &val)
|
|
|
|
{
|
|
|
|
ret = string_view { data(out), copy(out, val) };
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::user::account_data::get(const string_view &type,
|
|
|
|
const closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
if(!get(std::nothrow, type, closure))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"account data type '%s' for user %s not found",
|
|
|
|
type,
|
|
|
|
string_view{user.user_id}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-21 20:53:59 +01:00
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::user::account_data::get(std::nothrow_t,
|
|
|
|
const string_view &type,
|
|
|
|
const closure &closure)
|
2019-07-14 00:17:23 +02:00
|
|
|
const
|
2019-02-21 20:53:59 +01:00
|
|
|
{
|
|
|
|
const user::room user_room{user};
|
|
|
|
const room::state state{user_room};
|
|
|
|
const event::idx &event_idx
|
2018-04-09 04:12:22 +02:00
|
|
|
{
|
2019-02-21 20:53:59 +01:00
|
|
|
state.get(std::nothrow, "ircd.account_data", type)
|
|
|
|
};
|
2018-04-09 04:12:22 +02:00
|
|
|
|
2019-02-21 20:53:59 +01:00
|
|
|
return event_idx && m::get(std::nothrow, event_idx, "content", [&type, &closure]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
closure(type, content);
|
2018-04-09 04:12:22 +02:00
|
|
|
});
|
|
|
|
}
|
2019-02-21 20:53:59 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
2019-07-14 00:17:23 +02:00
|
|
|
ircd::m::user::account_data::for_each(const closure_bool &closure)
|
|
|
|
const
|
2018-04-09 04:12:22 +02:00
|
|
|
{
|
2019-02-21 20:53:59 +01:00
|
|
|
static const event::fetch::opts fopts
|
2018-04-09 04:12:22 +02:00
|
|
|
{
|
2019-02-21 20:53:59 +01:00
|
|
|
event::keys::include {"state_key", "content"}
|
2018-04-09 04:12:22 +02:00
|
|
|
};
|
|
|
|
|
2019-02-21 20:53:59 +01:00
|
|
|
const user::room user_room{user};
|
|
|
|
const room::state state
|
2018-04-09 04:12:22 +02:00
|
|
|
{
|
2019-02-21 20:53:59 +01:00
|
|
|
user_room, &fopts
|
2018-04-09 04:12:22 +02:00
|
|
|
};
|
|
|
|
|
2019-02-21 20:53:59 +01:00
|
|
|
return state.for_each("ircd.account_data", event::closure_bool{[&closure]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
const auto &key
|
|
|
|
{
|
|
|
|
at<"state_key"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &val
|
|
|
|
{
|
|
|
|
json::get<"content"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
return closure(key, val);
|
|
|
|
}});
|
2018-04-09 04:12:22 +02:00
|
|
|
}
|