mirror of
https://github.com/matrix-construct/construct
synced 2024-09-27 19:28:52 +02:00
modules/m_direct: Modify the user's account_data for m.direct.
This commit is contained in:
parent
7117e943ed
commit
dca95cd1e3
2 changed files with 129 additions and 0 deletions
|
@ -114,6 +114,7 @@ m_noop_la_SOURCES = m_noop.cc
|
|||
m_user_la_SOURCES = m_user.cc
|
||||
m_event_la_SOURCES = m_event.cc
|
||||
m_device_la_SOURCES = m_device.cc
|
||||
m_direct_la_SOURCES = m_direct.cc
|
||||
m_typing_la_SOURCES = m_typing.cc
|
||||
m_receipt_la_SOURCES = m_receipt.cc
|
||||
m_presence_la_SOURCES = m_presence.cc
|
||||
|
@ -137,6 +138,7 @@ m_module_LTLIBRARIES = \
|
|||
m_user.la \
|
||||
m_event.la \
|
||||
m_device.la \
|
||||
m_direct.la \
|
||||
m_typing.la \
|
||||
m_receipt.la \
|
||||
m_presence.la \
|
||||
|
|
127
modules/m_direct.cc
Normal file
127
modules/m_direct.cc
Normal file
|
@ -0,0 +1,127 @@
|
|||
// 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.
|
||||
|
||||
using namespace ircd;
|
||||
|
||||
mapi::header
|
||||
IRCD_MODULE
|
||||
{
|
||||
"Client 14.23 :Direct Messaging"
|
||||
};
|
||||
|
||||
static void
|
||||
_join_room__m_direct(const m::event &event,
|
||||
m::vm::eval &eval);
|
||||
|
||||
const m::hookfn<m::vm::eval &>
|
||||
_join_room__m_direct_hookfn
|
||||
{
|
||||
{
|
||||
{ "_site", "vm.effect" },
|
||||
{ "type", "m.room.member" },
|
||||
{ "membership", "join" },
|
||||
{ "origin", my_host() },
|
||||
},
|
||||
_join_room__m_direct
|
||||
};
|
||||
|
||||
void
|
||||
_join_room__m_direct(const m::event &event,
|
||||
m::vm::eval &eval)
|
||||
{
|
||||
const m::user::id &user_id
|
||||
{
|
||||
at<"sender"_>(event)
|
||||
};
|
||||
|
||||
const m::room room
|
||||
{
|
||||
at<"room_id"_>(event)
|
||||
};
|
||||
|
||||
const auto membership_event_idx
|
||||
{
|
||||
room.get(std::nothrow, "m.room.member", user_id)
|
||||
};
|
||||
|
||||
if(!membership_event_idx)
|
||||
return;
|
||||
|
||||
const auto prev_membership_event_idx
|
||||
{
|
||||
m::room::state::prev(membership_event_idx)
|
||||
};
|
||||
|
||||
if(!prev_membership_event_idx)
|
||||
return;
|
||||
|
||||
bool is_direct {false};
|
||||
m::get(std::nothrow, prev_membership_event_idx, "content", [&is_direct]
|
||||
(const json::object &content)
|
||||
{
|
||||
if(unquote(content.get("membership")) == "invite")
|
||||
is_direct = content.get<bool>("is_direct", false);
|
||||
});
|
||||
|
||||
if(!is_direct)
|
||||
return;
|
||||
|
||||
m::user::id::buf other_person;
|
||||
const m::room::members members{room};
|
||||
members.for_each(m::room::members::closure_bool{[&user_id, &other_person]
|
||||
(const auto &other_id)
|
||||
{
|
||||
if(other_id == user_id)
|
||||
return true;
|
||||
|
||||
other_person = other_id;
|
||||
return false;
|
||||
}});
|
||||
|
||||
if(!other_person)
|
||||
return;
|
||||
|
||||
const unique_buffer<mutable_buffer> buf
|
||||
{
|
||||
48_KiB
|
||||
};
|
||||
|
||||
json::stack out{buf};
|
||||
json::stack::object top{out};
|
||||
const m::user::account_data account_data{user_id};
|
||||
account_data.get("m.direct", [&room, &other_person, &top]
|
||||
(const string_view &, const json::object &object)
|
||||
{
|
||||
for(const auto &[user_id, room_ids] : object)
|
||||
{
|
||||
if(user_id != other_person)
|
||||
{
|
||||
json::stack::member
|
||||
{
|
||||
top, user_id, json::array(room_ids)
|
||||
};
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
json::stack::array user_rooms
|
||||
{
|
||||
top, user_id
|
||||
};
|
||||
|
||||
user_rooms.append(room.room_id);
|
||||
for(const auto &room_id : json::array(room_ids))
|
||||
user_rooms.append(room_id);
|
||||
}
|
||||
});
|
||||
|
||||
top.~object();
|
||||
account_data.set("m.direct", json::object(out.completed()));
|
||||
}
|
Loading…
Reference in a new issue