diff --git a/activity.go b/activity.go new file mode 100644 index 0000000..e2738ac --- /dev/null +++ b/activity.go @@ -0,0 +1,58 @@ +package notionapi + +// Author represents the author of an Edit +type Author struct { + ID string `json:"id"` + Table string `json:"table"` +} + +// Edit represents a Notion edit (ie. a change made during an Activity) +type Edit struct { + SpaceID string `json:"space_id"` + Authors []Author `json:"authors"` + Timestamp int64 `json:"timestamp"` + Type string `json:"type"` + Version int `json:"version"` + + CommentData Comment `json:"comment_data"` + CommentID string `json:"comment_id"` + DiscussionID string `json:"discussion_id"` + + BlockID string `json:"block_id"` + BlockData struct { + BlockValue Block `json:"block_value"` + } `json:"block_data"` + NavigableBlockID string `json:"navigable_block_id"` + + CollectionID string `json:"collection_id"` + CollectionRowID string `json:"collection_row_id"` +} + +// Activity represents a Notion activity (ie. event) +type Activity struct { + Role string `json:"role"` + + ID string `json:"id"` + SpaceID string `json:"space_id"` + StartTime string `json:"start_time"` + EndTime string `json:"end_time"` + Type string `json:"type"` + Version int `json:"version"` + + ParentID string `json:"parent_id"` + ParentTable string `json:"parent_table"` + + // If the edit was to a block inside a regular page + NavigableBlockID string `json:"navigable_block_id"` + + // If the edit was to a block inside a collection or collection row + CollectionID string `json:"collection_id"` + CollectionRowID string `json:"collection_row_id"` + + Edits []Edit `json:"edits"` + + Index int `json:"index"` + Invalid bool `json:"invalid"` + + RawJSON map[string]interface{} `json:"-"` +} diff --git a/constants.go b/constants.go index 26cf105..6fefc35 100644 --- a/constants.go +++ b/constants.go @@ -33,6 +33,7 @@ const ( const ( // those are Record.Type and determine the type of Record.Value TableSpace = "space" + TableActivity = "activity" TableBlock = "block" TableUser = "notion_user" TableCollection = "collection" diff --git a/get_activity_log.go b/get_activity_log.go new file mode 100644 index 0000000..015e858 --- /dev/null +++ b/get_activity_log.go @@ -0,0 +1,42 @@ +package notionapi + +// /api/v3/getActivityLog request +type getActivityLogRequest struct { + SpaceID string `json:"spaceId"` + StartingAfterID string `json:"startingAfterId,omitempty"` + Limit int `json:"limit"` +} + +// LoadPageChunkResponse is a response to /api/v3/loadPageChunk api +type GetActivityLogResponse struct { + ActivityIDs []string `json:"activityIds"` + RecordMap *RecordMap `json:"recordMap"` + NextID string `json:"-"` + + RawJSON map[string]interface{} `json:"-"` +} + +// GetActivityLog executes a raw API call /api/v3/getActivityLog. +// If startingAfterId is "", starts at the most recent log entry. +func (c *Client) GetActivityLog(spaceID string, startingAfterID string, limit int) (*GetActivityLogResponse, error) { + apiURL := "/api/v3/getActivityLog" + req := &getActivityLogRequest{ + SpaceID: spaceID, + StartingAfterID: startingAfterID, + Limit: limit, + } + var rsp GetActivityLogResponse + var err error + if rsp.RawJSON, err = doNotionAPI(c, apiURL, req, &rsp); err != nil { + return nil, err + } + if err = parseRecordMap(rsp.RecordMap); err != nil { + return nil, err + } + if len(rsp.ActivityIDs) > 0 { + rsp.NextID = rsp.ActivityIDs[len(rsp.ActivityIDs)-1] + } else { + rsp.NextID = "" + } + return &rsp, nil +} diff --git a/get_record_values.go b/get_record_values.go index fe96528..4bcb66a 100644 --- a/get_record_values.go +++ b/get_record_values.go @@ -27,6 +27,7 @@ type Record struct { ID string `json:"-"` Table string `json:"-"` + Activity *Activity `json:"-"` Block *Block `json:"-"` Space *Space `json:"-"` User *User `json:"-"` @@ -62,6 +63,10 @@ func parseRecord(table string, r *Record) error { var pRawJSON *map[string]interface{} var obj interface{} switch table { + case TableActivity: + r.Activity = &Activity{} + obj = r.Activity + pRawJSON = &r.Activity.RawJSON case TableBlock: r.Block = &Block{} obj = r.Block diff --git a/load_page_chunk.go b/load_page_chunk.go index 5d27861..db60c20 100644 --- a/load_page_chunk.go +++ b/load_page_chunk.go @@ -29,6 +29,7 @@ type LoadPageChunkResponse struct { // RecordMap contains a collections of blocks, a space, users, and collections. type RecordMap struct { + Activities map[string]*Record `json:"activity"` Blocks map[string]*Record `json:"block"` Spaces map[string]*Record `json:"space"` Users map[string]*Record `json:"notion_user"` @@ -69,6 +70,12 @@ func (c *Client) LoadPageChunk(pageID string, chunkNo int, cur *cursor) (*LoadPa } func parseRecordMap(recordMap *RecordMap) error { + for _, r := range recordMap.Activities { + if err := parseRecord(TableActivity, r); err != nil { + return err + } + } + for _, r := range recordMap.Blocks { if err := parseRecord(TableBlock, r); err != nil { return err