sort article list by recently edited first

This commit is contained in:
Felix Ableitner 2024-02-01 12:10:55 +01:00
parent 4e53b13b70
commit 90f174bf53
3 changed files with 14 additions and 8 deletions

View File

@ -20,7 +20,7 @@ use axum::Extension;
use axum::Form; use axum::Form;
use axum::Json; use axum::Json;
use axum_macros::debug_handler; use axum_macros::debug_handler;
use chrono::Utc; use chrono::{Utc};
use diffy::create_patch; use diffy::create_patch;
/// Create a new article with empty text, and federate it to followers. /// Create a new article with empty text, and federate it to followers.

View File

@ -6,6 +6,7 @@ use crate::common::EditVersion;
use crate::common::{ArticleView, DbArticle}; use crate::common::{ArticleView, DbArticle};
use activitypub_federation::fetch::collection_id::CollectionId; use activitypub_federation::fetch::collection_id::CollectionId;
use activitypub_federation::fetch::object_id::ObjectId; use activitypub_federation::fetch::object_id::ObjectId;
use diesel::dsl::max;
use diesel::pg::PgConnection; use diesel::pg::PgConnection;
use diesel::ExpressionMethods; use diesel::ExpressionMethods;
use diesel::{ use diesel::{
@ -118,9 +119,14 @@ impl DbArticle {
.get_result(conn.deref_mut())?) .get_result(conn.deref_mut())?)
} }
/// Read all articles, ordered by most recently edited first.
pub fn read_all(only_local: bool, conn: &Mutex<PgConnection>) -> MyResult<Vec<Self>> { pub fn read_all(only_local: bool, conn: &Mutex<PgConnection>) -> MyResult<Vec<Self>> {
let mut conn = conn.lock().unwrap(); let mut conn = conn.lock().unwrap();
let query = article::table.into_boxed(); let query = article::table
.inner_join(edit::table)
.group_by(article::dsl::id)
.order_by(max(edit::dsl::created).desc())
.select(article::all_columns);
Ok(if only_local { Ok(if only_local {
query query
.filter(article::dsl::local.eq(true)) .filter(article::dsl::local.eq(true))

View File

@ -3,7 +3,7 @@ extern crate ibis_lib;
mod common; mod common;
use crate::common::{TestData, TEST_ARTICLE_DEFAULT_TEXT}; use crate::common::{TestData, TEST_ARTICLE_DEFAULT_TEXT};
use ibis_lib::common::{ArticleView, EditArticleData, ForkArticleData, GetArticleData}; use ibis_lib::common::{ArticleView, EditArticleData, ForkArticleData, GetArticleData, ListArticlesData};
use ibis_lib::common::{CreateArticleData, SearchArticleData}; use ibis_lib::common::{CreateArticleData, SearchArticleData};
use ibis_lib::common::{LoginUserData, RegisterUserData}; use ibis_lib::common::{LoginUserData, RegisterUserData};
use ibis_lib::frontend::error::MyResult; use ibis_lib::frontend::error::MyResult;
@ -59,11 +59,11 @@ async fn test_create_read_and_edit_local_article() -> MyResult<()> {
assert_eq!(1, search_res.len()); assert_eq!(1, search_res.len());
assert_eq!(edit_res.article, search_res[0]); assert_eq!(edit_res.article, search_res[0]);
let list_articles = data.alpha.list_articles().await?; let list_articles = data.alpha.list_articles(ListArticlesData {
dbg!(&list_articles); only_local: Some(false),
// default main page and article created by this test }).await?;
assert_eq!(2, list_articles.len()); assert_eq!(1, list_articles.len());
assert_eq!(edit_res.article, list_articles[1]); assert_eq!(edit_res.article, list_articles[0]);
data.stop() data.stop()
} }