forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail-digest.go
170 lines (152 loc) · 5 KB
/
mail-digest.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
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package actions
import (
"context"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/errors"
"go.uber.org/zap"
activity2 "github.com/pydio/cells/broker/activity"
"github.com/pydio/cells/broker/activity/render"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/auth"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/activity"
"github.com/pydio/cells/common/proto/idm"
"github.com/pydio/cells/common/proto/jobs"
"github.com/pydio/cells/common/proto/mailer"
"github.com/pydio/cells/common/utils"
"github.com/pydio/cells/scheduler/actions"
)
const (
digestActionName = "broker.activity.actions.mail-digest"
)
type MailDigestAction struct {
mailerClient mailer.MailerServiceClient
activityClient activity.ActivityServiceClient
userClient idm.UserServiceClient
dryRun bool
dryMail string
}
// GetName returns the Unique Identifier of the MailDigestAction.
func (m *MailDigestAction) GetName() string {
return digestActionName
}
// Init passes parameters to a newly created instance.
func (m *MailDigestAction) Init(job *jobs.Job, cl client.Client, action *jobs.Action) error {
if dR, ok := action.Parameters["dryRun"]; ok && dR == "true" {
m.dryRun = true
}
if email, ok := action.Parameters["dryMail"]; ok && email != "" {
m.dryMail = email
}
m.mailerClient = mailer.NewMailerServiceClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_MAILER, cl)
m.activityClient = activity.NewActivityServiceClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_ACTIVITY, cl)
m.userClient = idm.NewUserServiceClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_USER, cl)
return nil
}
// Run processes the actual action code
func (m *MailDigestAction) Run(ctx context.Context, channels *actions.RunnableChannels, input jobs.ActionMessage) (jobs.ActionMessage, error) {
if len(input.Users) == 0 {
e := errors.BadRequest(digestActionName, "action should be triggered with one user in input")
return input.WithError(e), e
}
userObject := input.Users[0]
ctx = auth.WithImpersonate(ctx, input.Users[0])
var email, displayName string
var has bool
if email, has = userObject.Attributes["email"]; !has {
// Ignoring as the user has no email address set up
return input.WithIgnore(), nil
}
if displayName, has = userObject.Attributes["displayName"]; !has {
displayName = userObject.Login
}
lang := utils.UserLanguage(ctx, userObject)
query := &activity.StreamActivitiesRequest{
Context: activity.StreamContext_USER_ID,
ContextData: userObject.Login,
BoxName: "inbox",
AsDigest: true,
}
streamer, e := m.activityClient.StreamActivities(ctx, query)
if e != nil {
output := input.WithError(e)
return output, e
}
defer streamer.Close()
var collection []*activity.Object
for {
resp, e := streamer.Recv()
if e != nil {
break
}
if resp == nil {
continue
}
collection = append(collection, resp.Activity)
}
if len(collection) == 0 {
input.AppendOutput(&jobs.ActionOutput{
Ignored: true,
StringBody: "No activities to send",
})
return input, nil
}
digest, err := activity2.Digest(ctx, collection)
if err != nil {
return input.WithError(err), err
}
user := &mailer.User{
Uuid: userObject.Uuid,
Address: email,
Name: displayName,
}
if m.dryRun && m.dryMail != "" {
user.Address = m.dryMail
}
_, err = m.mailerClient.SendMail(ctx, &mailer.SendMailRequest{
Mail: &mailer.Mail{
TemplateId: "Digest",
ContentMarkdown: render.Markdown(digest, activity.SummaryPointOfView_GENERIC, lang),
To: []*mailer.User{user},
},
})
if err != nil {
log.Logger(ctx).Error("could not send digest email", zap.Error(err))
return input.WithError(err), err
}
input.AppendOutput(&jobs.ActionOutput{
Success: true,
StringBody: "Daily Digest sent to user " + userObject.Uuid,
})
if len(collection) > 0 && !m.dryRun {
lastActivity := collection[0] // Activities are in reverse order, the first one is the last id
_, err := m.activityClient.SetUserLastActivity(ctx, &activity.UserLastActivityRequest{
ActivityId: lastActivity.Id,
UserId: userObject.Login,
BoxName: "lastsent",
})
if err != nil {
return input.WithError(err), err
}
}
return input, nil
}