[fractal/backend: 6/9] backend: Store power_levels hashmap



commit 067dd1fa6cfebb5ed8d9bb6c32be6e33e8e76795
Author: Daniel GarcĂ­a Moreno <danigm wadobo com>
Date:   Sun Jan 6 14:28:42 2019 +0100

    backend: Store power_levels hashmap

 fractal-backend/src/model/room.rs | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
---
diff --git a/fractal-backend/src/model/room.rs b/fractal-backend/src/model/room.rs
index 455a315c..77171596 100644
--- a/fractal-backend/src/model/room.rs
+++ b/fractal-backend/src/model/room.rs
@@ -7,6 +7,8 @@ use failure::Error;
 use rusqlite::types::ToSql;
 use rusqlite::Row;
 
+use serde_json;
+
 use super::conn;
 use super::Model;
 
@@ -36,16 +38,14 @@ impl Model for Room {
             "inv",
             "direct",
             "prev_batch",
+            "power_levels",
         ]
     }
 
     fn create_sql() -> String {
         //TODO: implements relations for:
         //  members: MemberList,
-        //  messages: Vec<Message>,
         //  inv_sender: Option<Member>,
-        // TODO: maybe we should store power_level in the Member Table
-        //  power_levels: HashMap<String, i32>,
         format!(
             "
         CREATE TABLE if not exists {} (
@@ -63,7 +63,8 @@ impl Model for Room {
             left BOOLEAN NOT NULL,
             inv BOOLEAN NOT NULL,
             direct BOOLEAN NOT NULL,
-            prev_batch TEXT
+            prev_batch TEXT,
+            power_levels TEXT NOT NULL
         )
         ",
             Self::table_name()
@@ -84,6 +85,8 @@ impl Model for Room {
             questions
         );
 
+        let power_levels = serde_json::to_string(&self.power_levels)?;
+
         conn(
             move |c| {
                 c.execute(
@@ -104,6 +107,7 @@ impl Model for Room {
                         &self.inv,
                         &self.direct,
                         &self.prev_batch,
+                        &power_levels,
                     ],
                 )
                 .map(|_| ())
@@ -114,6 +118,9 @@ impl Model for Room {
     }
 
     fn map_row(row: &Row) -> Self {
+        let strp: String = row.get(15);
+        let power_levels: HashMap<String, i32> = serde_json::from_str(&strp).unwrap_or_default();
+
         Self {
             id: row.get(0),
             avatar: row.get(1),
@@ -130,9 +137,9 @@ impl Model for Room {
             inv: row.get(12),
             direct: row.get(13),
             prev_batch: row.get(14),
+            power_levels: power_levels,
 
             inv_sender: None,
-            power_levels: HashMap::new(),
             messages: vec![],
             members: HashMap::new(),
         }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]