[fractal/backend: 9/9] Fix model tables with latest master changes
- From: Daniel Garcia Moreno <danigm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [fractal/backend: 9/9] Fix model tables with latest master changes
- Date: Wed, 30 Jan 2019 11:54:02 +0000 (UTC)
commit eabb8c9288d74335bc1f7a4e13f7fcea223bc9dc
Author: Daniel GarcĂa Moreno <dani danigm net>
Date: Wed Jan 30 12:50:44 2019 +0100
Fix model tables with latest master changes
fractal-backend/src/model/message.rs | 6 +-----
fractal-backend/src/model/room.rs | 31 ++++++++++++-------------------
fractal-backend/tests/models.rs | 30 +++++++++++++++++++++---------
3 files changed, 34 insertions(+), 33 deletions(-)
---
diff --git a/fractal-backend/src/model/message.rs b/fractal-backend/src/model/message.rs
index 30a7abd8..cc445652 100644
--- a/fractal-backend/src/model/message.rs
+++ b/fractal-backend/src/model/message.rs
@@ -21,12 +21,8 @@ impl Model for Message {
"message"
}
- // TODO: we need a non optional id here
fn get_id(&self) -> &str {
- match self.id.as_ref() {
- Some(r) => r,
- None => "",
- }
+ &self.id
}
fn fields() -> Vec<&'static str> {
diff --git a/fractal-backend/src/model/room.rs b/fractal-backend/src/model/room.rs
index 4b389bd5..3d846a1b 100644
--- a/fractal-backend/src/model/room.rs
+++ b/fractal-backend/src/model/room.rs
@@ -1,6 +1,6 @@
use std::collections::HashMap;
-pub use api::types::Room;
+pub use api::types::{Room, RoomMembership};
use failure::err_msg;
use failure::Error;
@@ -33,9 +33,7 @@ impl Model for Room {
"n_members",
"notifications",
"highlight",
- "fav",
- "left",
- "inv",
+ "membership",
"direct",
"prev_batch",
"power_levels",
@@ -43,8 +41,6 @@ impl Model for Room {
}
fn create_sql() -> String {
- //TODO: implements relations for:
- // inv_sender: Option<Member>,
format!(
"
CREATE TABLE if not exists {} (
@@ -58,9 +54,7 @@ impl Model for Room {
n_members NUMBER NOT NULL,
notifications NUMBER NOT NULL,
highlight NUMBER NOT NULL,
- fav BOOLEAN NOT NULL,
- left BOOLEAN NOT NULL,
- inv BOOLEAN NOT NULL,
+ membership TEXT NOT NULL,
direct BOOLEAN NOT NULL,
prev_batch TEXT,
power_levels TEXT NOT NULL
@@ -85,6 +79,7 @@ impl Model for Room {
);
let power_levels = serde_json::to_string(&self.power_levels)?;
+ let membership = serde_json::to_string(&self.membership)?;
conn(
move |c| {
@@ -101,9 +96,7 @@ impl Model for Room {
&self.n_members,
&self.notifications,
&self.highlight,
- &self.fav,
- &self.left,
- &self.inv,
+ &membership,
&self.direct,
&self.prev_batch,
&power_levels,
@@ -117,7 +110,10 @@ impl Model for Room {
}
fn map_row(row: &Row) -> Self {
- let strp: String = row.get(15);
+ let strp: String = row.get(10);
+ let membership: RoomMembership = serde_json::from_str(&strp).unwrap_or_default();
+
+ let strp: String = row.get(13);
let power_levels: HashMap<String, i32> = serde_json::from_str(&strp).unwrap_or_default();
Self {
@@ -131,14 +127,11 @@ impl Model for Room {
n_members: row.get(7),
notifications: row.get(8),
highlight: row.get(9),
- fav: row.get(10),
- left: row.get(11),
- inv: row.get(12),
- direct: row.get(13),
- prev_batch: row.get(14),
+ membership: membership,
+ direct: row.get(11),
+ prev_batch: row.get(12),
power_levels: power_levels,
- inv_sender: None,
messages: vec![],
members: HashMap::new(),
}
diff --git a/fractal-backend/tests/models.rs b/fractal-backend/tests/models.rs
index 684b41f9..80983e11 100644
--- a/fractal-backend/tests/models.rs
+++ b/fractal-backend/tests/models.rs
@@ -1,5 +1,6 @@
extern crate chrono;
extern crate fractal_backend;
+extern crate fractal_matrix_api as api;
use fractal_backend::init_local as init;
use fractal_backend::model::Member;
@@ -9,8 +10,19 @@ use fractal_backend::model::MessageModel;
use fractal_backend::model::Model;
use fractal_backend::model::Room;
+use api::types::{RoomMembership, RoomTag};
+
use chrono::prelude::*;
+fn defmsg() -> Message {
+ Message::new(
+ "ROOM".to_string(),
+ "SENDER".to_string(),
+ "BODY".to_string(),
+ "m.text".to_string(),
+ )
+}
+
#[test]
fn room_model() {
let _ = init("").unwrap();
@@ -18,7 +30,7 @@ fn room_model() {
let created = Room::create_table();
assert!(created.is_ok());
- let mut r = Room::new("ROOM ID".to_string(), Some("ROOM NAME".to_string()));
+ let mut r = Room::new("ROOM ID".to_string(), RoomMembership::Joined(RoomTag::None));
let stored = r.store();
assert!(stored.is_ok());
@@ -51,8 +63,8 @@ fn message_model() {
let created = Message::create_table();
assert!(created.is_ok());
- let mut msg = Message::default();
- msg.id = Some("MSGID".to_string());
+ let mut msg = defmsg();
+ msg.id = "MSGID".to_string();
let stored = msg.store();
assert!(stored.is_ok());
@@ -75,22 +87,22 @@ fn message_room_relation() {
let created = Message::create_table();
assert!(created.is_ok());
- let r = Room::new("ROOM ID".to_string(), Some("ROOM NAME".to_string()));
+ let r = Room::new("ROOM ID".to_string(), RoomMembership::Joined(RoomTag::None));
let stored = r.store();
assert!(stored.is_ok());
- let mut msg = Message::default();
+ let mut msg = defmsg();
msg.room = r.id.clone();
for i in 0..100 {
- msg.id = Some(format!("MSG {}", i));
+ msg.id = format!("MSG {}", i);
msg.date = Local.ymd(1970, 1, 1).and_hms(0, i / 60, i % 60);
let _ = msg.store();
}
msg.room = "ROOM ID 2".to_string();
for i in 0..100 {
- msg.id = Some(format!("MSG ROOM2 {}", i));
+ msg.id = format!("MSG ROOM2 {}", i);
msg.date = Local.ymd(1970, 1, 1).and_hms(0, i / 60, i % 60);
let _ = msg.store();
}
@@ -99,7 +111,7 @@ fn message_room_relation() {
let items = Message::get_range(&r.id, Some(10), Some(i * 10)).unwrap();
for (j, m) in items.iter().enumerate() {
let idx = 99 - (10 * i as usize + j);
- assert_eq!(m.id, Some(format!("MSG {}", idx)));
+ assert_eq!(m.id, format!("MSG {}", idx));
}
}
@@ -157,7 +169,7 @@ fn member_room_relation() {
assert!(created.is_ok());
assert!(Member::create_relation_table().is_ok());
- let r = Room::new("ROOM ID".to_string(), Some("ROOM NAME".to_string()));
+ let r = Room::new("ROOM ID".to_string(), RoomMembership::Joined(RoomTag::None));
let stored = r.store();
assert!(stored.is_ok());
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]