mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 03:29:16 +00:00
adding notification support
This commit is contained in:
parent
dc2c45df1d
commit
664e179fba
@ -3914,7 +3914,6 @@ components:
|
|||||||
required:
|
required:
|
||||||
- profile
|
- profile
|
||||||
- content
|
- content
|
||||||
- view # revision increment on sharing changes
|
|
||||||
- group
|
- group
|
||||||
- label
|
- label
|
||||||
- card
|
- card
|
||||||
|
@ -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 {
|
||||||
|
|
||||||
|
@ -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,
|
||||||
|
132
net/server/internal/notify.go
Normal file
132
net/server/internal/notify.go
Normal 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(¬ifications).Error; err != nil {
|
||||||
|
ErrMsg(err)
|
||||||
|
}
|
||||||
|
for _, notification := range notifications {
|
||||||
|
notify <- ¬ification
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -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
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user