From 0f100213ba4eb4b54b0288c0843716c11ce19ccc Mon Sep 17 00:00:00 2001
From: Paul <paulmakles@gmail.com>
Date: Sat, 8 May 2021 17:08:45 +0100
Subject: [PATCH] Update January definitions; add servers collection

---
 set_version.sh                                |  2 +-
 src/database/entities/january.rs              |  9 +++++++++
 src/database/entities/mod.rs                  |  4 ++--
 src/database/entities/{guild.rs => server.rs} | 13 ++++++-------
 src/database/migrations/init.rs               |  4 ++++
 src/database/migrations/scripts.rs            | 13 +++++++++++--
 src/version.rs                                |  2 +-
 7 files changed, 34 insertions(+), 13 deletions(-)
 rename src/database/entities/{guild.rs => server.rs} (78%)

diff --git a/set_version.sh b/set_version.sh
index bc53540..f3e28f9 100755
--- a/set_version.sh
+++ b/set_version.sh
@@ -1,3 +1,3 @@
 #!/bin/bash
-export version=0.4.1-alpha.8
+export version=0.4.1-alpha.9
 echo "pub const VERSION: &str = \"${version}\";" > src/version.rs
diff --git a/src/database/entities/january.rs b/src/database/entities/january.rs
index 8810908..043561a 100644
--- a/src/database/entities/january.rs
+++ b/src/database/entities/january.rs
@@ -18,12 +18,21 @@ pub struct Media {
 
 #[derive(Serialize, Deserialize, Debug, Clone)]
 pub struct Metadata {
+    #[serde(skip_serializing_if = "Option::is_none")]
+    url: Option<String>,
     #[serde(skip_serializing_if = "Option::is_none")]
     title: Option<String>,
     #[serde(skip_serializing_if = "Option::is_none")]
     description: Option<String>,
     #[serde(skip_serializing_if = "Option::is_none")]
     image: Option<Media>,
+    
+    #[serde(skip_serializing_if = "Option::is_none")]
+    site_name: Option<String>,
+    #[serde(skip_serializing_if = "Option::is_none")]
+    icon_url: Option<String>,
+    #[serde(skip_serializing_if = "Option::is_none")]
+    color: Option<String>,
 }
 
 #[derive(Serialize, Deserialize, Debug, Clone)]
diff --git a/src/database/entities/mod.rs b/src/database/entities/mod.rs
index 90d99cb..49ea91b 100644
--- a/src/database/entities/mod.rs
+++ b/src/database/entities/mod.rs
@@ -1,4 +1,4 @@
-mod guild;
+mod server;
 mod autumn;
 mod january;
 mod channel;
@@ -8,6 +8,6 @@ mod user;
 pub use january::*;
 pub use autumn::*;
 pub use channel::*;
-pub use guild::*;
+pub use server::*;
 pub use message::*;
 pub use user::*;
diff --git a/src/database/entities/guild.rs b/src/database/entities/server.rs
similarity index 78%
rename from src/database/entities/guild.rs
rename to src/database/entities/server.rs
index 4b69b43..5180953 100644
--- a/src/database/entities/guild.rs
+++ b/src/database/entities/server.rs
@@ -1,6 +1,6 @@
-// use serde::{Deserialize, Serialize};
+use serde::{Deserialize, Serialize};
 
-/*#[derive(Serialize, Deserialize, Debug, Clone)]
+#[derive(Serialize, Deserialize, Debug, Clone)]
 pub struct MemberCompositeKey {
     pub guild: String,
     pub user: String,
@@ -27,12 +27,11 @@ pub struct Ban {
 }
 
 #[derive(Serialize, Deserialize, Debug, Clone)]
-pub struct Guild {
+pub struct Server {
     #[serde(rename = "_id")]
     pub id: String,
-    // pub nonce: String, used internally
-    pub name: String,
-    pub description: String,
+    #[serde(skip_serializing_if = "Option::is_none")]
+    pub nonce: Option<String>,
     pub owner: String,
 
     pub channels: Vec<String>,
@@ -40,4 +39,4 @@ pub struct Guild {
     pub bans: Vec<Ban>,
 
     pub default_permissions: u32,
-}*/
+}
diff --git a/src/database/migrations/init.rs b/src/database/migrations/init.rs
index 3a57826..fa1ab83 100644
--- a/src/database/migrations/init.rs
+++ b/src/database/migrations/init.rs
@@ -25,6 +25,10 @@ pub async fn create_database() {
         .await
         .expect("Failed to create messages collection.");
 
+    db.create_collection("servers", None)
+        .await
+        .expect("Failed to create servers collection.");
+
     db.create_collection("migrations", None)
         .await
         .expect("Failed to create migrations collection.");
diff --git a/src/database/migrations/scripts.rs b/src/database/migrations/scripts.rs
index d926ec0..d123ab2 100644
--- a/src/database/migrations/scripts.rs
+++ b/src/database/migrations/scripts.rs
@@ -1,4 +1,4 @@
-use crate::database::get_collection;
+use crate::database::{get_collection, get_db};
 
 use log::info;
 use mongodb::bson::{doc, from_document};
@@ -10,7 +10,7 @@ struct MigrationInfo {
     revision: i32,
 }
 
-pub const LATEST_REVISION: i32 = 2;
+pub const LATEST_REVISION: i32 = 3;
 
 pub async fn migrate_database() {
     let migrations = get_collection("migrations");
@@ -78,6 +78,15 @@ pub async fn run_migrations(revision: i32) -> i32 {
             .expect("Failed to update attachments.");
     }
 
+    if revision <= 2 {
+        info!("Running migration [revision 2 / 2021-05-08]: Add servers collection.");
+
+        get_db()
+            .create_collection("servers", None)
+            .await
+            .expect("Failed to create servers collection.");
+    }
+
     // Reminder to update LATEST_REVISION when adding new migrations.
     LATEST_REVISION
 }
diff --git a/src/version.rs b/src/version.rs
index f0629a0..5716a7d 100644
--- a/src/version.rs
+++ b/src/version.rs
@@ -1 +1 @@
-pub const VERSION: &str = "0.4.1-alpha.7-patch.2";
+pub const VERSION: &str = "0.4.1-alpha.9";
-- 
GitLab