2022-01-06 09:00:24 +01:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/k3a/html2text"
|
2023-03-29 00:09:06 +02:00
|
|
|
"github.com/zitadel/logging"
|
2022-01-06 09:00:24 +01:00
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/notification/channels"
|
|
|
|
"github.com/zitadel/zitadel/internal/notification/messages"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-01-06 09:00:24 +01:00
|
|
|
)
|
|
|
|
|
2023-03-16 18:24:30 +01:00
|
|
|
func InitFSChannel(config Config) (channels.NotificationChannel, error) {
|
2022-04-25 11:16:36 +02:00
|
|
|
if err := os.MkdirAll(config.Path, os.ModePerm); err != nil {
|
2022-01-06 09:00:24 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-25 11:16:36 +02:00
|
|
|
logging.Debug("successfully initialized filesystem email and sms channel")
|
2022-01-06 09:00:24 +01:00
|
|
|
|
|
|
|
return channels.HandleMessageFunc(func(message channels.Message) error {
|
|
|
|
|
|
|
|
fileName := fmt.Sprintf("%d_", time.Now().Unix())
|
2023-03-29 00:09:06 +02:00
|
|
|
content, err := message.GetContent()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-06 09:00:24 +01:00
|
|
|
switch msg := message.(type) {
|
|
|
|
case *messages.Email:
|
|
|
|
recipients := make([]string, len(msg.Recipients))
|
|
|
|
copy(recipients, msg.Recipients)
|
|
|
|
sort.Strings(recipients)
|
|
|
|
fileName = fileName + "mail_to_" + strings.Join(recipients, "_") + ".html"
|
|
|
|
if config.Compact {
|
|
|
|
content = html2text.HTML2Text(content)
|
|
|
|
}
|
|
|
|
case *messages.SMS:
|
|
|
|
fileName = fileName + "sms_to_" + msg.RecipientPhoneNumber + ".txt"
|
2023-03-29 00:09:06 +02:00
|
|
|
case *messages.JSON:
|
|
|
|
fileName = "message.json"
|
2022-01-06 09:00:24 +01:00
|
|
|
default:
|
2023-12-08 16:30:55 +02:00
|
|
|
return zerrors.ThrowUnimplementedf(nil, "NOTIF-6f9a1", "filesystem provider doesn't support message type %T", message)
|
2022-01-06 09:00:24 +01:00
|
|
|
}
|
|
|
|
|
2023-03-29 00:09:06 +02:00
|
|
|
return os.WriteFile(filepath.Join(config.Path, fileName), []byte(content), 0666)
|
2022-01-06 09:00:24 +01:00
|
|
|
}), nil
|
|
|
|
}
|