0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-03 04:02:47 +01:00

modules/client/rooms/messages: Simplify the messages iteration.

This commit is contained in:
Jason Volk 2019-03-04 15:46:44 -08:00
parent 1dcf36adc8
commit d8a9f484a4

View file

@ -110,70 +110,72 @@ get__messages(client &client,
response.buf, response.flusher() response.buf, response.flusher()
}; };
json::stack::object ret json::stack::object top
{ {
out out
}; };
// Spec sez the 'from' token is exclusive // Spec sez the 'from' token is exclusive
if(it && page.dir == 'b') if(page.from && it)
{
if(page.dir == 'b')
--it; --it;
else if(it) else
++it; ++it;
}
m::event::id::buf start m::event::id::buf start
{ {
page.dir == 'b'? page.dir == 'b'? page.from : m::event::id::buf{}
page.from:
m::event::id::buf{}
}; };
m::event::id::buf end; m::event::id::buf end
{ {
json::stack::member member{ret, "chunk"}; page.dir == 'b'? page.to : m::event::id::buf{}
json::stack::array chunk{member}; };
json::stack::array chunk
{
top, "chunk"
};
size_t hit{0}, miss{0}; size_t hit{0}, miss{0};
for(; it; page.dir == 'b'? --it : ++it) for(; it; page.dir == 'b'? --it : ++it)
{ {
const m::event &event{*it}; const m::event &event{*it};
if(!visible(event, request.user_id))
break;
if(page.to && at<"event_id"_>(event) == page.to)
{
if(page.dir != 'b') if(page.dir != 'b')
start = at<"event_id"_>(event); start = at<"event_id"_>(event);
else
end = at<"event_id"_>(event);
if(hit >= page.limit || miss >= size_t(max_filter_miss))
break; break;
if(!visible(event, request.user_id))
{
++miss;
continue;
} }
if(empty(filter_json) || match(filter, event)) if(!empty(filter_json) && !match(filter, event))
{ {
++miss;
continue;
}
_append(chunk, event, it.event_idx()); _append(chunk, event, it.event_idx());
++hit; ++hit;
} }
else ++miss; chunk.~array();
if(hit >= page.limit || miss >= size_t(max_filter_miss))
{
if(page.dir == 'b')
end = at<"event_id"_>(event);
else
start = at<"event_id"_>(event);
break;
}
}
}
json::stack::member json::stack::member
{ {
ret, "start", json::value{start} top, "start", json::value{start}
}; };
json::stack::member json::stack::member
{ {
ret, "end", json::value{end} top, "end", json::value{end}
}; };
return {}; return {};