2020-08-26 10:27:38 +02:00
|
|
|
use super::{Cipher, User};
|
|
|
|
|
|
|
|
db_object! {
|
2021-03-13 22:04:04 +01:00
|
|
|
#[derive(Identifiable, Queryable, Insertable, Associations)]
|
2020-08-26 10:27:38 +02:00
|
|
|
#[table_name = "favorites"]
|
|
|
|
#[belongs_to(User, foreign_key = "user_uuid")]
|
|
|
|
#[belongs_to(Cipher, foreign_key = "cipher_uuid")]
|
|
|
|
#[primary_key(user_uuid, cipher_uuid)]
|
|
|
|
pub struct Favorite {
|
|
|
|
pub user_uuid: String,
|
|
|
|
pub cipher_uuid: String,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use crate::db::DbConn;
|
|
|
|
|
|
|
|
use crate::api::EmptyResult;
|
|
|
|
use crate::error::MapResult;
|
|
|
|
|
|
|
|
impl Favorite {
|
|
|
|
// Returns whether the specified cipher is a favorite of the specified user.
|
|
|
|
pub fn is_favorite(cipher_uuid: &str, user_uuid: &str, conn: &DbConn) -> bool {
|
2021-03-31 22:18:35 +02:00
|
|
|
db_run! { conn: {
|
2020-08-26 10:27:38 +02:00
|
|
|
let query = favorites::table
|
|
|
|
.filter(favorites::cipher_uuid.eq(cipher_uuid))
|
|
|
|
.filter(favorites::user_uuid.eq(user_uuid))
|
|
|
|
.count();
|
|
|
|
|
|
|
|
query.first::<i64>(conn).ok().unwrap_or(0) != 0
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets whether the specified cipher is a favorite of the specified user.
|
|
|
|
pub fn set_favorite(favorite: bool, cipher_uuid: &str, user_uuid: &str, conn: &DbConn) -> EmptyResult {
|
|
|
|
let (old, new) = (Self::is_favorite(cipher_uuid, user_uuid, &conn), favorite);
|
|
|
|
match (old, new) {
|
|
|
|
(false, true) => {
|
|
|
|
User::update_uuid_revision(user_uuid, &conn);
|
2021-03-31 22:18:35 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
diesel::insert_into(favorites::table)
|
|
|
|
.values((
|
|
|
|
favorites::user_uuid.eq(user_uuid),
|
|
|
|
favorites::cipher_uuid.eq(cipher_uuid),
|
|
|
|
))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error adding favorite")
|
|
|
|
}}
|
2020-08-26 10:27:38 +02:00
|
|
|
}
|
|
|
|
(true, false) => {
|
|
|
|
User::update_uuid_revision(user_uuid, &conn);
|
2021-03-31 22:18:35 +02:00
|
|
|
db_run! { conn: {
|
2020-08-26 10:27:38 +02:00
|
|
|
diesel::delete(
|
|
|
|
favorites::table
|
|
|
|
.filter(favorites::user_uuid.eq(user_uuid))
|
|
|
|
.filter(favorites::cipher_uuid.eq(cipher_uuid))
|
|
|
|
)
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error removing favorite")
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
// Otherwise, the favorite status is already what it should be.
|
2021-03-31 22:18:35 +02:00
|
|
|
_ => Ok(()),
|
2020-08-26 10:27:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all favorite entries associated with the specified cipher.
|
|
|
|
pub fn delete_all_by_cipher(cipher_uuid: &str, conn: &DbConn) -> EmptyResult {
|
|
|
|
db_run! { conn: {
|
|
|
|
diesel::delete(favorites::table.filter(favorites::cipher_uuid.eq(cipher_uuid)))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error removing favorites by cipher")
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all favorite entries associated with the specified user.
|
|
|
|
pub fn delete_all_by_user(user_uuid: &str, conn: &DbConn) -> EmptyResult {
|
|
|
|
db_run! { conn: {
|
|
|
|
diesel::delete(favorites::table.filter(favorites::user_uuid.eq(user_uuid)))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error removing favorites by user")
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
}
|