From 1320ab0ee0e084d845c676d5f1f471ad9308fc8c Mon Sep 17 00:00:00 2001 From: David Stotijn Date: Sat, 13 Aug 2022 19:46:00 +0200 Subject: [PATCH] Add `created_by` and `last_edited_by` support, add `archived` for DB (#30) --- block.go | 22 +++++++++++ client_test.go | 100 +++++++++++++++++++++++++++++++++++++++++-------- database.go | 4 ++ page.go | 2 + user.go | 9 ++++- 5 files changed, 120 insertions(+), 17 deletions(-) diff --git a/block.go b/block.go index dbfa75e..19c5dc0 100644 --- a/block.go +++ b/block.go @@ -12,6 +12,8 @@ type Block interface { ID() string Parent() Parent CreatedTime() time.Time + CreatedBy() BaseUser + LastEditedBy() BaseUser LastEditedTime() time.Time HasChildren() bool Archived() bool @@ -23,7 +25,9 @@ type blockDTO struct { Parent *Parent `json:"parent,omitempty"` Type BlockType `json:"type,omitempty"` CreatedTime *time.Time `json:"created_time,omitempty"` + CreatedBy *BaseUser `json:"created_by,omitempty"` LastEditedTime *time.Time `json:"last_edited_time,omitempty"` + LastEditedBy *BaseUser `json:"last_edited_by,omitempty"` HasChildren bool `json:"has_children,omitempty"` Archived *bool `json:"archived,omitempty"` @@ -64,7 +68,9 @@ type baseBlock struct { id string parent Parent createdTime time.Time + createdBy BaseUser lastEditedTime time.Time + lastEditedBy BaseUser hasChildren bool archived bool } @@ -78,10 +84,18 @@ func (b baseBlock) CreatedTime() time.Time { return b.createdTime } +func (b baseBlock) CreatedBy() BaseUser { + return b.createdBy +} + func (b baseBlock) LastEditedTime() time.Time { return b.lastEditedTime } +func (b baseBlock) LastEditedBy() BaseUser { + return b.lastEditedBy +} + func (b baseBlock) HasChildren() bool { return b.hasChildren } @@ -846,10 +860,18 @@ func (dto blockDTO) Block() Block { baseBlock.createdTime = *dto.CreatedTime } + if dto.CreatedBy != nil { + baseBlock.createdBy = *dto.CreatedBy + } + if dto.LastEditedTime != nil { baseBlock.lastEditedTime = *dto.LastEditedTime } + if dto.LastEditedBy != nil { + baseBlock.lastEditedBy = *dto.LastEditedBy + } + if dto.Archived != nil { baseBlock.archived = *dto.Archived } diff --git a/client_test.go b/client_test.go index 1f22a7f..3aebb08 100644 --- a/client_test.go +++ b/client_test.go @@ -98,6 +98,14 @@ func TestFindDatabaseByID(t *testing.T) { "id": "668d797c-76fa-4934-9b05-ad288df2d136", "created_time": "2020-03-17T19:10:04.968Z", "last_edited_time": "2020-03-17T21:49:37.913Z", + "created_by": { + "object": "user", + "id": "71e95936-2737-4e11-b03d-f174f6f13087" + }, + "last_edited_by": { + "object": "user", + "id": "5ba97cc9-e5e0-4363-b33a-1d80a635577f" + }, "url": "https://www.notion.so/668d797c76fa49349b05ad288df2d136", "title": [ { @@ -238,7 +246,13 @@ func TestFindDatabaseByID(t *testing.T) { ID: "668d797c-76fa-4934-9b05-ad288df2d136", CreatedTime: mustParseTime(time.RFC3339, "2020-03-17T19:10:04.968Z"), LastEditedTime: mustParseTime(time.RFC3339, "2020-03-17T21:49:37.913Z"), - URL: "https://www.notion.so/668d797c76fa49349b05ad288df2d136", + CreatedBy: notion.BaseUser{ + ID: "71e95936-2737-4e11-b03d-f174f6f13087", + }, + LastEditedBy: notion.BaseUser{ + ID: "5ba97cc9-e5e0-4363-b33a-1d80a635577f", + }, + URL: "https://www.notion.so/668d797c76fa49349b05ad288df2d136", Title: []notion.RichText{ { Type: notion.RichTextTypeText, @@ -710,7 +724,9 @@ func TestQueryDatabase(t *testing.T) { Name: "People", People: []notion.User{ { - ID: "be32e790-8292-46df-a248-b784fdf483cf", + BaseUser: notion.BaseUser{ + ID: "be32e790-8292-46df-a248-b784fdf483cf", + }, Name: "Jane Doe", AvatarURL: "https://example.com/image.png", Type: notion.UserTypePerson, @@ -774,7 +790,9 @@ func TestQueryDatabase(t *testing.T) { Type: notion.DBPropTypeCreatedBy, Name: "Created by", CreatedBy: ¬ion.User{ - ID: "be32e790-8292-46df-a248-b784fdf483cf", + BaseUser: notion.BaseUser{ + ID: "be32e790-8292-46df-a248-b784fdf483cf", + }, Name: "Jane Doe", AvatarURL: "https://example.com/image.png", Type: notion.UserTypePerson, @@ -794,7 +812,9 @@ func TestQueryDatabase(t *testing.T) { Type: notion.DBPropTypeLastEditedBy, Name: "Last edited by", LastEditedBy: ¬ion.User{ - ID: "be32e790-8292-46df-a248-b784fdf483cf", + BaseUser: notion.BaseUser{ + ID: "be32e790-8292-46df-a248-b784fdf483cf", + }, Name: "Jane Doe", AvatarURL: "https://example.com/image.png", Type: notion.UserTypePerson, @@ -1529,7 +1549,15 @@ func TestFindPageByID(t *testing.T) { "object": "page", "id": "606ed832-7d79-46de-bbed-5b4896e7bc02", "created_time": "2021-05-19T18:34:00.000Z", + "created_by": { + "object": "user", + "id": "71e95936-2737-4e11-b03d-f174f6f13087" + }, "last_edited_time": "2021-05-19T18:34:00.000Z", + "last_edited_by": { + "object": "user", + "id": "5ba97cc9-e5e0-4363-b33a-1d80a635577f" + }, "parent": { "type": "page_id", "page_id": "b0668f48-8d66-4733-9bdb-2f82215707f7" @@ -1566,10 +1594,16 @@ func TestFindPageByID(t *testing.T) { }, respStatusCode: http.StatusOK, expPage: notion.Page{ - ID: "606ed832-7d79-46de-bbed-5b4896e7bc02", - CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T18:34:00.000Z"), + ID: "606ed832-7d79-46de-bbed-5b4896e7bc02", + CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T18:34:00.000Z"), + CreatedBy: ¬ion.BaseUser{ + ID: "71e95936-2737-4e11-b03d-f174f6f13087", + }, LastEditedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T18:34:00.000Z"), - URL: "https://www.notion.so/Avocado-251d2b5f268c4de2afe9c71ff92ca95c", + LastEditedBy: ¬ion.BaseUser{ + ID: "5ba97cc9-e5e0-4363-b33a-1d80a635577f", + }, + URL: "https://www.notion.so/Avocado-251d2b5f268c4de2afe9c71ff92ca95c", Parent: notion.Parent{ Type: notion.ParentTypePage, PageID: "b0668f48-8d66-4733-9bdb-2f82215707f7", @@ -3369,7 +3403,9 @@ func TestFindUserByID(t *testing.T) { }, respStatusCode: http.StatusOK, expUser: notion.User{ - ID: "be32e790-8292-46df-a248-b784fdf483cf", + BaseUser: notion.BaseUser{ + ID: "be32e790-8292-46df-a248-b784fdf483cf", + }, Name: "Jane Doe", AvatarURL: "https://example.com/avatar.png", Type: notion.UserTypePerson, @@ -3469,13 +3505,17 @@ func TestFindCurrentUser(t *testing.T) { }, respStatusCode: http.StatusOK, expUser: notion.User{ - ID: "be32e790-8292-46df-a248-b784fdf483cf", + BaseUser: notion.BaseUser{ + ID: "be32e790-8292-46df-a248-b784fdf483cf", + }, Type: notion.UserTypeBot, Bot: ¬ion.Bot{ Owner: notion.BotOwner{ Type: notion.BotOwnerTypeUser, User: ¬ion.User{ - ID: "5389a034-eb5c-47b5-8a9e-f79c99ef166c", + BaseUser: notion.BaseUser{ + ID: "5389a034-eb5c-47b5-8a9e-f79c99ef166c", + }, Name: "Jane Doe", AvatarURL: "https://example.com/avatar.png", Type: notion.UserTypePerson, @@ -3595,7 +3635,9 @@ func TestListUsers(t *testing.T) { expResponse: notion.ListUsersResponse{ Results: []notion.User{ { - ID: "be32e790-8292-46df-a248-b784fdf483cf", + BaseUser: notion.BaseUser{ + ID: "be32e790-8292-46df-a248-b784fdf483cf", + }, Name: "Jane Doe", AvatarURL: "https://example.com/avatar.png", Type: notion.UserTypePerson, @@ -3604,7 +3646,9 @@ func TestListUsers(t *testing.T) { }, }, { - ID: "25c9cc08-1afd-4d22-b9e6-31b0f6e7b44f", + BaseUser: notion.BaseUser{ + ID: "25c9cc08-1afd-4d22-b9e6-31b0f6e7b44f", + }, Name: "Johnny 5", Type: notion.UserTypeBot, Bot: ¬ion.Bot{}, @@ -4013,7 +4057,9 @@ func TestFindBlockByID(t *testing.T) { expID string expParent notion.Parent expCreatedTime time.Time + expCreatedBy notion.BaseUser expLastEditedTime time.Time + expLastEditedBy notion.BaseUser expHasChildren bool expArchived bool expError error @@ -4031,7 +4077,15 @@ func TestFindBlockByID(t *testing.T) { "page_id": "59833787-2cf9-4fdf-8782-e53db20768a5" }, "created_time": "2021-10-02T06:09:00.000Z", + "created_by": { + "object": "user", + "id": "71e95936-2737-4e11-b03d-f174f6f13087" + }, "last_edited_time": "2021-10-02T06:31:00.000Z", + "last_edited_by": { + "object": "user", + "id": "5ba97cc9-e5e0-4363-b33a-1d80a635577f" + }, "has_children": true, "archived": false, "type": "child_page", @@ -4050,11 +4104,17 @@ func TestFindBlockByID(t *testing.T) { Type: notion.ParentTypePage, PageID: "59833787-2cf9-4fdf-8782-e53db20768a5", }, - expCreatedTime: mustParseTime(time.RFC3339, "2021-10-02T06:09:00Z"), + expCreatedTime: mustParseTime(time.RFC3339, "2021-10-02T06:09:00Z"), + expCreatedBy: notion.BaseUser{ + ID: "71e95936-2737-4e11-b03d-f174f6f13087", + }, expLastEditedTime: mustParseTime(time.RFC3339, "2021-10-02T06:31:00Z"), - expHasChildren: true, - expArchived: false, - expError: nil, + expLastEditedBy: notion.BaseUser{ + ID: "5ba97cc9-e5e0-4363-b33a-1d80a635577f", + }, + expHasChildren: true, + expArchived: false, + expError: nil, }, { name: "error response not found", @@ -4118,10 +4178,18 @@ func TestFindBlockByID(t *testing.T) { t.Fatalf("createdTime not equal (expected: %v, got: %v)", tt.expCreatedTime, block.CreatedTime()) } + if tt.expCreatedBy != block.CreatedBy() { + t.Fatalf("createdBy not equal (expected: %v, got: %v)", tt.expCreatedBy, block.CreatedBy()) + } + if tt.expLastEditedTime != block.LastEditedTime() { t.Fatalf("lastEditedTime not equal (expected: %v, got: %v)", tt.expLastEditedTime, block.LastEditedTime()) } + if tt.expLastEditedBy != block.LastEditedBy() { + t.Fatalf("lastEditedBy not equal (expected: %v, got: %v)", tt.expLastEditedBy, block.LastEditedBy()) + } + if tt.expHasChildren != block.HasChildren() { t.Fatalf("hasChildren not equal (expected: %v, got: %v)", tt.expHasChildren, block.HasChildren()) } diff --git a/database.go b/database.go index 01e215a..0e96005 100644 --- a/database.go +++ b/database.go @@ -11,13 +11,16 @@ import ( type Database struct { ID string `json:"id"` CreatedTime time.Time `json:"created_time"` + CreatedBy BaseUser `json:"created_by"` LastEditedTime time.Time `json:"last_edited_time"` + LastEditedBy BaseUser `json:"last_edited_by"` URL string `json:"url"` Title []RichText `json:"title"` Properties DatabaseProperties `json:"properties"` Parent Parent `json:"parent"` Icon *Icon `json:"icon,omitempty"` Cover *Cover `json:"cover,omitempty"` + Archived bool `json:"archived"` } // DatabaseProperties is a mapping of properties defined on a database. @@ -496,6 +499,7 @@ type UpdateDatabaseParams struct { Properties map[string]*DatabaseProperty `json:"properties,omitempty"` Icon *Icon `json:"icon,omitempty"` Cover *Cover `json:"cover,omitempty"` + Archived *bool `json:"archived,omitempty"` } // Validate validates params for updating a database. diff --git a/page.go b/page.go index de60708..8af5c0a 100644 --- a/page.go +++ b/page.go @@ -13,7 +13,9 @@ import ( type Page struct { ID string `json:"id"` CreatedTime time.Time `json:"created_time"` + CreatedBy *BaseUser `json:"created_by,omitempty"` LastEditedTime time.Time `json:"last_edited_time"` + LastEditedBy *BaseUser `json:"last_edited_by,omitempty"` Parent Parent `json:"parent"` Archived bool `json:"archived"` URL string `json:"url"` diff --git a/user.go b/user.go index b24451d..c4d2f32 100644 --- a/user.go +++ b/user.go @@ -28,8 +28,15 @@ type BotOwner struct { User *User `json:"user"` } +// BaseUser contains the fields that are always returned for user objects. +// See: https://developers.notion.com/reference/user#where-user-objects-appear-in-the-api +type BaseUser struct { + ID string `json:"id"` +} + type User struct { - ID string `json:"id"` + BaseUser + Type UserType `json:"type"` Name string `json:"name"` AvatarURL string `json:"avatar_url"`