From 1ae7544cdb4fcebcecce5899b7b0145724f439aa Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Fri, 10 Jun 2022 00:13:21 -0700 Subject: [PATCH] notifying remote contacts --- net/server/internal/notify.go | 43 ++++++++++++++++++++++++++++- net/server/internal/store/schema.go | 1 + 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/net/server/internal/notify.go b/net/server/internal/notify.go index 2ef5a17b..c9367b6d 100644 --- a/net/server/internal/notify.go +++ b/net/server/internal/notify.go @@ -1,7 +1,10 @@ package databag import ( + "bytes" "errors" + "net/http" + "encoding/json" "gorm.io/gorm" "databag/internal/store" ) @@ -78,7 +81,41 @@ func SendLocalNotification(notification *store.Notification) { } func SendRemoteNotification(notification *store.Notification) { - // TODO send remote notification + + var module string + if notification.Module == APP_NOTIFYPROFILE { + module = "profile" + } else if notification.Module == APP_NOTIFYARTICLE { + module = "article" + } else if notification.Module == APP_NOTIFYCHANNEL { + module = "channel" + } else if notification.Module == APP_NOTIFYVIEW { + module = "view" + } 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 + req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(body)) + if err != nil { + ErrMsg(err) + return + } + req.Header.Set("Content-Type", "application/json; charset=utf-8") + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + ErrMsg(err); + } + if resp.StatusCode != 200 { + LogMsg("failed to notify contact"); + } } // notify all cards of profile change @@ -97,6 +134,7 @@ func SetProfileNotification(account *store.Account) { notification := &store.Notification{ Node: card.Node, Module: APP_NOTIFYPROFILE, + Guid: card.Guid, Token: card.OutToken, Revision: account.ProfileRevision, } @@ -125,6 +163,7 @@ func SetContactArticleNotification(account *store.Account, card *store.Card) { notification := &store.Notification{ Node: card.Node, Module: APP_NOTIFYARTICLE, + Guid: card.Guid, Token: card.OutToken, Revision: account.ArticleRevision, } @@ -149,6 +188,7 @@ func SetContactViewNotification(account *store.Account, card *store.Card) { notification := &store.Notification{ Node: card.Node, Module: APP_NOTIFYVIEW, + Guid: card.Guid, Token: card.OutToken, Revision: card.ViewRevision, } @@ -172,6 +212,7 @@ func SetContactChannelNotification(account *store.Account, card *store.Card) { notification := &store.Notification{ Node: card.Node, Module: APP_NOTIFYCHANNEL, + Guid: card.Guid, Token: card.OutToken, Revision: account.ChannelRevision, } diff --git a/net/server/internal/store/schema.go b/net/server/internal/store/schema.go index 9564e6c2..32fd0e21 100644 --- a/net/server/internal/store/schema.go +++ b/net/server/internal/store/schema.go @@ -27,6 +27,7 @@ func AutoMigrate(db *gorm.DB) { type Notification struct { ID uint `gorm:"primaryKey;not null;unique;autoIncrement"` Node string `gorm:"not null"` + Guid string `gorm:"not null"` Module string `gorm:"not null"` Token string `gorm:"not null"` Revision int64 `gorm:"not null"`