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

This commit is contained in:
Valentin Kolb 2024-06-21 15:55:34 +02:00
parent 294fc8ac3d
commit c2f5680dff
1 changed files with 60 additions and 54 deletions

View File

@ -1,7 +1,6 @@
package messages
import (
"fmt"
"git.stuve.uni-ulm.de/stuve-it/stuve-it-backend/logger"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase"
@ -72,6 +71,65 @@ func sendEmailNotification(app *pocketbase.PocketBase, registry *template.Regist
}
}
func messageNotifier(app *pocketbase.PocketBase, createdMessageRecord *models.Record) {
registry := template.NewRegistry()
// 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 {
// return new error with all errors
logger.LogErrorF("Error expanding created message record: %v", errs)
return
}
recipients := createdMessageRecord.ExpandedAll("recipients")
for _, recipient := range recipients {
sendEmailNotification(app, registry, recipient, createdMessageRecord)
}
// get eventList entries to notify and event admins
eventList := createdMessageRecord.ExpandedOne("eventList")
if eventList != nil {
// get all eventListSlotEntries for the eventList
listEntryRecords, err := app.Dao().FindRecordsByFilter(
"eventListSlotEntries",
"eventListsSlot.eventList.id={:listId}",
"",
-1,
0,
dbx.Params{"listId": eventList.GetId()},
)
if err != nil {
logger.LogErrorF("Error getting eventListSlotEntries for EmailNotifier: %v", err)
}
// send messages to all eventListSlotEntries
for _, record := range listEntryRecords {
// expand the createdMessageRecord to get the user
if errs := app.Dao().ExpandRecord(record, []string{"user"}, nil); len(errs) > 0 {
logger.LogErrorF("Error expanding user for EmailNotifier: %v", errs)
}
sendEmailNotification(app, registry, record.ExpandedOne("user"), createdMessageRecord)
}
// get event and eventAdmins
if errs := app.Dao().ExpandRecord(eventList, []string{"event"}, nil); len(errs) > 0 {
logger.LogErrorF("Error expanding event for EmailNotifier: %v", errs)
}
event := eventList.ExpandedOne("event")
if errs := app.Dao().ExpandRecord(event, []string{"eventAdmins"}, nil); len(errs) > 0 {
logger.LogErrorF("Error expanding eventAdmins for EmailNotifier: %v", errs)
}
eventAdmins := event.ExpandedAll("eventAdmins")
// send messages to all eventAdmins
for _, record := range eventAdmins {
sendEmailNotification(app, registry, record, createdMessageRecord)
}
}
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
@ -79,8 +137,6 @@ func InitMessages(app *pocketbase.PocketBase, e *core.ServeEvent) error {
logger.LogInfoF("Adding messages email notifier")
registry := template.NewRegistry()
app.OnModelAfterCreate("messages").Add(func(e *core.ModelEvent) error {
// get created message record
@ -89,57 +145,7 @@ func InitMessages(app *pocketbase.PocketBase, e *core.ServeEvent) error {
return err
}
// 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 {
// return new error with all errors
return fmt.Errorf("error expanding created message record: %v", errs)
}
recipients := createdMessageRecord.ExpandedAll("recipients")
for _, recipient := range recipients {
sendEmailNotification(app, registry, recipient, createdMessageRecord)
}
// get eventList entries to notify and event admins
eventList := createdMessageRecord.ExpandedOne("eventList")
if eventList != nil {
// get all eventListSlotEntries for the eventList
listEntryRecords, err := app.Dao().FindRecordsByFilter(
"eventListSlotEntries",
"eventListsSlot.eventList.id={:listId}",
"",
-1,
0,
dbx.Params{"listId": eventList.GetId()},
)
if err != nil {
logger.LogErrorF("Error getting eventListSlotEntries for EmailNotifier: %v", err)
}
// send messages to all eventListSlotEntries
for _, record := range listEntryRecords {
// expand the createdMessageRecord to get the user
if errs := app.Dao().ExpandRecord(record, []string{"user"}, nil); len(errs) > 0 {
logger.LogErrorF("Error expanding user for EmailNotifier: %v", errs)
}
sendEmailNotification(app, registry, record.ExpandedOne("user"), createdMessageRecord)
}
// get event and eventAdmins
if errs := app.Dao().ExpandRecord(eventList, []string{"event"}, nil); len(errs) > 0 {
logger.LogErrorF("Error expanding event for EmailNotifier: %v", errs)
}
event := eventList.ExpandedOne("event")
if errs := app.Dao().ExpandRecord(event, []string{"eventAdmins"}, nil); len(errs) > 0 {
logger.LogErrorF("Error expanding eventAdmins for EmailNotifier: %v", errs)
}
eventAdmins := event.ExpandedAll("eventAdmins")
// send messages to all eventAdmins
for _, record := range eventAdmins {
sendEmailNotification(app, registry, record, createdMessageRecord)
}
}
go messageNotifier(app, createdMessageRecord)
return nil
})