diff --git a/net/server/internal/api_addChannel.go b/net/server/internal/api_addChannel.go index 4259934b..64fc3af1 100644 --- a/net/server/internal/api_addChannel.go +++ b/net/server/internal/api_addChannel.go @@ -85,6 +85,7 @@ func AddChannel(w http.ResponseWriter, r *http.Request) { SetStatus(account) for _, card := range cards { SetContactChannelNotification(account, card) + SetContactPushNotification(card, "content.addChannel." + params.DataType) } video := getBoolConfigValue(CNFEnableVideo, true); diff --git a/net/server/internal/api_addChannelTopic.go b/net/server/internal/api_addChannelTopic.go index b938e21f..d698a4a1 100644 --- a/net/server/internal/api_addChannelTopic.go +++ b/net/server/internal/api_addChannelTopic.go @@ -87,6 +87,9 @@ func AddChannelTopic(w http.ResponseWriter, r *http.Request) { SetStatus(act) for _, card := range cards { SetContactChannelNotification(act, &card) + if card.GUID != guid { + SetContactPushNotification(&card, "content.addChannelTopic." + channelSlot.Channel.DataType) + } } WriteResponse(w, getTopicModel(topicSlot)) } diff --git a/net/server/internal/api_setChannelCard.go b/net/server/internal/api_setChannelCard.go index 0f0ffba9..a069caac 100644 --- a/net/server/internal/api_setChannelCard.go +++ b/net/server/internal/api_setChannelCard.go @@ -96,6 +96,7 @@ func SetChannelCard(w http.ResponseWriter, r *http.Request) { for _, card := range cards { SetContactChannelNotification(account, &card) } + SetContactPushNotification(cardSlot.Card, "content.addChannel." + channelSlot.Channel.DataType) video := getBoolConfigValue(CNFEnableVideo, true); audio := getBoolConfigValue(CNFEnableAudio, true); diff --git a/net/server/internal/api_setOpenMessage.go b/net/server/internal/api_setOpenMessage.go index b7ffafa9..583be5cf 100644 --- a/net/server/internal/api_setOpenMessage.go +++ b/net/server/internal/api_setOpenMessage.go @@ -101,7 +101,7 @@ func SetOpenMessage(w http.ResponseWriter, r *http.Request) { } // push event - SendPushEvent(account, "contact.updateCard"); + SendPushEvent(account, "contact.addCard"); } else { // update profile if revision changed @@ -159,7 +159,7 @@ func SetOpenMessage(w http.ResponseWriter, r *http.Request) { } // push event - SendPushEvent(account, "contact.addCard"); + SendPushEvent(account, "contact.updateCard"); } status := &ContactStatus{ diff --git a/net/server/internal/api_setPushEvent.go b/net/server/internal/api_setPushEvent.go index cbb346df..0ef9c792 100644 --- a/net/server/internal/api_setPushEvent.go +++ b/net/server/internal/api_setPushEvent.go @@ -3,12 +3,19 @@ package databag import ( "databag/internal/store" "net/http" + "bytes" + "encoding/json" + "errors" ) -type push struct { - PushToken string - MessageTitle string - MessageBody string +type Payload struct { + Title string `json:"title"` + Body string `json:"body"` +} + +type Message struct { + Notification Payload `json:"notification"` + To string `json:"to"` } //AddPushEvent notify account of event to push notify @@ -45,11 +52,36 @@ func SendPushEvent(account store.Account, event string) { return } for rows.Next() { - PrintMsg("IN ROW"); var pushToken string var messageTitle string var messageBody string rows.Scan(&pushToken, &messageTitle, &messageBody) + + url := "https://fcm.googleapis.com/fcm/send" + payload := Payload{ Title: messageTitle, Body: messageBody }; + message := Message{ Notification: payload, To: pushToken }; + + body, err := json.Marshal(message) + if err != nil { + ErrMsg(err) + continue + } + req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(body)) + if err != nil { + ErrMsg(err) + continue + } + req.Header.Set("Content-Type", "application/json; charset=utf-8") + req.Header.Set("Authorization", "key=AAAAkgDXt8c:APA91bEjH67QpUWU6uAfCIXLqm0kf6AdPNVICZPCcWbmgW9NGYIErAxMDTy4LEbe4ik93Ho4Z-AJNIhr6nXXKC9qKmyKkkYHJWAEVH47_FXBQV6rsoi9ZB_oiuV66XKKAy1V40GmvfaX") + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + ErrMsg(err) + continue + } + if resp.StatusCode != 200 { + ErrMsg(errors.New("failed to push notification")); + } } } diff --git a/net/server/internal/store/alloc.go b/net/server/internal/store/alloc.go index 26ed1980..2ab3714e 100644 --- a/net/server/internal/store/alloc.go +++ b/net/server/internal/store/alloc.go @@ -10,7 +10,7 @@ var DB *gorm.DB; func SetPath(path string) { db, err := gorm.Open(sqlite.Open(path), &gorm.Config{ - Logger: logger.Default.LogMode(logger.Info), + Logger: logger.Default.LogMode(logger.Silent), }) if err != nil { panic("failed to connect database")