fix: dedicated methods for uris

so i dont always wonder if its plural or not
This commit is contained in:
əlemi 2024-03-20 09:19:31 +01:00
parent 44478d7d2b
commit 178ad1b75d
Signed by: alemi
GPG key ID: A4895B84D311642C
4 changed files with 17 additions and 5 deletions

View file

@ -5,8 +5,7 @@ use crate::{activitystream::Base, model::activity, server::Context};
pub async fn view(State(ctx) : State<Context>, Path(id): Path<String>) -> Result<Json<serde_json::Value>, StatusCode> { pub async fn view(State(ctx) : State<Context>, Path(id): Path<String>) -> Result<Json<serde_json::Value>, StatusCode> {
let uri = format!("http://localhost:3000/activities/{id}"); match activity::Entity::find_by_id(ctx.activity_uri(id)).one(ctx.db()).await {
match activity::Entity::find_by_id(uri).one(ctx.db()).await {
Ok(Some(activity)) => Ok(Json(activity.underlying_json_object())), Ok(Some(activity)) => Ok(Json(activity.underlying_json_object())),
Ok(None) => Err(StatusCode::NOT_FOUND), Ok(None) => Err(StatusCode::NOT_FOUND),
Err(e) => { Err(e) => {

View file

@ -5,7 +5,7 @@ use crate::{activitystream::Base, model::object, server::Context};
pub async fn view(State(ctx) : State<Context>, Path(id): Path<String>) -> Result<Json<serde_json::Value>, StatusCode> { pub async fn view(State(ctx) : State<Context>, Path(id): Path<String>) -> Result<Json<serde_json::Value>, StatusCode> {
match object::Entity::find_by_id(ctx.uri("objects", id)).one(ctx.db()).await { match object::Entity::find_by_id(ctx.object_uri(id)).one(ctx.db()).await {
Ok(Some(object)) => Ok(Json(object.underlying_json_object())), Ok(Some(object)) => Ok(Json(object.underlying_json_object())),
Ok(None) => Err(StatusCode::NOT_FOUND), Ok(None) => Err(StatusCode::NOT_FOUND),
Err(e) => { Err(e) => {

View file

@ -10,7 +10,7 @@ pub async fn list(State(_db) : State<Arc<DatabaseConnection>>) -> Result<Json<se
} }
pub async fn view(State(ctx) : State<Context>, Path(id): Path<String>) -> Result<Json<serde_json::Value>, StatusCode> { pub async fn view(State(ctx) : State<Context>, Path(id): Path<String>) -> Result<Json<serde_json::Value>, StatusCode> {
match user::Entity::find_by_id(ctx.uri("users", id)).one(ctx.db()).await { match user::Entity::find_by_id(ctx.user_uri(id)).one(ctx.db()).await {
Ok(Some(user)) => Ok(Json(user.underlying_json_object())), Ok(Some(user)) => Ok(Json(user.underlying_json_object())),
Ok(None) => Err(StatusCode::NOT_FOUND), Ok(None) => Err(StatusCode::NOT_FOUND),
Err(e) => { Err(e) => {
@ -29,7 +29,7 @@ pub async fn outbox(
// find requested recent post, to filter based on its date (use now() as fallback) // find requested recent post, to filter based on its date (use now() as fallback)
let before = if let Some(before) = page.max_id { let before = if let Some(before) = page.max_id {
match model::activity::Entity::find_by_id(ctx.uri("activities", before)) match model::activity::Entity::find_by_id(ctx.activity_uri(before))
.one(ctx.db()).await .one(ctx.db()).await
{ {
Ok(None) => return Err(StatusCode::NOT_FOUND), Ok(None) => return Err(StatusCode::NOT_FOUND),

View file

@ -10,6 +10,7 @@ struct ContextInner {
db: DatabaseConnection, db: DatabaseConnection,
domain: String, domain: String,
} }
impl Context { impl Context {
pub fn new(db: DatabaseConnection, mut domain: String) -> Self { pub fn new(db: DatabaseConnection, mut domain: String) -> Self {
if !domain.starts_with("http") { if !domain.starts_with("http") {
@ -31,6 +32,18 @@ impl Context {
} }
} }
pub fn user_uri(&self, id: String) -> String {
self.uri("users", id)
}
pub fn object_uri(&self, id: String) -> String {
self.uri("objects", id)
}
pub fn activity_uri(&self, id: String) -> String {
self.uri("activities", id)
}
pub fn id(&self, id: String) -> String { pub fn id(&self, id: String) -> String {
if id.starts_with(&self.0.domain) { if id.starts_with(&self.0.domain) {
let mut out = id.replace(&self.0.domain, ""); let mut out = id.replace(&self.0.domain, "");