-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
service.go
91 lines (78 loc) · 3.47 KB
/
service.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
package sync
import (
"context"
"fmt"
"github.com/SyncYomi/SyncYomi/internal/notification"
"github.com/SyncYomi/SyncYomi/internal/domain"
"github.com/SyncYomi/SyncYomi/internal/logger"
"github.com/rs/zerolog"
)
type Service interface {
// Get etag of sync data.
// For avoid memory usage, only the etag will be returnedj
GetSyncDataETag(ctx context.Context, apiKey string) (*string, error)
// Get sync data and etag
GetSyncDataAndETag(ctx context.Context, apiKey string) ([]byte, *string, error)
// Create or replace sync data, returns the new etag.
SetSyncData(ctx context.Context, apiKey string, data []byte) (*string, error)
// Replace sync data only if the etag matches,
// returns the new etag if updated, or nil if not.
SetSyncDataIfMatch(ctx context.Context, apiKey string, etag string, data []byte) (*string, error)
}
func NewService(log logger.Logger, repo domain.SyncRepo, notificationSvc notification.Service, apiRepo domain.APIRepo) Service {
return &service{
log: log.With().Str("module", "sync").Logger(),
repo: repo,
notificationService: notificationSvc,
apiRepo: apiRepo,
}
}
type service struct {
log zerolog.Logger
repo domain.SyncRepo
notificationService notification.Service
apiRepo domain.APIRepo
}
// Get etag of sync data.
// For avoid memory usage, only the etag will be returned.
func (s service) GetSyncDataETag(ctx context.Context, apiKey string) (*string, error) {
return s.repo.GetSyncDataETag(ctx, apiKey)
}
// Get sync data and etag
func (s service) GetSyncDataAndETag(ctx context.Context, apiKey string) ([]byte, *string, error) {
return s.repo.GetSyncDataAndETag(ctx, apiKey)
}
// Create or replace sync data, returns the new etag.
func (s service) SetSyncData(ctx context.Context, apiKey string, data []byte) (*string, error) {
return s.repo.SetSyncData(ctx, apiKey, data)
}
// Replace sync data only if the etag matches,
// returns the new etag if updated, or nil if not.
func (s service) SetSyncDataIfMatch(ctx context.Context, apiKey string, etag string, data []byte) (*string, error) {
return s.repo.SetSyncDataIfMatch(ctx, apiKey, etag, data)
}
func (s service) notifySyncStarted(apiKeyName string) {
s.notificationService.Send(domain.NotificationEventSyncStarted, domain.NotificationPayload{
Subject: "Sync Initiated",
Message: fmt.Sprintf("A sync operation between Tachiyomi and your library has been initiated for user **%s**. "+
"Please wait for the process to complete.", apiKeyName),
})
}
func (s service) notifySyncSuccess(apiKeyName string) {
s.notificationService.Send(domain.NotificationEventSyncSuccess, domain.NotificationPayload{
Subject: "Sync Completed Successfully",
Message: fmt.Sprintf("The synchronization with your Tachiyomi library has completed successfully for user **%s**.", apiKeyName),
})
}
func (s service) notifySyncFailed(apiKeyName string, errMsg string) {
s.notificationService.Send(domain.NotificationEventSyncFailed, domain.NotificationPayload{
Subject: "Sync Operation Failed",
Message: fmt.Sprintf("The synchronization with Tachiyomi failed for user **%s**. Error: %s", apiKeyName, errMsg),
})
}
func (s service) notifySyncError(apiKeyName string, errMsg string) {
s.notificationService.Send(domain.NotificationEventSyncError, domain.NotificationPayload{
Subject: "Error During Sync",
Message: fmt.Sprintf("An error occurred during synchronization with Tachiyomi for user **%s**. Error: %s", apiKeyName, errMsg),
})
}