-
Notifications
You must be signed in to change notification settings - Fork 0
/
broadcast.go
284 lines (245 loc) · 8.23 KB
/
broadcast.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
. "backend/common"
"bytes"
"context"
_ "embed"
"encoding/json"
"errors"
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/go-playground/validator/v10"
"github.com/mailerlite/mailerlite-go"
"html"
"log"
"math"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"text/template"
"time"
)
//go:embed newsletter.html
var newsletterFile string
func main() {
lambda.Start(handleBroadcast)
}
// handleBroadcast creates a campaign and schedules to be sent.
func handleBroadcast(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
origin := req.Headers["origin"]
if !InitSuccess() {
return GenerateResponse(origin, http.StatusInternalServerError, "Something went wrong"), nil
}
status, msg := processBroadcastRequest(ctx, req.Body)
return GenerateResponse(origin, status, msg), nil
}
// processBroadcastRequest schedules an email campaign about an hour form now.
// For Ghost not to retry the request because of short timeout, update webhook timeout. See https://forum.ghost.org/t/webhook-getting-triggered-multiple-times/16503/3
func processBroadcastRequest(ctx context.Context, body string) (int, string) {
var reqData lambdaReqBody
err := json.Unmarshal([]byte(body), &reqData)
if err != nil {
return http.StatusBadRequest, fmt.Sprintf("invalid request body")
}
// Validate the request body
validate := validator.New()
err = validate.Struct(reqData)
if err != nil {
return http.StatusBadRequest, err.Error()
}
// 1. To prevent sending notification for drafts, confirm this post is published.
// 2. To prevent sending emails for unpublished and republished posts, ensure
// that the diff b/w publication and updated dates is not more than 30 minutes.
if !reqData.canBroadcast() {
return http.StatusBadRequest, fmt.Sprintf(
"this post is too old to be rescheduled. It was created at %v and updated at %v ",
reqData.Post.Current.PublishedAt,
reqData.Post.Current.UpdatedAt,
)
}
post := reqData.toPost()
content, err := parseEmailTemplate(post)
if err != nil {
log.Println("parse template error: ", err)
return http.StatusInternalServerError, "something went wrong"
}
campaignId, err := createCampaign(ctx, post, content)
if err != nil {
log.Println("campaign creation error: ", err)
return http.StatusBadGateway, "something went wrong while creating campaign"
}
err = scheduleCampaign(ctx, campaignId)
if err != nil {
log.Println("campaign scheduling error: ", err)
return http.StatusBadGateway, "something went wrong while scheduling campaign"
}
return http.StatusOK, "Successfully scheduled"
}
func scheduleCampaign(ctx context.Context, campaignId string) error {
timezone := AppConfig.TimeZone
when := time.Now().Add(time.Hour)
list, _, err := MailClient.Timezone.List(ctx)
if err != nil {
return err
}
var timezoneId int
for _, tz := range list.Data {
if tz.Name == timezone {
timezoneId, err = strconv.Atoi(tz.Id)
if err != nil {
return err
}
break
}
}
if timezoneId == 0 {
return fmt.Errorf("no valid timezone id found for %v. OS timezone: %v", timezone, os.Getenv("TZ"))
}
schedule := &mailerlite.ScheduleCampaign{
Delivery: mailerlite.CampaignScheduleTypeScheduled,
Schedule: &mailerlite.Schedule{
Date: when.Format("2006-01-02"),
Hours: fmt.Sprintf("%2d", when.Hour()),
Minutes: fmt.Sprintf("%2d", when.Minute()),
TimezoneID: timezoneId,
},
}
_, _, err = MailClient.Campaign.Schedule(ctx, campaignId, schedule)
if err != nil {
return err
}
return nil
}
func createCampaign(ctx context.Context, post Post, content string) (string, error) {
allSegments, _, err := MailClient.Segment.List(ctx, &mailerlite.ListSegmentOptions{})
if err != nil {
return "", err
}
supportedSegments := []string{
fmt.Sprintf("%v: %v", Blog, Photography),
fmt.Sprintf("%v: %v", Blog, Programming),
}
// Get the first Segment that matches any of the supported segments
var segment string
for _, s := range allSegments.Data {
for _, tag := range supportedSegments {
if strings.EqualFold(s.Name, tag) {
segment = s.ID
}
}
}
if segment == "" {
return "", errors.New("won't send campaigns for non-(programming or photography) articles")
}
sender := AppConfig.EmailSender
emails := &[]mailerlite.Emails{
{
Subject: post.Title,
FromName: post.Author,
From: sender,
Content: content,
},
}
campaign := &mailerlite.CreateCampaign{
Name: fmt.Sprintf("New Publication: %v", post.Title),
Type: mailerlite.CampaignTypeRegular,
Emails: *emails,
Segments: []string{segment},
}
c, _, err := MailClient.Campaign.Create(ctx, campaign)
if err != nil {
return "", err
}
return c.Data.ID, nil
}
func parseEmailTemplate(post Post) (string, error) {
var emailContent bytes.Buffer
t, err := template.New("newsletter").Parse(newsletterFile)
if err != nil {
return "", err
}
err = t.Execute(&emailContent, post)
if err != nil {
return "", err
}
return emailContent.String(), nil
}
func (l lambdaReqBody) canBroadcast() bool {
// 1. To prevent sending notification for drafts, confirm this post is published.
// 2. To prevent sending emails for a posts that are unpublished and then republished, ensure
// that the diff b/w publication and updated dates is not more than 30 minutes.
postData := l.Post.Current
return postData.Status == "published" && math.Abs(postData.PublishedAt.Sub(postData.UpdatedAt).Minutes()) <= 30
}
func (l lambdaReqBody) toPost() Post {
p := l.Post.Current
featureImage := p.FeatureImage
// Check if this post is a reference to an external article and retrieve the feature image.
doc, err := goquery.NewDocumentFromReader(strings.NewReader(p.HTML))
if err == nil {
bookmark := doc.Find("figure.kg-bookmark-card")
// A post which is just a reference to an external article
// will contain nothing but the bookmark card and the reading time.
if bookmark.Length() > 0 && bookmark.Children().Length() != 2 {
// Only set the feature image if this post didn't have one
if featureImage == "" {
img := doc.Find("div.kg-bookmark-thumbnail img")
if img.Length() > 0 {
featureImage, _ = img.Attr("src")
}
}
}
}
// Remove html tags from the caption
cleanedCaption := regexp.MustCompile("<[^>]+>").ReplaceAllString(p.FeatureImageCaption, "")
featureImageCaption := html.UnescapeString(cleanedCaption)
return Post{
Author: p.PrimaryAuthor.Name,
Title: p.Title,
PubDate: p.PublishedAt.Format("02 Jan 2006"),
FeatureImage: featureImage,
FeatureImageCaption: featureImageCaption,
Excerpt: p.Excerpt,
URL: p.URL,
Tag: p.PrimaryTag.Slug,
}
}
type Post struct {
Author string
Title string
PubDate string
FeatureImage string
FeatureImageCaption string
Excerpt string
URL string
Tag string
}
type lambdaReqBody struct {
Post struct {
Current struct {
Excerpt string `json:"excerpt" validate:"required"`
FeatureImage string `json:"feature_image" validate:"http_url"`
FeatureImageCaption string `json:"feature_image_caption" validate:"required"`
ID string `json:"id" validate:"required"`
PublishedAt time.Time `json:"published_at" validate:"required"`
ReadingTime int64 `json:"reading_time" validate:"required"`
Status string `json:"status" validate:"required"`
Title string `json:"title" validate:"required"`
UpdatedAt time.Time `json:"updated_at" validate:"required"`
URL string `json:"url" validate:"http_url"`
Visibility string `json:"visibility" validate:"required"`
HTML string `json:"html" validate:"required"`
PrimaryAuthor struct {
Name string `json:"name" validate:"required"`
} `json:"primary_author" validate:"required"`
PrimaryTag struct {
Name string `json:"name" validate:"required"`
Slug string `json:"slug" validate:"required"`
} `json:"primary_tag" validate:"required"`
} `json:"current" validate:"required"`
} `json:"post" validate:"required"`
}