add: global notes
This commit is contained in:
parent
59581f69c9
commit
2cd04b0db0
24 changed files with 371 additions and 608 deletions
|
@ -40,7 +40,6 @@ impl DataManager {
|
|||
execute!(&conn, common::CREATE_TABLE_NOTES).unwrap();
|
||||
execute!(&conn, common::CREATE_TABLE_MESSAGE_REACTIONS).unwrap();
|
||||
execute!(&conn, common::CREATE_TABLE_INVITE_CODES).unwrap();
|
||||
execute!(&conn, common::CREATE_TABLE_LINKS).unwrap();
|
||||
|
||||
self.0
|
||||
.1
|
||||
|
|
|
@ -27,4 +27,3 @@ pub const CREATE_TABLE_JOURNALS: &str = include_str!("./sql/create_journals.sql"
|
|||
pub const CREATE_TABLE_NOTES: &str = include_str!("./sql/create_notes.sql");
|
||||
pub const CREATE_TABLE_MESSAGE_REACTIONS: &str = include_str!("./sql/create_message_reactions.sql");
|
||||
pub const CREATE_TABLE_INVITE_CODES: &str = include_str!("./sql/create_invite_codes.sql");
|
||||
pub const CREATE_TABLE_LINKS: &str = include_str!("./sql/create_links.sql");
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
CREATE TABLE IF NOT EXISTS links (
|
||||
id BIGINT NOT NULL PRIMARY KEY,
|
||||
created BIGINT NOT NULL,
|
||||
owner BIGINT NOT NULL,
|
||||
label TEXT NOT NULL,
|
||||
href TEXT NOT NULL,
|
||||
upload_id BIGINT NOT NULL,
|
||||
clicks INT NOT NULL,
|
||||
position INT NOT NULL
|
||||
)
|
|
@ -7,5 +7,6 @@ CREATE TABLE IF NOT EXISTS notes (
|
|||
content TEXT NOT NULL,
|
||||
edited BIGINT NOT NULL,
|
||||
dir BIGINT NOT NULL,
|
||||
tags TEXT NOT NULL
|
||||
tags TEXT NOT NULL,
|
||||
is_global INT NOT NULL
|
||||
)
|
||||
|
|
|
@ -1,146 +0,0 @@
|
|||
use oiseau::{cache::Cache, query_row, query_rows};
|
||||
use crate::model::{auth::User, links::Link, permissions::FinePermission, Error, Result};
|
||||
use crate::{auto_method, DataManager};
|
||||
use oiseau::{PostgresRow, execute, get, params};
|
||||
|
||||
impl DataManager {
|
||||
/// Get a [`Link`] from an SQL row.
|
||||
pub(crate) fn get_link_from_row(x: &PostgresRow) -> Link {
|
||||
Link {
|
||||
id: get!(x->0(i64)) as usize,
|
||||
created: get!(x->1(i64)) as usize,
|
||||
owner: get!(x->2(i64)) as usize,
|
||||
label: get!(x->3(String)),
|
||||
href: get!(x->4(String)),
|
||||
upload_id: get!(x->5(i64)) as usize,
|
||||
clicks: get!(x->6(i32)) as usize,
|
||||
position: get!(x->7(i32)) as usize,
|
||||
}
|
||||
}
|
||||
|
||||
auto_method!(get_link_by_id()@get_link_from_row -> "SELECT * FROM links WHERE id = $1" --name="link" --returns=Link --cache-key-tmpl="atto.link:{}");
|
||||
|
||||
/// Get links by `owner`.
|
||||
pub async fn get_links_by_owner(&self, owner: usize) -> Result<Vec<Link>> {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = query_rows!(
|
||||
&conn,
|
||||
"SELECT * FROM links WHERE owner = $1 ORDER BY position DESC",
|
||||
&[&(owner as i64)],
|
||||
|x| { Self::get_link_from_row(x) }
|
||||
);
|
||||
|
||||
if res.is_err() {
|
||||
return Err(Error::GeneralNotFound("link".to_string()));
|
||||
}
|
||||
|
||||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
/// Get links by `owner`.
|
||||
pub async fn get_links_by_owner_count(&self, owner: usize) -> Result<i32> {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = query_row!(
|
||||
&conn,
|
||||
"SELECT COUNT(*)::int FROM links WHERE owner = $1",
|
||||
&[&(owner as i64)],
|
||||
|x| Ok(x.get::<usize, i32>(0))
|
||||
);
|
||||
|
||||
if res.is_err() {
|
||||
return Err(Error::GeneralNotFound("link".to_string()));
|
||||
}
|
||||
|
||||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
const MAXIMUM_FREE_LINKS: usize = 10;
|
||||
const MAXIMUM_SUPPORTER_LINKS: usize = 20;
|
||||
|
||||
/// Create a new link in the database.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `data` - a mock [`Link`] object to insert
|
||||
pub async fn create_link(&self, data: Link, user: &User) -> Result<Link> {
|
||||
if !user.permissions.check(FinePermission::SUPPORTER) {
|
||||
if (self.get_links_by_owner_count(user.id).await? as usize) >= Self::MAXIMUM_FREE_LINKS
|
||||
{
|
||||
return Err(Error::MiscError(
|
||||
"You already have the maximum number of links you can create".to_string(),
|
||||
));
|
||||
}
|
||||
} else if !user.permissions.check(FinePermission::MANAGE_USERS) {
|
||||
if (self.get_links_by_owner_count(user.id).await? as usize)
|
||||
>= Self::MAXIMUM_SUPPORTER_LINKS
|
||||
{
|
||||
return Err(Error::MiscError(
|
||||
"You already have the maximum number of links you can create".to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = execute!(
|
||||
&conn,
|
||||
"INSERT INTO links VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
|
||||
params![
|
||||
&(data.id as i64),
|
||||
&(data.created as i64),
|
||||
&(data.owner as i64),
|
||||
&data.label,
|
||||
&data.href,
|
||||
&(data.upload_id as i64),
|
||||
&(data.clicks as i32),
|
||||
&(data.position as i32),
|
||||
]
|
||||
);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
pub async fn delete_link(&self, id: usize) -> Result<()> {
|
||||
let y = self.get_link_by_id(id).await?;
|
||||
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = execute!(&conn, "DELETE FROM links WHERE id = $1", &[&(id as i64)]);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
// delete upload
|
||||
if y.upload_id != 0 {
|
||||
self.delete_upload(id).await?;
|
||||
}
|
||||
|
||||
// ...
|
||||
self.0.1.remove(format!("atto.link:{}", id)).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
auto_method!(update_link_label(&str) -> "UPDATE links SET label = $1 WHERE id = $2" --cache-key-tmpl="atto.link:{}");
|
||||
auto_method!(update_link_href(&str) -> "UPDATE links SET href = $1 WHERE id = $2" --cache-key-tmpl="atto.link:{}");
|
||||
auto_method!(update_link_upload_id(i64) -> "UPDATE links SET upload_id = $1 WHERE id = $2" --cache-key-tmpl="atto.link:{}");
|
||||
auto_method!(update_link_position(i32) -> "UPDATE links SET position = $1 WHERE id = $2" --cache-key-tmpl="atto.link:{}");
|
||||
auto_method!(incr_link_clicks() -> "UPDATE links SET clicks = $1 WHERE id = $2" --cache-key-tmpl="atto.link:{}" --incr);
|
||||
}
|
|
@ -12,7 +12,6 @@ mod invite_codes;
|
|||
mod ipbans;
|
||||
mod ipblocks;
|
||||
mod journals;
|
||||
mod links;
|
||||
mod memberships;
|
||||
mod message_reactions;
|
||||
mod messages;
|
||||
|
|
|
@ -17,10 +17,33 @@ impl DataManager {
|
|||
edited: get!(x->6(i64)) as usize,
|
||||
dir: get!(x->7(i64)) as usize,
|
||||
tags: serde_json::from_str(&get!(x->8(String))).unwrap(),
|
||||
is_global: get!(x->9(i32)) as i8 == 1,
|
||||
}
|
||||
}
|
||||
|
||||
auto_method!(get_note_by_id(usize as i64)@get_note_from_row -> "SELECT * FROM notes WHERE id = $1" --name="note" --returns=Note --cache-key-tmpl="atto.note:{}");
|
||||
auto_method!(get_global_note_by_title(&str)@get_note_from_row -> "SELECT * FROM notes WHERE title = $1 AND is_global = 1" --name="note" --returns=Note --cache-key-tmpl="atto.note:{}");
|
||||
|
||||
/// Get the number of global notes a user has.
|
||||
pub async fn get_user_global_notes_count(&self, owner: usize) -> Result<i32> {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = query_row!(
|
||||
&conn,
|
||||
"SELECT COUNT(*)::int FROM notes WHERE owner = $1 AND is_global = 1",
|
||||
&[&(owner as i64)],
|
||||
|x| Ok(x.get::<usize, i32>(0))
|
||||
);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
/// Get a note by `journal` and `title`.
|
||||
pub async fn get_note_by_journal_title(&self, journal: usize, title: &str) -> Result<Note> {
|
||||
|
@ -94,6 +117,9 @@ impl DataManager {
|
|||
|
||||
const MAXIMUM_FREE_NOTES_PER_JOURNAL: usize = 10;
|
||||
|
||||
pub const MAXIMUM_FREE_GLOBAL_NOTES: usize = 10;
|
||||
pub const MAXIMUM_SUPPORTER_GLOBAL_NOTES: usize = 50;
|
||||
|
||||
/// Create a new note in the database.
|
||||
///
|
||||
/// # Arguments
|
||||
|
@ -164,7 +190,7 @@ impl DataManager {
|
|||
|
||||
let res = execute!(
|
||||
&conn,
|
||||
"INSERT INTO notes VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
|
||||
"INSERT INTO notes VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)",
|
||||
params![
|
||||
&(data.id as i64),
|
||||
&(data.created as i64),
|
||||
|
@ -175,6 +201,7 @@ impl DataManager {
|
|||
&(data.edited as i64),
|
||||
&(data.dir as i64),
|
||||
&serde_json::to_string(&data.tags).unwrap(),
|
||||
&if data.is_global { 1 } else { 0 }
|
||||
]
|
||||
);
|
||||
|
||||
|
@ -206,7 +233,7 @@ impl DataManager {
|
|||
}
|
||||
|
||||
// ...
|
||||
self.0.1.remove(format!("atto.note:{}", id)).await;
|
||||
self.cache_clear_note(¬e).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -246,9 +273,26 @@ impl DataManager {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
auto_method!(update_note_title(&str)@get_note_by_id:MANAGE_NOTES -> "UPDATE notes SET title = $1 WHERE id = $2" --cache-key-tmpl="atto.note:{}");
|
||||
auto_method!(update_note_content(&str)@get_note_by_id:MANAGE_NOTES -> "UPDATE notes SET content = $1 WHERE id = $2" --cache-key-tmpl="atto.note:{}");
|
||||
auto_method!(update_note_dir(i64)@get_note_by_id:MANAGE_NOTES -> "UPDATE notes SET dir = $1 WHERE id = $2" --cache-key-tmpl="atto.note:{}");
|
||||
auto_method!(update_note_tags(Vec<String>)@get_note_by_id:MANAGE_NOTES -> "UPDATE notes SET tags = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.note:{}");
|
||||
auto_method!(update_note_edited(i64) -> "UPDATE notes SET edited = $1 WHERE id = $2" --cache-key-tmpl="atto.note:{}");
|
||||
/// Incremenet note views. Views are only stored in the cache.
|
||||
///
|
||||
/// This should only be done for global notes.
|
||||
pub async fn incr_note_views(&self, id: usize) {
|
||||
self.0.1.incr(format!("atto.note:{id}/views")).await;
|
||||
}
|
||||
|
||||
pub async fn get_note_views(&self, id: usize) -> Option<String> {
|
||||
self.0.1.get(format!("atto.note:{id}/views")).await
|
||||
}
|
||||
|
||||
pub async fn cache_clear_note(&self, x: &Note) {
|
||||
self.0.1.remove(format!("atto.note:{}", x.id)).await;
|
||||
self.0.1.remove(format!("atto.note:{}", x.title)).await;
|
||||
}
|
||||
|
||||
auto_method!(update_note_title(&str)@get_note_by_id:MANAGE_NOTES -> "UPDATE notes SET title = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_note);
|
||||
auto_method!(update_note_content(&str)@get_note_by_id:MANAGE_NOTES -> "UPDATE notes SET content = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_note);
|
||||
auto_method!(update_note_dir(i64)@get_note_by_id:MANAGE_NOTES -> "UPDATE notes SET dir = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_note);
|
||||
auto_method!(update_note_tags(Vec<String>)@get_note_by_id:MANAGE_NOTES -> "UPDATE notes SET tags = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_note);
|
||||
auto_method!(update_note_edited(i64)@get_note_by_id -> "UPDATE notes SET edited = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_note);
|
||||
auto_method!(update_note_is_global(i32)@get_note_by_id -> "UPDATE notes SET is_global = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_note);
|
||||
}
|
||||
|
|
|
@ -175,7 +175,7 @@ impl DataManager {
|
|||
&(data.owner as i64),
|
||||
&(data.asset as i64),
|
||||
&serde_json::to_string(&data.asset_type).unwrap().as_str(),
|
||||
&{ if data.is_like { 1 } else { 0 } }
|
||||
&if data.is_like { 1 } else { 0 }
|
||||
]
|
||||
);
|
||||
|
||||
|
|
|
@ -60,6 +60,7 @@ pub struct Note {
|
|||
pub dir: usize,
|
||||
/// An array of tags associated with the note.
|
||||
pub tags: Vec<String>,
|
||||
pub is_global: bool,
|
||||
}
|
||||
|
||||
impl Note {
|
||||
|
@ -77,6 +78,7 @@ impl Note {
|
|||
edited: created,
|
||||
dir: 0,
|
||||
tags: Vec::new(),
|
||||
is_global: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
use serde::{Serialize, Deserialize};
|
||||
use tetratto_shared::{snow::Snowflake, unix_epoch_timestamp};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct Link {
|
||||
pub id: usize,
|
||||
pub created: usize,
|
||||
/// Links should be selected by their owner, not their link list ID.
|
||||
/// This is why we do not store link list ID.
|
||||
pub owner: usize,
|
||||
pub label: String,
|
||||
pub href: String,
|
||||
/// As link icons are optional, `upload_id` is allowed to be 0.
|
||||
pub upload_id: usize,
|
||||
/// Clicks are tracked for supporters only.
|
||||
///
|
||||
/// When a user clicks on a link through the UI, they'll be redirect to
|
||||
/// `/links/{id}`. If the link's owner is a supporter, the link's clicks will
|
||||
/// be incremented.
|
||||
///
|
||||
/// The page should just serve a simple HTML document with a meta tag to redirect.
|
||||
/// We only really care about knowing they clicked it, so an automatic redirect will do.
|
||||
pub clicks: usize,
|
||||
pub position: usize,
|
||||
}
|
||||
|
||||
impl Link {
|
||||
/// Create a new [`Link`].
|
||||
pub fn new(owner: usize, label: String, href: String, position: usize) -> Self {
|
||||
Self {
|
||||
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
|
||||
created: unix_epoch_timestamp(),
|
||||
owner,
|
||||
label,
|
||||
href,
|
||||
upload_id: 0,
|
||||
clicks: 0,
|
||||
position,
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,7 +6,6 @@ pub mod channels;
|
|||
pub mod communities;
|
||||
pub mod communities_permissions;
|
||||
pub mod journals;
|
||||
pub mod links;
|
||||
pub mod moderation;
|
||||
pub mod oauth;
|
||||
pub mod permissions;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue