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

merge-requests/22/head
Timo 4 years ago committed by Devin Ragotzy
parent 672bf4f473
commit 972babbc79

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

@ -14,7 +14,7 @@ use std::collections::BTreeMap;
)] )]
pub fn search_events_route( pub fn search_events_route(
db: State<'_, Database>, db: State<'_, Database>,
body: Ruma<search_events::Request>, body: Ruma<search_events::IncomingRequest>,
) -> ConduitResult<search_events::Response> { ) -> ConduitResult<search_events::Response> {
let sender_id = body.sender_id.as_ref().expect("user is authenticated"); let sender_id = body.sender_id.as_ref().expect("user is authenticated");
@ -56,7 +56,8 @@ pub fn search_events_route(
result: db result: db
.rooms .rooms
.get_pdu_from_id(&result)? .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()) .filter_map(|r| r.ok())
@ -70,17 +71,15 @@ pub fn search_events_route(
Some((skip + limit).to_string()) Some((skip + limit).to_string())
}; };
Ok(search_events::Response { Ok(search_events::Response::new(ResultCategories {
search_categories: ResultCategories { room_events: Some(ResultRoomEvents {
room_events: Some(ResultRoomEvents { count: uint!(0), // TODO
count: uint!(0), // TODO groups: BTreeMap::new(), // TODO
groups: BTreeMap::new(), // TODO next_batch,
next_batch, results,
results, state: BTreeMap::new(), // TODO
state: BTreeMap::new(), // TODO highlights: search.1,
highlights: search.1, }),
}), })
},
}
.into()) .into())
} }

@ -532,7 +532,7 @@ impl Rooms {
self.append_state_pdu(&pdu.room_id, &pdu_id, state_key, &pdu.kind)?; self.append_state_pdu(&pdu.room_id, &pdu_id, state_key, &pdu.kind)?;
} }
match pdu.kind { match &pdu.kind {
EventType::RoomRedaction => { EventType::RoomRedaction => {
if let Some(redact_id) = &pdu.redacts { if let Some(redact_id) = &pdu.redacts {
// TODO: Reason // TODO: Reason
@ -553,7 +553,7 @@ impl Rooms {
} }
} }
EventType::RoomMember => { 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 // if the state_key fails
let target_user_id = UserId::try_from(state_key.as_str()) let target_user_id = UserId::try_from(state_key.as_str())
.expect("This state_key was previously validated"); .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)?; self.edus.room_read_set(&pdu.room_id, &pdu.sender, index)?;

@ -2,7 +2,7 @@ use crate::{Error, Result};
use js_int::UInt; use js_int::UInt;
use ruma::{ use ruma::{
events::{ events::{
pdu::EventHash, room::member::MemberEventContent, AnyRoomEvent, AnyStateEvent, pdu::EventHash, room::member::MemberEventContent, AnyEvent, AnyRoomEvent, AnyStateEvent,
AnyStrippedStateEvent, AnySyncRoomEvent, AnySyncStateEvent, EventType, StateEvent, AnyStrippedStateEvent, AnySyncRoomEvent, AnySyncStateEvent, EventType, StateEvent,
}, },
EventId, Raw, RoomId, ServerName, UserId, EventId, Raw, RoomId, ServerName, UserId,
@ -99,6 +99,28 @@ impl PduEvent {
serde_json::from_value(json).expect("Raw::from_value always works") 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> { pub fn to_room_event(&self) -> Raw<AnyRoomEvent> {
let mut json = json!({ let mut json = json!({
"content": self.content, "content": self.content,

Loading…
Cancel
Save