2022-01-21 21:18:35 +00:00
|
|
|
package databag
|
|
|
|
|
|
|
|
import (
|
2022-01-22 07:00:47 +00:00
|
|
|
"errors"
|
2022-01-21 21:18:35 +00:00
|
|
|
"gorm.io/gorm"
|
|
|
|
"databag/internal/store"
|
|
|
|
)
|
|
|
|
|
2022-01-25 05:22:33 +00:00
|
|
|
var notify = make(chan *store.Notification, APP_NOTIFYBUFFER)
|
2022-01-21 22:26:31 +00:00
|
|
|
var notifyExit = make(chan bool)
|
2022-01-21 21:18:35 +00:00
|
|
|
|
|
|
|
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:
|
2022-01-21 22:26:31 +00:00
|
|
|
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)
|
|
|
|
}
|
2022-01-21 21:18:35 +00:00
|
|
|
case <-notifyExit:
|
2022-01-21 22:26:31 +00:00
|
|
|
return
|
2022-01-21 21:18:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-21 22:26:31 +00:00
|
|
|
func SendLocalNotification(notification *store.Notification) {
|
2022-01-22 07:00:47 +00:00
|
|
|
|
|
|
|
// pull reference account
|
|
|
|
var card store.Card
|
2022-02-03 19:18:50 +00:00
|
|
|
if err := store.DB.Preload("Account").Preload("CardSlot").Where("in_token = ?", notification.Token).First(&card).Error; err != nil {
|
2022-01-22 07:00:47 +00:00
|
|
|
ErrMsg(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if card.Account.Disabled {
|
|
|
|
ErrMsg(errors.New("account is inactive"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-28 21:39:31 +00:00
|
|
|
if notification.Module == APP_NOTIFYPROFILE {
|
2022-01-22 07:00:47 +00:00
|
|
|
if err := NotifyProfileRevision(&card, notification.Revision); err != nil {
|
2022-01-21 22:26:31 +00:00
|
|
|
ErrMsg(err)
|
|
|
|
}
|
2022-01-28 21:39:31 +00:00
|
|
|
} else if notification.Module == APP_NOTIFYCONTENT {
|
2022-01-22 07:00:47 +00:00
|
|
|
if err := NotifyContentRevision(&card, notification.Revision); err != nil {
|
2022-01-21 22:26:31 +00:00
|
|
|
ErrMsg(err)
|
|
|
|
}
|
2022-02-03 08:30:43 +00:00
|
|
|
} else if notification.Module == APP_NOTIFYLABEL {
|
|
|
|
if err := NotifyLabelRevision(&card, notification.Revision); err != nil {
|
|
|
|
ErrMsg(err)
|
|
|
|
}
|
2022-01-28 21:39:31 +00:00
|
|
|
} else if notification.Module == APP_NOTIFYVIEW {
|
|
|
|
if err := NotifyViewRevision(&card, notification.Revision); err != nil {
|
|
|
|
ErrMsg(err)
|
|
|
|
}
|
2022-01-21 22:26:31 +00:00
|
|
|
} else {
|
|
|
|
LogMsg("unknown notification type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SendRemoteNotification(notification *store.Notification) {
|
2022-01-22 18:45:39 +00:00
|
|
|
// TODO send remote notification
|
2022-01-21 22:26:31 +00:00
|
|
|
}
|
|
|
|
|
2022-01-21 21:18:35 +00:00
|
|
|
// notify all cards of profile change
|
|
|
|
func SetProfileNotification(account *store.Account) {
|
|
|
|
|
|
|
|
// select all connected cards
|
|
|
|
var cards []store.Card
|
2022-01-22 18:16:33 +00:00
|
|
|
if err := store.DB.Where("account_id = ? AND status = ?", account.Guid, APP_CARDCONNECTED).Find(&cards).Error; err != nil {
|
2022-01-21 21:18:35 +00:00
|
|
|
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{
|
2022-01-21 22:26:31 +00:00
|
|
|
Node: card.Node,
|
2022-01-28 21:39:31 +00:00
|
|
|
Module: APP_NOTIFYPROFILE,
|
2022-01-21 21:18:35 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 08:20:57 +00:00
|
|
|
// notify single card of profile revision
|
|
|
|
func SetContactProfileNotification(account *store.Account, card *store.Card) {
|
|
|
|
|
|
|
|
if card.Status != APP_CARDCONNECTED {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// add new notification for card
|
|
|
|
notification := &store.Notification{
|
|
|
|
Node: card.Node,
|
|
|
|
Module: APP_NOTIFYPROFILE,
|
|
|
|
Token: card.OutToken,
|
|
|
|
Revision: account.ProfileRevision,
|
|
|
|
}
|
|
|
|
|
|
|
|
if res := store.DB.Save(notification).Error; res != nil {
|
|
|
|
ErrMsg(res)
|
|
|
|
} else {
|
|
|
|
notify <- notification
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-21 21:18:35 +00:00
|
|
|
// notify all cards of content change
|
2022-01-28 23:05:30 +00:00
|
|
|
// account.Content incremented by adding, updating, removing article & setting or clearning group or label from article
|
2022-01-21 21:18:35 +00:00
|
|
|
func SetContentNotification(account *store.Account) {
|
|
|
|
|
|
|
|
// select all connected cards
|
|
|
|
var cards []store.Card
|
2022-01-22 18:16:33 +00:00
|
|
|
if err := store.DB.Where("account_id = ? AND status = ?", account.Guid, APP_CARDCONNECTED).Find(&cards).Error; err != nil {
|
2022-01-21 21:18:35 +00:00
|
|
|
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{
|
2022-01-21 22:26:31 +00:00
|
|
|
Node: card.Node,
|
2022-01-28 21:39:31 +00:00
|
|
|
Module: APP_NOTIFYCONTENT,
|
2022-01-21 21:18:35 +00:00
|
|
|
Token: card.OutToken,
|
2022-01-28 21:39:31 +00:00
|
|
|
Revision: account.ContentRevision,
|
2022-01-21 21:18:35 +00:00
|
|
|
}
|
|
|
|
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
|
2022-01-25 05:22:33 +00:00
|
|
|
func SetContactContentNotification(account *store.Account, card *store.Card) {
|
2022-01-21 21:18:35 +00:00
|
|
|
|
2022-01-25 05:22:33 +00:00
|
|
|
if card.Status != APP_CARDCONNECTED {
|
2022-01-21 21:18:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// add new notification for card
|
2022-01-25 05:22:33 +00:00
|
|
|
notification := &store.Notification{
|
|
|
|
Node: card.Node,
|
2022-01-28 21:39:31 +00:00
|
|
|
Module: APP_NOTIFYCONTENT,
|
|
|
|
Token: card.OutToken,
|
|
|
|
Revision: account.ContentRevision,
|
|
|
|
}
|
|
|
|
|
|
|
|
if res := store.DB.Save(notification).Error; res != nil {
|
|
|
|
ErrMsg(res)
|
|
|
|
} else {
|
|
|
|
notify <- notification
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// notify all cards of view change
|
2022-01-28 23:05:30 +00:00
|
|
|
// account.View incremented by removing a group or label or adding or removing a group from a label
|
2022-01-28 21:39:31 +00:00
|
|
|
func SetViewNotification(account *store.Account) {
|
|
|
|
|
|
|
|
// select all connected cards
|
|
|
|
var cards []store.Card
|
|
|
|
if err := store.DB.Where("account_id = ? AND status = ?", account.Guid, 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{
|
|
|
|
Node: card.Node,
|
|
|
|
Module: APP_NOTIFYVIEW,
|
|
|
|
Token: card.OutToken,
|
|
|
|
Revision: account.ViewRevision + 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 SetContactViewNotification(account *store.Account, card *store.Card) {
|
|
|
|
|
|
|
|
if card.Status != APP_CARDCONNECTED {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// add new notification for card
|
|
|
|
notification := &store.Notification{
|
|
|
|
Node: card.Node,
|
|
|
|
Module: APP_NOTIFYVIEW,
|
2022-01-25 05:22:33 +00:00
|
|
|
Token: card.OutToken,
|
2022-01-28 21:39:31 +00:00
|
|
|
Revision: account.ViewRevision + card.ViewRevision,
|
2022-01-25 05:22:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if res := store.DB.Save(notification).Error; res != nil {
|
|
|
|
ErrMsg(res)
|
|
|
|
} else {
|
|
|
|
notify <- notification
|
2022-01-21 21:18:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-03 08:30:43 +00:00
|
|
|
// notify all cards of label change
|
|
|
|
// account.Label incremented by adding, updating, removing a label & setting or clearning group from article
|
|
|
|
func SetLabelNotification(account *store.Account) {
|
|
|
|
|
|
|
|
// select all connected cards
|
|
|
|
var cards []store.Card
|
|
|
|
if err := store.DB.Where("account_id = ? AND status = ?", account.Guid, 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{
|
|
|
|
Node: card.Node,
|
|
|
|
Module: APP_NOTIFYLABEL,
|
|
|
|
Token: card.OutToken,
|
|
|
|
Revision: account.LabelRevision,
|
|
|
|
}
|
|
|
|
if err := tx.Save(notification).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
notify <- notification
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ErrMsg(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// notify single card of label change
|
|
|
|
// card.Label incremented by adding or removing group from label
|
|
|
|
func SetContactLabelNotification(account *store.Account, card *store.Card) {
|
|
|
|
|
|
|
|
if card.Status != APP_CARDCONNECTED {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// add new notification for card
|
|
|
|
notification := &store.Notification{
|
|
|
|
Node: card.Node,
|
|
|
|
Module: APP_NOTIFYLABEL,
|
|
|
|
Token: card.OutToken,
|
|
|
|
Revision: account.LabelRevision,
|
|
|
|
}
|
|
|
|
|
|
|
|
if res := store.DB.Save(notification).Error; res != nil {
|
|
|
|
ErrMsg(res)
|
|
|
|
} else {
|
|
|
|
notify <- notification
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-21 21:18:35 +00:00
|
|
|
|