feat(messages): send messages in second thread and don't block record creation
Build and Push Docker image / build-and-push (push) Successful in 2m29s
Details
Build and Push Docker image / build-and-push (push) Successful in 2m29s
Details
This commit is contained in:
parent
294fc8ac3d
commit
c2f5680dff
|
@ -1,7 +1,6 @@
|
||||||
package messages
|
package messages
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"git.stuve.uni-ulm.de/stuve-it/stuve-it-backend/logger"
|
"git.stuve.uni-ulm.de/stuve-it/stuve-it-backend/logger"
|
||||||
"github.com/pocketbase/dbx"
|
"github.com/pocketbase/dbx"
|
||||||
"github.com/pocketbase/pocketbase"
|
"github.com/pocketbase/pocketbase"
|
||||||
|
@ -72,27 +71,14 @@ func sendEmailNotification(app *pocketbase.PocketBase, registry *template.Regist
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitMessages initializes the messages email notifier
|
func messageNotifier(app *pocketbase.PocketBase, createdMessageRecord *models.Record) {
|
||||||
//
|
|
||||||
// the function sends an email notification to all recipients of a message after the message has been created
|
|
||||||
func InitMessages(app *pocketbase.PocketBase, e *core.ServeEvent) error {
|
|
||||||
|
|
||||||
logger.LogInfoF("Adding messages email notifier")
|
|
||||||
|
|
||||||
registry := template.NewRegistry()
|
registry := template.NewRegistry()
|
||||||
|
|
||||||
app.OnModelAfterCreate("messages").Add(func(e *core.ModelEvent) error {
|
|
||||||
|
|
||||||
// get created message record
|
|
||||||
createdMessageRecord, err := app.Dao().FindRecordById("messages", e.Model.GetId())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// expand the createdMessageRecord to get recipient user and send email notification if recipient is set
|
// expand the createdMessageRecord to get recipient user and send email notification if recipient is set
|
||||||
if errs := app.Dao().ExpandRecord(createdMessageRecord, []string{"recipients", "eventList"}, nil); len(errs) > 0 {
|
if errs := app.Dao().ExpandRecord(createdMessageRecord, []string{"recipients", "eventList"}, nil); len(errs) > 0 {
|
||||||
// return new error with all errors
|
// return new error with all errors
|
||||||
return fmt.Errorf("error expanding created message record: %v", errs)
|
logger.LogErrorF("Error expanding created message record: %v", errs)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
recipients := createdMessageRecord.ExpandedAll("recipients")
|
recipients := createdMessageRecord.ExpandedAll("recipients")
|
||||||
for _, recipient := range recipients {
|
for _, recipient := range recipients {
|
||||||
|
@ -141,6 +127,26 @@ func InitMessages(app *pocketbase.PocketBase, e *core.ServeEvent) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// InitMessages initializes the messages email notifier
|
||||||
|
//
|
||||||
|
// the function sends an email notification to all recipients of a message after the message has been created
|
||||||
|
func InitMessages(app *pocketbase.PocketBase, e *core.ServeEvent) error {
|
||||||
|
|
||||||
|
logger.LogInfoF("Adding messages email notifier")
|
||||||
|
|
||||||
|
app.OnModelAfterCreate("messages").Add(func(e *core.ModelEvent) error {
|
||||||
|
|
||||||
|
// get created message record
|
||||||
|
createdMessageRecord, err := app.Dao().FindRecordById("messages", e.Model.GetId())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
go messageNotifier(app, createdMessageRecord)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue