94 lines
2.3 KiB
Rust
94 lines
2.3 KiB
Rust
use crate::models::users::User;
|
|
use crate::schema::characters;
|
|
use diesel_derive_enum::DbEnum;
|
|
use serde_derive::Serialize;
|
|
|
|
/// Control system visibility of a character for a particular user.
|
|
/// Implemented as a trait because there are multiple character
|
|
/// structs that need this.
|
|
pub(crate) trait Visibility {
|
|
/// User ID that owns this character.
|
|
fn user_id(&self) -> i32;
|
|
|
|
/// If the character is publicly visible.
|
|
fn viewable(&self) -> bool;
|
|
|
|
/// Transform to an Option that holds the character, if the
|
|
/// character is viewable to a potentially existing user. A
|
|
/// character is "visible" if the public viewable property is set
|
|
/// to true, or the user is the owner of the character. Consumes
|
|
/// self.
|
|
fn as_visible_for(self, user: Option<&User>) -> Option<Self>
|
|
where
|
|
Self: std::marker::Sized,
|
|
{
|
|
if self.viewable() || user.map(|u| u.id) == Some(self.user_id()) {
|
|
Some(self)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(DbEnum, Debug, Serialize, PartialEq)]
|
|
pub enum CharacterDataType {
|
|
ChroniclesOfDarknessV1,
|
|
ChangelingV1,
|
|
}
|
|
|
|
/// An entry that appears in a user's character list. Properties are
|
|
/// in order of table columns.
|
|
#[derive(Serialize, Debug, Queryable)]
|
|
pub struct Character {
|
|
pub id: i32,
|
|
pub user_id: i32,
|
|
pub viewable: bool,
|
|
pub character_name: String,
|
|
pub data_type: CharacterDataType,
|
|
pub data_version: i32,
|
|
pub data: Vec<u8>,
|
|
}
|
|
|
|
impl Visibility for Character {
|
|
fn user_id(&self) -> i32 {
|
|
self.user_id
|
|
}
|
|
|
|
fn viewable(&self) -> bool {
|
|
self.viewable
|
|
}
|
|
}
|
|
|
|
#[derive(Serialize, Debug, Queryable)]
|
|
pub struct StrippedCharacter {
|
|
pub id: i32,
|
|
pub user_id: i32,
|
|
pub viewable: bool,
|
|
pub character_name: String,
|
|
pub data_type: CharacterDataType,
|
|
pub data_version: i32,
|
|
}
|
|
|
|
impl Visibility for StrippedCharacter {
|
|
fn user_id(&self) -> i32 {
|
|
self.user_id
|
|
}
|
|
|
|
fn viewable(&self) -> bool {
|
|
self.viewable
|
|
}
|
|
}
|
|
|
|
/// Represents insert of a new character into the database. Property
|
|
/// names correspond to columns.
|
|
#[derive(Insertable)]
|
|
#[table_name = "characters"]
|
|
pub struct NewCharacter<'a> {
|
|
pub user_id: i32,
|
|
pub viewable: bool,
|
|
pub character_name: &'a str,
|
|
pub data_type: CharacterDataType,
|
|
pub data_version: i32,
|
|
pub data: &'a [u8],
|
|
}
|