1
0
mirror of https://github.com/dstotijn/go-notion.git synced 2024-11-28 08:58:51 +02:00

Add created_by and last_edited_by support, add archived for DB (#30)

This commit is contained in:
David Stotijn 2022-08-13 19:46:00 +02:00 committed by GitHub
parent 264ec2c28f
commit 1320ab0ee0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 120 additions and 17 deletions

View File

@ -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
}

View File

@ -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,6 +246,12 @@ 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"),
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{
{
@ -710,7 +724,9 @@ func TestQueryDatabase(t *testing.T) {
Name: "People",
People: []notion.User{
{
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: &notion.User{
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: &notion.User{
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"
@ -1568,7 +1596,13 @@ func TestFindPageByID(t *testing.T) {
expPage: notion.Page{
ID: "606ed832-7d79-46de-bbed-5b4896e7bc02",
CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T18:34:00.000Z"),
CreatedBy: &notion.BaseUser{
ID: "71e95936-2737-4e11-b03d-f174f6f13087",
},
LastEditedTime: mustParseTime(time.RFC3339Nano, "2021-05-19T18:34:00.000Z"),
LastEditedBy: &notion.BaseUser{
ID: "5ba97cc9-e5e0-4363-b33a-1d80a635577f",
},
URL: "https://www.notion.so/Avocado-251d2b5f268c4de2afe9c71ff92ca95c",
Parent: notion.Parent{
Type: notion.ParentTypePage,
@ -3369,7 +3403,9 @@ func TestFindUserByID(t *testing.T) {
},
respStatusCode: http.StatusOK,
expUser: notion.User{
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{
BaseUser: notion.BaseUser{
ID: "be32e790-8292-46df-a248-b784fdf483cf",
},
Type: notion.UserTypeBot,
Bot: &notion.Bot{
Owner: notion.BotOwner{
Type: notion.BotOwnerTypeUser,
User: &notion.User{
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{
{
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) {
},
},
{
BaseUser: notion.BaseUser{
ID: "25c9cc08-1afd-4d22-b9e6-31b0f6e7b44f",
},
Name: "Johnny 5",
Type: notion.UserTypeBot,
Bot: &notion.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",
@ -4051,7 +4105,13 @@ func TestFindBlockByID(t *testing.T) {
PageID: "59833787-2cf9-4fdf-8782-e53db20768a5",
},
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"),
expLastEditedBy: notion.BaseUser{
ID: "5ba97cc9-e5e0-4363-b33a-1d80a635577f",
},
expHasChildren: true,
expArchived: false,
expError: nil,
@ -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())
}

View File

@ -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.

View File

@ -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"`

View File

@ -28,8 +28,15 @@ type BotOwner struct {
User *User `json:"user"`
}
type User struct {
// 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 {
BaseUser
Type UserType `json:"type"`
Name string `json:"name"`
AvatarURL string `json:"avatar_url"`