-
Notifications
You must be signed in to change notification settings - Fork 184
/
service.go
173 lines (150 loc) · 4.59 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package service
import (
"context"
"errors"
"fmt"
"net/url"
"os"
"os/signal"
"path"
"syscall"
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
group "github.com/cs3org/go-cs3apis/cs3/identity/group/v1beta1"
user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/v2/pkg/events"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
"github.com/owncloud/ocis/v2/services/notifications/pkg/channels"
"github.com/owncloud/ocis/v2/services/notifications/pkg/email"
"google.golang.org/protobuf/types/known/fieldmaskpb"
)
// Service should be named `Runner`
type Service interface {
Run() error
}
// NewEventsNotifier provides a new eventsNotifier
func NewEventsNotifier(
events <-chan events.Event,
channel channels.Channel,
logger log.Logger,
gwClient gateway.GatewayAPIClient,
machineAuthAPIKey, emailTemplatePath, ocisURL string) Service {
return eventsNotifier{
logger: logger,
channel: channel,
events: events,
signals: make(chan os.Signal, 1),
gwClient: gwClient,
machineAuthAPIKey: machineAuthAPIKey,
emailTemplatePath: emailTemplatePath,
ocisURL: ocisURL,
}
}
type eventsNotifier struct {
logger log.Logger
channel channels.Channel
events <-chan events.Event
signals chan os.Signal
gwClient gateway.GatewayAPIClient
machineAuthAPIKey string
emailTemplatePath string
ocisURL string
}
func (s eventsNotifier) Run() error {
signal.Notify(s.signals, syscall.SIGINT, syscall.SIGTERM)
s.logger.Debug().
Msg("eventsNotifier started")
for {
select {
case evt := <-s.events:
go func() {
switch e := evt.Event.(type) {
case events.SpaceShared:
s.handleSpaceShared(e)
case events.SpaceUnshared:
s.handleSpaceUnshared(e)
case events.SpaceMembershipExpired:
s.handleSpaceMembershipExpired(e)
case events.ShareCreated:
s.handleShareCreated(e)
case events.ShareExpired:
s.handleShareExpired(e)
}
}()
case <-s.signals:
s.logger.Debug().
Msg("eventsNotifier stopped")
return nil
}
}
}
func (s eventsNotifier) render(bodyTemplate string, subjTemplate string, values map[string]string) (string, string, error) {
msg, err := email.RenderEmailTemplate(bodyTemplate, values, s.emailTemplatePath)
if err != nil {
return "", "", err
}
sub, err := email.RenderEmailTemplate(subjTemplate, values, s.emailTemplatePath)
if err != nil {
return "", "", err
}
return msg, sub, nil
}
func (s eventsNotifier) send(ctx context.Context, u *user.UserId, g *group.GroupId, msg, subj, sender string) error {
if u != nil {
return s.channel.SendMessage(ctx, []string{u.GetOpaqueId()}, msg, subj, sender)
}
if g != nil {
return s.channel.SendMessageToGroup(ctx, g, msg, subj, sender)
}
return nil
}
func (s eventsNotifier) getGranteeName(ctx context.Context, u *user.UserId, g *group.GroupId) (string, error) {
switch {
case u != nil:
r, err := s.gwClient.GetUser(ctx, &user.GetUserRequest{UserId: u})
if err != nil {
return "", err
}
if r.Status.Code != rpc.Code_CODE_OK {
return "", fmt.Errorf("unexpected status code from gateway client: %d", r.GetStatus().GetCode())
}
return r.GetUser().GetDisplayName(), nil
case g != nil:
r, err := s.gwClient.GetGroup(ctx, &group.GetGroupRequest{GroupId: g})
if err != nil {
return "", err
}
if r.GetStatus().GetCode() != rpc.Code_CODE_OK {
return "", fmt.Errorf("unexpected status code from gateway client: %d", r.GetStatus().GetCode())
}
return r.GetGroup().GetDisplayName(), nil
default:
return "", errors.New("Need at least one non-nil grantee")
}
}
func (s eventsNotifier) getResourceInfo(ctx context.Context, resourceID *provider.ResourceId, fieldmask *fieldmaskpb.FieldMask) (*provider.ResourceInfo, error) {
// TODO: maybe cache this stat to reduce storage iops
md, err := s.gwClient.Stat(ctx, &provider.StatRequest{
Ref: &provider.Reference{
ResourceId: resourceID,
},
FieldMask: fieldmask,
})
if err != nil {
return nil, err
}
if md.Status.Code != rpc.Code_CODE_OK {
return nil, fmt.Errorf("could not resource info: %s", md.Status.Message)
}
return md.GetInfo(), nil
}
// TODO: this function is a backport for go1.19 url.JoinPath, upon go bump, replace this
func urlJoinPath(base string, elements ...string) (string, error) {
u, err := url.Parse(base)
if err != nil {
return "", err
}
u.Path = path.Join(append([]string{u.Path}, elements...)...)
return u.String(), nil
}