/
events.go
127 lines (116 loc) · 3.58 KB
/
events.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
// Copyright (c) 2022 Proton AG
//
// This file is part of Proton Mail Bridge.Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
package fakeapi
import (
"context"
"github.com/ProtonMail/proton-bridge/v2/pkg/pmapi"
)
func (api *FakePMAPI) GetEvent(_ context.Context, eventID string) (*pmapi.Event, error) {
if err := api.checkAndRecordCall(GET, "/events/"+eventID, nil); err != nil {
return nil, err
}
// Request for empty ID returns the latest event.
if eventID == "" {
if len(api.events) == 0 {
return &pmapi.Event{EventID: "first-event-id"}, nil
}
return api.events[len(api.events)-1], nil
}
// Otherwise it tries to find specific ID and return all next events merged into one.
var foundEvent *pmapi.Event
mergedEvent := &pmapi.Event{}
for _, event := range api.events {
if event.EventID == eventID {
foundEvent = event
continue
}
if foundEvent != nil {
mergedEvent.EventID = event.EventID
mergedEvent.Refresh |= event.Refresh
mergedEvent.Messages = append(mergedEvent.Messages, event.Messages...)
mergedEvent.MessageCounts = append(mergedEvent.MessageCounts, event.MessageCounts...)
mergedEvent.Labels = append(mergedEvent.Labels, event.Labels...)
mergedEvent.Addresses = append(mergedEvent.Addresses, event.Addresses...)
mergedEvent.Notices = append(mergedEvent.Notices, event.Notices...)
mergedEvent.User = event.User
}
}
// If there isn't next event, return the same one.
if mergedEvent.EventID == "" {
return foundEvent, nil
}
return mergedEvent, nil
}
func (api *FakePMAPI) addEventLabel(action pmapi.EventAction, label *pmapi.Label) {
api.addEvent(&pmapi.Event{
EventID: api.eventIDGenerator.next("event"),
Labels: []*pmapi.EventLabel{{
EventItem: pmapi.EventItem{
ID: label.ID,
Action: action,
},
Label: label,
}},
})
}
func (api *FakePMAPI) addEventMessage(action pmapi.EventAction, message *pmapi.Message) {
created := message
updated := &pmapi.EventMessageUpdated{
ID: message.ID,
Subject: &message.Subject,
Unread: &message.Unread,
Flags: &message.Flags,
Sender: message.Sender,
ToList: &message.ToList,
CCList: &message.CCList,
BCCList: &message.BCCList,
Time: message.Time,
LabelIDs: message.LabelIDs,
}
if action == pmapi.EventCreate {
updated = nil
} else {
created = nil
}
api.addEvent(&pmapi.Event{
EventID: api.eventIDGenerator.next("event"),
Messages: []*pmapi.EventMessage{{
EventItem: pmapi.EventItem{
ID: message.ID,
Action: action,
},
Created: created,
Updated: updated,
}},
MessageCounts: api.getAllCounts(),
})
}
func (api *FakePMAPI) addEventAddress(action pmapi.EventAction, address *pmapi.Address) {
api.addEvent(&pmapi.Event{
EventID: api.eventIDGenerator.next("event"),
Addresses: []*pmapi.EventAddress{{
EventItem: pmapi.EventItem{
ID: address.ID,
Action: action,
},
Address: address,
}},
})
}
func (api *FakePMAPI) addEvent(event *pmapi.Event) {
api.events = append(api.events, event)
}