2019-11-16 16:06:34 +01:00
|
|
|
package event
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/leandro-lugaresi/hub"
|
2020-07-04 12:54:35 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
2019-11-16 16:06:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Hub = hub.Hub
|
|
|
|
type Data = hub.Fields
|
|
|
|
type Message = hub.Message
|
2019-12-02 00:30:58 +01:00
|
|
|
|
2020-07-19 16:48:04 +02:00
|
|
|
var channelCap = 100
|
2019-11-16 16:06:34 +01:00
|
|
|
var sharedHub = NewHub()
|
|
|
|
|
2019-12-11 14:10:20 +01:00
|
|
|
func NewHub() *Hub {
|
2019-11-16 16:06:34 +01:00
|
|
|
return hub.New()
|
|
|
|
}
|
|
|
|
|
|
|
|
func SharedHub() *Hub {
|
|
|
|
return sharedHub
|
|
|
|
}
|
|
|
|
|
|
|
|
func Error(msg string) {
|
2019-12-02 00:30:58 +01:00
|
|
|
Log.Error(msg)
|
2020-07-07 10:51:55 +02:00
|
|
|
Publish("notify.error", Data{"message": msg})
|
2019-11-16 16:06:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Success(msg string) {
|
2019-12-02 00:30:58 +01:00
|
|
|
Log.Info(msg)
|
2020-07-07 10:51:55 +02:00
|
|
|
Publish("notify.success", Data{"message": msg})
|
2019-11-16 16:06:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Info(msg string) {
|
2019-12-02 00:30:58 +01:00
|
|
|
Log.Info(msg)
|
2020-07-07 10:51:55 +02:00
|
|
|
Publish("notify.info", Data{"message": msg})
|
2019-11-16 16:06:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Warning(msg string) {
|
2019-12-02 00:30:58 +01:00
|
|
|
Log.Warn(msg)
|
2020-07-07 10:51:55 +02:00
|
|
|
Publish("notify.warning", Data{"message": msg})
|
2019-11-16 16:06:34 +01:00
|
|
|
}
|
|
|
|
|
2020-07-04 12:54:35 +02:00
|
|
|
func ErrorMsg(id i18n.Message, params ...interface{}) {
|
|
|
|
Error(i18n.Msg(id, params...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func SuccessMsg(id i18n.Message, params ...interface{}) {
|
|
|
|
Success(i18n.Msg(id, params...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func InfoMsg(id i18n.Message, params ...interface{}) {
|
|
|
|
Info(i18n.Msg(id, params...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func WarningMsg(id i18n.Message, params ...interface{}) {
|
|
|
|
Warning(i18n.Msg(id, params...))
|
|
|
|
}
|
|
|
|
|
2019-12-11 14:10:20 +01:00
|
|
|
func Publish(event string, data Data) {
|
2019-11-16 16:06:34 +01:00
|
|
|
SharedHub().Publish(Message{
|
|
|
|
Name: event,
|
|
|
|
Fields: data,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Subscribe(topics ...string) hub.Subscription {
|
2020-07-19 16:48:04 +02:00
|
|
|
return SharedHub().NonBlockingSubscribe(channelCap, topics...)
|
2019-11-16 16:06:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Unsubscribe(s hub.Subscription) {
|
|
|
|
SharedHub().Unsubscribe(s)
|
|
|
|
}
|