-
Notifications
You must be signed in to change notification settings - Fork 12
/
mail.go
187 lines (154 loc) · 6.4 KB
/
mail.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package routes
import (
"bytes"
"context"
"errors"
"fmt"
"github.com/gin-gonic/gin"
c "github.com/openline-ai/openline-customer-os/packages/server/comms-api/config"
"github.com/openline-ai/openline-customer-os/packages/server/comms-api/model"
"github.com/openline-ai/openline-customer-os/packages/server/comms-api/routes/ContactHub"
s "github.com/openline-ai/openline-customer-os/packages/server/comms-api/service"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/service/security"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/tracing"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/utils"
neo4jenum "github.com/openline-ai/openline-customer-os/packages/server/customer-os-neo4j-repository/enum"
neo4jmapper "github.com/openline-ai/openline-customer-os/packages/server/customer-os-neo4j-repository/mapper"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-neo4j-repository/neo4jutil"
tracingLog "github.com/opentracing/opentracing-go/log"
"image"
"image/color"
"image/png"
"log"
"net/http"
)
func addMailRoutes(conf *c.Config, rg *gin.RouterGroup, services *s.Services, hub *ContactHub.ContactHub) {
//Preload 1px transparent image
px := image.NewRGBA(image.Rect(0, 0, 1, 1))
px.Set(0, 0, color.Transparent)
var spyPixel bytes.Buffer
err := png.Encode(&spyPixel, px)
if err != nil {
log.Printf("unable to encode image: %v", err)
}
var spyPixelBytes = spyPixel.Bytes()
rg.POST("/mail/send", func(c *gin.Context) {
ctx, span := tracing.StartHttpServerTracerSpanWithHeader(context.Background(), "mail/send", c.Request.Header)
defer span.Finish()
var request model.MailReplyRequest
if err := c.BindJSON(&request); err != nil {
tracing.TraceErr(span, err)
c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
return
}
if conf.Mail.ApiKey != c.GetHeader("X-Openline-Mail-Api-Key") {
errorMsg := "invalid mail API Key!"
tracing.TraceErr(span, errors.New(errorMsg))
log.Printf(errorMsg)
c.JSON(http.StatusForbidden, gin.H{"error": errorMsg})
return
}
username := c.GetHeader(security.UsernameHeader)
if username == "" {
c.JSON(http.StatusForbidden, gin.H{})
return
}
userId, tenant, _, err := services.CommonServices.Neo4jRepositories.UserReadRepository.FindUserByEmail(ctx, username)
if err != nil {
tracing.TraceErr(span, err)
errorMsg := fmt.Sprintf("unable to get user by email: %v", err.Error())
log.Printf(errorMsg)
c.JSON(http.StatusInternalServerError, gin.H{"error": errorMsg})
return
}
if userId == "" {
c.JSON(http.StatusForbidden, gin.H{})
return
}
span.LogFields(tracingLog.String("userId", userId))
uniqueInternalIdentifier := utils.GenerateRandomString(64)
request.UniqueInternalIdentifier = &uniqueInternalIdentifier
// Append an image tag pointing to the spy endpoint to the request content
imgTag := "<img id=\"customer-os-email-track-open\" height=1 width=1 src=\"" + conf.Service.PublicPath + "/mail/" + uniqueInternalIdentifier + "/track\" />"
request.Content += imgTag
replyMail, err := services.MailService.SendMail(ctx, &request, &username)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
return
}
mail, err := services.MailService.SaveMail(replyMail, tenant, request.Username, uniqueInternalIdentifier)
if err != nil {
tracing.TraceErr(span, err)
errorMsg := fmt.Sprintf("unable to save email: %v", err.Error())
log.Printf(errorMsg)
c.JSON(http.StatusInternalServerError, gin.H{"error": errorMsg})
return
}
span.LogFields(tracingLog.String("result - interactionEventId", (*mail).InteractionEventCreate.Id))
c.JSON(http.StatusOK, gin.H{
"result": fmt.Sprintf("interaction event created with id: %s", (*mail).InteractionEventCreate.Id),
})
})
rg.GET("/mail/:customerOSInternalIdentifier/track", func(c *gin.Context) {
customerOSInternalIdentifier := c.Param("customerOSInternalIdentifier")
ctx, span := tracing.StartHttpServerTracerSpanWithHeader(context.Background(), "/mail/"+customerOSInternalIdentifier+"/track", c.Request.Header)
defer span.Finish()
if customerOSInternalIdentifier == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing customerOSInternalIdentifier"})
return
}
span.LogFields(tracingLog.String("customerOSInternalIdentifier", customerOSInternalIdentifier))
//log all headers
for name, values := range c.Request.Header {
for _, value := range values {
span.LogFields(tracingLog.String("Header: "+name, value))
}
}
interactionEventNode, err := services.CommonServices.Neo4jRepositories.InteractionEventReadRepository.GetInteractionEventByCustomerOSIdentifier(ctx, customerOSInternalIdentifier)
if err != nil {
tracing.TraceErr(span, err)
errorMsg := fmt.Sprintf("unable to get interaction event: %v", err.Error())
log.Printf(errorMsg)
c.JSON(http.StatusInternalServerError, gin.H{"error": errorMsg})
return
}
if interactionEventNode == nil {
span.LogFields(tracingLog.String("interactionEventId", "not found"))
c.JSON(http.StatusBadRequest, gin.H{})
return
}
interactionEvent := neo4jmapper.MapDbNodeToInteractionEventEntity(interactionEventNode)
span.LogFields(tracingLog.String("interactionEventId", interactionEvent.Id))
tenant := neo4jutil.GetTenantFromLabels(interactionEventNode.Labels, neo4jutil.NodeLabelInteractionEvent)
if tenant == "" {
c.JSON(http.StatusBadRequest, gin.H{})
return
}
metadata, err := utils.ToJson(map[string]interface{}{
"User-Agent": c.GetHeader("User-Agent"),
"Cf-Connecting-Ip": c.GetHeader("Cf-Connecting-Ip"),
})
if err != nil {
tracing.TraceErr(span, err)
c.JSON(http.StatusBadRequest, gin.H{})
return
}
_, err = services.CommonServices.Neo4jRepositories.ActionWriteRepository.Create(ctx, tenant, interactionEvent.Id, neo4jenum.INTERACTION_EVENT, neo4jenum.ActionInteractionEventRead, "", metadata, utils.Now(), "comms-api")
if err != nil {
tracing.TraceErr(span, err)
c.JSON(http.StatusBadRequest, gin.H{})
return
}
c.Data(http.StatusOK, "image/png", spyPixelBytes)
})
}
func validateMailPostRequest(req model.MailFwdRequest) error {
// Add validation checks for other fields in the MailPostRequest struct
if req.Tenant == "" {
return errors.New("missing tenant field")
}
if req.RawMessage == "" {
return errors.New("missing raw message field")
}
return nil
}