Merge branch 'main' into API-endpoint-for-listing-read-posts-#4934-

This commit is contained in:
leoseg 2024-12-21 15:15:48 +01:00 committed by GitHub
commit c98f83fb76
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 325 additions and 411 deletions

74
Cargo.lock generated
View file

@ -460,9 +460,9 @@ dependencies = [
[[package]] [[package]]
name = "async-compression" name = "async-compression"
version = "0.4.12" version = "0.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fec134f64e2bc57411226dfc4e52dec859ddfc7e711fc5e07b612584f000e4aa" checksum = "df895a515f70646414f4b45c0b79082783b80552b373a68283012928df56f522"
dependencies = [ dependencies = [
"flate2", "flate2",
"futures-core", "futures-core",
@ -495,15 +495,15 @@ dependencies = [
[[package]] [[package]]
name = "atom_syndication" name = "atom_syndication"
version = "0.12.4" version = "0.12.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a3a5ed3201df5658d1aa45060c5a57dc9dba8a8ada20d696d67cb0c479ee043" checksum = "3ee79fb83c725eae67b55218870813d2fc39fd85e4f1583848ef9f4f823cfe7c"
dependencies = [ dependencies = [
"chrono", "chrono",
"derive_builder", "derive_builder",
"diligent-date-parser", "diligent-date-parser",
"never", "never",
"quick-xml 0.36.1", "quick-xml 0.37.1",
] ]
[[package]] [[package]]
@ -514,21 +514,20 @@ checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0"
[[package]] [[package]]
name = "aws-lc-rs" name = "aws-lc-rs"
version = "1.9.0" version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f95446d919226d587817a7d21379e6eb099b97b45110a7f272a444ca5c54070" checksum = "f47bb8cc16b669d267eeccf585aea077d0882f4777b1c1f740217885d6e6e5a3"
dependencies = [ dependencies = [
"aws-lc-sys", "aws-lc-sys",
"mirai-annotations",
"paste", "paste",
"zeroize", "zeroize",
] ]
[[package]] [[package]]
name = "aws-lc-sys" name = "aws-lc-sys"
version = "0.21.1" version = "0.23.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "234314bd569802ec87011d653d6815c6d7b9ffb969e9fee5b8b20ef860e8dce9" checksum = "a2101df3813227bbaaaa0b04cd61c534c7954b22bd68d399b440be937dc63ff7"
dependencies = [ dependencies = [
"bindgen", "bindgen",
"cc", "cc",
@ -618,9 +617,9 @@ dependencies = [
[[package]] [[package]]
name = "bindgen" name = "bindgen"
version = "0.69.4" version = "0.69.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a00dc851838a2120612785d195287475a3ac45514741da670b735818822129a0" checksum = "271383c67ccabffb7381723dea0672a673f292304fcb45c01cc648c7a8d58088"
dependencies = [ dependencies = [
"bitflags 2.6.0", "bitflags 2.6.0",
"cexpr", "cexpr",
@ -907,9 +906,9 @@ dependencies = [
[[package]] [[package]]
name = "cmake" name = "cmake"
version = "0.1.51" version = "0.1.52"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb1e43aa7fd152b1f968787f7dbcdeb306d1867ff373c69955211876c053f91a" checksum = "c682c223677e0e5b6b7f63a64b9351844c3f1b1678a68b7ee617e30fb082620e"
dependencies = [ dependencies = [
"cc", "cc",
] ]
@ -1374,9 +1373,9 @@ dependencies = [
[[package]] [[package]]
name = "diligent-date-parser" name = "diligent-date-parser"
version = "0.1.4" version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6cf7fe294274a222363f84bcb63cdea762979a0443b4cf1f4f8fd17c86b1182" checksum = "c8ede7d79366f419921e2e2f67889c12125726692a313bffb474bd5f37a581e9"
dependencies = [ dependencies = [
"chrono", "chrono",
] ]
@ -1471,9 +1470,9 @@ dependencies = [
[[package]] [[package]]
name = "email-encoding" name = "email-encoding"
version = "0.3.0" version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60d1d33cdaede7e24091f039632eb5d3c7469fe5b066a985281a34fc70fa317f" checksum = "ea3d894bbbab314476b265f9b2d46bf24b123a36dd0e96b06a1b49545b9d9dcc"
dependencies = [ dependencies = [
"base64 0.22.1", "base64 0.22.1",
"memchr", "memchr",
@ -1599,9 +1598,9 @@ dependencies = [
[[package]] [[package]]
name = "fastrand" name = "fastrand"
version = "2.1.1" version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6" checksum = "486f806e73c5707928240ddc295403b1b93c96a02038563881c4a2fd84b81ac4"
[[package]] [[package]]
name = "fdeflate" name = "fdeflate"
@ -2806,7 +2805,6 @@ dependencies = [
"lemmy_utils", "lemmy_utils",
"pretty_assertions", "pretty_assertions",
"prometheus", "prometheus",
"reqwest 0.12.8",
"reqwest-middleware", "reqwest-middleware",
"reqwest-tracing", "reqwest-tracing",
"rustls 0.23.16", "rustls 0.23.16",
@ -2897,12 +2895,12 @@ checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439"
[[package]] [[package]]
name = "libloading" name = "libloading"
version = "0.8.5" version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4" checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"windows-targets 0.52.6", "windows-targets 0.48.5",
] ]
[[package]] [[package]]
@ -3219,12 +3217,6 @@ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "mirai-annotations"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9be0862c1b3f26a88803c4a49de6889c10e608b3ee9344e6ef5b45fb37ad3d1"
[[package]] [[package]]
name = "mockall" name = "mockall"
version = "0.13.0" version = "0.13.0"
@ -3598,9 +3590,9 @@ dependencies = [
[[package]] [[package]]
name = "pkg-config" name = "pkg-config"
version = "0.3.30" version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2"
[[package]] [[package]]
name = "plist" name = "plist"
@ -3725,9 +3717,9 @@ dependencies = [
[[package]] [[package]]
name = "prettyplease" name = "prettyplease"
version = "0.2.22" version = "0.2.25"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "479cf940fbbb3426c32c5d5176f62ad57549a0bb84773423ba8be9d089f5faba" checksum = "64d1ec885c64d0457d564db4ec299b2dae3f9c02808b8ad9c3a089c591b18033"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"syn 2.0.87", "syn 2.0.87",
@ -3821,16 +3813,6 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "quick-xml"
version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96a05e2e8efddfa51a84ca47cec303fac86c8541b686d37cac5efc0e094417bc"
dependencies = [
"encoding_rs",
"memchr",
]
[[package]] [[package]]
name = "quick-xml" name = "quick-xml"
version = "0.37.1" version = "0.37.1"
@ -4324,9 +4306,9 @@ dependencies = [
[[package]] [[package]]
name = "rustversion" name = "rustversion"
version = "1.0.17" version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" checksum = "0e819f2bc632f285be6d7cd36e25940d45b2391dd6d9b939e79de557f7014248"
[[package]] [[package]]
name = "ryu" name = "ryu"

View file

@ -103,21 +103,21 @@ diesel-async = "0.5.1"
serde = { version = "1.0.215", features = ["derive"] } serde = { version = "1.0.215", features = ["derive"] }
serde_with = "3.9.0" serde_with = "3.9.0"
actix-web = { version = "4.9.0", default-features = false, features = [ actix-web = { version = "4.9.0", default-features = false, features = [
"macros",
"rustls-0_23",
"compress-brotli", "compress-brotli",
"compress-gzip", "compress-gzip",
"compress-zstd", "compress-zstd",
"cookies", "cookies",
"macros",
"rustls-0_23",
] } ] }
tracing = "0.1.40" tracing = "0.1.40"
tracing-actix-web = { version = "0.7.10", default-features = false } tracing-actix-web = { version = "0.7.10", default-features = false }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
url = { version = "2.5.3", features = ["serde"] } url = { version = "2.5.3", features = ["serde"] }
reqwest = { version = "0.12.7", default-features = false, features = [ reqwest = { version = "0.12.7", default-features = false, features = [
"json",
"blocking", "blocking",
"gzip", "gzip",
"json",
"rustls-tls", "rustls-tls",
] } ] }
reqwest-middleware = "0.3.3" reqwest-middleware = "0.3.3"
@ -126,17 +126,15 @@ clokwerk = "0.4.0"
doku = { version = "0.21.1", features = ["url-2"] } doku = { version = "0.21.1", features = ["url-2"] }
bcrypt = "0.15.1" bcrypt = "0.15.1"
chrono = { version = "0.4.38", features = [ chrono = { version = "0.4.38", features = [
"serde",
"now", "now",
"serde",
], default-features = false } ], default-features = false }
serde_json = { version = "1.0.132", features = ["preserve_order"] } serde_json = { version = "1.0.132", features = ["preserve_order"] }
base64 = "0.22.1" base64 = "0.22.1"
uuid = { version = "1.11.0", features = ["serde"] } uuid = { version = "1.11.0", features = ["serde"] }
async-trait = "0.1.83" async-trait = "0.1.83"
captcha = "0.0.9" captcha = "0.0.9"
anyhow = { version = "1.0.93", features = [ anyhow = { version = "1.0.93", features = ["backtrace"] }
"backtrace",
] } # backtrace is on by default on nightly, but not stable rust
diesel_ltree = "0.3.1" diesel_ltree = "0.3.1"
serial_test = "3.2.0" serial_test = "3.2.0"
tokio = { version = "1.41.1", features = ["full"] } tokio = { version = "1.41.1", features = ["full"] }
@ -149,7 +147,6 @@ futures = "0.3.31"
http = "1.1" http = "1.1"
rosetta-i18n = "0.1.3" rosetta-i18n = "0.1.3"
ts-rs = { version = "10.0.0", features = [ ts-rs = { version = "10.0.0", features = [
"serde-compat",
"chrono-impl", "chrono-impl",
"no-serde-warnings", "no-serde-warnings",
"url-impl", "url-impl",
@ -185,7 +182,6 @@ tracing = { workspace = true }
tracing-actix-web = { workspace = true } tracing-actix-web = { workspace = true }
tracing-subscriber = { workspace = true } tracing-subscriber = { workspace = true }
url = { workspace = true } url = { workspace = true }
reqwest = { workspace = true }
reqwest-middleware = { workspace = true } reqwest-middleware = { workspace = true }
reqwest-tracing = { workspace = true } reqwest-tracing = { workspace = true }
clokwerk = { workspace = true } clokwerk = { workspace = true }

View file

@ -67,9 +67,9 @@ urlencoding = { workspace = true }
mime = { version = "0.3.17", optional = true } mime = { version = "0.3.17", optional = true }
mime_guess = "2.0.5" mime_guess = "2.0.5"
infer = "0.16.0" infer = "0.16.0"
webpage = { version = "2.0", default-features = false, features = [ webpage = { version = "2.0", default-features = false, optional = true, features = [
"serde", "serde",
], optional = true } ] }
encoding_rs = { version = "0.8.35", optional = true } encoding_rs = { version = "0.8.35", optional = true }
jsonwebtoken = { version = "9.3.0", optional = true } jsonwebtoken = { version = "9.3.0", optional = true }

View file

@ -52,8 +52,8 @@ activitypub_federation = { workspace = true, optional = true }
lemmy_utils = { workspace = true, optional = true } lemmy_utils = { workspace = true, optional = true }
bcrypt = { workspace = true, optional = true } bcrypt = { workspace = true, optional = true }
diesel = { workspace = true, features = [ diesel = { workspace = true, features = [
"postgres",
"chrono", "chrono",
"postgres",
"serde_json", "serde_json",
"uuid", "uuid",
], optional = true } ], optional = true }
@ -61,14 +61,14 @@ diesel-derive-newtype = { workspace = true, optional = true }
diesel-derive-enum = { workspace = true, optional = true } diesel-derive-enum = { workspace = true, optional = true }
diesel_migrations = { workspace = true, optional = true } diesel_migrations = { workspace = true, optional = true }
diesel-async = { workspace = true, features = [ diesel-async = { workspace = true, features = [
"postgres",
"deadpool", "deadpool",
"postgres",
], optional = true } ], optional = true }
regex = { workspace = true, optional = true } regex = { workspace = true, optional = true }
diesel_ltree = { workspace = true, optional = true } diesel_ltree = { workspace = true, optional = true }
async-trait = { workspace = true } async-trait = { workspace = true }
tracing = { workspace = true } tracing = { workspace = true }
deadpool = { version = "0.12.1", features = ["rt_tokio_1"], optional = true } deadpool = { version = "0.12.1", optional = true, features = ["rt_tokio_1"] }
ts-rs = { workspace = true, optional = true } ts-rs = { workspace = true, optional = true }
futures-util = { workspace = true } futures-util = { workspace = true }
tokio = { workspace = true, optional = true } tokio = { workspace = true, optional = true }

View file

@ -186,26 +186,26 @@ BEGIN
AND pe.bot_account = FALSE AND pe.bot_account = FALSE
UNION UNION
SELECT SELECT
pl.person_id, pa.person_id,
p.community_id p.community_id
FROM FROM
post_like pl post_actions pa
INNER JOIN post p ON pl.post_id = p.id INNER JOIN post p ON pa.post_id = p.id
INNER JOIN person pe ON pl.person_id = pe.id INNER JOIN person pe ON pa.person_id = pe.id
WHERE WHERE
pl.published > ('now'::timestamp - i::interval) pa.liked > ('now'::timestamp - i::interval)
AND pe.bot_account = FALSE AND pe.bot_account = FALSE
UNION UNION
SELECT SELECT
cl.person_id, ca.person_id,
p.community_id p.community_id
FROM FROM
comment_like cl comment_actions ca
INNER JOIN comment c ON cl.comment_id = c.id INNER JOIN comment c ON ca.comment_id = c.id
INNER JOIN post p ON c.post_id = p.id INNER JOIN post p ON c.post_id = p.id
INNER JOIN person pe ON cl.person_id = pe.id INNER JOIN person pe ON ca.person_id = pe.id
WHERE WHERE
cl.published > ('now'::timestamp - i::interval) ca.liked > ('now'::timestamp - i::interval)
AND pe.bot_account = FALSE) a AND pe.bot_account = FALSE) a
GROUP BY GROUP BY
community_id; community_id;
@ -244,22 +244,22 @@ BEGIN
AND pe.bot_account = FALSE AND pe.bot_account = FALSE
UNION UNION
SELECT SELECT
pl.person_id pa.person_id
FROM FROM
post_like pl post_actions pa
INNER JOIN person pe ON pl.person_id = pe.id INNER JOIN person pe ON pa.person_id = pe.id
WHERE WHERE
pl.published > ('now'::timestamp - i::interval) pa.liked > ('now'::timestamp - i::interval)
AND pe.local = TRUE AND pe.local = TRUE
AND pe.bot_account = FALSE AND pe.bot_account = FALSE
UNION UNION
SELECT SELECT
cl.person_id ca.person_id
FROM FROM
comment_like cl comment_actions ca
INNER JOIN person pe ON cl.person_id = pe.id INNER JOIN person pe ON ca.person_id = pe.id
WHERE WHERE
cl.published > ('now'::timestamp - i::interval) ca.liked > ('now'::timestamp - i::interval)
AND pe.local = TRUE AND pe.local = TRUE
AND pe.bot_account = FALSE) a; AND pe.bot_account = FALSE) a;
RETURN count_; RETURN count_;

View file

@ -26,13 +26,13 @@ full = [
[dependencies] [dependencies]
lemmy_db_schema = { workspace = true } lemmy_db_schema = { workspace = true }
diesel = { workspace = true, features = [ diesel = { workspace = true, features = [
"postgres",
"chrono", "chrono",
"postgres",
"serde_json", "serde_json",
], optional = true } ], optional = true }
diesel-async = { workspace = true, features = [ diesel-async = { workspace = true, features = [
"postgres",
"deadpool", "deadpool",
"postgres",
], optional = true } ], optional = true }
serde = { workspace = true } serde = { workspace = true }
serde_with = { workspace = true } serde_with = { workspace = true }

View file

@ -20,13 +20,13 @@ full = ["lemmy_db_schema/full", "diesel", "diesel-async", "ts-rs"]
[dependencies] [dependencies]
lemmy_db_schema = { workspace = true } lemmy_db_schema = { workspace = true }
diesel = { workspace = true, features = [ diesel = { workspace = true, features = [
"postgres",
"chrono", "chrono",
"postgres",
"serde_json", "serde_json",
], optional = true } ], optional = true }
diesel-async = { workspace = true, features = [ diesel-async = { workspace = true, features = [
"postgres",
"deadpool", "deadpool",
"postgres",
], optional = true } ], optional = true }
serde = { workspace = true } serde = { workspace = true }
serde_with = { workspace = true } serde_with = { workspace = true }

View file

@ -25,7 +25,7 @@ activitypub_federation.workspace = true
anyhow.workspace = true anyhow.workspace = true
futures.workspace = true futures.workspace = true
chrono.workspace = true chrono.workspace = true
diesel = { workspace = true, features = ["postgres", "chrono", "serde_json"] } diesel = { workspace = true, features = ["chrono", "postgres", "serde_json"] }
diesel-async = { workspace = true, features = ["deadpool", "postgres"] } diesel-async = { workspace = true, features = ["deadpool", "postgres"] }
reqwest.workspace = true reqwest.workspace = true
serde_json.workspace = true serde_json.workspace = true

View file

@ -65,10 +65,10 @@ anyhow = { workspace = true, optional = true }
reqwest-middleware = { workspace = true, optional = true } reqwest-middleware = { workspace = true, optional = true }
strum = { workspace = true } strum = { workspace = true }
futures = { workspace = true, optional = true } futures = { workspace = true, optional = true }
diesel = { workspace = true, features = ["chrono"], optional = true } diesel = { workspace = true, optional = true, features = ["chrono"] }
http = { workspace = true, optional = true } http = { workspace = true, optional = true }
doku = { workspace = true, features = ["url-2"], optional = true } doku = { workspace = true, features = ["url-2"], optional = true }
uuid = { workspace = true, features = ["serde", "v4"], optional = true } uuid = { workspace = true, optional = true, features = ["v4"] }
rosetta-i18n = { workspace = true, optional = true } rosetta-i18n = { workspace = true, optional = true }
tokio = { workspace = true, optional = true } tokio = { workspace = true, optional = true }
urlencoding = { workspace = true, optional = true } urlencoding = { workspace = true, optional = true }
@ -77,9 +77,8 @@ deser-hjson = { version = "2.2.4", optional = true }
smart-default = { version = "0.7.1", optional = true } smart-default = { version = "0.7.1", optional = true }
lettre = { version = "0.11.10", default-features = false, features = [ lettre = { version = "0.11.10", default-features = false, features = [
"builder", "builder",
"tokio1",
"tokio1-rustls-tls",
"smtp-transport", "smtp-transport",
"tokio1-rustls-tls",
], optional = true } ], optional = true }
markdown-it = { version = "0.6.1", optional = true } markdown-it = { version = "0.6.1", optional = true }
ts-rs = { workspace = true, optional = true } ts-rs = { workspace = true, optional = true }

View file

@ -0,0 +1,3 @@
SELECT
1;

View file

@ -0,0 +1,3 @@
SELECT
1;

View file

@ -370,3 +370,18 @@ fn cors_config(settings: &Settings) -> Cors {
_ => cors_default, _ => cors_default,
} }
} }
#[cfg(test)]
pub mod tests {
use activitypub_federation::config::Data;
use lemmy_api_common::context::LemmyContext;
use std::env::set_current_dir;
pub async fn test_context() -> Data<LemmyContext> {
// hack, necessary so that config file can be loaded from hardcoded, relative path.
// Ignore errors as this gets called once for every test (so changing dir again would fail).
set_current_dir("crates/utils").ok();
LemmyContext::init_test_context().await
}
}

View file

@ -40,16 +40,19 @@ use lemmy_db_schema::{
utils::{find_action, functions::coalesce, get_conn, now, DbPool, DELETED_REPLACEMENT_TEXT}, utils::{find_action, functions::coalesce, get_conn, now, DbPool, DELETED_REPLACEMENT_TEXT},
}; };
use lemmy_routes::nodeinfo::{NodeInfo, NodeInfoWellKnown}; use lemmy_routes::nodeinfo::{NodeInfo, NodeInfoWellKnown};
use lemmy_utils::error::LemmyResult; use lemmy_utils::error::{LemmyErrorType, LemmyResult};
use reqwest_middleware::ClientWithMiddleware; use reqwest_middleware::ClientWithMiddleware;
use std::time::Duration; use std::time::Duration;
use tracing::{error, info, warn}; use tracing::{info, warn};
/// Schedules various cleanup tasks for lemmy in a background thread /// Schedules various cleanup tasks for lemmy in a background thread
pub async fn setup(context: Data<LemmyContext>) -> LemmyResult<()> { pub async fn setup(context: Data<LemmyContext>) -> LemmyResult<()> {
// Setup the connections // Setup the connections
let mut scheduler = AsyncScheduler::new(); let mut scheduler = AsyncScheduler::new();
startup_jobs(&mut context.pool()).await; startup_jobs(&mut context.pool())
.await
.inspect_err(|e| warn!("Failed to run startup tasks: {e}"))
.ok();
let context_1 = context.clone(); let context_1 = context.clone();
// Update active counts expired bans and unpublished posts every hour // Update active counts expired bans and unpublished posts every hour
@ -57,9 +60,18 @@ pub async fn setup(context: Data<LemmyContext>) -> LemmyResult<()> {
let context = context_1.clone(); let context = context_1.clone();
async move { async move {
active_counts(&mut context.pool()).await; active_counts(&mut context.pool())
update_banned_when_expired(&mut context.pool()).await; .await
delete_instance_block_when_expired(&mut context.pool()).await; .inspect_err(|e| warn!("Failed to update active counts: {e}"))
.ok();
update_banned_when_expired(&mut context.pool())
.await
.inspect_err(|e| warn!("Failed to update expired bans: {e}"))
.ok();
delete_instance_block_when_expired(&mut context.pool())
.await
.inspect_err(|e| warn!("Failed to delete expired instance bans: {e}"))
.ok();
} }
}); });
@ -69,9 +81,18 @@ pub async fn setup(context: Data<LemmyContext>) -> LemmyResult<()> {
let context = context_1.reset_request_count(); let context = context_1.reset_request_count();
async move { async move {
update_hot_ranks(&mut context.pool()).await; update_hot_ranks(&mut context.pool())
delete_expired_captcha_answers(&mut context.pool()).await; .await
publish_scheduled_posts(&context).await; .inspect_err(|e| warn!("Failed to update hot ranks: {e}"))
.ok();
delete_expired_captcha_answers(&mut context.pool())
.await
.inspect_err(|e| warn!("Failed to delete expired captcha answers: {e}"))
.ok();
publish_scheduled_posts(&context)
.await
.inspect_err(|e| warn!("Failed to publish scheduled posts: {e}"))
.ok();
} }
}); });
@ -81,7 +102,10 @@ pub async fn setup(context: Data<LemmyContext>) -> LemmyResult<()> {
let context = context_1.clone(); let context = context_1.clone();
async move { async move {
clear_old_activities(&mut context.pool()).await; clear_old_activities(&mut context.pool())
.await
.inspect_err(|e| warn!("Failed to clear old activities: {e}"))
.ok();
} }
}); });
@ -94,8 +118,14 @@ pub async fn setup(context: Data<LemmyContext>) -> LemmyResult<()> {
let context = context_1.clone(); let context = context_1.clone();
async move { async move {
overwrite_deleted_posts_and_comments(&mut context.pool()).await; overwrite_deleted_posts_and_comments(&mut context.pool())
delete_old_denied_users(&mut context.pool()).await; .await
.inspect_err(|e| warn!("Failed to overwrite deleted posts/comments: {e}"))
.ok();
delete_old_denied_users(&mut context.pool())
.await
.inspect_err(|e| warn!("Failed to delete old denied users: {e}"))
.ok();
update_instance_software(&mut context.pool(), context.client()) update_instance_software(&mut context.pool(), context.client())
.await .await
.inspect_err(|e| warn!("Failed to update instance software: {e}")) .inspect_err(|e| warn!("Failed to update instance software: {e}"))
@ -111,49 +141,44 @@ pub async fn setup(context: Data<LemmyContext>) -> LemmyResult<()> {
} }
/// Run these on server startup /// Run these on server startup
async fn startup_jobs(pool: &mut DbPool<'_>) { async fn startup_jobs(pool: &mut DbPool<'_>) -> LemmyResult<()> {
active_counts(pool).await; active_counts(pool).await?;
update_hot_ranks(pool).await; update_hot_ranks(pool).await?;
update_banned_when_expired(pool).await; update_banned_when_expired(pool).await?;
delete_instance_block_when_expired(pool).await; delete_instance_block_when_expired(pool).await?;
clear_old_activities(pool).await; clear_old_activities(pool).await?;
overwrite_deleted_posts_and_comments(pool).await; overwrite_deleted_posts_and_comments(pool).await?;
delete_old_denied_users(pool).await; delete_old_denied_users(pool).await?;
Ok(())
} }
/// Update the hot_rank columns for the aggregates tables /// Update the hot_rank columns for the aggregates tables
/// Runs in batches until all necessary rows are updated once /// Runs in batches until all necessary rows are updated once
async fn update_hot_ranks(pool: &mut DbPool<'_>) { async fn update_hot_ranks(pool: &mut DbPool<'_>) -> LemmyResult<()> {
info!("Updating hot ranks for all history..."); info!("Updating hot ranks for all history...");
let conn = get_conn(pool).await; let mut conn = get_conn(pool).await?;
match conn { process_post_aggregates_ranks_in_batches(&mut conn).await?;
Ok(mut conn) => {
process_post_aggregates_ranks_in_batches(&mut conn).await;
process_ranks_in_batches( process_ranks_in_batches(
&mut conn, &mut conn,
"comment", "comment",
"a.hot_rank != 0", "a.hot_rank != 0",
"SET hot_rank = r.hot_rank(a.score, a.published)", "SET hot_rank = r.hot_rank(a.score, a.published)",
) )
.await; .await?;
process_ranks_in_batches( process_ranks_in_batches(
&mut conn, &mut conn,
"community", "community",
"a.hot_rank != 0", "a.hot_rank != 0",
"SET hot_rank = r.hot_rank(a.subscribers, a.published)", "SET hot_rank = r.hot_rank(a.subscribers, a.published)",
) )
.await; .await?;
info!("Finished hot ranks update!"); info!("Finished hot ranks update!");
} Ok(())
Err(e) => {
error!("Failed to get connection from pool: {e}");
}
}
} }
#[derive(QueryableByName)] #[derive(QueryableByName)]
@ -171,7 +196,7 @@ async fn process_ranks_in_batches(
table_name: &str, table_name: &str,
where_clause: &str, where_clause: &str,
set_clause: &str, set_clause: &str,
) { ) -> LemmyResult<()> {
let process_start_time: DateTime<Utc> = Utc.timestamp_opt(0, 0).single().unwrap_or_default(); let process_start_time: DateTime<Utc> = Utc.timestamp_opt(0, 0).single().unwrap_or_default();
let update_batch_size = 1000; // Bigger batches than this tend to cause seq scans let update_batch_size = 1000; // Bigger batches than this tend to cause seq scans
@ -180,7 +205,7 @@ async fn process_ranks_in_batches(
while let Some(previous_batch_last_published) = previous_batch_result { while let Some(previous_batch_last_published) = previous_batch_result {
// Raw `sql_query` is used as a performance optimization - Diesel does not support doing this // Raw `sql_query` is used as a performance optimization - Diesel does not support doing this
// in a single query (neither as a CTE, nor using a subquery) // in a single query (neither as a CTE, nor using a subquery)
let result = sql_query(format!( let updated_rows = sql_query(format!(
r#"WITH batch AS (SELECT a.{id_column} r#"WITH batch AS (SELECT a.{id_column}
FROM {aggregates_table} a FROM {aggregates_table} a
WHERE a.published > $1 AND ({where_clause}) WHERE a.published > $1 AND ({where_clause})
@ -196,35 +221,31 @@ async fn process_ranks_in_batches(
.bind::<Timestamptz, _>(previous_batch_last_published) .bind::<Timestamptz, _>(previous_batch_last_published)
.bind::<Integer, _>(update_batch_size) .bind::<Integer, _>(update_batch_size)
.get_results::<HotRanksUpdateResult>(conn) .get_results::<HotRanksUpdateResult>(conn)
.await; .await
.map_err(|e| {
LemmyErrorType::Unknown(format!("Failed to update {} hot_ranks: {}", table_name, e))
})?;
match result { processed_rows_count += updated_rows.len();
Ok(updated_rows) => { previous_batch_result = updated_rows.last().map(|row| row.published);
processed_rows_count += updated_rows.len();
previous_batch_result = updated_rows.last().map(|row| row.published);
}
Err(e) => {
error!("Failed to update {} hot_ranks: {}", table_name, e);
break;
}
}
} }
info!( info!(
"Finished process_hot_ranks_in_batches execution for {} (processed {} rows)", "Finished process_hot_ranks_in_batches execution for {} (processed {} rows)",
table_name, processed_rows_count table_name, processed_rows_count
); );
Ok(())
} }
/// Post aggregates is a special case, since it needs to join to the community_aggregates /// Post aggregates is a special case, since it needs to join to the community_aggregates
/// table, to get the active monthly user counts. /// table, to get the active monthly user counts.
async fn process_post_aggregates_ranks_in_batches(conn: &mut AsyncPgConnection) { async fn process_post_aggregates_ranks_in_batches(conn: &mut AsyncPgConnection) -> LemmyResult<()> {
let process_start_time: DateTime<Utc> = Utc.timestamp_opt(0, 0).single().unwrap_or_default(); let process_start_time: DateTime<Utc> = Utc.timestamp_opt(0, 0).single().unwrap_or_default();
let update_batch_size = 1000; // Bigger batches than this tend to cause seq scans let update_batch_size = 1000; // Bigger batches than this tend to cause seq scans
let mut processed_rows_count = 0; let mut processed_rows_count = 0;
let mut previous_batch_result = Some(process_start_time); let mut previous_batch_result = Some(process_start_time);
while let Some(previous_batch_last_published) = previous_batch_result { while let Some(previous_batch_last_published) = previous_batch_result {
let result = sql_query( let updated_rows = sql_query(
r#"WITH batch AS (SELECT pa.post_id r#"WITH batch AS (SELECT pa.post_id
FROM post_aggregates pa FROM post_aggregates pa
WHERE pa.published > $1 WHERE pa.published > $1
@ -243,283 +264,190 @@ async fn process_post_aggregates_ranks_in_batches(conn: &mut AsyncPgConnection)
.bind::<Timestamptz, _>(previous_batch_last_published) .bind::<Timestamptz, _>(previous_batch_last_published)
.bind::<Integer, _>(update_batch_size) .bind::<Integer, _>(update_batch_size)
.get_results::<HotRanksUpdateResult>(conn) .get_results::<HotRanksUpdateResult>(conn)
.await; .await.map_err(|e| LemmyErrorType::Unknown(format!("Failed to update {} hot_ranks: {}", "post_aggregates", e)))?;
match result { processed_rows_count += updated_rows.len();
Ok(updated_rows) => { previous_batch_result = updated_rows.last().map(|row| row.published);
processed_rows_count += updated_rows.len();
previous_batch_result = updated_rows.last().map(|row| row.published);
}
Err(e) => {
error!("Failed to update {} hot_ranks: {}", "post_aggregates", e);
break;
}
}
} }
info!( info!(
"Finished process_hot_ranks_in_batches execution for {} (processed {} rows)", "Finished process_hot_ranks_in_batches execution for {} (processed {} rows)",
"post_aggregates", processed_rows_count "post_aggregates", processed_rows_count
); );
Ok(())
} }
async fn delete_expired_captcha_answers(pool: &mut DbPool<'_>) { async fn delete_expired_captcha_answers(pool: &mut DbPool<'_>) -> LemmyResult<()> {
let conn = get_conn(pool).await; let mut conn = get_conn(pool).await?;
match conn { diesel::delete(
Ok(mut conn) => { captcha_answer::table.filter(captcha_answer::published.lt(now() - IntervalDsl::minutes(10))),
diesel::delete( )
captcha_answer::table .execute(&mut conn)
.filter(captcha_answer::published.lt(now() - IntervalDsl::minutes(10))), .await?;
) info!("Done.");
.execute(&mut conn)
.await Ok(())
.map(|_| {
info!("Done.");
})
.inspect_err(|e| error!("Failed to clear old captcha answers: {e}"))
.ok();
}
Err(e) => {
error!("Failed to get connection from pool: {e}");
}
}
} }
/// Clear old activities (this table gets very large) /// Clear old activities (this table gets very large)
async fn clear_old_activities(pool: &mut DbPool<'_>) { async fn clear_old_activities(pool: &mut DbPool<'_>) -> LemmyResult<()> {
info!("Clearing old activities..."); info!("Clearing old activities...");
let conn = get_conn(pool).await; let mut conn = get_conn(pool).await?;
match conn { diesel::delete(
Ok(mut conn) => { sent_activity::table.filter(sent_activity::published.lt(now() - IntervalDsl::days(7))),
diesel::delete( )
sent_activity::table.filter(sent_activity::published.lt(now() - IntervalDsl::days(7))), .execute(&mut conn)
) .await?;
.execute(&mut conn)
.await
.inspect_err(|e| error!("Failed to clear old sent activities: {e}"))
.ok();
diesel::delete( diesel::delete(
received_activity::table received_activity::table.filter(received_activity::published.lt(now() - IntervalDsl::days(7))),
.filter(received_activity::published.lt(now() - IntervalDsl::days(7))), )
) .execute(&mut conn)
.execute(&mut conn) .await?;
.await info!("Done.");
.map(|_| info!("Done.")) Ok(())
.inspect_err(|e| error!("Failed to clear old received activities: {e}"))
.ok();
}
Err(e) => {
error!("Failed to get connection from pool: {e}");
}
}
} }
async fn delete_old_denied_users(pool: &mut DbPool<'_>) { async fn delete_old_denied_users(pool: &mut DbPool<'_>) -> LemmyResult<()> {
LocalUser::delete_old_denied_local_users(pool) LocalUser::delete_old_denied_local_users(pool).await?;
.await info!("Done.");
.map(|_| { Ok(())
info!("Done.");
})
.inspect_err(|e| error!("Failed to deleted old denied users: {e}"))
.ok();
} }
/// overwrite posts and comments 30d after deletion /// overwrite posts and comments 30d after deletion
async fn overwrite_deleted_posts_and_comments(pool: &mut DbPool<'_>) { async fn overwrite_deleted_posts_and_comments(pool: &mut DbPool<'_>) -> LemmyResult<()> {
info!("Overwriting deleted posts..."); info!("Overwriting deleted posts...");
let conn = get_conn(pool).await; let mut conn = get_conn(pool).await?;
match conn { diesel::update(
Ok(mut conn) => { post::table
diesel::update( .filter(post::deleted.eq(true))
post::table .filter(post::updated.lt(now().nullable() - 1.months()))
.filter(post::deleted.eq(true)) .filter(post::body.ne(DELETED_REPLACEMENT_TEXT)),
.filter(post::updated.lt(now().nullable() - 1.months())) )
.filter(post::body.ne(DELETED_REPLACEMENT_TEXT)), .set((
) post::body.eq(DELETED_REPLACEMENT_TEXT),
.set(( post::name.eq(DELETED_REPLACEMENT_TEXT),
post::body.eq(DELETED_REPLACEMENT_TEXT), ))
post::name.eq(DELETED_REPLACEMENT_TEXT), .execute(&mut conn)
)) .await?;
.execute(&mut conn)
.await
.map(|_| {
info!("Done.");
})
.inspect_err(|e| error!("Failed to overwrite deleted posts: {e}"))
.ok();
info!("Overwriting deleted comments..."); info!("Overwriting deleted comments...");
diesel::update( diesel::update(
comment::table comment::table
.filter(comment::deleted.eq(true)) .filter(comment::deleted.eq(true))
.filter(comment::updated.lt(now().nullable() - 1.months())) .filter(comment::updated.lt(now().nullable() - 1.months()))
.filter(comment::content.ne(DELETED_REPLACEMENT_TEXT)), .filter(comment::content.ne(DELETED_REPLACEMENT_TEXT)),
) )
.set(comment::content.eq(DELETED_REPLACEMENT_TEXT)) .set(comment::content.eq(DELETED_REPLACEMENT_TEXT))
.execute(&mut conn) .execute(&mut conn)
.await .await?;
.map(|_| { info!("Done.");
info!("Done."); Ok(())
})
.inspect_err(|e| error!("Failed to overwrite deleted comments: {e}"))
.ok();
}
Err(e) => {
error!("Failed to get connection from pool: {e}");
}
}
} }
/// Re-calculate the site and community active counts every 12 hours /// Re-calculate the site and community active counts every 12 hours
async fn active_counts(pool: &mut DbPool<'_>) { async fn active_counts(pool: &mut DbPool<'_>) -> LemmyResult<()> {
info!("Updating active site and community aggregates ..."); info!("Updating active site and community aggregates ...");
let conn = get_conn(pool).await; let mut conn = get_conn(pool).await?;
match conn { let intervals = vec![
Ok(mut conn) => { ("1 day", "day"),
let intervals = vec![ ("1 week", "week"),
("1 day", "day"), ("1 month", "month"),
("1 week", "week"), ("6 months", "half_year"),
("1 month", "month"), ];
("6 months", "half_year"),
];
for (full_form, abbr) in &intervals { for (full_form, abbr) in &intervals {
let update_site_stmt = format!( let update_site_stmt = format!(
"update site_aggregates set users_active_{} = (select * from r.site_aggregates_activity('{}')) where site_id = 1", "update site_aggregates set users_active_{} = (select * from r.site_aggregates_activity('{}')) where site_id = 1",
abbr, full_form abbr, full_form
); );
sql_query(update_site_stmt) sql_query(update_site_stmt).execute(&mut conn).await?;
.execute(&mut conn)
.await
.inspect_err(|e| error!("Failed to update site stats: {e}"))
.ok();
let update_community_stmt = format!("update community_aggregates ca set users_active_{} = mv.count_ from r.community_aggregates_activity('{}') mv where ca.community_id = mv.community_id_", abbr, full_form); let update_community_stmt = format!("update community_aggregates ca set users_active_{} = mv.count_ from r.community_aggregates_activity('{}') mv where ca.community_id = mv.community_id_", abbr, full_form);
sql_query(update_community_stmt) sql_query(update_community_stmt).execute(&mut conn).await?;
.execute(&mut conn)
.await
.inspect_err(|e| error!("Failed to update community stats: {e}"))
.ok();
}
info!("Done.");
}
Err(e) => {
error!("Failed to get connection from pool: {e}");
}
} }
info!("Done.");
Ok(())
} }
/// Set banned to false after ban expires /// Set banned to false after ban expires
async fn update_banned_when_expired(pool: &mut DbPool<'_>) { async fn update_banned_when_expired(pool: &mut DbPool<'_>) -> LemmyResult<()> {
info!("Updating banned column if it expires ..."); info!("Updating banned column if it expires ...");
let conn = get_conn(pool).await; let mut conn = get_conn(pool).await?;
match conn { diesel::update(
Ok(mut conn) => { person::table
diesel::update( .filter(person::banned.eq(true))
person::table .filter(person::ban_expires.lt(now().nullable())),
.filter(person::banned.eq(true)) )
.filter(person::ban_expires.lt(now().nullable())), .set(person::banned.eq(false))
) .execute(&mut conn)
.set(person::banned.eq(false)) .await?;
.execute(&mut conn)
.await
.inspect_err(|e| error!("Failed to update person.banned when expires: {e}"))
.ok();
diesel::delete( diesel::delete(
community_actions::table.filter(community_actions::ban_expires.lt(now().nullable())), community_actions::table.filter(community_actions::ban_expires.lt(now().nullable())),
) )
.execute(&mut conn) .execute(&mut conn)
.await .await?;
.inspect_err(|e| error!("Failed to remove community_ban expired rows: {e}")) Ok(())
.ok();
}
Err(e) => {
error!("Failed to get connection from pool: {e}");
}
}
} }
/// Set banned to false after ban expires /// Set banned to false after ban expires
async fn delete_instance_block_when_expired(pool: &mut DbPool<'_>) { async fn delete_instance_block_when_expired(pool: &mut DbPool<'_>) -> LemmyResult<()> {
info!("Delete instance blocks when expired ..."); info!("Delete instance blocks when expired ...");
let conn = get_conn(pool).await; let mut conn = get_conn(pool).await?;
match conn { diesel::delete(
Ok(mut conn) => { federation_blocklist::table.filter(federation_blocklist::expires.lt(now().nullable())),
diesel::delete( )
federation_blocklist::table.filter(federation_blocklist::expires.lt(now().nullable())), .execute(&mut conn)
) .await?;
.execute(&mut conn) Ok(())
.await
.inspect_err(|e| error!("Failed to remove federation_blocklist expired rows: {e}"))
.ok();
}
Err(e) => {
error!("Failed to get connection from pool: {e}");
}
}
} }
/// Find all unpublished posts with scheduled date in the future, and publish them. /// Find all unpublished posts with scheduled date in the future, and publish them.
async fn publish_scheduled_posts(context: &Data<LemmyContext>) { async fn publish_scheduled_posts(context: &Data<LemmyContext>) -> LemmyResult<()> {
let pool = &mut context.pool(); let pool = &mut context.pool();
let conn = get_conn(pool).await; let mut conn = get_conn(pool).await?;
match conn { let scheduled_posts: Vec<_> = post::table
Ok(mut conn) => { .inner_join(community::table)
let scheduled_posts: Vec<_> = post::table .inner_join(person::table)
.inner_join(community::table) // find all posts which have scheduled_publish_time that is in the past
.inner_join(person::table) .filter(post::scheduled_publish_time.is_not_null())
// find all posts which have scheduled_publish_time that is in the past .filter(coalesce(post::scheduled_publish_time, now()).lt(now()))
.filter(post::scheduled_publish_time.is_not_null()) // make sure the post, person and community are still around
.filter(coalesce(post::scheduled_publish_time, now()).lt(now())) .filter(not(post::deleted.or(post::removed)))
// make sure the post, person and community are still around .filter(not(person::banned.or(person::deleted)))
.filter(not(post::deleted.or(post::removed))) .filter(not(community::removed.or(community::deleted)))
.filter(not(person::banned.or(person::deleted))) // ensure that user isnt banned from community
.filter(not(community::removed.or(community::deleted))) .filter(not(exists(find_action(
// ensure that user isnt banned from community community_actions::received_ban,
.filter(not(exists(find_action( (person::id, community::id),
community_actions::received_ban, ))))
(person::id, community::id), .select((post::all_columns, community::all_columns))
)))) .get_results::<(Post, Community)>(&mut conn)
.select((post::all_columns, community::all_columns)) .await?;
.get_results::<(Post, Community)>(&mut conn)
.await
.inspect_err(|e| error!("Failed to read unpublished posts: {e}"))
.ok()
.unwrap_or_default();
for (post, community) in scheduled_posts { for (post, community) in scheduled_posts {
// mark post as published in db // mark post as published in db
let form = PostUpdateForm { let form = PostUpdateForm {
scheduled_publish_time: Some(None), scheduled_publish_time: Some(None),
..Default::default() ..Default::default()
}; };
Post::update(&mut context.pool(), post.id, &form) Post::update(&mut context.pool(), post.id, &form).await?;
.await
.inspect_err(|e| error!("Failed update scheduled post: {e}"))
.ok();
// send out post via federation and webmention // send out post via federation and webmention
let send_activity = SendActivityData::CreatePost(post.clone()); let send_activity = SendActivityData::CreatePost(post.clone());
ActivityChannel::submit_activity(send_activity, context) ActivityChannel::submit_activity(send_activity, context)?;
.inspect_err(|e| error!("Failed federate scheduled post: {e}")) send_webmention(post, community);
.ok();
send_webmention(post, community);
}
}
Err(e) => {
error!("Failed to get connection from pool: {e}");
}
} }
Ok(())
} }
/// Updates the instance software and version. /// Updates the instance software and version.
@ -533,23 +461,16 @@ async fn update_instance_software(
client: &ClientWithMiddleware, client: &ClientWithMiddleware,
) -> LemmyResult<()> { ) -> LemmyResult<()> {
info!("Updating instances software and versions..."); info!("Updating instances software and versions...");
let conn = get_conn(pool).await; let mut conn = get_conn(pool).await?;
match conn { let instances = instance::table.get_results::<Instance>(&mut conn).await?;
Ok(mut conn) => {
let instances = instance::table.get_results::<Instance>(&mut conn).await?;
for instance in instances { for instance in instances {
if let Some(form) = build_update_instance_form(&instance.domain, client).await { if let Some(form) = build_update_instance_form(&instance.domain, client).await {
Instance::update(pool, instance.id, form).await?; Instance::update(pool, instance.id, form).await?;
}
}
info!("Finished updating instances software and versions...");
}
Err(e) => {
error!("Failed to get connection from pool: {e}");
} }
} }
info!("Finished updating instances software and versions...");
Ok(()) Ok(())
} }
@ -621,7 +542,8 @@ async fn build_update_instance_form(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::scheduled_tasks::build_update_instance_form; use super::*;
use crate::{scheduled_tasks::build_update_instance_form, tests::test_context};
use lemmy_api_common::request::client_builder; use lemmy_api_common::request::client_builder;
use lemmy_utils::{ use lemmy_utils::{
error::{LemmyErrorType, LemmyResult}, error::{LemmyErrorType, LemmyResult},
@ -632,7 +554,6 @@ mod tests {
use serial_test::serial; use serial_test::serial;
#[tokio::test] #[tokio::test]
#[serial]
async fn test_nodeinfo_lemmy_ml() -> LemmyResult<()> { async fn test_nodeinfo_lemmy_ml() -> LemmyResult<()> {
let client = ClientBuilder::new(client_builder(&Settings::default()).build()?).build(); let client = ClientBuilder::new(client_builder(&Settings::default()).build()?).build();
let form = build_update_instance_form("lemmy.ml", &client) let form = build_update_instance_form("lemmy.ml", &client)
@ -643,7 +564,6 @@ mod tests {
} }
#[tokio::test] #[tokio::test]
#[serial]
async fn test_nodeinfo_mastodon_social() -> LemmyResult<()> { async fn test_nodeinfo_mastodon_social() -> LemmyResult<()> {
let client = ClientBuilder::new(client_builder(&Settings::default()).build()?).build(); let client = ClientBuilder::new(client_builder(&Settings::default()).build()?).build();
let form = build_update_instance_form("mastodon.social", &client) let form = build_update_instance_form("mastodon.social", &client)
@ -652,4 +572,16 @@ mod tests {
assert_eq!(form.software.ok_or(LemmyErrorType::NotFound)?, "mastodon"); assert_eq!(form.software.ok_or(LemmyErrorType::NotFound)?, "mastodon");
Ok(()) Ok(())
} }
#[tokio::test]
#[serial]
async fn test_scheduled_tasks_no_errors() -> LemmyResult<()> {
let context = test_context().await;
startup_jobs(&mut context.pool()).await?;
update_instance_software(&mut context.pool(), context.client()).await?;
delete_expired_captcha_answers(&mut context.pool()).await?;
publish_scheduled_posts(&context).await?;
Ok(())
}
} }

View file

@ -99,7 +99,7 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use crate::tests::test_context;
use actix_web::test::TestRequest; use actix_web::test::TestRequest;
use lemmy_api_common::claims::Claims; use lemmy_api_common::claims::Claims;
use lemmy_db_schema::{ use lemmy_db_schema::{
@ -107,45 +107,29 @@ mod tests {
instance::Instance, instance::Instance,
local_user::{LocalUser, LocalUserInsertForm}, local_user::{LocalUser, LocalUserInsertForm},
person::{Person, PersonInsertForm}, person::{Person, PersonInsertForm},
secret::Secret,
}, },
traits::Crud, traits::Crud,
utils::build_db_pool_for_tests,
}; };
use lemmy_utils::{error::LemmyResult, rate_limit::RateLimitCell}; use lemmy_utils::error::LemmyResult;
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
use reqwest::Client;
use reqwest_middleware::ClientBuilder;
use serial_test::serial; use serial_test::serial;
use std::env::set_current_dir;
#[tokio::test] #[tokio::test]
#[serial] #[serial]
async fn test_session_auth() -> LemmyResult<()> { async fn test_session_auth() -> LemmyResult<()> {
// hack, necessary so that config file can be loaded from hardcoded, relative path let context = test_context().await;
set_current_dir("crates/utils")?;
let pool_ = build_db_pool_for_tests(); let inserted_instance =
let pool = &mut (&pool_).into(); Instance::read_or_create(&mut context.pool(), "my_domain.tld".to_string()).await?;
let secret = Secret::init(pool).await?;
let context = LemmyContext::create(
pool_.clone(),
ClientBuilder::new(Client::default()).build(),
secret,
RateLimitCell::with_test_config(),
);
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;
let new_person = PersonInsertForm::test_form(inserted_instance.id, "Gerry9812"); let new_person = PersonInsertForm::test_form(inserted_instance.id, "Gerry9812");
let inserted_person = Person::create(pool, &new_person).await?; let inserted_person = Person::create(&mut context.pool(), &new_person).await?;
let local_user_form = LocalUserInsertForm::test_form(inserted_person.id); let local_user_form = LocalUserInsertForm::test_form(inserted_person.id);
let inserted_local_user = LocalUser::create(pool, &local_user_form, vec![]).await?; let inserted_local_user =
LocalUser::create(&mut context.pool(), &local_user_form, vec![]).await?;
let req = TestRequest::default().to_http_request(); let req = TestRequest::default().to_http_request();
let jwt = Claims::generate(inserted_local_user.id, req, &context).await?; let jwt = Claims::generate(inserted_local_user.id, req, &context).await?;
@ -153,7 +137,7 @@ mod tests {
let valid = Claims::validate(&jwt, &context).await; let valid = Claims::validate(&jwt, &context).await;
assert!(valid.is_ok()); assert!(valid.is_ok());
let num_deleted = Person::delete(pool, inserted_person.id).await?; let num_deleted = Person::delete(&mut context.pool(), inserted_person.id).await?;
assert_eq!(1, num_deleted); assert_eq!(1, num_deleted);
Ok(()) Ok(())