Skip to content

Commit

Permalink
feat: add list_latest publications api
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Oct 4, 2023
1 parent c69af84 commit e39e609
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "writing"
version = "1.1.5"
version = "1.1.6"
edition = "2021"
rust-version = "1.64"
description = ""
Expand Down
33 changes: 32 additions & 1 deletion src/api/publication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use scylla_orm::ColumnsMap;

use crate::api::{
get_fields, token_from_xid, token_to_xid, validate_cbor_content, AppState, GIDPagination,
QueryGidCid,
Pagination, QueryGidCid,
};
use crate::db::{self, meili};

Expand Down Expand Up @@ -481,6 +481,37 @@ pub async fn list_by_gids_beta(
}))
}

pub async fn list_latest(
State(app): State<Arc<AppState>>,
Extension(ctx): Extension<Arc<ReqContext>>,
to: PackObject<Pagination>,
) -> Result<PackObject<SuccessResponse<Vec<PublicationOutput>>>, HTTPError> {
let (to, input) = to.unpack();
input.validate()?;
valid_user(ctx.user)?;

ctx.set_kvs(vec![("action", "list_latest_publications".into())])
.await;

let fields = input.fields.unwrap_or_default();
let (res, next_page_token) = db::PublicationIndex::list_latest(
&app.scylla,
token_to_xid(&input.page_token),
ctx.language,
)
.await?;

let docs = db::Publication::batch_get(&app.scylla, res, fields).await?;
Ok(to.with(SuccessResponse {
total_size: None,
next_page_token: to.with_option(token_from_xid(next_page_token)),
result: docs
.iter()
.map(|r| PublicationOutput::from(r.to_owned(), &to))
.collect(),
}))
}

pub async fn get_publish_list(
State(app): State<Arc<AppState>>,
Extension(ctx): Extension<Arc<ReqContext>>,
Expand Down
67 changes: 67 additions & 0 deletions src/db/model_publication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,73 @@ impl PublicationIndex {
Ok(false)
}

pub async fn list_latest(
db: &scylladb::ScyllaDB,
page_token: Option<xid::Id>,
language: Option<Language>,
) -> anyhow::Result<(Vec<PublicationIndex>, Option<xid::Id>)> {
let fields = Self::fields();

let mut res: Vec<PublicationIndex> = Vec::new();
let query = format!(
"SELECT {} FROM pub_index WHERE day=? LIMIT 1000 USING TIMEOUT 3s",
fields.clone().join(",")
);

let mut day = if let Some(cid) = page_token {
xid_day(cid) - 1
} else {
(unix_ms() / (1000 * 3600 * 24)) as i32
};

let min = (unix_ms() / (1000 * 3600 * 24)) as i32 - 30;
while day >= min {
let params = (day,);
let rows = db.execute_iter(query.as_str(), params).await?;
for row in rows {
let mut doc = PublicationIndex::default();
let mut cols = ColumnsMap::with_capacity(fields.len());
cols.fill(row, &fields)?;
doc.fill(&cols);
doc._fields = fields.clone();
if res.is_empty() {
res.push(doc);
} else {
let prev = res.last_mut().unwrap();
if prev.cid != doc.cid {
res.push(doc);
} else if prev.language != doc.language {
match language {
// prefer language match
Some(lang) if lang == doc.language => *prev = doc,
// or original language
None if doc.original => *prev = doc,
_ => {} // ignore
}
}
}
}

// result should >= 6 for first page.
if (page_token.is_none() && res.len() >= 6) || (page_token.is_some() && res.len() >= 3)
{
let next_id = res.last().unwrap().cid;
res.sort_by(|a, b| b.cid.partial_cmp(&a.cid).unwrap());
return Ok((res, Some(next_id)));
}

day -= 1;
}

let next = if res.is_empty() {
None
} else {
Some(res.last().unwrap().cid)
};
res.sort_by(|a, b| b.cid.partial_cmp(&a.cid).unwrap());
Ok((res, next))
}

pub async fn list_by_gids(
db: &scylladb::ScyllaDB,
gids: Vec<xid::Id>,
Expand Down
3 changes: 2 additions & 1 deletion src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ pub async fn new(cfg: conf::Conf) -> anyhow::Result<(Arc<api::AppState>, Router)
.route(
"/list_by_gids",
routing::post(api::publication::list_by_gids_beta),
),
)
.route("/list_latest", routing::post(api::publication::list_latest)),
)
.nest(
"/v1/bookmark",
Expand Down

0 comments on commit e39e609

Please sign in to comment.