Skip to content

Commit

Permalink
refactor: logout
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielolivrp committed Oct 20, 2023
1 parent d7a9896 commit b15b693
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 34 deletions.
31 changes: 31 additions & 0 deletions services/account_service.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package services

import (
"fmt"
"os"
"path/filepath"
"zapmeow/models"
"zapmeow/repositories"
"zapmeow/utils"
)

type AccountService interface {
CreateAccount(account *models.Account) error
GetConnectedAccounts() ([]models.Account, error)
GetAccountByInstanceID(instanceID string) (*models.Account, error)
UpdateAccount(instanceID string, data map[string]interface{}) error
DeleteAccountMessages(instanceID string) error
}

type accountService struct {
Expand Down Expand Up @@ -39,3 +44,29 @@ func (a *accountService) GetAccountByInstanceID(instanceID string) (*models.Acco
func (a *accountService) UpdateAccount(instanceID string, data map[string]interface{}) error {
return a.accountRepo.UpdateAccount(instanceID, data)
}

func (a *accountService) DeleteAccountMessages(instanceID string) error {
err := a.messageService.DeleteMessagesByInstanceID(instanceID)
if err != nil {
return err
}
return a.deleteAccountDirectory(instanceID)
}

func (a *accountService) deleteAccountDirectory(instanceID string) error {
dirPath := utils.MakeAccountStoragePath(instanceID)
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
err = os.Remove(path)
if err != nil {
return err
}
fmt.Printf("File removed: %s\n", path)
}
return nil
})
return err
}
38 changes: 5 additions & 33 deletions services/wpp_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"time"
"zapmeow/configs"
"zapmeow/models"
Expand Down Expand Up @@ -50,9 +48,8 @@ type WppService interface {
SendAudioMessage(instanceID string, jid types.JID, audio *dataurl.DataURL, mimitype string) (*SendMessageResponse, error)
SendImageMessage(instanceID string, jid types.JID, image *dataurl.DataURL, mimitype string) (*SendMessageResponse, error)
UploadMedia(instanceID string, media *dataurl.DataURL, type_ string) (*whatsmeow.UploadResponse, error)
DestroyInstance(instanceID string) error
Logout(instanceID string) error
DeleteInstanceMessages(instanceID string) error
destroyInstance(instanceID string) error
}

func NewWppService(
Expand Down Expand Up @@ -206,13 +203,13 @@ func (w *wppService) UploadMedia(instanceID string, media *dataurl.DataURL, type
return &uploaded, nil
}

func (w *wppService) DestroyInstance(instanceID string) error {
func (w *wppService) destroyInstance(instanceID string) error {
instance, err := w.GetInstance(instanceID)
if err != nil {
return err
}

err = w.DeleteInstanceMessages(instanceID)
err = w.accountService.DeleteAccountMessages(instanceID)
if err != nil {
return err
}
Expand Down Expand Up @@ -241,15 +238,7 @@ func (w *wppService) Logout(instanceID string) error {
return err
}

return w.DestroyInstance(instanceID)
}

func (a *wppService) DeleteInstanceMessages(instanceID string) error {
err := a.messageService.DeleteMessagesByInstanceID(instanceID)
if err != nil {
return err
}
return a.deleteInstanceDirectory(instanceID)
return w.destroyInstance(instanceID)
}

func (w *wppService) GetContactInfo(instanceID string, jid types.JID) (*ContactInfo, error) {
Expand Down Expand Up @@ -380,23 +369,6 @@ func (w *wppService) qrcode(instanceID string) {
}
}

func (a *wppService) deleteInstanceDirectory(instanceID string) error {
dirPath := utils.MakeAccountStoragePath(instanceID)
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
err = os.Remove(path)
if err != nil {
return err
}
fmt.Printf("File removed: %s\n", path)
}
return nil
})
return err
}
func (w *wppService) eventHandler(instanceID string, rawEvt interface{}) {
switch evt := rawEvt.(type) {
case *events.Message:
Expand Down Expand Up @@ -445,7 +417,7 @@ func (w *wppService) handleConnected(instanceID string) {
}

func (w *wppService) handleLoggedOut(instanceID string) {
err := w.DestroyInstance(instanceID)
err := w.destroyInstance(instanceID)
if err != nil {
fmt.Println("Error", err)
}
Expand Down
2 changes: 1 addition & 1 deletion workers/history_sync_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (q *historySyncWorker) ProcessQueue() {
}

if !account.WasSynced {
if err := q.wppService.DeleteInstanceMessages(account.InstanceID); err != nil {
if err := q.accountService.DeleteAccountMessages(account.InstanceID); err != nil {
fmt.Println(err)
continue
}
Expand Down

0 comments on commit b15b693

Please sign in to comment.