generated from padok-team/yatas-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyatasDatabase.go
145 lines (127 loc) · 3.92 KB
/
yatasDatabase.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"context"
"log"
"time"
"github.com/jomei/notionapi"
)
func initYatasDatabase(client *NotionClient, account *NotionAccount) bool {
// If exists retrieve the Yatas databaseID else create a new inline database.
if isYatasDatabaseExistInPage(client, account) {
log.Printf("[LOADED] DatabaseID = %v", account.DatabaseID)
return true
} else {
// Needs to create Yatas database
log.Printf("Start database creation")
return createYatasInlineDatabase(client, account)
}
}
func isYatasDatabaseExistInPage(client *NotionClient, account *NotionAccount) bool {
// Getting Default client
defaultClient := client.NotionClientV1.JomeiClient
// Type casting
pageID := notionapi.PageID(account.PageID)
blockID := notionapi.BlockID(account.PageID)
pagination := notionapi.Pagination{
PageSize: 10,
}
// Getting pages
_, pageError := defaultClient.Page.Get(context.Background(), pageID)
blocks, blockError := defaultClient.Block.GetChildren(context.Background(), blockID, &pagination)
// Is the provided page exist ?
if pageError != nil {
log.Printf("Error getting the page : %v ... %v", account.PageID, pageError)
return false
}
// Is the Yatas database exist in the provided page?
if blockError != nil {
log.Printf("Error getting the block : %v ... %v", blockID, blockError)
return false
} else {
for _, block := range blocks.Results {
if block.GetType() == "child_database" {
// Is the Yatas database ?
if isYatasDatabase(client, notionapi.DatabaseID(block.GetID())) {
// Update account to re-use databaseID
log.Printf("Yatas database found !")
account.DatabaseID = string(block.GetID())
return true
}
}
}
}
return false
}
func isYatasDatabase(client *NotionClient, databaseID notionapi.DatabaseID) bool {
// Getting Default client
defaultClient := client.NotionClientV1.JomeiClient
// Getting the database
db, dbError := defaultClient.Database.Get(context.Background(), databaseID)
// Is the database exist ?
if dbError != nil {
log.Printf("Error getting the database : %v ... %v", databaseID, dbError)
} else {
log.Printf("Getting database %v successed ! %v", databaseID, *(&(db.Title[0].Text).Content))
// Is the database have the good name ?
if *(&(db.Title[0].Text).Content) == "Yatas instances" {
return true
}
}
return false
}
func createYatasInlineDatabase(client *NotionClient, account *NotionAccount) bool {
// Get ClientV1 to create inline databases
clientv1 := client.NotionClientV1
// Create a inline database creation request
inlineDatabase := yatasInlineDatabaseCreateRequest(account.PageID)
// Try to create inline database
newDatabase, err := clientv1.Database.Create(context.Background(), &inlineDatabase)
// Is the database created with success ?
if err != nil {
log.Printf("Error during the creation of the database ... %v", err)
return false
} else {
account.DatabaseID = string(newDatabase.ID)
//Wait the db to be created
time.Sleep(1 * time.Second)
// Make the database as a list
viewID, viewType, exist := client.GetTableViewType(account.DatabaseID)
if exist {
if viewType != "list" {
client.UpdateTableViewList(viewID, "list")
}
client.ShowAllProperties(viewID, account.DatabaseID)
}
return true
}
}
func yatasInlineDatabaseCreateRequest(pageID string) DatabaseV1CreateRequest {
title := notionapi.Text{
Content: "Yatas instances",
}
emoji := notionapi.Emoji("🧠")
database := DatabaseV1CreateRequest{
Parent: notionapi.Parent{PageID: notionapi.PageID(pageID)},
Title: []notionapi.RichText{
{
Text: &title,
},
},
Icon: ¬ionapi.Icon{
Type: `emoji`,
Emoji: &emoji,
},
Properties: notionapi.PropertyConfigs{
"Name": notionapi.TitlePropertyConfig{
Type: "title",
Title: struct{}{},
},
"Created time": notionapi.CreatedTimePropertyConfig{
Type: "created_time",
CreatedTime: struct{}{},
},
},
IsInline: true,
}
return database
}