2018-05-29 13:01:36 +02: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.
|
|
|
|
|
2020-10-26 08:37:26 +01:00
|
|
|
namespace ircd::m::search
|
|
|
|
{
|
2020-12-28 10:52:45 +01:00
|
|
|
static bool handle_result(result &, const query &);
|
|
|
|
static bool handle_content(result &, const query &, const json::object &);
|
|
|
|
static bool query_all_rooms(result &, const query &);
|
|
|
|
static bool query_room(result &, const query &, const room::id &);
|
|
|
|
static bool query_rooms(result &, const query &);
|
2020-10-26 08:37:26 +01:00
|
|
|
static void handle_room_events(client &, const resource::request &, const json::object &, json::stack::object &);
|
|
|
|
static resource::response search_post_handle(client &, const resource::request &);
|
2020-12-28 10:52:45 +01:00
|
|
|
|
2021-01-02 06:24:08 +01:00
|
|
|
extern conf::item<size_t> limit_override;
|
2020-12-28 10:52:45 +01:00
|
|
|
extern conf::item<bool> count_total;
|
2020-10-26 08:37:26 +01:00
|
|
|
extern resource::method search_post;
|
|
|
|
extern resource search_resource;
|
|
|
|
extern log::log log;
|
|
|
|
}
|
2018-05-29 13:01:36 +02:00
|
|
|
|
2019-09-14 22:14:16 +02:00
|
|
|
ircd::mapi::header
|
2018-05-29 13:01:36 +02:00
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Client 11.14 :Server Side Search"
|
|
|
|
};
|
|
|
|
|
2020-10-26 08:37:26 +01:00
|
|
|
decltype(ircd::m::search::log)
|
|
|
|
ircd::m::search::log
|
|
|
|
{
|
|
|
|
"m.search"
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::search::search_resource)
|
|
|
|
ircd::m::search::search_resource
|
2018-05-29 13:01:36 +02:00
|
|
|
{
|
|
|
|
"/_matrix/client/r0/search",
|
|
|
|
{
|
|
|
|
"(11.14.1) The search API allows clients to perform full text search"
|
|
|
|
" across events in all rooms that the user has been in, including"
|
|
|
|
" those that they have left. Only events that the user is allowed to"
|
|
|
|
" see will be searched, e.g. it won't include events in rooms that"
|
|
|
|
" happened after you left."
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-26 08:37:26 +01:00
|
|
|
decltype(ircd::m::search::search_post)
|
|
|
|
ircd::m::search::search_post
|
2019-09-14 22:14:16 +02:00
|
|
|
{
|
2020-10-26 08:37:26 +01:00
|
|
|
search_resource, "POST", search_post_handle,
|
2019-09-14 22:14:16 +02:00
|
|
|
{
|
2020-12-28 10:52:45 +01:00
|
|
|
search_post.REQUIRES_AUTH,
|
|
|
|
|
|
|
|
// Some queries can take a really long time, especially under
|
|
|
|
// development. We don't need the default request timer getting
|
|
|
|
// in the way for now.
|
|
|
|
60s,
|
2019-09-14 22:14:16 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-28 10:52:45 +01:00
|
|
|
decltype(ircd::m::search::count_total)
|
|
|
|
ircd::m::search::count_total
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.search.count.total" },
|
|
|
|
{ "default", false },
|
|
|
|
};
|
|
|
|
|
2021-01-02 06:24:08 +01:00
|
|
|
decltype(ircd::m::search::limit_override)
|
|
|
|
ircd::m::search::limit_override
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.search.limit.override" },
|
|
|
|
{ "default", 1L },
|
|
|
|
};
|
|
|
|
|
2020-10-26 08:37:26 +01:00
|
|
|
ircd::m::resource::response
|
|
|
|
ircd::m::search::search_post_handle(client &client,
|
|
|
|
const resource::request &request)
|
2018-05-29 13:01:36 +02:00
|
|
|
{
|
2019-09-14 22:14:16 +02:00
|
|
|
const json::object &search_categories
|
|
|
|
{
|
|
|
|
request["search_categories"]
|
|
|
|
};
|
2018-05-29 13:01:36 +02:00
|
|
|
|
2019-09-14 22:14:16 +02:00
|
|
|
resource::response::chunked response
|
2018-05-29 13:01:36 +02:00
|
|
|
{
|
2019-09-14 22:14:16 +02:00
|
|
|
client, http::OK
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack out
|
|
|
|
{
|
|
|
|
response.buf, response.flusher()
|
2018-05-29 13:01:36 +02:00
|
|
|
};
|
2019-09-14 22:14:16 +02:00
|
|
|
|
|
|
|
json::stack::object top
|
|
|
|
{
|
|
|
|
out
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object result_categories
|
|
|
|
{
|
|
|
|
top, "search_categories"
|
|
|
|
};
|
|
|
|
|
2020-10-26 08:37:26 +01:00
|
|
|
if(search_categories.has("room_events"))
|
|
|
|
{
|
|
|
|
json::stack::object room_events_result
|
|
|
|
{
|
|
|
|
result_categories, "room_events"
|
|
|
|
};
|
|
|
|
|
|
|
|
handle_room_events(client, request, search_categories, room_events_result);
|
|
|
|
}
|
|
|
|
|
2020-02-12 20:30:37 +01:00
|
|
|
return std::move(response);
|
2018-05-29 13:01:36 +02:00
|
|
|
}
|
|
|
|
|
2019-09-14 22:14:16 +02:00
|
|
|
void
|
2020-10-26 08:37:26 +01:00
|
|
|
ircd::m::search::handle_room_events(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const json::object &search_categories,
|
|
|
|
json::stack::object &room_events_result)
|
2019-09-14 22:14:16 +02:00
|
|
|
try
|
2018-05-29 13:01:36 +02:00
|
|
|
{
|
2019-09-14 22:14:16 +02:00
|
|
|
const m::search::room_events room_events
|
|
|
|
{
|
|
|
|
search_categories["room_events"]
|
|
|
|
};
|
|
|
|
|
2020-12-28 10:52:45 +01:00
|
|
|
const m::room_event_filter room_event_filter
|
2019-09-14 22:14:16 +02:00
|
|
|
{
|
2020-12-28 10:52:45 +01:00
|
|
|
json::get<"filter"_>(room_events)
|
2019-09-14 22:14:16 +02:00
|
|
|
};
|
|
|
|
|
2020-12-28 10:52:45 +01:00
|
|
|
const json::object &event_context
|
2019-09-14 22:14:16 +02:00
|
|
|
{
|
2020-12-28 10:52:45 +01:00
|
|
|
json::get<"event_context"_>(room_events)
|
2019-09-14 22:14:16 +02:00
|
|
|
};
|
|
|
|
|
2020-12-28 10:52:45 +01:00
|
|
|
// Spec sez default is 5. Reference client does not make any use of
|
|
|
|
// result context if provided.
|
|
|
|
const ushort context_default
|
2019-09-14 22:14:16 +02:00
|
|
|
{
|
2020-12-28 10:52:45 +01:00
|
|
|
0
|
2019-09-14 22:14:16 +02:00
|
|
|
};
|
|
|
|
|
2021-01-02 06:24:08 +01:00
|
|
|
// Override the limit to 1 to return a result and appease the user as
|
|
|
|
// quickly as possible. The client can call us again for more results.
|
|
|
|
const size_t limit
|
|
|
|
{
|
|
|
|
limit_override?
|
|
|
|
size_t(limit_override):
|
|
|
|
size_t(json::get<"limit"_>(room_event_filter))
|
|
|
|
};
|
|
|
|
|
2020-12-28 10:52:45 +01:00
|
|
|
const search::query query
|
2019-09-14 22:14:16 +02:00
|
|
|
{
|
2020-12-28 10:52:45 +01:00
|
|
|
request.user_id,
|
|
|
|
request.query.get<size_t>("next_batch", 0UL),
|
|
|
|
room_events,
|
|
|
|
room_event_filter,
|
|
|
|
at<"search_term"_>(room_events),
|
2021-01-02 06:24:08 +01:00
|
|
|
limit,
|
2020-12-28 10:52:45 +01:00
|
|
|
event_context.get("before_limit", context_default),
|
|
|
|
event_context.get("after_limit", context_default),
|
|
|
|
};
|
|
|
|
|
|
|
|
log::logf
|
|
|
|
{
|
|
|
|
log, log::DEBUG,
|
|
|
|
"Query '%s' by %s batch:%ld order_by:%s inc_state:%b rooms:%zu limit:%zu",
|
|
|
|
query.search_term,
|
|
|
|
string_view{query.user_id},
|
|
|
|
query.batch,
|
|
|
|
json::get<"order_by"_>(query.room_events),
|
|
|
|
json::get<"include_state"_>(query.room_events),
|
|
|
|
json::get<"rooms"_>(query.filter).size(),
|
|
|
|
query.limit,
|
|
|
|
};
|
|
|
|
|
|
|
|
search::result result
|
|
|
|
{
|
|
|
|
room_events_result.s
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool finished
|
|
|
|
{
|
|
|
|
query_rooms(result, query)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Spec sez this is total results, but riot doesn't use it. Counting total
|
|
|
|
// results is very expensive right now, so we'll just report the count we
|
|
|
|
// have for now...
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
room_events_result, "count", json::value
|
|
|
|
{
|
|
|
|
long(result.count + !finished)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//TODO: XXX
|
|
|
|
json::stack::array
|
|
|
|
{
|
|
|
|
room_events_result, "highlights"
|
|
|
|
};
|
|
|
|
|
|
|
|
//TODO: XXX
|
|
|
|
json::stack::object
|
|
|
|
{
|
|
|
|
room_events_result, "state"
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!finished)
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
room_events_result, "next_batch", json::value
|
|
|
|
{
|
|
|
|
lex_cast(result.skipped + result.checked), json::STRING
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
char tmbuf[48];
|
|
|
|
log::logf
|
|
|
|
{
|
|
|
|
log, log::DEBUG,
|
|
|
|
"Result '%s' by %s batch[%ld -> %ld] count:%lu append:%lu match:%lu check:%lu skip:%lu in %s",
|
|
|
|
query.search_term,
|
|
|
|
string_view{query.user_id},
|
|
|
|
query.batch,
|
|
|
|
result.event_idx,
|
|
|
|
result.count,
|
|
|
|
result.appends,
|
|
|
|
result.matched,
|
|
|
|
result.checked,
|
|
|
|
result.skipped,
|
|
|
|
result.elapsed.pretty(tmbuf),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const std::system_error &)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "search :%s",
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::search::query_rooms(result &result,
|
|
|
|
const query &query)
|
|
|
|
{
|
|
|
|
const json::array rooms
|
|
|
|
{
|
|
|
|
json::get<"rooms"_>(query.filter)
|
2019-09-14 22:14:16 +02:00
|
|
|
};
|
|
|
|
|
2020-10-26 08:37:26 +01:00
|
|
|
json::stack::array results
|
|
|
|
{
|
2020-12-28 10:52:45 +01:00
|
|
|
*result.out, "results"
|
2020-10-26 08:37:26 +01:00
|
|
|
};
|
|
|
|
|
2020-12-28 10:52:45 +01:00
|
|
|
if(rooms.empty())
|
|
|
|
return query_all_rooms(result, query);
|
|
|
|
|
|
|
|
for(const json::string room_id : rooms)
|
|
|
|
if(!query_room(result, query, room_id))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::search::query_room(result &result,
|
|
|
|
const query &query,
|
|
|
|
const room::id &room_id)
|
|
|
|
{
|
|
|
|
const m::room room
|
2019-09-14 22:14:16 +02:00
|
|
|
{
|
2020-12-28 10:52:45 +01:00
|
|
|
room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!visible(room, query.user_id))
|
|
|
|
throw m::ACCESS_DENIED
|
2019-09-14 22:14:16 +02:00
|
|
|
{
|
2020-12-28 10:52:45 +01:00
|
|
|
"You are not permitted to view %s",
|
|
|
|
string_view{room_id},
|
2019-09-14 22:14:16 +02:00
|
|
|
};
|
|
|
|
|
2020-12-28 10:52:45 +01:00
|
|
|
const m::room::content content
|
|
|
|
{
|
|
|
|
room
|
|
|
|
};
|
|
|
|
|
|
|
|
return content.for_each([&result, &query]
|
|
|
|
(const json::object &content, const auto &depth, const auto &event_idx)
|
|
|
|
{
|
|
|
|
result.event_idx = event_idx;
|
|
|
|
return handle_content(result, query, content);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::search::query_all_rooms(result &result,
|
|
|
|
const query &query)
|
|
|
|
{
|
|
|
|
if(!is_oper(query.user_id))
|
|
|
|
throw m::ACCESS_DENIED
|
2019-09-14 22:14:16 +02:00
|
|
|
{
|
2020-12-28 10:52:45 +01:00
|
|
|
"You are not an operator."
|
2019-09-14 22:14:16 +02:00
|
|
|
};
|
|
|
|
|
2020-12-28 10:52:45 +01:00
|
|
|
return m::events::content::for_each([&result, &query]
|
|
|
|
(const auto &event_idx, const json::object &content)
|
|
|
|
{
|
|
|
|
result.event_idx = event_idx;
|
|
|
|
return handle_content(result, query, content);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::search::handle_content(result &result,
|
|
|
|
const query &query,
|
|
|
|
const json::object &content)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if(result.skipped < query.batch)
|
|
|
|
{
|
|
|
|
++result.skipped;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const json::string body
|
|
|
|
{
|
|
|
|
content["body"]
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool match
|
|
|
|
{
|
|
|
|
has(body, query.search_term)
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool handled
|
|
|
|
{
|
|
|
|
match && handle_result(result, query)
|
|
|
|
};
|
|
|
|
|
|
|
|
result.checked += 1;
|
|
|
|
result.matched += match;
|
|
|
|
result.count += handled;
|
|
|
|
return result.count < query.limit;
|
|
|
|
}
|
|
|
|
catch(const ctx::interrupted &e)
|
|
|
|
{
|
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
log, "Query handling '%s' by '%s' event_idx:%lu :%s",
|
|
|
|
query.search_term,
|
|
|
|
string_view{query.user_id},
|
|
|
|
result.event_idx,
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::system_error &e)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
log, "Query handling for '%s' by '%s' event_idx:%lu :%s",
|
|
|
|
query.search_term,
|
|
|
|
string_view{query.user_id},
|
|
|
|
result.event_idx,
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "Query handling for '%s' by '%s' event_idx:%lu :%s",
|
|
|
|
query.search_term,
|
|
|
|
string_view{query.user_id},
|
|
|
|
result.event_idx,
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::search::handle_result(result &result,
|
|
|
|
const query &query)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const m::event_filter event_filter
|
|
|
|
{
|
|
|
|
query.filter
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::event::fetch event
|
|
|
|
{
|
|
|
|
result.event_idx
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(result.out);
|
|
|
|
json::stack::checkpoint cp
|
|
|
|
{
|
|
|
|
*result.out, false
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object object
|
|
|
|
{
|
|
|
|
*result.out
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
object, "rank", json::value(result.rank)
|
|
|
|
};
|
|
|
|
|
|
|
|
m::event::append::opts opts;
|
|
|
|
opts.event_idx = &result.event_idx;
|
|
|
|
opts.user_id = &query.user_id;
|
|
|
|
opts.event_filter = &event_filter;
|
|
|
|
opts.query_prev_state = false;
|
|
|
|
opts.query_visible = true;
|
|
|
|
bool ret{false};
|
|
|
|
{
|
2019-09-14 22:14:16 +02:00
|
|
|
json::stack::object result_event
|
|
|
|
{
|
2020-12-28 10:52:45 +01:00
|
|
|
object, "result"
|
2019-09-14 22:14:16 +02:00
|
|
|
};
|
|
|
|
|
2020-12-28 10:52:45 +01:00
|
|
|
ret = event::append(result_event, event, opts);
|
|
|
|
result.appends += ret;
|
|
|
|
cp.committing(ret);
|
2019-09-14 22:14:16 +02:00
|
|
|
}
|
|
|
|
|
2020-12-28 10:52:45 +01:00
|
|
|
if(!query.before_limit && !query.after_limit)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
const m::room room
|
2019-09-14 22:14:16 +02:00
|
|
|
{
|
2020-12-28 10:52:45 +01:00
|
|
|
json::get<"room_id"_>(event)
|
2019-09-14 22:14:16 +02:00
|
|
|
};
|
|
|
|
|
2020-12-28 10:52:45 +01:00
|
|
|
m::room::events it
|
2019-09-14 22:14:16 +02:00
|
|
|
{
|
2020-12-28 10:52:45 +01:00
|
|
|
room
|
2019-09-14 22:14:16 +02:00
|
|
|
};
|
|
|
|
|
2020-12-28 10:52:45 +01:00
|
|
|
json::stack::object result_context
|
2019-09-14 22:14:16 +02:00
|
|
|
{
|
2020-12-28 10:52:45 +01:00
|
|
|
object, "context"
|
2019-09-14 22:14:16 +02:00
|
|
|
};
|
2020-12-28 10:52:45 +01:00
|
|
|
|
|
|
|
size_t before(0);
|
|
|
|
if(likely(!it.seek(result.event_idx)))
|
|
|
|
{
|
|
|
|
json::stack::array events_before
|
|
|
|
{
|
|
|
|
result_context, "events_before"
|
|
|
|
};
|
|
|
|
|
|
|
|
for(--it; it && before < query.before_limit; ++before, --it)
|
|
|
|
{
|
|
|
|
const event::idx event_idx
|
|
|
|
{
|
|
|
|
it.event_idx()
|
|
|
|
};
|
|
|
|
|
|
|
|
opts.event_idx = &event_idx;
|
|
|
|
result.appends += event::append(events_before, event, opts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t after(0);
|
|
|
|
if(likely(it.seek(result.event_idx)))
|
|
|
|
{
|
|
|
|
json::stack::array events_after
|
|
|
|
{
|
|
|
|
result_context, "events_after"
|
|
|
|
};
|
|
|
|
|
|
|
|
for(++it; it && after < query.after_limit; ++after, ++it)
|
|
|
|
{
|
|
|
|
const event::idx event_idx
|
|
|
|
{
|
|
|
|
it.event_idx()
|
|
|
|
};
|
|
|
|
|
|
|
|
opts.event_idx = &event_idx;
|
|
|
|
result.appends += event::append(events_after, event, opts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2019-09-14 22:14:16 +02:00
|
|
|
}
|
2020-12-28 10:52:45 +01:00
|
|
|
catch(const ctx::interrupted &)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::system_error &e)
|
2019-09-14 22:14:16 +02:00
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2020-12-28 10:52:45 +01:00
|
|
|
log, "Result handling for '%s' by '%s' event_idx:%lu :%s",
|
|
|
|
query.search_term,
|
|
|
|
string_view{query.user_id},
|
|
|
|
result.event_idx,
|
|
|
|
e.what(),
|
2019-09-14 22:14:16 +02:00
|
|
|
};
|
2020-12-28 10:52:45 +01:00
|
|
|
|
|
|
|
return false;
|
2019-09-14 22:14:16 +02:00
|
|
|
}
|