Skip to content

Commit e1d80ac

Browse files
committed
Fix file representation
1 parent b9d7f57 commit e1d80ac

File tree

11 files changed

+137
-39
lines changed

11 files changed

+137
-39
lines changed

src/models/block.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pub struct BlockCommon {
1616
#[serde(with = "time::serde::iso8601")]
1717
pub last_edited_time: OffsetDateTime,
1818
pub has_children: bool,
19+
pub archived: bool,
20+
pub in_trash: bool,
1921
pub created_by: UserCommon,
2022
pub last_edited_by: UserCommon,
2123
}
@@ -58,17 +60,33 @@ pub struct ExternalFileObject {
5860
#[serde(tag = "type")]
5961
#[serde(rename_all = "snake_case")]
6062
pub enum FileOrEmojiObject {
61-
Emoji { emoji: String },
62-
File,
63-
External,
63+
Emoji {
64+
emoji: String,
65+
},
66+
File {
67+
caption: Vec<RichText>,
68+
file: InternalFileObject,
69+
#[serde(skip_serializing_if = "Option::is_none")]
70+
name: Option<String>,
71+
},
72+
External {
73+
external: ExternalFileObject,
74+
},
6475
}
6576

6677
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
6778
#[serde(tag = "type")]
6879
#[serde(rename_all = "snake_case")]
6980
pub enum FileObject {
70-
File,
71-
External,
81+
File {
82+
caption: Vec<RichText>,
83+
file: InternalFileObject,
84+
#[serde(skip_serializing_if = "Option::is_none")]
85+
name: Option<String>,
86+
},
87+
External {
88+
external: ExternalFileObject,
89+
},
7290
}
7391

7492
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
@@ -354,7 +372,6 @@ pub enum Block {
354372
#[serde(flatten)]
355373
common: BlockCommon,
356374
file: FileObject,
357-
caption: Text,
358375
},
359376
Pdf {
360377
#[serde(flatten)]
@@ -509,7 +526,7 @@ impl From<Block> for CreateBlock {
509526
Block::Embed { embed, .. } => CreateBlock::Embed { embed },
510527
Block::Image { image, .. } => CreateBlock::Image { image },
511528
Block::Video { video, .. } => CreateBlock::Video { video },
512-
Block::File { file, caption, .. } => CreateBlock::File { file, caption },
529+
Block::File { file, .. } => CreateBlock::File { file },
513530
Block::Pdf { pdf, .. } => CreateBlock::Pdf { pdf },
514531
Block::Bookmark { bookmark, .. } => CreateBlock::Bookmark { bookmark },
515532
Block::Equation { equation, .. } => CreateBlock::Equation { equation },
@@ -590,7 +607,6 @@ pub enum CreateBlock {
590607
},
591608
File {
592609
file: FileObject,
593-
caption: Text,
594610
},
595611
Pdf {
596612
pdf: FileObject,

src/models/block/tests.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ mod tests {
3232
)
3333
.unwrap(),
3434
has_children: false,
35+
archived: false,
36+
in_trash: false,
3537
created_by: UserCommon {
3638
id: UserId::from_str("6419f912-5293-4ea8-b2c8-9c3ce44f90e3").unwrap(),
3739
name: None,
@@ -229,14 +231,38 @@ mod tests {
229231
fn file_object() {
230232
let file_object: FileOrEmojiObject =
231233
serde_json::from_str(include_str!("tests/file_object.json")).unwrap();
232-
assert_eq!(file_object, FileOrEmojiObject::File)
234+
assert_eq!(file_object,
235+
FileOrEmojiObject::File {
236+
caption: Vec::new(),
237+
name: Some("blah.rs".into()),
238+
file: InternalFileObject {
239+
expiry_time: OffsetDateTime::parse(
240+
"2022-05-13T21:10:35.817Z",
241+
&Iso8601::DEFAULT
242+
).unwrap(),
243+
url: "https://s3.us-west-2.amazonaws.com/secure.notion-static.com/2703e742-ace5-428c-a74d-1c587ceddc32/DiRT_Rally.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20220513%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20220513T201035Z&X-Amz-Expires=3600&X-Amz-Signature=714b49bde0b499fb8f3aae1a88a8cbd374f2b09c1d128e91cac49e85ce0e00fb&X-Amz-SignedHeaders=host&x-id=GetObject".into()
244+
}
245+
}
246+
)
247+
}
248+
249+
#[test]
250+
fn file_page() {
251+
let _: Object = serde_json::from_str(include_str!("tests/file_page.json")).unwrap();
233252
}
234253

235254
#[test]
236255
fn external_file_object() {
237256
let external_file_object: FileOrEmojiObject =
238257
serde_json::from_str(include_str!("tests/external_file_object.json")).unwrap();
239-
assert_eq!(external_file_object, FileOrEmojiObject::External)
258+
assert_eq!(
259+
external_file_object,
260+
FileOrEmojiObject::External {
261+
external: ExternalFileObject {
262+
url: "https://nerdist.com/wp-content/uploads/2020/07/maxresdefault.jpg".into()
263+
}
264+
}
265+
)
240266
}
241267

242268
#[test]
@@ -259,6 +285,8 @@ mod tests {
259285
)
260286
.unwrap(),
261287
has_children: true,
288+
archived: false,
289+
in_trash: false,
262290
created_by: UserCommon {
263291
id: UserId::from_str("e2507360-468c-4e0f-a928-7bbcbbb45353").unwrap(),
264292
name: None,

src/models/block/tests/callout.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
"has_children": true,
1515
"archived": false,
16+
"in_trash": false,
1617
"type": "callout",
1718
"callout": {
1819
"rich_text": [
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
2+
"caption": [],
23
"type": "file",
34
"file": {
45
"url": "https://s3.us-west-2.amazonaws.com/secure.notion-static.com/2703e742-ace5-428c-a74d-1c587ceddc32/DiRT_Rally.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20220513%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20220513T201035Z&X-Amz-Expires=3600&X-Amz-Signature=714b49bde0b499fb8f3aae1a88a8cbd374f2b09c1d128e91cac49e85ce0e00fb&X-Amz-SignedHeaders=host&x-id=GetObject",
56
"expiry_time": "2022-05-13T21:10:35.817Z"
6-
}
7+
},
8+
"name": "blah.rs"
79
}

src/models/block/tests/file_page.json

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"object": "list",
3+
"results": [
4+
{
5+
"object": "block",
6+
"id": "47a920e4-346c-4df8-ae78-905ce10adcb8",
7+
"parent": {
8+
"type": "page_id",
9+
"page_id": "13d6da82-2f93-43fa-8ec1-4c89b8184d5a"
10+
},
11+
"created_time": "2022-12-15T00:18:00.000Z",
12+
"last_edited_time": "2022-12-15T00:18:00.000Z",
13+
"created_by": {
14+
"object": "user",
15+
"id": "c2f20311-9e54-4d11-8c79-7398424ae41e"
16+
},
17+
"last_edited_by": {
18+
"object": "user",
19+
"id": "c2f20311-9e54-4d11-8c79-7398424ae41e"
20+
},
21+
"has_children": false,
22+
"archived": false,
23+
"in_trash": false,
24+
"type": "paragraph",
25+
"paragraph": {
26+
"rich_text": [],
27+
"color": "default"
28+
}
29+
},
30+
{
31+
"object": "block",
32+
"id": "3c29dedf-00a5-4915-b137-120c61f5e5d8",
33+
"parent": {
34+
"type": "page_id",
35+
"page_id": "13d6da82-2f93-43fa-8ec1-4c89b8184d5a"
36+
},
37+
"created_time": "2022-12-15T00:18:00.000Z",
38+
"last_edited_time": "2022-12-15T00:18:00.000Z",
39+
"created_by": {
40+
"object": "user",
41+
"id": "c2f20311-9e54-4d11-8c79-7398424ae41e"
42+
},
43+
"last_edited_by": {
44+
"object": "user",
45+
"id": "c2f20311-9e54-4d11-8c79-7398424ae41e"
46+
},
47+
"has_children": false,
48+
"archived": false,
49+
"in_trash": false,
50+
"type": "file",
51+
"file": {
52+
"caption": [],
53+
"type": "file",
54+
"file": {
55+
"url": "https://s3.us-west-2.amazonaws.com/secure.notion-static.com/fa6c03f0-e608-45d0-9327-4cd7a5e56e71/TestFile.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20221215%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20221215T002012Z&X-Amz-Expires=3600&X-Amz-Signature=bf13ca59f618077852298cb92aedc4dd1becdc961c31d73cbc030ef93f2853c4&X-Amz-SignedHeaders=host&x-id=GetObject",
56+
"expiry_time": "2022-12-15T01:20:12.928Z"
57+
}
58+
}
59+
}
60+
],
61+
"next_cursor": null,
62+
"has_more": false,
63+
"type": "block",
64+
"block": {}
65+
}

src/models/block/tests/heading_1.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
"has_children": false,
1515
"archived": false,
16+
"in_trash": false,
1617
"type": "heading_1",
1718
"heading_1": {
1819
"rich_text": [

src/models/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use std::hash::{Hash, Hasher};
1818
use crate::ids::{AsIdentifier, BlockId, DatabaseId, PageId};
1919
use crate::models::block::{Block, CreateBlock, FileObject, FileOrEmojiObject};
2020
use block::ExternalFileObject;
21-
use serde_json::Value;
2221

2322
use crate::models::error::ErrorResponse;
2423
use crate::models::paging::PagingCursor;
@@ -284,11 +283,10 @@ pub struct Page {
284283
/// The archived status of the page.
285284
pub archived: bool,
286285
pub properties: Properties,
286+
#[serde(skip_serializing_if = "Option::is_none")]
287287
pub icon: Option<IconObject>,
288288
pub parent: Parent,
289289
#[serde(skip_serializing_if = "Option::is_none")]
290-
pub icon: Option<FileOrEmojiObject>,
291-
#[serde(skip_serializing_if = "Option::is_none")]
292290
pub url: Option<String>,
293291
#[serde(skip_serializing_if = "Option::is_none")]
294292
pub blocks: Option<Vec<Block>>,

src/models/properties.rs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ pub enum PropertyConfiguration {
255255
},
256256
/// Undocumented property of wiki pages
257257
Verification {
258-
id: PropertyId
258+
id: PropertyId,
259259
},
260260
/// See <https://developers.notion.com/reference/database#last-edited-by-configuration>
261261
LastEditBy {
@@ -385,25 +385,16 @@ pub enum PropertyValue {
385385
rollup: Option<RollupValue>,
386386
},
387387
/// <https://developers.notion.com/reference/property-object#people-configuration>
388-
People {
389-
id: PropertyId,
390-
people: Vec<User>,
391-
},
388+
People { id: PropertyId, people: Vec<User> },
392389
/// <https://developers.notion.com/reference/property-object#files-configuration>
393390
Files {
394391
id: PropertyId,
395392
files: Option<Vec<FileReference>>,
396393
},
397394
/// <https://developers.notion.com/reference/property-object#checkbox-configuration>
398-
Checkbox {
399-
id: PropertyId,
400-
checkbox: bool,
401-
},
395+
Checkbox { id: PropertyId, checkbox: bool },
402396
/// <https://developers.notion.com/reference/property-object#url-configuration>
403-
Url {
404-
id: PropertyId,
405-
url: Option<String>,
406-
},
397+
Url { id: PropertyId, url: Option<String> },
407398
/// <https://developers.notion.com/reference/property-object#email-configuration>
408399
Email {
409400
id: PropertyId,
@@ -421,10 +412,7 @@ pub enum PropertyValue {
421412
created_time: OffsetDateTime,
422413
},
423414
/// <https://developers.notion.com/reference/property-object#created-by-configuration>
424-
CreatedBy {
425-
id: PropertyId,
426-
created_by: User,
427-
},
415+
CreatedBy { id: PropertyId, created_by: User },
428416
/// <https://developers.notion.com/reference/property-object#last-edited-time-configuration>
429417
LastEditedTime {
430418
id: PropertyId,
@@ -457,11 +445,7 @@ pub struct VerificationValue {
457445
pub struct UniqueIdValue {
458446
pub number: Number,
459447
pub prefix: Option<String>,
460-
unique_id: UniqueidValue,
461-
},
462-
Button {
463-
id: PropertyId,
464-
},
448+
pub unique_id: UniqueidValue,
465449
}
466450

467451
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]

src/models/tests/page.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"created_time": "2020-03-17T19:10:04.968Z",
55
"last_edited_time": "2020-03-17T21:49:37.913Z",
66
"archived": false,
7+
"in_trash": false,
78
"parent": {
89
"type": "workspace"
910
},
@@ -55,4 +56,4 @@
5556
]
5657
}
5758
}
58-
}
59+
}

src/models/tests/query_result.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"database_id": "5d794de0-2224-49d3-86f9-3540db13d884"
1212
},
1313
"archived": false,
14+
"in_trash": false,
1415
"properties": {
1516
"Name": {
1617
"id": "title",
@@ -40,4 +41,4 @@
4041
],
4142
"next_cursor": null,
4243
"has_more": false
43-
}
44+
}

0 commit comments

Comments
 (0)