diff --git a/net/server/internal/api_contact.go b/net/server/internal/api_contact.go index 54ff93a0..26634731 100644 --- a/net/server/internal/api_contact.go +++ b/net/server/internal/api_contact.go @@ -68,12 +68,3 @@ func SetCloseMessage(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } -func SetContentRevision(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) -} - -func SetProfileRevision(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) -} diff --git a/net/server/internal/api_setContentRevision.go b/net/server/internal/api_setContentRevision.go new file mode 100644 index 00000000..ae23f8ff --- /dev/null +++ b/net/server/internal/api_setContentRevision.go @@ -0,0 +1,14 @@ +package databag + +import ( + "net/http" +) + +func SetContentRevision(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) +} + +func NotifyContentRevision(token string, revision int64) error { + return nil +} diff --git a/net/server/internal/api_setProfile.go b/net/server/internal/api_setProfile.go index 6ab4e77e..46f80f49 100644 --- a/net/server/internal/api_setProfile.go +++ b/net/server/internal/api_setProfile.go @@ -42,6 +42,7 @@ func SetProfile(w http.ResponseWriter, r *http.Request) { return } + SetProfileNotification(account) SetStatus(account) WriteResponse(w, nil) } diff --git a/net/server/internal/api_setProfileRevision.go b/net/server/internal/api_setProfileRevision.go new file mode 100644 index 00000000..5c5f65be --- /dev/null +++ b/net/server/internal/api_setProfileRevision.go @@ -0,0 +1,15 @@ +package databag + +import ( + "net/http" +) + +func SetProfileRevision(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) +} + +func NotifyProfileRevision(token string, revision int64) error { + return nil +} + diff --git a/net/server/internal/appValues.go b/net/server/internal/appValues.go index a698fc10..35a6db52 100644 --- a/net/server/internal/appValues.go +++ b/net/server/internal/appValues.go @@ -19,6 +19,8 @@ const APP_CARDCONFIRMED = "confirmed" const APP_CARDREQUESTED = "requested" const APP_CARDCONNECTING = "connecting" const APP_CARDCONNECTED = "connected" +const APP_MODULEPROFILE = "profile" +const APP_MODULECONTENT = "content" func AppCardStatus(status string) bool { if status == APP_CARDPENDING { diff --git a/net/server/internal/notify.go b/net/server/internal/notify.go index fc20a3b2..4e3c8af7 100644 --- a/net/server/internal/notify.go +++ b/net/server/internal/notify.go @@ -6,7 +6,7 @@ import ( ) var notify = make(chan *store.Notification) -var notifyExit = make(chan bool, 1) +var notifyExit = make(chan bool) func ExitNotifications() { notifyExit <- true @@ -27,14 +27,39 @@ func SendNotifications() { for { select { case notification := <-notify: - PrintMsg("SENDING") - PrintMsg(notification) + node := "https://" + getStrConfigValue(CONFIG_DOMAIN, "") + "/" + if notification.Node == node { + SendLocalNotification(notification) + } else { + SendRemoteNotification(notification) + } + if err := store.DB.Delete(¬ification).Error; err != nil { + ErrMsg(err) + } case <-notifyExit: - PrintMsg("EXITING") + return } } } +func SendLocalNotification(notification *store.Notification) { + if notification.Module == APP_MODULEPROFILE { + if err := NotifyProfileRevision(notification.Token, notification.Revision); err != nil { + ErrMsg(err) + } + } else if notification.Module == APP_MODULECONTENT { + if err := NotifyContentRevision(notification.Token, notification.Revision); err != nil { + ErrMsg(err) + } + } else { + LogMsg("unknown notification type") + } +} + +func SendRemoteNotification(notification *store.Notification) { + PrintMsg(notification) +} + // notify all cards of profile change func SetProfileNotification(account *store.Account) { @@ -49,7 +74,8 @@ func SetProfileNotification(account *store.Account) { err := store.DB.Transaction(func(tx *gorm.DB) error { for _, card := range cards { notification := &store.Notification{ - Url: card.Node + "/contact/profile/revision", + Node: card.Node, + Module: APP_MODULEPROFILE, Token: card.OutToken, Revision: account.ProfileRevision, } @@ -81,7 +107,8 @@ func SetContentNotification(account *store.Account) { err := store.DB.Transaction(func(tx *gorm.DB) error { for _, card := range cards { notification := &store.Notification{ - Url: card.Node + "/contact/content/revision", + Node: card.Node, + Module: APP_MODULECONTENT, Token: card.OutToken, Revision: account.ViewRevision + account.ContentRevision + card.ViewRevision, } @@ -112,7 +139,8 @@ func SetContactContentNotification(account *store.Account, cardId string) { err := store.DB.Transaction(func(tx *gorm.DB) error { for _, card := range cards { notification := &store.Notification{ - Url: card.Node + "/contact/content/revision", + Node: card.Node, + Module: APP_MODULECONTENT, Token: card.OutToken, Revision: account.ViewRevision + account.ContentRevision + card.ViewRevision, } diff --git a/net/server/internal/store/schema.go b/net/server/internal/store/schema.go index 8e69ccc1..26193de3 100644 --- a/net/server/internal/store/schema.go +++ b/net/server/internal/store/schema.go @@ -29,7 +29,8 @@ func AutoMigrate(db *gorm.DB) { type Notification struct { ID uint `gorm:"primaryKey;not null;unique;autoIncrement"` - Url string `gorm:"not null"` + Node string `gorm:"not null"` + Module string `gorm:"not null"` Token string `gorm:"not null"` Revision int64 `gorm:"not null"` }