databag/net/server/internal/notify.go

227 lines
5.6 KiB
Go
Raw Normal View History

2022-01-21 21:18:35 +00:00
package databag
import (
2022-06-10 07:13:21 +00:00
"bytes"
2022-01-22 07:00:47 +00:00
"errors"
2022-06-10 07:13:21 +00:00
"net/http"
"encoding/json"
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(&notifications).Error; err != nil {
ErrMsg(err)
}
for _, notification := range notifications {
notify <- &notification
}
// send notifications until exit
for {
select {
case notification := <-notify:
2022-03-17 21:42:51 +00:00
node := getStrConfigValue(CONFIG_DOMAIN, "")
2022-01-21 22:26:31 +00:00
if notification.Node == node {
SendLocalNotification(notification)
} else {
SendRemoteNotification(notification)
}
if err := store.DB.Delete(&notification).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
}
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-02-08 20:24:42 +00:00
} else if notification.Module == APP_NOTIFYARTICLE {
if err := NotifyArticleRevision(&card, notification.Revision); err != nil {
2022-01-21 22:26:31 +00:00
ErrMsg(err)
}
2022-02-08 20:24:42 +00:00
} else if notification.Module == APP_NOTIFYCHANNEL {
if err := NotifyChannelRevision(&card, notification.Revision); err != nil {
ErrMsg(err)
}
} 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-06-10 07:13:21 +00:00
var module string
if notification.Module == APP_NOTIFYPROFILE {
module = "profile"
} else if notification.Module == APP_NOTIFYARTICLE {
module = "article"
} else if notification.Module == APP_NOTIFYCHANNEL {
module = "channel"
} else if notification.Module == APP_NOTIFYVIEW {
module = "view"
} else {
LogMsg("unknown notification type")
return
}
body, err := json.Marshal(notification.Revision);
if err != nil {
ErrMsg(err);
return
}
url := "https://" + notification.Node + "/contact/" + module + "/revision?contact=" + notification.Guid + "." + notification.Token
req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(body))
if err != nil {
ErrMsg(err)
return
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
ErrMsg(err);
}
if resp.StatusCode != 200 {
LogMsg("failed to notify contact");
}
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
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,
Module: APP_NOTIFYPROFILE,
2022-06-10 07:13:21 +00:00
Guid: card.Guid,
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-09 18:04:32 +00:00
// notify single card of article change:
// for each card of groups in updated artcile data
// for each card of group set or cleared from article (does not update data)
func SetContactArticleNotification(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-02-08 20:24:42 +00:00
Module: APP_NOTIFYARTICLE,
2022-06-10 07:13:21 +00:00
Guid: card.Guid,
Token: card.OutToken,
2022-02-08 20:24:42 +00:00
Revision: account.ArticleRevision,
}
if res := store.DB.Save(notification).Error; res != nil {
ErrMsg(res)
} else {
notify <- notification
}
}
2022-02-09 18:04:32 +00:00
// notify single card of view change:
// card set or cleared from a group
// for each card in deleted group
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-06-10 07:13:21 +00:00
Guid: card.Guid,
2022-01-25 05:22:33 +00:00
Token: card.OutToken,
2022-02-06 04:41:52 +00:00
Revision: 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-09 18:04:32 +00:00
// notify single card of channel change:
// for each card in updated channel data
2022-02-09 18:04:32 +00:00
func SetContactChannelNotification(account *store.Account, card *store.Card) {
if card.Status != APP_CARDCONNECTED {
return
}
// add new notification for card
notification := &store.Notification{
Node: card.Node,
2022-02-08 20:24:42 +00:00
Module: APP_NOTIFYCHANNEL,
2022-06-10 07:13:21 +00:00
Guid: card.Guid,
Token: card.OutToken,
2022-02-08 20:24:42 +00:00
Revision: account.ChannelRevision,
}
if res := store.DB.Save(notification).Error; res != nil {
ErrMsg(res)
} else {
notify <- notification
}
}