0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-02 10:08:56 +02:00

ircd:Ⓜ️ Preliminary reorg around experimental presence interface.

This commit is contained in:
Jason Volk 2018-03-04 07:15:50 -08:00
parent d3d58d8ee9
commit 69b0acde63
5 changed files with 152 additions and 70 deletions

View file

@ -13,7 +13,7 @@
namespace ircd::m
{
struct presence;
}
#pragma GCC diagnostic push
@ -23,6 +23,7 @@ struct ircd::m::edu::m_presence
<
json::property<name::user_id, json::string>,
json::property<name::presence, json::string>,
json::property<name::status_msg, json::string>,
json::property<name::last_active_ago, time_t>,
json::property<name::currently_active, bool>
>
@ -31,3 +32,16 @@ struct ircd::m::edu::m_presence
using super_type::operator=;
};
#pragma GCC diagnostic pop
struct ircd::m::presence
:edu::m_presence
{
static bool valid_state(const string_view &state);
static json::object get(const user &, const mutable_buffer &);
static event::id::buf set(const presence &);
static event::id::buf set(const user &, const string_view &, const string_view &status = {});
presence(const user &, const mutable_buffer &);
using edu::m_presence::m_presence;
};

View file

@ -39,8 +39,6 @@ struct ircd::m::user
bool is_active() const;
bool is_password(const string_view &password) const noexcept;
event::id::buf presence(const string_view &, const string_view &status = {});
void password(const string_view &password);
void deactivate(const json::members &contents = {});
event::id::buf activate(const json::members &contents = {});

View file

@ -259,7 +259,7 @@ try
{ "default", "Wanna chat? IRCd at your service!" }
};
me.presence("online", online_status_msg);
presence::set(me, "online", online_status_msg);
join(my_room, me.user_id);
}
catch(const m::ALREADY_MEMBER &e)
@ -277,25 +277,74 @@ ircd::m::leave_ircd_room()
};
leave(my_room, me.user_id);
me.presence("offline", offline_status_msg);
presence::set(me, "offline", offline_status_msg);
}
///////////////////////////////////////////////////////////////////////////////
//
// m/user.h
// m/presence.h
//
ircd::m::event::id::buf
ircd::m::user::activate(const json::members &contents)
ircd::m::presence::presence(const user &user,
const mutable_buffer &buf)
:presence
{
using prototype = event::id::buf (const m::user &, const json::members &);
get(user, buf)
}
{
}
ircd::m::event::id::buf
ircd::m::presence::set(const user &user,
const string_view &presence,
const string_view &status_msg)
{
return set(m::presence
{
{ "user_id", user.user_id },
{ "presence", presence },
{ "status_msg", status_msg },
});
}
ircd::m::event::id::buf
ircd::m::presence::set(const presence &object)
{
using prototype = event::id::buf (const presence &);
static import<prototype> function
{
"client_register", "register__user"
"m_presence", "presence_set"
};
return function(*this, contents);
return function(object);
}
ircd::json::object
ircd::m::presence::get(const user &user,
const mutable_buffer &buffer)
{
using prototype = json::object (const m::user &, const mutable_buffer &);
static import<prototype> function
{
"m_presence", "presence_get"
};
return function(user, buffer);
}
bool
ircd::m::presence::valid_state(const string_view &state)
{
using prototype = bool (const string_view &);
static import<prototype> function
{
"m_presence", "presence_valid_state"
};
return function(state);
}
ircd::m::event::id::buf

View file

@ -30,20 +30,6 @@ presence_resource
// put
//
const string_view
valid_states[]
{
"online", "offline", "unavailable"
};
static bool
valid_state(const string_view &state);
extern "C" m::event::id::buf
set__user_presence_status(const m::user::id &user_id,
const string_view &presence,
const string_view &status_msg);
static resource::response
put__presence_status(client &client,
const resource::request &request,
@ -115,61 +101,23 @@ put__presence_status(client &client,
unquote(request.at("presence"))
};
const string_view &status_msg
{
trunc(unquote(request["status_msg"]), 390)
};
set__user_presence_status(request.user_id, presence, status_msg);
return resource::response
{
client, http::OK
};
}
m::event::id::buf
set__user_presence_status(const m::user::id &user_id,
const string_view &presence,
const string_view &status_msg)
{
if(!valid_state(presence))
if(!m::presence::valid_state(presence))
throw m::UNSUPPORTED
{
"That presence state is not supported"
};
json::iov content;
const json::iov::push _presence[]
const string_view &status_msg
{
{ content, { "presence", presence } },
{ content, { "user_id", user_id } },
trunc(unquote(request["status_msg"]), 390)
};
const json::iov::set_if _status_msg
const m::user user{request.user_id};
m::presence::set(user, presence, status_msg);
return resource::response
{
content, !empty(status_msg),
{
"status_msg", status_msg
}
client, http::OK
};
const m::user::room user_room
{
user_id
};
return send(user_room, user_id, "m.presence", content);
}
bool
valid_state(const string_view &state)
{
return std::any_of(begin(valid_states), end(valid_states), [&state]
(const string_view &valid)
{
return state == valid;
});
}
//

73
modules/m_presence.cc Normal file
View file

@ -0,0 +1,73 @@
// 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.
using namespace ircd;
mapi::header
IRCD_MODULE
{
"Matrix Presence"
};
const string_view
valid_states[]
{
"online", "offline", "unavailable",
};
extern "C" bool
presence_valid_state(const string_view &state)
{
return std::any_of(begin(valid_states), end(valid_states), [&state]
(const string_view &valid)
{
return state == valid;
});
}
extern "C" m::event::id::buf
presence_set(const m::presence &content)
{
const m::user user
{
at<"user_id"_>(content)
};
const m::user::room user_room
{
user
};
return send(user_room, user.user_id, "m.presence", json::strung{content});
}
extern "C" json::object
presence_get(const m::user &user,
const mutable_buffer &buffer)
{
const m::user::room user_room
{
user
};
json::object ret;
user_room.get(std::nothrow, "m.presence", [&ret, &buffer]
(const m::event &event)
{
const string_view &content
{
at<"content"_>(event)
};
ret = { data(buffer), copy(buffer, content) };
});
return ret;
}