2019-07-11 21:48:42 +02:00
|
|
|
// 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.
|
|
|
|
|
2019-09-17 04:18:19 +02:00
|
|
|
namespace ircd::m
|
|
|
|
{
|
|
|
|
static void handle_breadcrumb_rooms_focus_out(const event &, vm::eval &, const json::array &);
|
|
|
|
static void handle_breadcrumb_rooms_focus_in(const event &, vm::eval &, const json::array &);
|
|
|
|
static void handle_breadcrumb_rooms(const event &, vm::eval &);
|
|
|
|
extern hookfn<vm::eval &> hook_breadcrumb_rooms;
|
|
|
|
}
|
|
|
|
|
2019-07-27 01:57:27 +02:00
|
|
|
ircd::mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Matrix Breadcrumb Rooms"
|
|
|
|
};
|
2019-07-11 21:48:42 +02:00
|
|
|
|
2019-09-17 04:18:19 +02:00
|
|
|
decltype(ircd::m::hook_breadcrumb_rooms)
|
|
|
|
ircd::m::hook_breadcrumb_rooms
|
|
|
|
{
|
|
|
|
handle_breadcrumb_rooms,
|
|
|
|
{
|
|
|
|
{ "_site", "vm.effect" },
|
|
|
|
{ "type", "ircd.account_data" },
|
|
|
|
{ "state_key", "im.vector.riot.breadcrumb_rooms" },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::handle_breadcrumb_rooms(const event &event,
|
|
|
|
vm::eval &eval)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const m::user::id &sender
|
|
|
|
{
|
|
|
|
at<"sender"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
sender
|
|
|
|
};
|
|
|
|
|
|
|
|
// We only want to hook events in the user's user room.
|
|
|
|
if(at<"room_id"_>(event) != user_room.room_id)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto &content
|
|
|
|
{
|
|
|
|
json::get<"content"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::array &rooms
|
|
|
|
{
|
|
|
|
content.get("rooms")
|
|
|
|
};
|
|
|
|
|
|
|
|
handle_breadcrumb_rooms_focus_out(event, eval, rooms);
|
|
|
|
handle_breadcrumb_rooms_focus_in(event, eval, rooms);
|
|
|
|
}
|
|
|
|
catch(const ctx::interrupted &)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
m::log, "breadcrumb_rooms hook :%s",
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::handle_breadcrumb_rooms_focus_in(const event &event,
|
|
|
|
vm::eval &eval,
|
|
|
|
const json::array &rooms)
|
|
|
|
{
|
|
|
|
const room::id &focus_in
|
|
|
|
{
|
|
|
|
valid(id::ROOM, json::string{rooms[0]})?
|
|
|
|
room::id{json::string{rooms[0]}}:
|
|
|
|
room::id{}
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!focus_in)
|
|
|
|
return;
|
|
|
|
|
2019-09-25 00:42:44 +02:00
|
|
|
const size_t prefetched
|
2019-09-17 04:18:19 +02:00
|
|
|
{
|
2019-09-25 00:42:44 +02:00
|
|
|
m::room::events::prefetch_viewport(focus_in)
|
2019-09-17 04:18:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
2019-09-25 00:42:44 +02:00
|
|
|
m::log, "Prefetched %zu recent events to focus %s for %s",
|
2019-09-17 04:18:19 +02:00
|
|
|
prefetched,
|
|
|
|
string_view{focus_in},
|
|
|
|
at<"sender"_>(event),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::handle_breadcrumb_rooms_focus_out(const event &event,
|
|
|
|
vm::eval &eval,
|
|
|
|
const json::array &rooms)
|
|
|
|
{
|
|
|
|
const room::id &focus_out
|
|
|
|
{
|
|
|
|
valid(id::ROOM, json::string{rooms[1]})?
|
|
|
|
room::id{json::string{rooms[1]}}:
|
|
|
|
room::id{}
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!focus_out)
|
|
|
|
return;
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
m::log, "%s for %s out of focus",
|
|
|
|
string_view{focus_out},
|
|
|
|
at<"sender"_>(event),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-11 21:48:42 +02:00
|
|
|
ircd::m::event::id::buf
|
2019-09-25 20:35:17 +02:00
|
|
|
IRCD_MODULE_EXPORT
|
2019-07-11 21:48:42 +02:00
|
|
|
ircd::m::breadcrumb_rooms::set(const json::array &rooms)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const json::strung object
|
|
|
|
{
|
|
|
|
json::members
|
|
|
|
{
|
|
|
|
{ "rooms", rooms }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return account_data.set("im.vector.riot.breadcrumb_rooms", object);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-09-25 20:35:17 +02:00
|
|
|
IRCD_MODULE_EXPORT
|
2019-07-11 21:48:42 +02:00
|
|
|
ircd::m::breadcrumb_rooms::for_each(const closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
bool ret{true};
|
|
|
|
get(std::nothrow, [&closure, &ret]
|
|
|
|
(const json::array &rooms)
|
|
|
|
{
|
|
|
|
for(const json::string &room : rooms)
|
|
|
|
if(!closure(room))
|
|
|
|
{
|
|
|
|
ret = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-09-25 20:35:17 +02:00
|
|
|
IRCD_MODULE_EXPORT
|
2019-07-11 21:48:42 +02:00
|
|
|
ircd::m::breadcrumb_rooms::get(const closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
if(!get(std::nothrow, closure))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"User has no breadcrumb_rooms set in their account_data."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-09-25 20:35:17 +02:00
|
|
|
IRCD_MODULE_EXPORT
|
2019-07-11 21:48:42 +02:00
|
|
|
ircd::m::breadcrumb_rooms::get(std::nothrow_t,
|
|
|
|
const closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return account_data.get(std::nothrow, "im.vector.riot.breadcrumb_rooms", [&closure]
|
|
|
|
(const string_view &key, const json::object &object)
|
|
|
|
{
|
|
|
|
const json::array &rooms
|
|
|
|
{
|
|
|
|
object["rooms"]
|
|
|
|
};
|
|
|
|
|
|
|
|
closure(rooms);
|
|
|
|
});
|
|
|
|
}
|