From 14a899f5d86bfe0309ffe08588b4ddc09bc9ea18 Mon Sep 17 00:00:00 2001 From: harehare Date: Tue, 10 Oct 2023 22:40:43 +0900 Subject: [PATCH] :bug: Fix bugs --- backend/internal/domain/service/service.go | 2 +- .../infra/firebase/item/diagramitem.go | 2 +- backend/internal/infra/firebase/storage.go | 7 +++-- frontend/src/elm/Api/Request.elm | 13 ++++++-- frontend/src/elm/Effect.elm | 2 +- frontend/src/elm/Effect/Diagram.elm | 2 +- frontend/src/elm/Main.elm | 8 ++++- frontend/src/elm/Models/Diagram/Item.elm | 14 ++++++--- frontend/src/elm/Page/List.elm | 31 ++++++++++++++++--- frontend/src/ts/auth.ts | 6 ++-- 10 files changed, 66 insertions(+), 21 deletions(-) diff --git a/backend/internal/domain/service/service.go b/backend/internal/domain/service/service.go index abcce67a8..e86bb2a0d 100644 --- a/backend/internal/domain/service/service.go +++ b/backend/internal/domain/service/service.go @@ -57,7 +57,7 @@ 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") + shouldLoadText := slices.Contains(graphql.CollectAllFields(ctx), "text") if err := isAuthenticated(ctx); err != nil { return mo.Err[[]*diagramitem.DiagramItem](err) diff --git a/backend/internal/infra/firebase/item/diagramitem.go b/backend/internal/infra/firebase/item/diagramitem.go index df890cc20..d77438853 100644 --- a/backend/internal/infra/firebase/item/diagramitem.go +++ b/backend/internal/infra/firebase/item/diagramitem.go @@ -247,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) } diff --git a/backend/internal/infra/firebase/storage.go b/backend/internal/infra/firebase/storage.go index 3c0ad78ce..57b50bc19 100644 --- a/backend/internal/infra/firebase/storage.go +++ b/backend/internal/infra/firebase/storage.go @@ -4,6 +4,7 @@ import ( "bytes" "compress/gzip" "context" + "fmt" "io" "strings" @@ -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) } diff --git a/frontend/src/elm/Api/Request.elm b/frontend/src/elm/Api/Request.elm index 0c0dc47ed..2f34b7d4e 100644 --- a/frontend/src/elm/Api/Request.elm +++ b/frontend/src/elm/Api/Request.elm @@ -2,6 +2,7 @@ module Api.Request exposing ( allItems , allItemsWithText , bookmark + , bulkSave , delete , deleteGist , gistItem @@ -160,9 +161,17 @@ publicItem idToken id = |> Task.mapError toError -save : Maybe IdToken -> InputItem -> Bool -> Task RequestError DiagramItem +bulkSave : Maybe IdToken -> List InputItem -> Bool -> Task RequestError (List DiagramItem) +bulkSave idToken inputs isPublic = + List.map + (save idToken isPublic) + inputs + |> Task.sequence + + +save : Maybe IdToken -> Bool -> InputItem -> Task RequestError DiagramItem save idToken input isPublic = - Mutation.save input isPublic + Mutation.save isPublic input |> Http.mutationRequest graphQLUrl |> authHeaders idToken |> Http.toTask diff --git a/frontend/src/elm/Effect.elm b/frontend/src/elm/Effect.elm index 1d8baf153..063c8cfbb 100644 --- a/frontend/src/elm/Effect.elm +++ b/frontend/src/elm/Effect.elm @@ -29,8 +29,8 @@ changePublicState : (Result DiagramItem DiagramItem -> msg) -> { isPublic : Bool changePublicState msg { isPublic, item, session } = Request.save (Session.getIdToken session) - (DiagramItem.toInputItem item) isPublic + (DiagramItem.toInputItem item) |> Task.mapError (\_ -> item) |> Task.attempt msg |> Return.command diff --git a/frontend/src/elm/Effect/Diagram.elm b/frontend/src/elm/Effect/Diagram.elm index 2fd85de39..ab774dfe6 100644 --- a/frontend/src/elm/Effect/Diagram.elm +++ b/frontend/src/elm/Effect/Diagram.elm @@ -262,7 +262,7 @@ saveToRemote msg { diagram, session, settings } = Request.saveGist (Session.getIdToken session) accessToken (DiagramItem.toInputGistItem diagram) (Text.toString diagram.text) _ -> - Request.save (Session.getIdToken session) (DiagramItem.toInputItem diagram) diagram.isPublic + Request.save (Session.getIdToken session) diagram.isPublic (DiagramItem.toInputItem diagram) ) |> Task.attempt msg |> Return.command diff --git a/frontend/src/elm/Main.elm b/frontend/src/elm/Main.elm index ba86bbe58..f16dd62ee 100644 --- a/frontend/src/elm/Main.elm +++ b/frontend/src/elm/Main.elm @@ -1397,7 +1397,7 @@ processDiagramListMsg msg = DiagramList.GotDiagrams (Err _) -> showErrorMessage Message.messagEerrorOccurred - DiagramList.ImportComplete json -> + DiagramList.ImportDiagrams json -> case DiagramItem.stringToList json of Ok _ -> showInfoMessage Message.messageImportCompleted @@ -1405,6 +1405,12 @@ processDiagramListMsg msg = Err _ -> showErrorMessage Message.messagEerrorOccurred + DiagramList.ImportedRemoteDiagrams (Ok _) -> + showInfoMessage Message.messageImportCompleted + + DiagramList.ImportedRemoteDiagrams (Err _) -> + showErrorMessage Message.messagEerrorOccurred + _ -> stopProgress diff --git a/frontend/src/elm/Models/Diagram/Item.elm b/frontend/src/elm/Models/Diagram/Item.elm index bc0f8fca7..a04ad28a4 100644 --- a/frontend/src/elm/Models/Diagram/Item.elm +++ b/frontend/src/elm/Models/Diagram/Item.elm @@ -6,6 +6,7 @@ module Models.Diagram.Item exposing , empty , encoder , getId + , id , isRemoteDiagram , listToString , listToValue @@ -220,6 +221,11 @@ location = Lens .location (\b a -> { a | location = b }) +id : Lens DiagramItem (Maybe DiagramId) +id = + Lens .id (\b a -> { a | id = b }) + + stringToList : String -> Result D.Error (List DiagramItem) stringToList json = D.decodeString (D.list decoder) json @@ -229,8 +235,8 @@ toInputGistItem : DiagramItem -> InputGistItem toInputGistItem item = { id = case item.id of - Just id -> - OptionalArgument.Present <| Graphql.Scalar.Id <| DiagramId.toString id + Just id_ -> + OptionalArgument.Present <| Graphql.Scalar.Id <| DiagramId.toString id_ Nothing -> OptionalArgument.Absent @@ -252,8 +258,8 @@ toInputItem : DiagramItem -> InputItem toInputItem item = { id = case item.id of - Just id -> - OptionalArgument.Present <| Graphql.Scalar.Id <| DiagramId.toString id + Just id_ -> + OptionalArgument.Present <| Graphql.Scalar.Id <| DiagramId.toString id_ Nothing -> OptionalArgument.Absent diff --git a/frontend/src/elm/Page/List.elm b/frontend/src/elm/Page/List.elm index e26304871..ac52544bb 100644 --- a/frontend/src/elm/Page/List.elm +++ b/frontend/src/elm/Page/List.elm @@ -145,7 +145,8 @@ type Msg | Export | Import | ImportFile File - | ImportComplete String + | ImportDiagrams String + | ImportedRemoteDiagrams (Result RequestError (List DiagramItem)) | ShowConfirmDialog DiagramItem @@ -1011,17 +1012,37 @@ update model message = Return.command <| Select.file [ "application/json" ] ImportFile ImportFile file -> - Return.command <| Task.perform ImportComplete <| File.toString file + Return.command <| Task.perform ImportDiagrams <| File.toString file - ImportComplete json -> + ImportDiagrams json -> DiagramItem.stringToList json |> Result.toMaybe |> Maybe.map (\diagrams -> - Return.command <| importDiagram <| DiagramItem.listToValue diagrams + if Session.isSignedIn model.session then + Request.bulkSave (Session.getIdToken model.session) + (List.map + (\diagram -> + (DiagramItem.location.set (Just DiagramLocation.Remote) >> DiagramItem.id.set Nothing) diagram + |> DiagramItem.toInputItem + ) + diagrams + ) + False + |> Task.attempt ImportedRemoteDiagrams + |> Return.command + + else + Return.command <| importDiagram <| DiagramItem.listToValue diagrams ) |> Maybe.withDefault Return.zero + ImportedRemoteDiagrams (Ok _) -> + reload + + ImportedRemoteDiagrams (Err _) -> + Return.zero + Export -> case model.diagramList of DiagramList.AllList (Success _) _ _ -> @@ -1037,7 +1058,7 @@ update model message = GotExportDiagrams (Ok diagrams) -> case model.diagramList of DiagramList.AllList (Success localDiagrams) _ _ -> - List.concat [ localDiagrams, diagrams ] + List.concat [ diagrams, localDiagrams ] |> ListEx.uniqueBy .id |> DiagramItem.listToString |> Download.string "textusm.json" "application/json" diff --git a/frontend/src/ts/auth.ts b/frontend/src/ts/auth.ts index 914b47920..7c7d5f746 100644 --- a/frontend/src/ts/auth.ts +++ b/frontend/src/ts/auth.ts @@ -2,13 +2,13 @@ import { type FirebaseOptions, initializeApp } from 'firebase/app'; import { type AuthProvider, signOut as firebaseSignOut, - signInWithRedirect, signInWithPopup as firebaseSignInWithPopup, GoogleAuthProvider, GithubAuthProvider, getAuth, onAuthStateChanged as firebaseOnAuthStateChanged, connectAuthEmulator, + type UserCredential, } from 'firebase/auth'; import { getPerformance } from 'firebase/performance'; @@ -38,9 +38,9 @@ if (import.meta.env.DEV && process.env.FIREBASE_AUTH_EMULATOR_HOST) { connectAuthEmulator(auth, `http://${process.env.FIREBASE_AUTH_EMULATOR_HOST}`); } -export const signIn = async (provider: AuthProvider): Promise => +export const signIn = async (provider: AuthProvider): Promise => new Promise((resolve, reject) => { - signInWithRedirect(auth, provider) + firebaseSignInWithPopup(auth, provider) .then((result) => { resolve(result); })