Skip to content

Commit

Permalink
Add GetActivityLog() in the style of LoadPageChunk().
Browse files Browse the repository at this point in the history
This lets us retrieve the list of recent changes to pages in a given
notion database.
  • Loading branch information
apenwarr committed Jun 28, 2020
1 parent 4e5b3cd commit 042f4af
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
58 changes: 58 additions & 0 deletions 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:"-"`
}
1 change: 1 addition & 0 deletions constants.go
Expand Up @@ -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"
Expand Down
42 changes: 42 additions & 0 deletions 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
}
5 changes: 5 additions & 0 deletions get_record_values.go
Expand Up @@ -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:"-"`
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions load_page_chunk.go
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 042f4af

Please sign in to comment.