adding notification support

This commit is contained in:
Roland Osborne 2022-01-21 13:18:35 -08:00
parent dc2c45df1d
commit 664e179fba
6 changed files with 146 additions and 3 deletions

View File

@ -3914,7 +3914,6 @@ components:
required: required:
- profile - profile
- content - content
- view # revision increment on sharing changes
- group - group
- label - label
- card - card

View File

@ -49,6 +49,7 @@ func AddCard(w http.ResponseWriter, r *http.Request) {
card.Node = identity.Node card.Node = identity.Node
card.ProfileRevision = identity.Revision card.ProfileRevision = identity.Revision
card.Status = APP_CARDCONFIRMED card.Status = APP_CARDCONFIRMED
card.ViewRevision = 0
} else { } else {

View File

@ -36,7 +36,7 @@ func GetOpenMessage(w http.ResponseWriter, r *http.Request) {
connect := &Connect{ connect := &Connect{
Contact: card.Guid, Contact: card.Guid,
Token: card.InToken, Token: card.InToken,
ContentRevision: account.ViewRevision, ContentRevision: account.ContentRevision + account.ViewRevision + card.ViewRevision,
ProfileRevision: account.ProfileRevision, ProfileRevision: account.ProfileRevision,
Handle: account.Username, Handle: account.Username,
Name: detail.Name, Name: detail.Name,

View File

@ -0,0 +1,132 @@
package databag
import (
"gorm.io/gorm"
"databag/internal/store"
)
var notify = make(chan *store.Notification)
var notifyExit = make(chan bool, 1)
func ExitNotifications() {
notifyExit <- true
}
func SendNotifications() {
// queue all saved notifications
var notifications []store.Notification
if err := store.DB.Find(&notifications).Error; err != nil {
ErrMsg(err)
}
for _, notification := range notifications {
notify <- &notification
}
// send notifications until exit
for {
select {
case notification := <-notify:
PrintMsg("SENDING")
PrintMsg(notification)
case <-notifyExit:
PrintMsg("EXITING")
}
}
}
// notify all cards of profile change
func SetProfileNotification(account *store.Account) {
// select all connected cards
var cards []store.Card
if err := store.DB.Where("account_id = ? AND status = ?", account.ID, APP_CARDCONNECTED).Find(&cards).Error; err != nil {
ErrMsg(err)
return
}
// add new notification for each card
err := store.DB.Transaction(func(tx *gorm.DB) error {
for _, card := range cards {
notification := &store.Notification{
Url: card.Node + "/contact/profile/revision",
Token: card.OutToken,
Revision: account.ProfileRevision,
}
if err := tx.Save(notification).Error; err != nil {
return err
}
notify <- notification
}
return nil
})
if err != nil {
ErrMsg(err)
}
}
// notify all cards of content change
// account.Content incremented by adding, updating, removing article
// account.View incremented by removing a group or label or adding or removing a group with label
func SetContentNotification(account *store.Account) {
// select all connected cards
var cards []store.Card
if err := store.DB.Where("account_id = ? AND status = ?", account.ID, APP_CARDCONNECTED).Find(&cards).Error; err != nil {
ErrMsg(err)
return
}
// add new notification for each card
err := store.DB.Transaction(func(tx *gorm.DB) error {
for _, card := range cards {
notification := &store.Notification{
Url: card.Node + "/contact/content/revision",
Token: card.OutToken,
Revision: account.ViewRevision + account.ContentRevision + card.ViewRevision,
}
if err := tx.Save(notification).Error; err != nil {
return err
}
notify <- notification
}
return nil
})
if err != nil {
ErrMsg(err)
}
}
// notify single card of content change
// card.View incremented by adding or removing card from group or label
func SetContactContentNotification(account *store.Account, cardId string) {
// select card if connected
var cards []store.Card
if err := store.DB.Where("account_id = ? AND status = ? AND card_id = ?", account.ID, APP_CARDCONNECTED, cardId).Find(&cards).Error; err != nil {
ErrMsg(err)
return
}
// add new notification for card
err := store.DB.Transaction(func(tx *gorm.DB) error {
for _, card := range cards {
notification := &store.Notification{
Url: card.Node + "/contact/content/revision",
Token: card.OutToken,
Revision: account.ViewRevision + account.ContentRevision + card.ViewRevision,
}
if err := tx.Save(notification).Error; err != nil {
return err
}
notify <- notification
}
return nil
})
if err != nil {
ErrMsg(err)
}
}

View File

@ -28,6 +28,8 @@ type Routes []Route
func NewRouter() *mux.Router { func NewRouter() *mux.Router {
go SendNotifications()
router := mux.NewRouter().StrictSlash(true) router := mux.NewRouter().StrictSlash(true)
for _, route := range routes { for _, route := range routes {
var handler http.Handler var handler http.Handler

View File

@ -3,6 +3,7 @@ package store
import "gorm.io/gorm" import "gorm.io/gorm"
func AutoMigrate(db *gorm.DB) { func AutoMigrate(db *gorm.DB) {
db.AutoMigrate(&Notification{});
db.AutoMigrate(&Config{}); db.AutoMigrate(&Config{});
db.AutoMigrate(&App{}); db.AutoMigrate(&App{});
db.AutoMigrate(&Account{}); db.AutoMigrate(&Account{});
@ -26,6 +27,13 @@ func AutoMigrate(db *gorm.DB) {
db.AutoMigrate(&TopicTag{}); db.AutoMigrate(&TopicTag{});
} }
type Notification struct {
ID uint `gorm:"primaryKey;not null;unique;autoIncrement"`
Url string `gorm:"not null"`
Token string `gorm:"not null"`
Revision int64 `gorm:"not null"`
}
type Config struct { type Config struct {
ID uint `gorm:"primaryKey;not null;unique;autoIncrement"` ID uint `gorm:"primaryKey;not null;unique;autoIncrement"`
ConfigId string `gorm:"not null;uniqueIndex"` ConfigId string `gorm:"not null;uniqueIndex"`
@ -53,12 +61,12 @@ type Account struct {
Password []byte `gorm:"not null"` Password []byte `gorm:"not null"`
ProfileRevision int64 `gorm:"not null;default:1"` ProfileRevision int64 `gorm:"not null;default:1"`
ContentRevision int64 `gorm:"not null;default:1"` ContentRevision int64 `gorm:"not null;default:1"`
ViewRevision int64 `gorm:"not null;default:1"`
GroupRevision int64 `gorm:"not null;default:1"` GroupRevision int64 `gorm:"not null;default:1"`
LabelRevision int64 `gorm:"not null;default:1"` LabelRevision int64 `gorm:"not null;default:1"`
CardRevision int64 `gorm:"not null;default:1"` CardRevision int64 `gorm:"not null;default:1"`
DialogueRevision int64 `gorm:"not null;default:1"` DialogueRevision int64 `gorm:"not null;default:1"`
InsightRevision int64 `gorm:"not null;default:1"` InsightRevision int64 `gorm:"not null;default:1"`
ViewRevision int64 `gorm:"not null;default:1"`
Created int64 `gorm:"autoCreateTime"` Created int64 `gorm:"autoCreateTime"`
Disabled bool `gorm:"not null;default:false"` Disabled bool `gorm:"not null;default:false"`
AccountDetail AccountDetail AccountDetail AccountDetail
@ -132,6 +140,7 @@ type Card struct {
DataRevision int64 `gorm:"not null"` DataRevision int64 `gorm:"not null"`
Created int64 `gorm:"autoCreateTime"` Created int64 `gorm:"autoCreateTime"`
Updated int64 `gorm:"autoUpdateTime"` Updated int64 `gorm:"autoUpdateTime"`
ViewRevision int64 `gorm:"not null"`
RemoteProfile int64 RemoteProfile int64
RemoteContent int64 RemoteContent int64
Account Account Account Account