Skip to content

Commit

Permalink
Minor formatting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Mar 8, 2020
1 parent 709668d commit 40ae9cd
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 86 deletions.
4 changes: 2 additions & 2 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func initImporter(app *App) *subimporter.Importer {
}

// initMessengers initializes various messaging backends.
func initMessengers(r *manager.Manager) messenger.Messenger {
func initMessengers(m *manager.Manager) messenger.Messenger {
// Load SMTP configurations for the default e-mail Messenger.
var (
mapKeys = ko.MapKeys("smtp")
Expand Down Expand Up @@ -215,7 +215,7 @@ func initMessengers(r *manager.Manager) messenger.Messenger {
if err != nil {
lo.Fatalf("error loading e-mail messenger: %v", err)
}
if err := r.AddMessenger(msgr); err != nil {
if err := m.AddMessenger(msgr); err != nil {
lo.Printf("error registering messenger %s", err)
}

Expand Down
79 changes: 37 additions & 42 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ func (m *Manager) AddMessenger(msg messenger.Messenger) error {
return fmt.Errorf("messenger '%s' is already loaded", id)
}
m.messengers[id] = msg

return nil
}

Expand All @@ -132,7 +131,6 @@ func (m *Manager) GetMessengerNames() []string {
for n := range m.messengers {
names = append(names, n)
}

return names
}

Expand Down Expand Up @@ -196,9 +194,7 @@ func (m *Manager) Run(tick time.Duration) {
delete(m.msgErrorCounts, e.camp.ID)

// Notify admins.
m.sendNotif(e.camp,
models.CampaignStatusPaused,
"Too many errors")
m.sendNotif(e.camp, models.CampaignStatusPaused, "Too many errors")
}
}
}
Expand Down Expand Up @@ -256,6 +252,34 @@ func (m *Manager) SpawnWorkers() {
}
}

// TemplateFuncs returns the template functions to be applied into
// compiled campaign templates.
func (m *Manager) TemplateFuncs(c *models.Campaign) template.FuncMap {
return template.FuncMap{
"TrackLink": func(url string, msg *Message) string {
return m.trackLink(url, msg.Campaign.UUID, msg.Subscriber.UUID)
},
"TrackView": func(msg *Message) template.HTML {
return template.HTML(fmt.Sprintf(`<img src="%s" alt="" />`,
fmt.Sprintf(m.cfg.ViewTrackURL, msg.Campaign.UUID, msg.Subscriber.UUID)))
},
"UnsubscribeURL": func(msg *Message) string {
return msg.unsubURL
},
"OptinURL": func(msg *Message) string {
// Add list IDs.
// TODO: Show private lists list on optin e-mail
return fmt.Sprintf(m.cfg.OptinURL, msg.Subscriber.UUID, "")
},
"Date": func(layout string) string {
if layout == "" {
layout = time.ANSIC
}
return time.Now().Format(layout)
},
}
}

// addCampaign adds a campaign to the process queue.
func (m *Manager) addCampaign(c *models.Campaign) error {
// Validate messenger.
Expand All @@ -281,7 +305,6 @@ func (m *Manager) getPendingCampaignIDs() []int64 {
for _, c := range m.camps {
ids = append(ids, int64(c.ID))
}

return ids
}

Expand Down Expand Up @@ -358,17 +381,6 @@ func (m *Manager) exhaustCampaign(c *models.Campaign, status string) (*models.Ca
return cm, nil
}

// Render takes a Message, executes its pre-compiled Campaign.Tpl
// and applies the resultant bytes to Message.body to be used in messages.
func (m *Message) Render() error {
out := bytes.Buffer{}
if err := m.Campaign.Tpl.ExecuteTemplate(&out, models.BaseTpl, m); err != nil {
return err
}
m.Body = out.Bytes()
return nil
}

// trackLink register a URL and return its UUID to be used in message templates
// for tracking links.
func (m *Manager) trackLink(url, campUUID, subUUID string) string {
Expand Down Expand Up @@ -412,30 +424,13 @@ func (m *Manager) sendNotif(c *models.Campaign, status, reason string) error {
return m.notifCB(subject, data)
}

// TemplateFuncs returns the template functions to be applied into
// compiled campaign templates.
func (m *Manager) TemplateFuncs(c *models.Campaign) template.FuncMap {
return template.FuncMap{
"TrackLink": func(url string, msg *Message) string {
return m.trackLink(url, msg.Campaign.UUID, msg.Subscriber.UUID)
},
"TrackView": func(msg *Message) template.HTML {
return template.HTML(fmt.Sprintf(`<img src="%s" alt="" />`,
fmt.Sprintf(m.cfg.ViewTrackURL, msg.Campaign.UUID, msg.Subscriber.UUID)))
},
"UnsubscribeURL": func(msg *Message) string {
return msg.unsubURL
},
"OptinURL": func(msg *Message) string {
// Add list IDs.
// TODO: Show private lists list on optin e-mail
return fmt.Sprintf(m.cfg.OptinURL, msg.Subscriber.UUID, "")
},
"Date": func(layout string) string {
if layout == "" {
layout = time.ANSIC
}
return time.Now().Format(layout)
},
// Render takes a Message, executes its pre-compiled Campaign.Tpl
// and applies the resultant bytes to Message.body to be used in messages.
func (m *Message) Render() error {
out := bytes.Buffer{}
if err := m.Campaign.Tpl.ExecuteTemplate(&out, models.BaseTpl, m); err != nil {
return err
}
m.Body = out.Bytes()
return nil
}
14 changes: 5 additions & 9 deletions internal/subimporter/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ import (
"strings"
"sync"

"github.com/gofrs/uuid"
"github.com/lib/pq"

"github.com/asaskevich/govalidator"
"github.com/gofrs/uuid"
"github.com/knadh/listmonk/models"
"github.com/lib/pq"
)

const (
Expand All @@ -34,7 +33,10 @@ const (

// commitBatchSize is the number of inserts to commit in a single SQL transaction.
commitBatchSize = 10000
)

// Various import statuses.
const (
StatusNone = "none"
StatusImporting = "importing"
StatusStopping = "stopping"
Expand Down Expand Up @@ -113,7 +115,6 @@ func New(upsert *sql.Stmt, blacklist *sql.Stmt, updateListDate *sql.Stmt,
notifCB: notifCB,
status: Status{Status: StatusNone, logBuf: bytes.NewBuffer(nil)},
}

return &im
}

Expand Down Expand Up @@ -162,7 +163,6 @@ func (im *Importer) GetLogs() []byte {
if im.status.logBuf == nil {
return []byte{}
}

return im.status.logBuf.Bytes()
}

Expand Down Expand Up @@ -213,7 +213,6 @@ func (im *Importer) sendNotif(status string) error {
strings.Title(status),
s.Name)
)

return im.notifCB(subject, out)
}

Expand Down Expand Up @@ -526,7 +525,6 @@ func (s *Session) LoadCSV(srcPath string, delim rune) error {

close(s.subQueue)
failed = false

return nil
}

Expand Down Expand Up @@ -558,7 +556,6 @@ func (s *Session) mapCSVHeaders(csvHdrs []string, knownHdrs map[string]bool) map
s.log.Printf("ignoring unknown header '%s'", h)
continue
}

hdrKeys[h] = i
}

Expand Down Expand Up @@ -601,5 +598,4 @@ func countLines(r io.Reader) (int, error) {
return count, err
}
}

}
28 changes: 28 additions & 0 deletions media.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package main

import (
"bytes"
"fmt"
"mime/multipart"
"net/http"
"strconv"

"github.com/disintegration/imaging"
"github.com/gofrs/uuid"
"github.com/knadh/listmonk/internal/media"
"github.com/labstack/echo"
Expand Down Expand Up @@ -146,3 +149,28 @@ func handleDeleteMedia(c echo.Context) error {
app.media.Delete(thumbPrefix + m.Filename)
return c.JSON(http.StatusOK, okResp{true})
}

// createThumbnail reads the file object and returns a smaller image
func createThumbnail(file *multipart.FileHeader) (*bytes.Reader, error) {
src, err := file.Open()
if err != nil {
return nil, err
}
defer src.Close()

img, err := imaging.Decode(src)
if err != nil {
return nil, echo.NewHTTPError(http.StatusInternalServerError,
fmt.Sprintf("Error decoding image: %v", err))
}

// Encode the image into a byte slice as PNG.
var (
thumb = imaging.Resize(img, thumbnailSize, 0, imaging.Lanczos)
out bytes.Buffer
)
if err := imaging.Encode(&out, thumb, imaging.PNG); err != nil {
return nil, err
}
return bytes.NewReader(out.Bytes()), nil
}
34 changes: 1 addition & 33 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@ import (
"bytes"
"crypto/rand"
"fmt"
"log"
"mime/multipart"
"net/http"
"regexp"
"strconv"
"strings"

"github.com/disintegration/imaging"
"github.com/labstack/echo"
"github.com/lib/pq"
)

Expand Down Expand Up @@ -52,28 +47,6 @@ func generateFileName(fName string) string {
return name
}

// createThumbnail reads the file object and returns a smaller image
func createThumbnail(file *multipart.FileHeader) (*bytes.Reader, error) {
src, err := file.Open()
if err != nil {
return nil, err
}
defer src.Close()
img, err := imaging.Decode(src)
if err != nil {
return nil, echo.NewHTTPError(http.StatusInternalServerError,
fmt.Sprintf("Error decoding image: %v", err))
}
t := imaging.Resize(img, thumbnailSize, 0, imaging.Lanczos)
// Encode the image into a byte slice as PNG.
var buf bytes.Buffer
err = imaging.Encode(&buf, t, imaging.PNG)
if err != nil {
log.Fatal(err)
}
return bytes.NewReader(buf.Bytes()), nil
}

// Given an error, pqErrMsg will try to return pq error details
// if it's a pq error.
func pqErrMsg(err error) string {
Expand All @@ -82,7 +55,6 @@ func pqErrMsg(err error) string {
return fmt.Sprintf("%s. %s", err, err.Detail)
}
}

return err.Error()
}

Expand All @@ -103,7 +75,6 @@ func normalizeTags(tags []string) []string {
out = append(out, string(rep))
}
}

return out
}

Expand All @@ -118,7 +89,6 @@ func makeMsgTpl(pageTitle, heading, msg string) msgTpl {
err.Title = pageTitle
err.MessageTitle = heading
err.Message = msg

return err
}

Expand All @@ -127,7 +97,6 @@ func makeMsgTpl(pageTitle, heading, msg string) msgTpl {
// resultant values.
func parseStringIDs(s []string) ([]int64, error) {
vals := make([]int64, 0, len(s))

for _, v := range s {
i, err := strconv.ParseInt(v, 10, 64)
if err != nil {
Expand All @@ -147,12 +116,11 @@ func parseStringIDs(s []string) ([]int64, error) {
// generateRandomString generates a cryptographically random, alphanumeric string of length n.
func generateRandomString(n int) (string, error) {
const dictionary = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

var bytes = make([]byte, n)

if _, err := rand.Read(bytes); err != nil {
return "", err
}

for k, v := range bytes {
bytes[k] = dictionary[v%byte(len(dictionary))]
}
Expand Down

0 comments on commit 40ae9cd

Please sign in to comment.