From c1617fe9a0376e39a27d69a3d00cc56f1a832697 Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Mon, 14 Nov 2022 21:08:20 -0800 Subject: [PATCH] pushing contact events --- net/server/internal/api_setOpenMessage.go | 6 +++ net/server/internal/api_setPushEvent.go | 31 ++++++------ net/server/internal/appValues.go | 3 ++ net/server/internal/notify.go | 60 +++++++++++++++++++---- net/server/internal/store/alloc.go | 2 +- net/server/internal/store/schema.go | 1 + 6 files changed, 78 insertions(+), 25 deletions(-) diff --git a/net/server/internal/api_setOpenMessage.go b/net/server/internal/api_setOpenMessage.go index 852b53e9..b7ffafa9 100644 --- a/net/server/internal/api_setOpenMessage.go +++ b/net/server/internal/api_setOpenMessage.go @@ -99,6 +99,9 @@ func SetOpenMessage(w http.ResponseWriter, r *http.Request) { ErrResponse(w, http.StatusInternalServerError, err) return } + + // push event + SendPushEvent(account, "contact.updateCard"); } else { // update profile if revision changed @@ -154,6 +157,9 @@ func SetOpenMessage(w http.ResponseWriter, r *http.Request) { ErrResponse(w, http.StatusInternalServerError, err) return } + + // push event + SendPushEvent(account, "contact.addCard"); } status := &ContactStatus{ diff --git a/net/server/internal/api_setPushEvent.go b/net/server/internal/api_setPushEvent.go index 210c7354..cbb346df 100644 --- a/net/server/internal/api_setPushEvent.go +++ b/net/server/internal/api_setPushEvent.go @@ -26,27 +26,30 @@ func SetPushEvent(w http.ResponseWriter, r *http.Request) { return } - if code, err := SendPushEvent(card.Account, event); err != nil { - ErrResponse(w, code, err); - return; - } - + SendPushEvent(card.Account, event) WriteResponse(w, nil) } //SendPushEvent delivers notification to clients -func SendPushEvent(account store.Account, event string) (int, error) { +func SendPushEvent(account store.Account, event string) { - messages := []push{} - if err := store.DB.Model(&store.Session{}).Select("sessions.push_token, push_events.message_title, push_events.message_body").Joins("left join push_events on push_events.session_id = session.id").Where("sessions.account_id = ? AND session.push_enabled = ? AND push_events.event = ?", account.ID, true, event).Scan(messages).Error; err != nil { - return http.StatusInternalServerError, err + // check if server supports push + if getBoolConfigValue(CNFPushSupported, true) != true { + return } - // send push notification for each - for _, message := range messages { - PrintMsg(message); + // get all sessions supporting push for specified event + rows, err := store.DB.Table("sessions").Select("sessions.push_token, push_events.message_title, push_events.message_body").Joins("left join push_events on push_events.session_id = sessions.id").Where("sessions.account_id = ? AND sessions.push_enabled = ? AND push_events.event = ?", account.GUID, true, event).Rows(); + if err != nil { + ErrMsg(err); + return + } + for rows.Next() { + PrintMsg("IN ROW"); + var pushToken string + var messageTitle string + var messageBody string + rows.Scan(&pushToken, &messageTitle, &messageBody) } - - return http.StatusOK, nil } diff --git a/net/server/internal/appValues.go b/net/server/internal/appValues.go index efb50398..f0a6f66c 100644 --- a/net/server/internal/appValues.go +++ b/net/server/internal/appValues.go @@ -75,6 +75,9 @@ const APPNotifyArticle = "article" //APPNotifyChannel config for notification name for channel const APPNotifyChannel = "channel" +//APPPushNotify config for push notifications +const APPPushNotify = "push" + //APPNotifyView config for notification name for view const APPNotifyView = "view" diff --git a/net/server/internal/notify.go b/net/server/internal/notify.go index e92b1c4c..73c37a84 100644 --- a/net/server/internal/notify.go +++ b/net/server/internal/notify.go @@ -77,6 +77,8 @@ func sendLocalNotification(notification *store.Notification) { if err := NotifyViewRevision(&card, notification.Revision); err != nil { ErrMsg(err) } + } else if notification.Module == APPPushNotify { + SendPushEvent(card.Account, notification.Event) } else { LogMsg("unknown notification type") } @@ -86,24 +88,37 @@ func sendRemoteNotification(notification *store.Notification) { var module string if notification.Module == APPNotifyProfile { - module = "profile" + module = "profile/revision" } else if notification.Module == APPNotifyArticle { - module = "article" + module = "article/revision" } else if notification.Module == APPNotifyChannel { - module = "channel" + module = "channel/revision" } else if notification.Module == APPNotifyView { - module = "view" + module = "view/revision" + } else if notification.Module == APPPushNotify { + module = "notification" } else { LogMsg("unknown notification type") return } - body, err := json.Marshal(notification.Revision) - if err != nil { - ErrMsg(err) - return - } - url := "https://" + notification.Node + "/contact/" + module + "/revision?contact=" + notification.GUID + "." + notification.Token + var body []byte + var err error + if module == "notification" { + body, err = json.Marshal(notification.Event) + if err != nil { + ErrMsg(err) + return + } + } else { + body, err = json.Marshal(notification.Revision) + if err != nil { + ErrMsg(err) + return + } + } + + url := "https://" + notification.Node + "/contact/" + module + "?contact=" + notification.GUID + "." + notification.Token req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(body)) if err != nil { ErrMsg(err) @@ -228,3 +243,28 @@ func SetContactChannelNotification(account *store.Account, card *store.Card) { notify <- notification } } + +//SetContactPushNotification notifies contacts of push event +func SetContactPushNotification(card *store.Card, event string) { + + if card.Status != APPCardConnected { + return + } + + // add new notification for card + notification := &store.Notification{ + Node: card.Node, + Module: APPPushNotify, + GUID: card.GUID, + Token: card.OutToken, + Revision: 0, + Event: event, + }; + + if res := store.DB.Save(notification).Error; res != nil { + ErrMsg(res) + } else { + notify <- notification + } +} + diff --git a/net/server/internal/store/alloc.go b/net/server/internal/store/alloc.go index 2ab3714e..26ed1980 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.Silent), + Logger: logger.Default.LogMode(logger.Info), }) if err != nil { panic("failed to connect database") diff --git a/net/server/internal/store/schema.go b/net/server/internal/store/schema.go index e5d14530..af5276aa 100644 --- a/net/server/internal/store/schema.go +++ b/net/server/internal/store/schema.go @@ -35,6 +35,7 @@ type Notification struct { Module string `gorm:"not null"` Token string `gorm:"not null"` Revision int64 `gorm:"not null"` + Event string } type Config struct {