fix: set limited to true when skipping messages in /sync

next
Timo 2020-08-19 18:26:39 +02:00 committed by Devin Ragotzy
parent 672bf4f473
commit 972babbc79
4 changed files with 54 additions and 19 deletions

View File

@ -5,10 +5,9 @@ use ruma::{
error::ErrorKind,
r0::config::{get_global_account_data, set_global_account_data},
},
events::{custom::CustomEventContent, BasicEvent, EventType},
events::{custom::CustomEventContent, BasicEvent},
Raw,
};
use std::convert::TryFrom;
#[cfg(feature = "conduit_bin")]
use rocket::{get, put};

View File

@ -14,7 +14,7 @@ use std::collections::BTreeMap;
)]
pub fn search_events_route(
db: State<'_, Database>,
body: Ruma<search_events::Request>,
body: Ruma<search_events::IncomingRequest>,
) -> ConduitResult<search_events::Response> {
let sender_id = body.sender_id.as_ref().expect("user is authenticated");
@ -56,7 +56,8 @@ pub fn search_events_route(
result: db
.rooms
.get_pdu_from_id(&result)?
.map(|pdu| pdu.to_room_event()),
// TODO this is an awkward type conversion see method
.map(|pdu| pdu.to_any_event()),
})
})
.filter_map(|r| r.ok())
@ -70,8 +71,7 @@ pub fn search_events_route(
Some((skip + limit).to_string())
};
Ok(search_events::Response {
search_categories: ResultCategories {
Ok(search_events::Response::new(ResultCategories {
room_events: Some(ResultRoomEvents {
count: uint!(0), // TODO
groups: BTreeMap::new(), // TODO
@ -80,7 +80,6 @@ pub fn search_events_route(
state: BTreeMap::new(), // TODO
highlights: search.1,
}),
},
}
})
.into())
}

View File

@ -532,7 +532,7 @@ impl Rooms {
self.append_state_pdu(&pdu.room_id, &pdu_id, state_key, &pdu.kind)?;
}
match pdu.kind {
match &pdu.kind {
EventType::RoomRedaction => {
if let Some(redact_id) = &pdu.redacts {
// TODO: Reason
@ -553,7 +553,7 @@ impl Rooms {
}
}
EventType::RoomMember => {
if let Some(state_key) = &pdu.state_key {
if let Some(state_key) = pdu.state_key.as_ref() {
// if the state_key fails
let target_user_id = UserId::try_from(state_key.as_str())
.expect("This state_key was previously validated");
@ -576,6 +576,21 @@ impl Rooms {
)?;
}
}
EventType::RoomMessage => {
if let Some(body) = pdu.content.get("body").and_then(|b| b.as_str()) {
for word in body
.split_terminator(|c: char| !c.is_alphanumeric())
.map(str::to_lowercase)
{
let mut key = pdu.room_id.to_string().as_bytes().to_vec();
key.push(0xff);
key.extend_from_slice(word.as_bytes());
key.push(0xff);
key.extend_from_slice(&pdu_id);
self.tokenids.insert(key, &[])?;
}
}
}
_ => {}
}
self.edus.room_read_set(&pdu.room_id, &pdu.sender, index)?;

View File

@ -2,7 +2,7 @@ use crate::{Error, Result};
use js_int::UInt;
use ruma::{
events::{
pdu::EventHash, room::member::MemberEventContent, AnyRoomEvent, AnyStateEvent,
pdu::EventHash, room::member::MemberEventContent, AnyEvent, AnyRoomEvent, AnyStateEvent,
AnyStrippedStateEvent, AnySyncRoomEvent, AnySyncStateEvent, EventType, StateEvent,
},
EventId, Raw, RoomId, ServerName, UserId,
@ -99,6 +99,28 @@ impl PduEvent {
serde_json::from_value(json).expect("Raw::from_value always works")
}
/// This only works for events that are also AnyRoomEvents.
pub fn to_any_event(&self) -> Raw<AnyEvent> {
let mut json = json!({
"content": self.content,
"type": self.kind,
"event_id": self.event_id,
"sender": self.sender,
"origin_server_ts": self.origin_server_ts,
"unsigned": self.unsigned,
"room_id": self.room_id,
});
if let Some(state_key) = &self.state_key {
json["state_key"] = json!(state_key);
}
if let Some(redacts) = &self.redacts {
json["redacts"] = json!(redacts);
}
serde_json::from_value(json).expect("Raw::from_value always works")
}
pub fn to_room_event(&self) -> Raw<AnyRoomEvent> {
let mut json = json!({
"content": self.content,