adding revision notification thread

This commit is contained in:
Roland Osborne 2022-01-21 14:26:31 -08:00
parent 664e179fba
commit 57b6936226
7 changed files with 69 additions and 17 deletions

View File

@ -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)
}

View File

@ -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
}

View File

@ -42,6 +42,7 @@ func SetProfile(w http.ResponseWriter, r *http.Request) {
return
}
SetProfileNotification(account)
SetStatus(account)
WriteResponse(w, nil)
}

View File

@ -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
}

View File

@ -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 {

View File

@ -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(&notification).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,
}

View File

@ -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"`
}