license and some functions

This commit is contained in:
Mateusz Gruszczyński
2026-07-29 14:03:46 +02:00
parent 0655cfe48c
commit ef8dfc67c1
56 changed files with 2586 additions and 547 deletions
+9 -1
View File
@@ -1,3 +1,12 @@
/*
* Copyright (C) 2026 Mateusz Gruszczyński @linuxiarz.pl
* Source-Available Code / Dual-Licensed.
*
* Free for non-commercial and evaluation use under terms of BSL/GPLv3.
* Commercial or production use requires a valid paid license.
* See LICENSE file in repository root for details.
*/
use super::*;
#[derive(Debug, Clone, Copy)]
@@ -262,7 +271,6 @@ pub async fn delete_pad_file(
Ok(())
}
impl<'r> sqlx::FromRow<'r, AnyRow> for NoteFile {
fn from_row(row: &'r AnyRow) -> Result<Self, sqlx::Error> {
let attached: i64 = row.try_get("is_attached")?;
+9 -1
View File
@@ -1,3 +1,12 @@
/*
* Copyright (C) 2026 Mateusz Gruszczyński @linuxiarz.pl
* Source-Available Code / Dual-Licensed.
*
* Free for non-commercial and evaluation use under terms of BSL/GPLv3.
* Commercial or production use requires a valid paid license.
* See LICENSE file in repository root for details.
*/
use crate::{
database::{Database, DatabaseKind},
queries,
@@ -466,7 +475,6 @@ pub async fn list_pad_revisions(
.await
}
impl<'r> sqlx::FromRow<'r, AnyRow> for Workspace {
fn from_row(row: &'r AnyRow) -> Result<Self, sqlx::Error> {
Ok(Self {
+151 -32
View File
@@ -1,3 +1,12 @@
/*
* Copyright (C) 2026 Mateusz Gruszczyński @linuxiarz.pl
* Source-Available Code / Dual-Licensed.
*
* Free for non-commercial and evaluation use under terms of BSL/GPLv3.
* Commercial or production use requires a valid paid license.
* See LICENSE file in repository root for details.
*/
use super::*;
#[derive(Debug, Clone, Serialize)]
@@ -95,23 +104,39 @@ pub async fn publish_note(pool: &Database, note_id: i64) -> Result<String, sqlx:
}
pub async fn pad_public_page_enabled(pool: &Database, pad_id: i64) -> Result<bool, sqlx::Error> {
Ok(sqlx::query_scalar::<_, String>(queries::get(pool.kind(), queries::Q017))
.bind(pad_id).fetch_optional(pool.pool()).await?.is_some())
Ok(
sqlx::query_scalar::<_, String>(queries::get(pool.kind(), queries::Q017))
.bind(pad_id)
.fetch_optional(pool.pool())
.await?
.is_some(),
)
}
pub async fn note_public_page_enabled(pool: &Database, note_id: i64) -> Result<bool, sqlx::Error> {
Ok(sqlx::query_scalar::<_, String>(queries::get(pool.kind(), queries::Q019))
.bind(note_id).fetch_optional(pool.pool()).await?.is_some())
Ok(
sqlx::query_scalar::<_, String>(queries::get(pool.kind(), queries::Q019))
.bind(note_id)
.fetch_optional(pool.pool())
.await?
.is_some(),
)
}
pub async fn unpublish_pad(pool: &Database, pad_id: i64) -> Result<(), sqlx::Error> {
let sql = match pool.kind() { DatabaseKind::Postgres => "DELETE FROM published_pages WHERE pad_id = $1", _ => "DELETE FROM published_pages WHERE pad_id = ?" };
let sql = match pool.kind() {
DatabaseKind::Postgres => "DELETE FROM published_pages WHERE pad_id = $1",
_ => "DELETE FROM published_pages WHERE pad_id = ?",
};
sqlx::query(sql).bind(pad_id).execute(pool.pool()).await?;
Ok(())
}
pub async fn unpublish_note(pool: &Database, note_id: i64) -> Result<(), sqlx::Error> {
let sql = match pool.kind() { DatabaseKind::Postgres => "DELETE FROM published_pages WHERE note_id = $1", _ => "DELETE FROM published_pages WHERE note_id = ?" };
let sql = match pool.kind() {
DatabaseKind::Postgres => "DELETE FROM published_pages WHERE note_id = $1",
_ => "DELETE FROM published_pages WHERE note_id = ?",
};
sqlx::query(sql).bind(note_id).execute(pool.pool()).await?;
Ok(())
}
@@ -207,33 +232,80 @@ pub async fn set_note_public_task_updates(
Ok(())
}
pub async fn pad_public_page_unprotected(pool: &Database, pad_id: i64) -> Result<bool, sqlx::Error> {
pub async fn pad_public_page_unprotected(
pool: &Database,
pad_id: i64,
) -> Result<bool, sqlx::Error> {
if pool.kind() == DatabaseKind::Postgres {
return Ok(sqlx::query_scalar::<_, bool>(queries::get(pool.kind(), queries::Q048)).bind(pad_id).fetch_optional(pool.pool()).await?.unwrap_or(false));
return Ok(
sqlx::query_scalar::<_, bool>(queries::get(pool.kind(), queries::Q048))
.bind(pad_id)
.fetch_optional(pool.pool())
.await?
.unwrap_or(false),
);
}
Ok(sqlx::query_scalar::<_, i64>(queries::get(pool.kind(), queries::Q048)).bind(pad_id).fetch_optional(pool.pool()).await?.unwrap_or(0) != 0)
Ok(
sqlx::query_scalar::<_, i64>(queries::get(pool.kind(), queries::Q048))
.bind(pad_id)
.fetch_optional(pool.pool())
.await?
.unwrap_or(0)
!= 0,
)
}
pub async fn note_public_page_unprotected(pool: &Database, note_id: i64) -> Result<bool, sqlx::Error> {
pub async fn note_public_page_unprotected(
pool: &Database,
note_id: i64,
) -> Result<bool, sqlx::Error> {
if pool.kind() == DatabaseKind::Postgres {
return Ok(sqlx::query_scalar::<_, bool>(queries::get(pool.kind(), queries::Q049)).bind(note_id).fetch_optional(pool.pool()).await?.unwrap_or(false));
return Ok(
sqlx::query_scalar::<_, bool>(queries::get(pool.kind(), queries::Q049))
.bind(note_id)
.fetch_optional(pool.pool())
.await?
.unwrap_or(false),
);
}
Ok(sqlx::query_scalar::<_, i64>(queries::get(pool.kind(), queries::Q049)).bind(note_id).fetch_optional(pool.pool()).await?.unwrap_or(0) != 0)
Ok(
sqlx::query_scalar::<_, i64>(queries::get(pool.kind(), queries::Q049))
.bind(note_id)
.fetch_optional(pool.pool())
.await?
.unwrap_or(0)
!= 0,
)
}
pub async fn set_pad_public_page_unprotected(pool: &Database, pad_id: i64, value: bool) -> Result<(), sqlx::Error> {
pub async fn set_pad_public_page_unprotected(
pool: &Database,
pad_id: i64,
value: bool,
) -> Result<(), sqlx::Error> {
publish_pad(pool, pad_id).await?;
let mut query = sqlx::query(queries::get(pool.kind(), queries::Q050));
query = if pool.kind() == DatabaseKind::Postgres { query.bind(value) } else { query.bind(if value { 1i64 } else { 0i64 }) };
query = if pool.kind() == DatabaseKind::Postgres {
query.bind(value)
} else {
query.bind(if value { 1i64 } else { 0i64 })
};
query.bind(pad_id).execute(pool.pool()).await?;
Ok(())
}
pub async fn set_note_public_page_unprotected(pool: &Database, note_id: i64, value: bool) -> Result<(), sqlx::Error> {
pub async fn set_note_public_page_unprotected(
pool: &Database,
note_id: i64,
value: bool,
) -> Result<(), sqlx::Error> {
publish_note(pool, note_id).await?;
let mut query = sqlx::query(queries::get(pool.kind(), queries::Q051));
query = if pool.kind() == DatabaseKind::Postgres { query.bind(value) } else { query.bind(if value { 1i64 } else { 0i64 }) };
query = if pool.kind() == DatabaseKind::Postgres {
query.bind(value)
} else {
query.bind(if value { 1i64 } else { 0i64 })
};
query.bind(note_id).execute(pool.pool()).await?;
Ok(())
}
@@ -337,7 +409,6 @@ pub async fn note_file_token(pool: &Database, note_id: i64) -> Result<String, sq
.await
}
impl<'r> sqlx::FromRow<'r, AnyRow> for PublishedPageRow {
fn from_row(row: &'r AnyRow) -> Result<Self, sqlx::Error> {
Ok(Self {
@@ -366,27 +437,75 @@ impl<'r> sqlx::FromRow<'r, AnyRow> for PostgresPublishedPageRow {
}
pub async fn pad_public_page_disabled(pool: &Database, pad_id: i64) -> Result<bool, sqlx::Error> {
let sql = match pool.kind() { DatabaseKind::Postgres => "SELECT public_page_disabled FROM pads WHERE id = $1", _ => "SELECT public_page_disabled FROM pads WHERE id = ?" };
if pool.kind() == DatabaseKind::Postgres { return Ok(sqlx::query_scalar::<_, bool>(sql).bind(pad_id).fetch_one(pool.pool()).await?); }
Ok(sqlx::query_scalar::<_, i64>(sql).bind(pad_id).fetch_one(pool.pool()).await? != 0)
let sql = match pool.kind() {
DatabaseKind::Postgres => "SELECT public_page_disabled FROM pads WHERE id = $1",
_ => "SELECT public_page_disabled FROM pads WHERE id = ?",
};
if pool.kind() == DatabaseKind::Postgres {
return Ok(sqlx::query_scalar::<_, bool>(sql)
.bind(pad_id)
.fetch_one(pool.pool())
.await?);
}
Ok(sqlx::query_scalar::<_, i64>(sql)
.bind(pad_id)
.fetch_one(pool.pool())
.await?
!= 0)
}
pub async fn note_public_page_disabled(pool: &Database, note_id: i64) -> Result<bool, sqlx::Error> {
let sql = match pool.kind() { DatabaseKind::Postgres => "SELECT public_page_disabled FROM notes WHERE id = $1", _ => "SELECT public_page_disabled FROM notes WHERE id = ?" };
if pool.kind() == DatabaseKind::Postgres { return Ok(sqlx::query_scalar::<_, bool>(sql).bind(note_id).fetch_one(pool.pool()).await?); }
Ok(sqlx::query_scalar::<_, i64>(sql).bind(note_id).fetch_one(pool.pool()).await? != 0)
let sql = match pool.kind() {
DatabaseKind::Postgres => "SELECT public_page_disabled FROM notes WHERE id = $1",
_ => "SELECT public_page_disabled FROM notes WHERE id = ?",
};
if pool.kind() == DatabaseKind::Postgres {
return Ok(sqlx::query_scalar::<_, bool>(sql)
.bind(note_id)
.fetch_one(pool.pool())
.await?);
}
Ok(sqlx::query_scalar::<_, i64>(sql)
.bind(note_id)
.fetch_one(pool.pool())
.await?
!= 0)
}
pub async fn set_pad_public_page_disabled(pool: &Database, pad_id: i64, disabled: bool) -> Result<(), sqlx::Error> {
let sql = match pool.kind() { DatabaseKind::Postgres => "UPDATE pads SET public_page_disabled = $1 WHERE id = $2", _ => "UPDATE pads SET public_page_disabled = ? WHERE id = ?" };
pub async fn set_pad_public_page_disabled(
pool: &Database,
pad_id: i64,
disabled: bool,
) -> Result<(), sqlx::Error> {
let sql = match pool.kind() {
DatabaseKind::Postgres => "UPDATE pads SET public_page_disabled = $1 WHERE id = $2",
_ => "UPDATE pads SET public_page_disabled = ? WHERE id = ?",
};
let mut query = sqlx::query(sql);
query = if pool.kind() == DatabaseKind::Postgres { query.bind(disabled) } else { query.bind(if disabled { 1i64 } else { 0i64 }) };
query.bind(pad_id).execute(pool.pool()).await?; Ok(())
query = if pool.kind() == DatabaseKind::Postgres {
query.bind(disabled)
} else {
query.bind(if disabled { 1i64 } else { 0i64 })
};
query.bind(pad_id).execute(pool.pool()).await?;
Ok(())
}
pub async fn set_note_public_page_disabled(pool: &Database, note_id: i64, disabled: bool) -> Result<(), sqlx::Error> {
let sql = match pool.kind() { DatabaseKind::Postgres => "UPDATE notes SET public_page_disabled = $1 WHERE id = $2", _ => "UPDATE notes SET public_page_disabled = ? WHERE id = ?" };
pub async fn set_note_public_page_disabled(
pool: &Database,
note_id: i64,
disabled: bool,
) -> Result<(), sqlx::Error> {
let sql = match pool.kind() {
DatabaseKind::Postgres => "UPDATE notes SET public_page_disabled = $1 WHERE id = $2",
_ => "UPDATE notes SET public_page_disabled = ? WHERE id = ?",
};
let mut query = sqlx::query(sql);
query = if pool.kind() == DatabaseKind::Postgres { query.bind(disabled) } else { query.bind(if disabled { 1i64 } else { 0i64 }) };
query.bind(note_id).execute(pool.pool()).await?; Ok(())
query = if pool.kind() == DatabaseKind::Postgres {
query.bind(disabled)
} else {
query.bind(if disabled { 1i64 } else { 0i64 })
};
query.bind(note_id).execute(pool.pool()).await?;
Ok(())
}