Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions action/google.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package action

import (
"context"
"fmt"
"log"

"github.com/ValianceTekProject/AreaBack/db"
"github.com/ValianceTekProject/AreaBack/initializers"
)

func GetGoogleToken(ctx context.Context, config map[string]any) (string, string, error) {
actionID, ok := config["action_id"].(string)
if !ok {
return "", "", fmt.Errorf("Unable to retrieve actionId")
}
action, err := initializers.DB.Actions.FindUnique(
db.Actions.ID.Equals(actionID),
).With(
db.Actions.Area.Fetch().With(
db.Areas.User.Fetch().With(
db.Users.ServiceTokens.Fetch(),
),
),
db.Actions.Service.Fetch(),
).Exec(ctx)
if err != nil {
log.Printf("Failed to get Actions: %v", err)
}
area := action.Area()
user := area.User()
service := action.Service()
var googleToken string
for _, ust := range user.ServiceTokens() {
if ust.ServiceID == service.ID {
googleToken = ust.AccessToken
break
}
}
return actionID, googleToken, nil
}
75 changes: 75 additions & 0 deletions action/googleDriveNewFile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package action

import (
"context"
"fmt"
"log"
"net/http"
"time"

"github.com/ValianceTekProject/AreaBack/db"
"github.com/ValianceTekProject/AreaBack/initializers"
"google.golang.org/api/drive/v3"
"google.golang.org/api/option"
)

func ExecGoogleDriveNewFile(config map[string]any) error {
ctx := context.Background()

actionID, googleToken, err := GetGoogleToken(ctx, config)

if err != nil {
return err
}

if googleToken != "" {
execGoogleDriveNewFileAction(googleToken, actionID, ctx)
}
return nil
}

func execGoogleDriveNewFileAction(token string, actionID string, ctx context.Context) {
httpClient := &http.Client{
Transport: &tokenTransport{token: token},
}

driveService, err := drive.NewService(ctx, option.WithHTTPClient(httpClient))
if err != nil {
fmt.Printf("Failed to create Drive service: %v\n", err)
return
}

since := time.Now().Add(-1 * time.Minute).Format(time.RFC3339)
query := fmt.Sprintf("createdTime > '%s' and trashed = false", since)

fileList, err := driveService.Files.List().
Q(query).
PageSize(100).
Fields("files(id, name, createdTime, mimeType)").
Do()

if err != nil {
fmt.Printf("Failed to list files: %v\n", err)
return
}

if len(fileList.Files) > 0 {
_, err := initializers.DB.Actions.FindUnique(
db.Actions.ID.Equals(actionID),
).Update(
db.Actions.Triggered.Set(true),
).Exec(ctx)
if err != nil {
log.Printf("Error updating action trigger: %v", err)
}
}
}

type tokenTransport struct {
token string
}

func (t *tokenTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("Authorization", "Bearer "+t.token)
return http.DefaultTransport.RoundTrip(req)
}
1 change: 1 addition & 0 deletions authentification/googleAuth.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func getGoogleOAuthConfig() *oauth2.Config {
"https://www.googleapis.com/auth/gmail.send",
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/calendar",
"https://www.googleapis.com/auth/drive",
},
Endpoint: google.Endpoint,
}
Expand Down
16 changes: 16 additions & 0 deletions templates/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,22 @@ var Services = map[string]*Service{
},
"Google": {
Name: "Google",
Actions: map[string]*ActionDefinition{
"google_new_file_drive": {
Name: "google_new_file_drive",
Description: "New file created in drive",
Service: "Google",
Config: []ActionField{
{
Name: "channel_id",
Type: "text",
Label: "Channel ID",
Required: false,
},
},
Handler: action.ExecGoogleDriveNewFile,
},
},
Reactions: map[string]*ReactionDefinition{
"google_Send_Email": {
Name: "google_Send_Email",
Expand Down