2022-01-21 21:18:35 +00:00
|
|
|
package databag
|
|
|
|
|
|
|
|
import (
|
2022-07-22 19:28:14 +00:00
|
|
|
"bytes"
|
|
|
|
"databag/internal/store"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"net/http"
|
2022-01-21 21:18:35 +00:00
|
|
|
)
|
|
|
|
|
2022-07-22 17:52:13 +00:00
|
|
|
var notify = make(chan *store.Notification, APPNotifyBuffer)
|
2022-01-21 22:26:31 +00:00
|
|
|
var notifyExit = make(chan bool)
|
2022-01-21 21:18:35 +00:00
|
|
|
|
2022-07-22 21:40:10 +00:00
|
|
|
//ExitNotifications stop forwarding notifications
|
2022-01-21 21:18:35 +00:00
|
|
|
func ExitNotifications() {
|
2022-07-22 19:28:14 +00:00
|
|
|
notifyExit <- true
|
2022-01-21 21:18:35 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 21:40:10 +00:00
|
|
|
//SendNotifications forward notifcations to contacts
|
2022-01-21 21:18:35 +00:00
|
|
|
func SendNotifications() {
|
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
// 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:
|
|
|
|
node := getStrConfigValue(CNFDomain, "")
|
|
|
|
if notification.Node == node {
|
2022-07-22 21:40:10 +00:00
|
|
|
sendLocalNotification(notification)
|
2022-07-22 19:28:14 +00:00
|
|
|
} else {
|
2022-07-22 21:40:10 +00:00
|
|
|
sendRemoteNotification(notification)
|
2022-07-22 19:28:14 +00:00
|
|
|
}
|
|
|
|
if err := store.DB.Delete(¬ification).Error; err != nil {
|
|
|
|
ErrMsg(err)
|
|
|
|
}
|
|
|
|
case <-notifyExit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-01-21 21:18:35 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 21:40:10 +00:00
|
|
|
func sendLocalNotification(notification *store.Notification) {
|
2022-01-22 07:00:47 +00:00
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
// pull reference account
|
|
|
|
var card store.Card
|
|
|
|
if err := store.DB.Preload("Account").Preload("CardSlot").Where("in_token = ?", notification.Token).First(&card).Error; err != nil {
|
|
|
|
ErrMsg(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if card.Account.Disabled {
|
|
|
|
ErrMsg(errors.New("account is inactive"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if notification.Module == APPNotifyProfile {
|
|
|
|
if err := NotifyProfileRevision(&card, notification.Revision); err != nil {
|
|
|
|
ErrMsg(err)
|
|
|
|
}
|
|
|
|
} else if notification.Module == APPNotifyArticle {
|
|
|
|
if err := NotifyArticleRevision(&card, notification.Revision); err != nil {
|
|
|
|
ErrMsg(err)
|
|
|
|
}
|
|
|
|
} else if notification.Module == APPNotifyChannel {
|
|
|
|
if err := NotifyChannelRevision(&card, notification.Revision); err != nil {
|
|
|
|
ErrMsg(err)
|
|
|
|
}
|
|
|
|
} else if notification.Module == APPNotifyView {
|
|
|
|
if err := NotifyViewRevision(&card, notification.Revision); err != nil {
|
|
|
|
ErrMsg(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LogMsg("unknown notification type")
|
|
|
|
}
|
2022-01-21 22:26:31 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 21:40:10 +00:00
|
|
|
func sendRemoteNotification(notification *store.Notification) {
|
2022-06-10 07:13:21 +00:00
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
var module string
|
|
|
|
if notification.Module == APPNotifyProfile {
|
|
|
|
module = "profile"
|
|
|
|
} else if notification.Module == APPNotifyArticle {
|
|
|
|
module = "article"
|
|
|
|
} else if notification.Module == APPNotifyChannel {
|
|
|
|
module = "channel"
|
|
|
|
} else if notification.Module == APPNotifyView {
|
|
|
|
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-07-22 21:40:10 +00:00
|
|
|
//SetProfileNotification notifies all connected contacts of profile changes
|
2022-01-21 21:18:35 +00:00
|
|
|
func SetProfileNotification(account *store.Account) {
|
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
// select all connected cards
|
|
|
|
var cards []store.Card
|
|
|
|
if err := store.DB.Where("account_id = ? AND status = ?", account.GUID, APPCardConnected).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: APPNotifyProfile,
|
|
|
|
GUID: card.GUID,
|
|
|
|
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-01-21 21:18:35 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 21:40:10 +00:00
|
|
|
//SetContactArticleNotification notifies all connected contacts of article changes
|
2022-07-22 19:28:14 +00:00
|
|
|
// notify single card of article change:
|
2022-07-22 19:33:52 +00:00
|
|
|
// for each card of groups in updated article data
|
2022-02-09 18:04:32 +00:00
|
|
|
// 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-07-22 19:28:14 +00:00
|
|
|
if card.Status != APPCardConnected {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// add new notification for card
|
|
|
|
notification := &store.Notification{
|
|
|
|
Node: card.Node,
|
|
|
|
Module: APPNotifyArticle,
|
|
|
|
GUID: card.GUID,
|
|
|
|
Token: card.OutToken,
|
|
|
|
Revision: account.ArticleRevision,
|
|
|
|
}
|
|
|
|
|
|
|
|
if res := store.DB.Save(notification).Error; res != nil {
|
|
|
|
ErrMsg(res)
|
|
|
|
} else {
|
|
|
|
notify <- notification
|
|
|
|
}
|
2022-01-28 21:39:31 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 21:40:10 +00:00
|
|
|
//SetContactViewNotification notifies all connected contacts of view change
|
2022-07-22 19:28:14 +00:00
|
|
|
// notify single card of view change:
|
2022-02-09 18:04:32 +00:00
|
|
|
// card set or cleared from a group
|
|
|
|
// for each card in deleted group
|
2022-01-28 21:39:31 +00:00
|
|
|
func SetContactViewNotification(account *store.Account, card *store.Card) {
|
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
if card.Status != APPCardConnected {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// add new notification for card
|
|
|
|
notification := &store.Notification{
|
|
|
|
Node: card.Node,
|
|
|
|
Module: APPNotifyView,
|
|
|
|
GUID: card.GUID,
|
|
|
|
Token: card.OutToken,
|
|
|
|
Revision: card.ViewRevision,
|
|
|
|
}
|
|
|
|
|
|
|
|
if res := store.DB.Save(notification).Error; res != nil {
|
|
|
|
ErrMsg(res)
|
|
|
|
} else {
|
|
|
|
notify <- notification
|
|
|
|
}
|
2022-01-21 21:18:35 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 21:40:10 +00:00
|
|
|
//SetContactChannelNotification notifies all connected contacts of channel changes
|
2022-02-09 18:04:32 +00:00
|
|
|
// notify single card of channel change:
|
2022-02-12 16:28:36 +00:00
|
|
|
// for each card in updated channel data
|
2022-02-09 18:04:32 +00:00
|
|
|
func SetContactChannelNotification(account *store.Account, card *store.Card) {
|
2022-02-03 08:30:43 +00:00
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
if card.Status != APPCardConnected {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// add new notification for card
|
|
|
|
notification := &store.Notification{
|
|
|
|
Node: card.Node,
|
|
|
|
Module: APPNotifyChannel,
|
|
|
|
GUID: card.GUID,
|
|
|
|
Token: card.OutToken,
|
|
|
|
Revision: account.ChannelRevision,
|
|
|
|
}
|
|
|
|
|
|
|
|
if res := store.DB.Save(notification).Error; res != nil {
|
|
|
|
ErrMsg(res)
|
|
|
|
} else {
|
|
|
|
notify <- notification
|
|
|
|
}
|
2022-02-03 08:30:43 +00:00
|
|
|
}
|