Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import and Export Improvements #3166

Merged
merged 7 commits into from
Oct 12, 2023
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
2 changes: 1 addition & 1 deletion backend/internal/domain/repository/item/diagramitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

type ItemRepository interface {
FindByID(ctx context.Context, userID string, itemID string, isPublic bool) mo.Result[*diagramitem.DiagramItem]
Find(ctx context.Context, userID string, offset, limit int, isPublic bool, isBookmark bool) mo.Result[[]*diagramitem.DiagramItem]
Find(ctx context.Context, userID string, offset, limit int, isPublic bool, isBookmark bool, shouldLoadText bool) mo.Result[[]*diagramitem.DiagramItem]
Save(ctx context.Context, userID string, item *diagramitem.DiagramItem, isPublic bool) mo.Result[*diagramitem.DiagramItem]
Delete(ctx context.Context, userID string, itemID string, isPublic bool) mo.Result[bool]
}
6 changes: 5 additions & 1 deletion backend/internal/domain/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"errors"
"net"
"os"
"slices"
"time"

"github.com/99designs/gqlgen/graphql"
jwt "github.com/form3tech-oss/jwt-go"
"github.com/harehare/textusm/internal/context/values"
"github.com/harehare/textusm/internal/domain/model/item/diagramitem"
Expand Down Expand Up @@ -55,11 +57,13 @@ func isAuthenticated(ctx context.Context) error {
}

func (s *Service) Find(ctx context.Context, offset, limit int, isPublic bool, isBookmark bool, fields map[string]struct{}) mo.Result[[]*diagramitem.DiagramItem] {
shouldLoadText := slices.Contains(graphql.CollectAllFields(ctx), "text")

if err := isAuthenticated(ctx); err != nil {
return mo.Err[[]*diagramitem.DiagramItem](err)
}

return s.repo.Find(ctx, values.GetUID(ctx).OrEmpty(), offset, limit, isPublic, isBookmark)
return s.repo.Find(ctx, values.GetUID(ctx).OrEmpty(), offset, limit, isPublic, isBookmark, shouldLoadText)
}

func (s *Service) FindByID(ctx context.Context, itemID string, isPublic bool) mo.Result[*diagramitem.DiagramItem] {
Expand Down
16 changes: 13 additions & 3 deletions backend/internal/infra/firebase/item/diagramitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (r *FirestoreItemRepository) FindByID(ctx context.Context, userID string, i
})
}

func (r *FirestoreItemRepository) Find(ctx context.Context, userID string, offset, limit int, isPublic bool, isBookmark bool) mo.Result[[]*diagramitem.DiagramItem] {
func (r *FirestoreItemRepository) Find(ctx context.Context, userID string, offset, limit int, isPublic bool, isBookmark bool, shouldLoadText bool) mo.Result[[]*diagramitem.DiagramItem] {
var (
items []*diagramitem.DiagramItem
iter *firestore.DocumentIterator
Expand Down Expand Up @@ -79,7 +79,17 @@ func (r *FirestoreItemRepository) Find(ctx context.Context, userID string, offse
}

items = append(items, i.Map(func(v *diagramitem.DiagramItem) (*diagramitem.DiagramItem, error) {
return v.ClearText(), nil
if shouldLoadText && v.IsSaveToStorage() {
ret := r.findFromCloudStorage(ctx, userID, v.ID())
if ret.IsError() {
slog.Error("Failed find diagram", "userID", userID, "itemID", v.ID(), "isPublic", isPublic)
return nil, ret.Error()
}
v.UpdateEncryptedText(ret.OrEmpty())
return v, nil
} else {
return v.ClearText(), nil
}
}).OrEmpty())
}

Expand Down Expand Up @@ -237,5 +247,5 @@ func (r *FirestoreItemRepository) deleteToFirestore(ctx context.Context, userID

func (r *FirestoreItemRepository) deleteToCloudStorage(ctx context.Context, userID, itemID string) mo.Result[bool] {
storage := firebase.NewCloudStorage(r.storage)
return storage.Delete(ctx, userID, itemID)
return storage.Delete(ctx, storageRoot, userID, itemID)
}
7 changes: 5 additions & 2 deletions backend/internal/infra/firebase/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"compress/gzip"
"context"
"fmt"
"io"
"strings"

Expand Down Expand Up @@ -94,14 +95,16 @@ func (s *CloudStorage) Get(ctx context.Context, prefix string, paths ...string)
return mo.Ok(string(body))
}

func (s *CloudStorage) Delete(ctx context.Context, uid, itemID string) mo.Result[bool] {
func (s *CloudStorage) Delete(ctx context.Context, prefix, uid, itemID string) mo.Result[bool] {
bucket, err := s.client.DefaultBucket()

if err != nil {
return mo.Err[bool](err)
}

if err = bucket.Object(getObjectName(uid, itemID)).Delete(ctx); err != nil {
fmt.Println(getObjectName(prefix, uid, itemID))

if err = bucket.Object(getObjectName(prefix, uid, itemID)).Delete(ctx); err != nil {
return mo.Err[bool](err)
}

Expand Down
10 changes: 8 additions & 2 deletions firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
"hosting": {
"public": "frontend/dist",
"target": "textusm",
"ignore": ["firebase.json", "**/.*", "**/node_modules/**", "**/*.map"],
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**",
"**/*.map"
],
"rewrites": [
{
"source": "**",
Expand Down Expand Up @@ -74,7 +79,8 @@
},
"hosting": {
"port": 5000
}
},
"singleProjectMode": true
},
"storage": {
"rules": "storage.rules"
Expand Down
Loading
Loading