@@ -14,9 +14,9 @@ import (
1414 "strings"
1515 "time"
1616
17- "github.com/hectane/hectane/email"
18- "github.com/hectane/hectane/queue"
17+ "github.com/aquilax/truncate"
1918 "github.com/jackc/pgx"
19+ smtp2go "github.com/smtp2go-oss/smtp2go-go"
2020 gfm "github.com/sqlitebrowser/github_flavored_markdown"
2121 "golang.org/x/crypto/bcrypt"
2222)
@@ -2981,18 +2981,10 @@ func SaveDBSettings(userName, dbFolder, dbName, oneLineDesc, fullDesc, defaultTa
29812981
29822982// SendEmails sends status update emails to people watching databases
29832983func SendEmails () {
2984- // Create Hectane email queue
2985- cfg := & queue.Config {
2986- Directory : Conf .Event .EmailQueueDir ,
2987- DisableSSLVerification : true ,
2988- }
2989- q , err := queue .NewQueue (cfg )
2990- if err != nil {
2991- log .Printf ("Couldn't start Hectane queue: %s" , err .Error ())
2984+ // If the SMTP2Go API key hasn't been configured, there's no use in trying to send emails
2985+ if Conf .Event .Smtp2GoKey == "" && os .Getenv ("SMTP2GO_API_KEY" ) == "" {
29922986 return
29932987 }
2994- log .Printf ("Created Hectane email queue in '%s'. Queue processing loop refreshes every %d seconds" ,
2995- Conf .Event .EmailQueueDir , Conf .Event .EmailQueueProcessingDelay )
29962988
29972989 for {
29982990 // Retrieve unsent emails from the email_queue
@@ -3026,22 +3018,22 @@ func SendEmails() {
30263018
30273019 // Send emails
30283020 for _ , j := range emailList {
3029- e := & email.Email {
3030- From : "updates@dbhub.io" ,
3031- To : []string {j .Address },
3032- Subject : j .Subject ,
3033- Text : j .Body ,
3021+ e := smtp2go.Email {
3022+ From : "updates@dbhub.io" ,
3023+ To : []string {j .Address },
3024+ Subject : j .Subject ,
3025+ TextBody : j .Body ,
3026+ HtmlBody : j .Body ,
30343027 }
3035- msgs , err := e . Messages ( q . Storage )
3028+ _ , err = smtp2go . Send ( & e )
30363029 if err != nil {
3037- log .Printf ("Queuing email in Hectane failed: %v" , err .Error ())
3038- return // Abort, as we don't want to continuously resend the same emails
3039- }
3040- for _ , m := range msgs {
3041- q .Deliver (m )
3030+ log .Println (err )
30423031 }
30433032
3044- // Mark message as sent
3033+ log .Printf ("Email with subject '%v' sent to '%v'" ,
3034+ truncate .Truncate (j .Subject , 35 , "..." , truncate .PositionEnd ), j .Address )
3035+
3036+ // We only attempt delivery via smtp2go once (retries are handled on their end), so mark message as sent
30453037 dbQuery := `
30463038 UPDATE email_queue
30473039 SET sent = true, sent_timestamp = now()
@@ -3199,7 +3191,7 @@ func StatusUpdatesLoop() {
31993191 // For each event, add a status update to the status_updates list for each watcher it's for
32003192 for id , ev := range evList {
32013193 // Retrieve the list of watchers for the database the event occurred on
3202- dbQuery : = `
3194+ dbQuery = `
32033195 SELECT user_id
32043196 FROM watchers
32053197 WHERE db_id = $1`
@@ -3228,19 +3220,26 @@ func StatusUpdatesLoop() {
32283220 for _ , u := range users {
32293221 // Retrieve the current status updates list for the user
32303222 var eml pgx.NullString
3231- dbQuery : = `
3223+ dbQuery = `
32323224 SELECT user_name, email, status_updates
32333225 FROM users
32343226 WHERE user_id = $1`
32353227 userEvents := make (map [string ][]StatusUpdateEntry )
32363228 var userName string
3237- err : = tx .QueryRow (dbQuery , u ).Scan (& userName , & eml , & userEvents )
3229+ err = tx .QueryRow (dbQuery , u ).Scan (& userName , & eml , & userEvents )
32383230 if err != nil {
32393231 log .Printf ("Database query failed: %v\n " , err )
32403232 tx .Rollback ()
32413233 continue
32423234 }
32433235
3236+ // If the user generated this event themselves, skip them
3237+ if userName == ev .details .UserName {
3238+ log .Printf ("User '%v' generated this event (id: %v), so not adding it to their event list" ,
3239+ userName , ev .eventID )
3240+ continue
3241+ }
3242+
32443243 // * Add the new event to the users status updates list *
32453244
32463245 // Group the status updates by database, and coalesce multiple updates for the same discussion or MR
@@ -3281,7 +3280,7 @@ func StatusUpdatesLoop() {
32813280 }
32823281 if numRows := commandTag .RowsAffected (); numRows != 1 {
32833282 log .Printf ("Wrong number of rows affected (%v) when adding status update for database ID " +
3284- "'%d' to user '%s '" , numRows , ev .dbID , u )
3283+ "'%d' to user '%v '" , numRows , ev .dbID , u )
32853284 tx .Rollback ()
32863285 continue
32873286 }
@@ -3324,19 +3323,27 @@ func StatusUpdatesLoop() {
33243323 log .Printf ("Unknown message type when creating email message" )
33253324 }
33263325 if eml .Valid {
3327- // TODO: Check if the email is username@thisserver, which indicates a non-functional email address
3326+ // If the email address is of the form username@this_server (which indicates a non-functional email address), then skip it
3327+ serverName := strings .Split (Conf .Web .ServerName , ":" )
3328+ if strings .HasSuffix (eml .String , serverName [0 ]) {
3329+ log .Printf ("Skipping email '%v' to destination '%v', as it ends in '%v'" ,
3330+ truncate .Truncate (subj , 35 , "..." , truncate .PositionEnd ), eml .String , serverName [0 ])
3331+ continue
3332+ }
3333+
3334+ // Add the email to the queue
33283335 dbQuery = `
33293336 INSERT INTO email_queue (mail_to, subject, body)
33303337 VALUES ($1, $2, $3)`
33313338 commandTag , err = tx .Exec (dbQuery , eml .String , subj , msg )
33323339 if err != nil {
3333- log .Printf ("Adding status update to email queue for user '%s ' failed: %v" , u , err )
3340+ log .Printf ("Adding status update to email queue for user '%v ' failed: %v" , u , err )
33343341 tx .Rollback ()
33353342 continue
33363343 }
33373344 if numRows := commandTag .RowsAffected (); numRows != 1 {
33383345 log .Printf ("Wrong number of rows affected (%v) when adding status update to email" +
3339- "queue for user '%s '" , numRows , u )
3346+ "queue for user '%v '" , numRows , u )
33403347 tx .Rollback ()
33413348 continue
33423349 }
0 commit comments