server side preparation
This commit is contained in:
48
src/bin/server/db/data.rs
Normal file
48
src/bin/server/db/data.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use iris::core::util::HashSet;
|
||||
pub use openworm::net::{AccountToken, ServerPerms};
|
||||
|
||||
pub type UserId = u64;
|
||||
pub type MsgId = i128;
|
||||
pub type ChannelId = u64;
|
||||
pub type ImageId = u64;
|
||||
|
||||
#[derive(bitcode::Encode, bitcode::Decode)]
|
||||
pub struct User {
|
||||
pub username: String,
|
||||
pub password_hash: String,
|
||||
pub pfp: Option<ImageId>,
|
||||
pub bio: String,
|
||||
pub friends: Friends,
|
||||
pub server_perms: ServerPerms,
|
||||
}
|
||||
|
||||
impl User {
|
||||
pub fn new(username: String, password_hash: String, perms: ServerPerms) -> Self {
|
||||
Self {
|
||||
username,
|
||||
password_hash,
|
||||
bio: String::new(),
|
||||
pfp: None,
|
||||
friends: Default::default(),
|
||||
server_perms: perms,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct Friends {
|
||||
pub current: HashSet<UserId>,
|
||||
pub outgoing: HashSet<UserId>,
|
||||
pub incoming: HashSet<UserId>,
|
||||
}
|
||||
|
||||
#[derive(bitcode::Encode, bitcode::Decode)]
|
||||
pub struct Msg {
|
||||
pub content: String,
|
||||
pub author: UserId,
|
||||
}
|
||||
|
||||
pub struct Channel {
|
||||
pub name: String,
|
||||
pub desc: String,
|
||||
}
|
||||
@@ -1,31 +1,22 @@
|
||||
mod data;
|
||||
mod util;
|
||||
mod ver;
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
pub use data::*;
|
||||
use util::*;
|
||||
use ver::*;
|
||||
|
||||
pub const DB_VERSION: u64 = 0;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Db {
|
||||
db: Database,
|
||||
pub account_tokens: DbMap<String, ServerPerms>,
|
||||
pub account_tokens: DbMap<AccountToken, ServerPerms>,
|
||||
pub msgs: DbMap<MsgId, Msg>,
|
||||
pub users: DbMap<UserId, User>,
|
||||
pub usernames: DbMap<String, UserId>,
|
||||
}
|
||||
|
||||
pub type UserId = UserIdV0;
|
||||
pub type MsgId = MsgIdV0;
|
||||
pub type ChannelId = ChannelIdV0;
|
||||
pub type ImageId = ImageIdV0;
|
||||
pub type User = UserV0;
|
||||
pub type Msg = MsgV0;
|
||||
pub type ChannelInfo = ChannelV0;
|
||||
pub type ServerPerms = ServerPermsV0;
|
||||
|
||||
impl Db {
|
||||
pub fn open(path: impl AsRef<Path>) -> Db {
|
||||
let db = Database::open(path);
|
||||
|
||||
@@ -17,7 +17,7 @@ impl<K, V> Clone for DbMap<K, V> {
|
||||
Self {
|
||||
db: self.db.clone(),
|
||||
keyspace: self.keyspace.clone(),
|
||||
_pd: self._pd.clone(),
|
||||
_pd: self._pd,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -126,7 +126,7 @@ impl WriteTx {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get<K: Key<Output = K>, V: DecodeOwned>(&self, map: &DbMap<K, V>, k: K) -> Option<V> {
|
||||
pub fn get<K: Key<Output = K>, V: DecodeOwned>(&self, map: &DbMap<K, V>, k: &K) -> Option<V> {
|
||||
let k = Slice::new(k.to_bytes().as_ref());
|
||||
let v = self.0.get(&map.keyspace, k).unwrap()?;
|
||||
Some(bitcode::decode(&v).unwrap())
|
||||
@@ -137,7 +137,11 @@ impl WriteTx {
|
||||
self.0.get(&map.keyspace, k).unwrap().is_some()
|
||||
}
|
||||
|
||||
pub fn remove<K: Key<Output = K>, V: DecodeOwned>(&mut self, map: &DbMap<K, V>, k: K) -> Option<V> {
|
||||
pub fn remove<K: Key<Output = K>, V: DecodeOwned>(
|
||||
&mut self,
|
||||
map: &DbMap<K, V>,
|
||||
k: K,
|
||||
) -> Option<V> {
|
||||
let k = Slice::new(k.to_bytes().as_ref());
|
||||
let v = self.0.take(&map.keyspace, k).unwrap()?;
|
||||
Some(bitcode::decode(&v).unwrap())
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
pub type UserIdV0 = u64;
|
||||
pub type MsgIdV0 = i128;
|
||||
pub type ChannelIdV0 = u64;
|
||||
pub type ImageIdV0 = u64;
|
||||
|
||||
#[derive(Clone, Copy, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct ServerPermsV0(u32);
|
||||
impl ServerPermsV0 {
|
||||
pub const NONE: Self = Self(0);
|
||||
pub const ACCOUNT_TOKENS: Self = Self(1 << 0);
|
||||
pub const ALL: Self = Self(u32::MAX);
|
||||
}
|
||||
|
||||
#[derive(bitcode::Encode, bitcode::Decode)]
|
||||
pub struct UserV0 {
|
||||
pub username: String,
|
||||
pub password_hash: String,
|
||||
pub pfp: Option<ImageIdV0>,
|
||||
pub bio: String,
|
||||
pub server_perms: ServerPermsV0,
|
||||
}
|
||||
|
||||
#[derive(bitcode::Encode, bitcode::Decode)]
|
||||
pub struct MsgV0 {
|
||||
pub content: String,
|
||||
pub author: UserIdV0,
|
||||
}
|
||||
|
||||
pub struct ChannelV0 {
|
||||
pub name: String,
|
||||
pub desc: String,
|
||||
}
|
||||
|
||||
impl ServerPermsV0 {
|
||||
pub fn contains(&self, other: Self) -> bool {
|
||||
(self.0 & other.0) == other.0
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user