Skip to content

Commit

Permalink
Add rfd visibility field
Browse files Browse the repository at this point in the history
  • Loading branch information
augustuswm committed Oct 6, 2023
1 parent 3b19b7b commit bc76f93
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE rfd DROP COLUMN visibility;

DROP TYPE RFD_VISIBILITY;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TYPE RFD_VISIBILITY as ENUM('public', 'private');

ALTER TABLE rfd ADD COLUMN visibility RFD_VISIBILITY NOT NULL DEFAULT 'private';
5 changes: 2 additions & 3 deletions rfd-model/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
login_attempt, mapper, oauth_client, oauth_client_redirect_uri, oauth_client_secret, rfd,
rfd_pdf, rfd_revision,
},
schema_ext::{ContentFormat, LoginAttemptState, PdfSource},
schema_ext::{ContentFormat, LoginAttemptState, PdfSource, Visibility},
};

#[derive(Debug, Deserialize, Serialize, Queryable, Insertable)]
Expand All @@ -20,11 +20,10 @@ pub struct RfdModel {
pub id: Uuid,
pub rfd_number: i32,
pub link: Option<String>,
// pub relevant_components: Vec<Option<String>>,
// pub milestones: Vec<Option<String>>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub deleted_at: Option<DateTime<Utc>>,
pub visibility: Visibility,
}

#[derive(Debug, Deserialize, Serialize, Queryable, Insertable)]
Expand Down
8 changes: 3 additions & 5 deletions rfd-model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use db::{
};
use partial_struct::partial;
use permissions::Permissions;
use schema_ext::{ContentFormat, LoginAttemptState, PdfSource};
use schema_ext::{ContentFormat, LoginAttemptState, PdfSource, Visibility};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::Value;
Expand All @@ -30,14 +30,13 @@ pub struct Rfd {
pub id: Uuid,
pub rfd_number: i32,
pub link: Option<String>,
// pub relevant_components: Vec<Option<String>>,
// pub milestones: Vec<Option<String>>,
#[partial(NewRfd(skip))]
pub created_at: DateTime<Utc>,
#[partial(NewRfd(skip))]
pub updated_at: DateTime<Utc>,
#[partial(NewRfd(skip))]
pub deleted_at: Option<DateTime<Utc>>,
pub visibility: Visibility,
}

impl From<RfdModel> for Rfd {
Expand All @@ -46,11 +45,10 @@ impl From<RfdModel> for Rfd {
id: value.id,
rfd_number: value.rfd_number,
link: value.link,
// relevant_components: value.relevant_components,
// milestones: value.milestones,
created_at: value.created_at,
updated_at: value.updated_at,
deleted_at: value.deleted_at,
visibility: value.visibility,
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions rfd-model/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ pub mod sql_types {
#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "rfd_pdf_source"))]
pub struct RfdPdfSource;

#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "rfd_visibility"))]
pub struct RfdVisibility;
}

diesel::table! {
Expand Down Expand Up @@ -154,13 +158,17 @@ diesel::table! {
}

diesel::table! {
use diesel::sql_types::*;
use super::sql_types::RfdVisibility;

rfd (id) {
id -> Uuid,
rfd_number -> Int4,
link -> Nullable<Varchar>,
created_at -> Timestamptz,
updated_at -> Timestamptz,
deleted_at -> Nullable<Timestamptz>,
visibility -> RfdVisibility,
}
}

Expand Down
25 changes: 24 additions & 1 deletion rfd-model/src/schema_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::{

use crate::{
permissions::Permissions,
schema::sql_types::{AttemptState, RfdContentFormat, RfdPdfSource},
schema::sql_types::{AttemptState, RfdContentFormat, RfdPdfSource, RfdVisibility},
};

macro_rules! sql_conversion {
Expand Down Expand Up @@ -150,3 +150,26 @@ impl Default for LoginAttemptState {
Self::New
}
}

#[derive(Debug, PartialEq, Clone, FromSqlRow, AsExpression, Serialize, Deserialize, JsonSchema)]
#[diesel(sql_type = RfdVisibility)]
#[serde(rename_all = "lowercase")]
pub enum Visibility {
Public,
Private,
}

sql_conversion! {
RfdVisibility => Visibility,
Public => b"public",
Private => b"private",
}

impl Display for Visibility {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Visibility::Public => write!(f, "public"),
Visibility::Private => write!(f, "private"),
}
}
}
6 changes: 2 additions & 4 deletions rfd-model/src/storage/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,15 @@ impl RfdStore for PostgresStore {
rfd::id.eq(new_rfd.id),
rfd::rfd_number.eq(new_rfd.rfd_number.clone()),
rfd::link.eq(new_rfd.link.clone()),
// rfd::relevant_components.eq(new_rfd.relevant_components.clone()),
// rfd::milestones.eq(new_rfd.milestones.clone()),
rfd::visibility.eq(new_rfd.visibility.clone()),
))
.on_conflict(rfd::id)
.do_update()
.set((
rfd::rfd_number.eq(excluded(rfd::rfd_number)),
rfd::link.eq(excluded(rfd::link)),
// rfd::relevant_components.eq(excluded(rfd::relevant_components)),
// rfd::milestones.eq(excluded(rfd::milestones)),
rfd::updated_at.eq(Utc::now()),
rfd::visibility.eq(excluded(rfd::visibility)),
))
.get_result_async(&self.conn)
.await?;
Expand Down
11 changes: 5 additions & 6 deletions rfd-processor/src/rfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use chrono::{DateTime, Utc};
use octorust::{Client, ClientError};
use rfd_data::RfdNumber;
use rfd_model::{
schema_ext::ContentFormat,
schema_ext::{ContentFormat, Visibility},
storage::{
ListPagination, RfdFilter, RfdRevisionFilter, RfdRevisionStore, RfdStore, StoreError,
},
Expand Down Expand Up @@ -267,25 +267,24 @@ impl RemoteRfd {
let number = self.number;
let payload = self.into_payload()?;

let id = RfdStore::list(
let (id, visibility) = RfdStore::list(
storage,
RfdFilter::default().rfd_number(Some(vec![payload.number.into()])),
&ListPagination::latest(),
)
.await?
.into_iter()
.next()
.map(|rfd| rfd.id)
.unwrap_or_else(|| Uuid::new_v4());
.map(|rfd| (rfd.id, rfd.visibility))
.unwrap_or_else(|| (Uuid::new_v4(), Visibility::Private));

let rfd = RfdStore::upsert(
storage,
NewRfd {
id,
rfd_number: payload.number.into(),
link: payload.link.into(),
// relevant_components: vec![],
// milestones: vec![],
visibility,
},
)
.await?;
Expand Down

0 comments on commit bc76f93

Please sign in to comment.