-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.go
155 lines (145 loc) · 4.64 KB
/
logic.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package backend
import (
"fmt"
"github.com/TetAlius/GoSyncMyCalendars/api"
"github.com/TetAlius/GoSyncMyCalendars/customErrors"
log "github.com/TetAlius/GoSyncMyCalendars/logger"
)
func (s *Server) manageSynchronizationOutlook(notifications []api.OutlookSubscriptionNotification) (err error) {
tags := map[string]string{"sync": "outlook"}
for _, subscription := range notifications {
calendar, err := s.retrieveCalendar(subscription.SubscriptionID, tags)
if err != nil {
return err
}
if calendar == nil && err == nil {
return nil
}
calendar.GetAccount().Refresh()
go s.database.UpdateAccount(calendar.GetAccount())
tags["event"] = subscription.ChangeType
if subscription.ChangeType == "Missed" {
err = s.manageByCalendar(calendar, subscription.SubscriptionID, tags)
if err != nil {
return err
}
continue
}
err = s.manageSubscription(calendar, subscription.SubscriptionID, subscription.Data.ID, tags)
if err != nil {
log.Errorf("error managing outlook subscription ID: %s", subscription.SubscriptionID)
return err
}
}
return err
}
func (s *Server) manageByCalendar(calendar api.CalendarManager, subscriptionID string, tags map[string]string) (err error) {
eventIDs := make(map[string]string)
events, err := calendar.GetAllEvents()
if err != nil {
log.Errorf("error getting all events from cloud: %s", err.Error())
s.sentry.CaptureErrorAndWait(err, tags)
return err
}
for _, event := range events {
eventIDs[event.GetID()] = event.GetID()
}
IDs, err := s.database.GetEventIDs(subscriptionID)
if err != nil {
log.Errorf("error getting all events from db: %s", err.Error())
s.sentry.CaptureErrorAndWait(err, tags)
return err
}
for _, eventID := range IDs {
eventIDs[eventID] = eventID
}
for eventID := range eventIDs {
err = s.manageSubscription(calendar, subscriptionID, eventID, tags)
if err != nil {
log.Errorf("error managing subscription ID: %s", subscriptionID)
return err
}
}
return
}
func (s *Server) manageSynchronizationGoogle(subscriptionID string) (err error) {
tags := map[string]string{"sync": "google"}
calendar, err := s.retrieveCalendar(subscriptionID, tags)
if err != nil {
return err
}
if calendar == nil && err == nil {
return nil
}
calendar.GetAccount().Refresh()
go s.database.UpdateAccount(calendar.GetAccount())
return s.manageByCalendar(calendar, subscriptionID, tags)
}
func (s *Server) manageSubscription(calendar api.CalendarManager, subscriptionID string, eventID string, tags map[string]string) (err error) {
onCloud := true
event, err := calendar.GetEvent(eventID)
if _, ok := err.(*customErrors.NotFoundError); ok {
onCloud = false
err = nil
event = calendar.CreateEmptyEvent(eventID)
}
if err != nil {
s.sentry.CaptureErrorAndWait(err, tags)
log.Errorf("error retrieving event from account: %s", err.Error())
return err
}
events, onDB, err := s.database.RetrieveSyncedEventsWithSubscription(eventID, subscriptionID, calendar)
if err != nil {
s.sentry.CaptureErrorAndWait(err, tags)
log.Errorf("error retrieving events synced: %s", err.Error())
return err
}
if !onCloud && !onDB {
log.Warningf("event with id: %s already deleted", eventID)
return nil
}
if onCloud && onDB && s.database.EventAlreadyUpdated(event) {
return nil
}
event.SetRelations(events)
state := api.GetChangeType(onCloud, onDB)
if state == 0 {
err = fmt.Errorf("synchronization not supported for event: %s", eventID)
s.sentry.CaptureErrorAndWait(err, tags)
return err
}
event.SetState(state)
s.worker.Events <- event
return
}
func (s *Server) retrieveCalendar(subscriptionID string, tags map[string]string) (calendar api.CalendarManager, err error) {
ok, err := s.database.ExistsSubscriptionFromID(subscriptionID)
if err != nil && ok {
//Ignore this subscription
//Perhaps something went wrong when deleting subscription...
//s.sentry.CaptureMessageAndWait(fmt.Sprintf("outlook subscription with id: %s is notifying but not on db", subscriptionID), tags)
return nil, nil
} else if err != nil {
//Sentry already got this error
return nil, err
}
calendar, err = s.database.RetrieveCalendarFromSubscription(subscriptionID)
if err != nil {
s.sentry.CaptureErrorAndWait(err, tags)
log.Errorf("error refreshing outlook account")
return nil, err
}
recoveredPanic, sentryID := s.sentry.CapturePanicAndWait(func() {
err = calendar.GetAccount().Refresh()
}, tags)
if recoveredPanic != nil {
log.Errorf("panic recovered with sentry ID: %s", sentryID)
return nil, fmt.Errorf("panic was launched")
}
if err != nil {
s.sentry.CaptureErrorAndWait(err, tags)
log.Errorf("error refreshing outlook account")
return nil, err
}
return calendar, nil
}