A Matrix homeserver written in Rust https://conduit.rs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

128 lines
5.2 KiB

  1. pub(self) mod account_data;
  2. pub(self) mod global_edus;
  3. pub(self) mod globals;
  4. pub(self) mod key_backups;
  5. pub(self) mod media;
  6. pub(self) mod rooms;
  7. pub(self) mod uiaa;
  8. pub(self) mod users;
  9. use crate::{Error, Result};
  10. use directories::ProjectDirs;
  11. use log::info;
  12. use std::fs::remove_dir_all;
  13. use rocket::Config;
  14. pub struct Database {
  15. pub globals: globals::Globals,
  16. pub users: users::Users,
  17. pub uiaa: uiaa::Uiaa,
  18. pub rooms: rooms::Rooms,
  19. pub account_data: account_data::AccountData,
  20. pub global_edus: global_edus::GlobalEdus,
  21. pub media: media::Media,
  22. pub key_backups: key_backups::KeyBackups,
  23. pub _db: sled::Db,
  24. }
  25. impl Database {
  26. /// Tries to remove the old database but ignores all errors.
  27. pub fn try_remove(server_name: &str) -> Result<()> {
  28. let mut path = ProjectDirs::from("xyz", "koesters", "conduit")
  29. .ok_or(Error::BadConfig(
  30. "The OS didn't return a valid home directory path.",
  31. ))?
  32. .data_dir()
  33. .to_path_buf();
  34. path.push(server_name);
  35. let _ = remove_dir_all(path);
  36. Ok(())
  37. }
  38. /// Load an existing database or create a new one.
  39. pub fn load_or_create(config: &Config) -> Result<Self> {
  40. let server_name = config.get_str("server_name").unwrap_or("localhost");
  41. let path = config
  42. .get_str("database_path")
  43. .map(|x| Ok::<_, Error>(x.to_owned()))
  44. .unwrap_or_else(|_| {
  45. let path = ProjectDirs::from("xyz", "koesters", "conduit")
  46. .ok_or(Error::BadConfig(
  47. "The OS didn't return a valid home directory path.",
  48. ))?
  49. .data_dir()
  50. .join(server_name);
  51. Ok(path
  52. .to_str()
  53. .ok_or(Error::BadConfig("Database path contains invalid unicode."))?
  54. .to_owned())
  55. })?;
  56. let db = sled::open(&path)?;
  57. info!("Opened sled database at {}", path);
  58. Ok(Self {
  59. globals: globals::Globals::load(db.open_tree("global")?, config)?,
  60. users: users::Users {
  61. userid_password: db.open_tree("userid_password")?,
  62. userid_displayname: db.open_tree("userid_displayname")?,
  63. userid_avatarurl: db.open_tree("userid_avatarurl")?,
  64. userdeviceid_token: db.open_tree("userdeviceid_token")?,
  65. userdeviceid_metadata: db.open_tree("userdeviceid_metadata")?,
  66. token_userdeviceid: db.open_tree("token_userdeviceid")?,
  67. onetimekeyid_onetimekeys: db.open_tree("onetimekeyid_onetimekeys")?,
  68. keychangeid_userid: db.open_tree("devicekeychangeid_userid")?,
  69. keyid_key: db.open_tree("keyid_key")?,
  70. userid_masterkeyid: db.open_tree("userid_masterkeyid")?,
  71. userid_selfsigningkeyid: db.open_tree("userid_selfsigningkeyid")?,
  72. userid_usersigningkeyid: db.open_tree("userid_usersigningkeyid")?,
  73. todeviceid_events: db.open_tree("todeviceid_events")?,
  74. },
  75. uiaa: uiaa::Uiaa {
  76. userdeviceid_uiaainfo: db.open_tree("userdeviceid_uiaainfo")?,
  77. },
  78. rooms: rooms::Rooms {
  79. edus: rooms::RoomEdus {
  80. roomuserid_lastread: db.open_tree("roomuserid_lastread")?, // "Private" read receipt
  81. roomlatestid_roomlatest: db.open_tree("roomlatestid_roomlatest")?, // Read receipts
  82. roomactiveid_userid: db.open_tree("roomactiveid_userid")?, // Typing notifs
  83. roomid_lastroomactiveupdate: db.open_tree("roomid_lastroomactiveupdate")?,
  84. },
  85. pduid_pdu: db.open_tree("pduid_pdu")?,
  86. eventid_pduid: db.open_tree("eventid_pduid")?,
  87. roomid_pduleaves: db.open_tree("roomid_pduleaves")?,
  88. roomstateid_pdu: db.open_tree("roomstateid_pdu")?,
  89. alias_roomid: db.open_tree("alias_roomid")?,
  90. aliasid_alias: db.open_tree("alias_roomid")?,
  91. publicroomids: db.open_tree("publicroomids")?,
  92. userroomid_joined: db.open_tree("userroomid_joined")?,
  93. roomuserid_joined: db.open_tree("roomuserid_joined")?,
  94. userroomid_invited: db.open_tree("userroomid_invited")?,
  95. roomuserid_invited: db.open_tree("roomuserid_invited")?,
  96. userroomid_left: db.open_tree("userroomid_left")?,
  97. },
  98. account_data: account_data::AccountData {
  99. roomuserdataid_accountdata: db.open_tree("roomuserdataid_accountdata")?,
  100. },
  101. global_edus: global_edus::GlobalEdus {
  102. presenceid_presence: db.open_tree("presenceid_presence")?, // Presence
  103. },
  104. media: media::Media {
  105. mediaid_file: db.open_tree("mediaid_file")?,
  106. },
  107. key_backups: key_backups::KeyBackups {
  108. backupid_algorithm: db.open_tree("backupid_algorithm")?,
  109. backupid_etag: db.open_tree("backupid_etag")?,
  110. backupkeyid_backup: db.open_tree("backupkeyid_backupmetadata")?,
  111. },
  112. _db: db,
  113. })
  114. }
  115. }