Skip to content

Commit

Permalink
feat(notification): add i18n on local go notification
Browse files Browse the repository at this point in the history
Signed-off-by: Sacha Froment <sfroment42@gmail.com>
  • Loading branch information
sfroment committed Jan 10, 2019
1 parent 1e6fb12 commit a102b8b
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 23 deletions.
2 changes: 1 addition & 1 deletion core/cmd/berty/daemon.go
Expand Up @@ -46,7 +46,7 @@ type daemonOptions struct {
func daemonSetupFlags(flags *pflag.FlagSet, opts *daemonOptions) {
flags.StringVar(&opts.nickname, "nickname", "berty-daemon", "set account nickname")
flags.BoolVar(&opts.dropDatabase, "drop-database", false, "drop database to force a reinitialization")
flags.BoolVar(&opts.notification, "notification", false, "enable local notification")
flags.BoolVar(&opts.notification, "notification", true, "enable local notification")
flags.BoolVar(&opts.hideBanner, "hide-banner", false, "hide banner")
flags.BoolVar(&opts.initOnly, "init-only", false, "stop after node initialization (useful for integration tests")
flags.BoolVar(&opts.noP2P, "no-p2p", false, "Disable p2p Driver")
Expand Down
5 changes: 3 additions & 2 deletions core/manager/account/account.go
Expand Up @@ -29,6 +29,7 @@ import (
"berty.tech/core/network/netutil"
"berty.tech/core/node"
"berty.tech/core/pkg/errorcodes"
"berty.tech/core/pkg/i18n"
"berty.tech/core/pkg/notification"
"berty.tech/core/pkg/tracing"
"berty.tech/core/pkg/zapring"
Expand Down Expand Up @@ -499,8 +500,8 @@ func (a *Account) initNode(ctx context.Context) error {
}

a.node.DisplayNotification(notification.Payload{
Title: "Node started",
Body: "Berty daemon is now up and running",
Title: i18n.T("DaemonStartTitle", nil),
Body: i18n.T("DaemonStartBody", nil),
})
return nil
}
Expand Down
21 changes: 13 additions & 8 deletions core/node/event_handlers.go
Expand Up @@ -8,6 +8,7 @@ import (
"berty.tech/core/api/p2p"
"berty.tech/core/entity"
"berty.tech/core/pkg/errorcodes"
"berty.tech/core/pkg/i18n"
"berty.tech/core/pkg/notification"
bsql "berty.tech/core/sql"
"github.com/gofrs/uuid"
Expand Down Expand Up @@ -48,8 +49,10 @@ func (n *Node) handleContactRequest(ctx context.Context, input *p2p.Event) error
}

n.DisplayNotification(notification.Payload{
Title: "Contact request",
Body: attrs.Me.DisplayName + " wants to add you",
Title: i18n.T("ContactRequestTitle", nil),
Body: i18n.T("ContactRequestBody", map[string]interface{}{
"Name": attrs.Me.DisplayName,
}),
})
// nothing more to do, now we wait for the UI to accept the request
return nil
Expand Down Expand Up @@ -79,8 +82,10 @@ func (n *Node) handleContactRequestAccepted(ctx context.Context, input *p2p.Even
}

n.DisplayNotification(notification.Payload{
Title: "Contact request accepted",
Body: contact.DisplayName + " accepted your request",
Title: i18n.T("ContactRequestAccpetedTitle", nil),
Body: i18n.T("ContactRequestAccpetedBody", map[string]interface{}{
"Name": contact.DisplayName,
}),
})
return nil
}
Expand Down Expand Up @@ -139,8 +144,8 @@ func (n *Node) handleConversationInvite(ctx context.Context, input *p2p.Event) e
}

n.DisplayNotification(notification.Payload{
Title: "Conversation invite",
Body: "You have been invited to a new conversation",
Title: i18n.T("ConversationInviteTitle", nil),
Body: i18n.T("ConversationInviteBody", nil),
})

return nil
Expand All @@ -156,8 +161,8 @@ func (n *Node) handleConversationNewMessage(ctx context.Context, input *p2p.Even
n.sql(ctx).Save(&entity.Conversation{ID: input.ConversationID, ReadAt: time.Time{}})

n.DisplayNotification(notification.Payload{
Title: "New message",
Body: "You have a new message",
Title: i18n.T("NewMessageTitle", nil),
Body: i18n.T("NewMessageBody", nil),
})
return nil
}
Expand Down
10 changes: 5 additions & 5 deletions core/packrd/packed-packr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/pkg/errorcodes/code.go
Expand Up @@ -55,7 +55,7 @@ func (c Code) newError(placeholders map[string]string, stack *stack, cause error
if !ok {
grpcCode = codes.Unknown
}
localized := i18n.T(codeName) // FIXME: support placeholders
localized := i18n.T(codeName, nil) // FIXME: support placeholders
e := convert(
errors.New(localized),
grpcCode,
Expand Down
9 changes: 5 additions & 4 deletions core/pkg/i18n/i18n.go
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/gobuffalo/packr/v2"
goI18n "github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
)

type singleton struct {
Expand Down Expand Up @@ -58,11 +58,11 @@ func SetLanguage(tag *language.Tag) {
i18n().language = tag
}

func T(messageID string, templateData ...string) string {
return TCount(messageID, 1, templateData...)
func T(messageID string, templateData interface{}) string {
return TCount(messageID, 1, templateData)
}

func TCount(messageID string, count int, templateData ...string) string {
func TCount(messageID string, count int, templateData interface{}) string {
i18nInstance := i18n()

localizer := goI18n.NewLocalizer(i18nInstance.bundle, i18nInstance.language.String())
Expand All @@ -71,6 +71,7 @@ func TCount(messageID string, count int, templateData ...string) string {
DefaultMessage: &goI18n.Message{
ID: messageID,
},
TemplateData: templateData,
})

if err != nil {
Expand Down
30 changes: 30 additions & 0 deletions core/pkg/i18n/locales/active.en.yaml
Expand Up @@ -15,3 +15,33 @@ ErrContactReqExisting:

ErrContactReqMyself:
other: "You can't add yourself as a contact"

DaemonStartBody:
other: "Berty daemon is now up and running"

DaemonStartTitle:
other: "Node started"

ContactRequestTitle:
other: "Contact request"

ContactRequestBody:
other: "{{.Name}} wants to add you"

ContactRequestAccpetedTitle:
other: "Contact request accepted"

ContactRequestAccpetedBody:
other: "{{.Name}} accepted your request"

ConversationInviteTitle:
other: "Conversation invite"

ConversationInviteBody:
other: "You have been invited to a new conversation"

NewMessageTitle:
other: "New message"

NewMessageBody:
other: "You have a new message"
30 changes: 30 additions & 0 deletions core/pkg/i18n/locales/active.fr.yaml
Expand Up @@ -15,3 +15,33 @@ ErrContactReqExisting:

ErrContactReqMyself:
other: "Vous ne pouvez pas vous ajouter comme contact"

DaemonStartBody:
other: "Le daemon berty est désormais opérationnel"

DaemonStartTitle:
other: "Nœud démarré"

ContactRequestTitle:
other: "Demande de contact"

ContactRequestBody:
other: "{{.Name}} veut vous ajouter à sa liste de contact"

ContactRequestAccpetedTitle:
other: "Demande de contact acceptée"

ContactRequestAccpetedBody:
other: "{{.Name}} a accepté votre demande de contact"

ConversationInviteTitle:
other: "Invitation de conversation"

ConversationInviteBody:
other: "Vous avez été invité à joindre une conversation"

NewMessageTitle:
other: "Nouveau message"

NewMessageBody:
other: "Vous avez un nouveau message"
20 changes: 18 additions & 2 deletions core/pkg/notification/notification.go
Expand Up @@ -3,7 +3,12 @@
package notification

import (
"github.com/gen2brain/beeep"
"path"
"runtime"
"sync"

"github.com/0xAX/notificator"

"go.uber.org/zap"
)

Expand Down Expand Up @@ -55,6 +60,8 @@ func (n *NoopNotification) ReceivePushID(pushID, pushIDType string) {

// NoopNotification is a Driver
var _ Driver = (*DesktopNotification)(nil)
var notify *notificator.Notificator
var once sync.Once

func NewDesktopNotification() Driver {
return &DesktopNotification{}
Expand All @@ -63,7 +70,16 @@ func NewDesktopNotification() Driver {
type DesktopNotification struct{}

func (n *DesktopNotification) DisplayNotification(p Payload) error {
return beeep.Notify(p.Title, p.Body, p.Icon)
once.Do(func() {
_, filename, _, _ := runtime.Caller(0)
iconPath := path.Dir(filename) + "/../../../client/react-native/common/static/img/logo.png"
notify = notificator.New(notificator.Options{
DefaultIcon: iconPath,
AppName: "Berty",
})
})

return notify.Push(p.Title, p.Body, p.Icon, notificator.UR_NORMAL)
}

func (n *DesktopNotification) ReceiveNotification(data string) {}
Expand Down

0 comments on commit a102b8b

Please sign in to comment.