1
0
Fork 0
forked from alemi/upub

chore: refactored a little

This commit is contained in:
əlemi 2024-04-12 22:56:29 +02:00
parent 576e809b69
commit 95ab7a50ef
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 27 additions and 37 deletions

View file

@ -17,7 +17,7 @@ pub async fn page(
State(ctx): State<Context>,
AuthIdentity(auth): AuthIdentity,
Query(page): Query<Pagination>,
) -> Result<JsonLD<serde_json::Value>, UpubError> {
) -> crate::Result<JsonLD<serde_json::Value>> {
let limit = page.batch.unwrap_or(20).min(50);
let offset = page.offset.unwrap_or(0);
let activities = model::addressing::Entity::find_activities()

View file

@ -25,43 +25,33 @@ pub async fn page(
AuthIdentity(auth): AuthIdentity,
Query(page): Query<Pagination>,
) -> crate::Result<JsonLD<serde_json::Value>> {
let uid = ctx.uid(id.clone());
match auth {
Identity::Anonymous => Err(StatusCode::FORBIDDEN.into()),
Identity::Remote(_) => Err(StatusCode::FORBIDDEN.into()),
Identity::Local(user) => if uid == user {
let limit = page.batch.unwrap_or(20).min(50);
let offset = page.offset.unwrap_or(0);
match model::addressing::Entity::find_activities()
.filter(Condition::all().add(model::addressing::Column::Actor.eq(&user)))
.order_by(model::addressing::Column::Published, Order::Asc)
.offset(offset)
.limit(limit)
.into_model::<EmbeddedActivity>()
.all(ctx.db())
.await
{
Ok(activities) => {
Ok(JsonLD(
ctx.ap_collection_page(
&url!(ctx, "/users/{id}/inbox/page"),
offset, limit,
activities
.into_iter()
.map(|x| x.into())
.collect()
).ld_context()
))
},
Err(e) => {
tracing::error!("failed paginating user inbox for {id}: {e}");
Err(StatusCode::INTERNAL_SERVER_ERROR.into())
},
}
} else {
Err(StatusCode::FORBIDDEN.into())
},
let Identity::Local(uid) = auth else {
// local inbox is only for local users
return Err(UpubError::forbidden());
};
if uid != ctx.uid(id.clone()) {
return Err(UpubError::forbidden());
}
let limit = page.batch.unwrap_or(20).min(50);
let offset = page.offset.unwrap_or(0);
let activities = model::addressing::Entity::find_activities()
.filter(Condition::all().add(model::addressing::Column::Actor.eq(&uid)))
.order_by(model::addressing::Column::Published, Order::Asc)
.offset(offset)
.limit(limit)
.into_model::<EmbeddedActivity>()
.all(ctx.db())
.await?;
Ok(JsonLD(
ctx.ap_collection_page(
&url!(ctx, "/users/{id}/inbox/page"),
offset, limit,
activities
.into_iter()
.map(|x| x.into())
.collect()
).ld_context()
))
}
pub async fn post(