Skip to content

Commit

Permalink
Rework pdf endpoints. Start work on indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
augustuswm committed Jan 3, 2025
1 parent 3bac4bb commit 0ea125f
Show file tree
Hide file tree
Showing 12 changed files with 783 additions and 70 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

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

86 changes: 76 additions & 10 deletions rfd-api-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -1941,11 +1941,7 @@
"content": {
"application/json": {
"schema": {
"title": "Array_of_RfdPdf",
"type": "array",
"items": {
"$ref": "#/components/schemas/RfdPdf"
}
"$ref": "#/components/schemas/RfdRevisionPdf"
}
}
}
Expand Down Expand Up @@ -2216,11 +2212,7 @@
"content": {
"application/json": {
"schema": {
"title": "Array_of_RfdPdf",
"type": "array",
"items": {
"$ref": "#/components/schemas/RfdPdf"
}
"$ref": "#/components/schemas/RfdRevisionPdf"
}
}
}
Expand Down Expand Up @@ -4415,6 +4407,80 @@
"updated_at"
]
},
"RfdRevisionPdf": {
"type": "object",
"properties": {
"authors": {
"nullable": true,
"type": "string"
},
"commit": {
"$ref": "#/components/schemas/CommitSha"
},
"committed_at": {
"type": "string",
"format": "date-time"
},
"content": {
"type": "array",
"items": {
"$ref": "#/components/schemas/RfdPdf"
}
},
"content_format": {
"$ref": "#/components/schemas/ContentFormat"
},
"created_at": {
"type": "string",
"format": "date-time"
},
"deleted_at": {
"nullable": true,
"type": "string",
"format": "date-time"
},
"discussion": {
"nullable": true,
"type": "string"
},
"id": {
"$ref": "#/components/schemas/TypedUuidForRfdRevisionId"
},
"labels": {
"nullable": true,
"type": "string"
},
"rfd_id": {
"$ref": "#/components/schemas/TypedUuidForRfdId"
},
"sha": {
"$ref": "#/components/schemas/FileSha"
},
"state": {
"nullable": true,
"type": "string"
},
"title": {
"type": "string"
},
"updated_at": {
"type": "string",
"format": "date-time"
}
},
"required": [
"commit",
"committed_at",
"content",
"content_format",
"created_at",
"id",
"rfd_id",
"sha",
"title",
"updated_at"
]
},
"RfdState": {
"type": "string",
"enum": [
Expand Down
86 changes: 66 additions & 20 deletions rfd-api/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ use rfd_data::{
use rfd_github::{GitHubError, GitHubNewRfdNumber, GitHubRfdRepo};
use rfd_model::{
schema_ext::{ContentFormat, Visibility},
storage::{JobStore, RfdFilter, RfdMetaStore, RfdPdfFilter, RfdPdfStore, RfdStorage, RfdStore},
CommitSha, FileSha, Job, NewJob, Rfd, RfdId, RfdMeta, RfdPdf, RfdRevision, RfdRevisionId,
storage::{JobStore, RfdFilter, RfdMetaStore, RfdPdfsStore, RfdStorage, RfdStore},
CommitSha, FileSha, Job, NewJob, Rfd, RfdId, RfdMeta, RfdPdf, RfdPdfs, RfdRevision,
RfdRevisionId,
};
use rsa::{
pkcs1::{DecodeRsaPrivateKey, EncodeRsaPrivateKey},
Expand Down Expand Up @@ -89,8 +90,9 @@ pub enum UpdateRfdContentError {
}

#[partial(RfdWithoutContent)]
#[partial(RfdWithPdf)]
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
pub struct RfdWithContent {
pub struct RfdWithRaw {
pub id: TypedUuid<RfdId>,
pub rfd_number: i32,
pub link: Option<String>,
Expand All @@ -100,6 +102,7 @@ pub struct RfdWithContent {
pub authors: Option<String>,
pub labels: Option<String>,
#[partial(RfdWithoutContent(skip))]
#[partial(RfdWithPdf(retype = Vec<RfdPdf>))]
pub content: String,
pub format: ContentFormat,
pub sha: FileSha,
Expand All @@ -108,9 +111,9 @@ pub struct RfdWithContent {
pub visibility: Visibility,
}

impl From<Rfd> for RfdWithContent {
impl From<Rfd> for RfdWithRaw {
fn from(value: Rfd) -> Self {
RfdWithContent {
Self {
id: value.id,
rfd_number: value.rfd_number,
link: value.link,
Expand All @@ -131,7 +134,7 @@ impl From<Rfd> for RfdWithContent {

impl From<RfdMeta> for RfdWithoutContent {
fn from(value: RfdMeta) -> Self {
RfdWithoutContent {
Self {
id: value.id,
rfd_number: value.rfd_number,
link: value.link,
Expand All @@ -149,6 +152,27 @@ impl From<RfdMeta> for RfdWithoutContent {
}
}

impl From<RfdPdfs> for RfdWithPdf {
fn from(value: RfdPdfs) -> Self {
Self {
id: value.id,
rfd_number: value.rfd_number,
link: value.link,
discussion: value.content.discussion,
title: value.content.title,
state: value.content.state,
authors: value.content.authors,
labels: value.content.labels,
content: value.content.content,
format: value.content.content_format,
sha: value.content.sha,
commit: value.content.commit.into(),
committed_at: value.content.committed_at,
visibility: value.visibility,
}
}
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
pub struct PdfEntry {
pub source: String,
Expand Down Expand Up @@ -446,13 +470,46 @@ impl RfdContext {
}
}

#[instrument(skip(self, caller))]
async fn get_rfd_pdf(
&self,
caller: &Caller<RfdPermission>,
rfd_number: i32,
revision: Option<RfdRevisionIdentifier>,
) -> ResourceResult<RfdWithPdf, StoreError> {
let mut filter = RfdFilter::default().rfd_number(Some(vec![rfd_number]));
filter = match revision {
Some(RfdRevisionIdentifier::Commit(commit)) => filter.commit(Some(vec![commit])),
Some(RfdRevisionIdentifier::Id(revision)) => filter.revision(Some(vec![revision])),
None => filter,
};

let rfd = RfdPdfsStore::list(&*self.storage, vec![filter], &ListPagination::unlimited())
.await
.to_resource_result()?
.pop();

if let Some(rfd) = rfd {
if caller.can(&RfdPermission::GetRfdsAll)
|| caller.can(&RfdPermission::GetRfd(rfd.rfd_number))
|| rfd.visibility == Visibility::Public
{
Ok(rfd.into())
} else {
resource_not_found()
}
} else {
resource_not_found()
}
}

#[instrument(skip(self, caller))]
pub async fn view_rfd(
&self,
caller: &Caller<RfdPermission>,
rfd_number: i32,
revision: Option<RfdRevisionIdentifier>,
) -> ResourceResult<RfdWithContent, StoreError> {
) -> ResourceResult<RfdWithRaw, StoreError> {
let rfd = self.get_rfd(caller, rfd_number, revision).await?;
Ok(rfd.into())
}
Expand Down Expand Up @@ -493,19 +550,8 @@ impl RfdContext {
caller: &Caller<RfdPermission>,
rfd_number: i32,
revision: Option<RfdRevisionIdentifier>,
) -> ResourceResult<Vec<RfdPdf>, StoreError> {
let rfd = self.get_rfd_meta(caller, rfd_number, revision).await?;
let pdfs = RfdPdfStore::list(
&*self.storage,
vec![RfdPdfFilter::default()
.rfd(Some(vec![rfd.id]))
.rfd_revision(Some(vec![rfd.content.id]))],
&ListPagination::unlimited(),
)
.await
.to_resource_result()?;

Ok(pdfs)
) -> ResourceResult<RfdWithPdf, StoreError> {
self.get_rfd_pdf(caller, rfd_number, revision).await
}

#[instrument(skip(self, caller, content))]
Expand Down
Loading

0 comments on commit 0ea125f

Please sign in to comment.