mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 03:29:16 +00:00
notifying remote contacts
This commit is contained in:
parent
a33fd3a3f9
commit
1ae7544cdb
@ -1,7 +1,10 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"net/http"
|
||||
"encoding/json"
|
||||
"gorm.io/gorm"
|
||||
"databag/internal/store"
|
||||
)
|
||||
@ -78,7 +81,41 @@ func SendLocalNotification(notification *store.Notification) {
|
||||
}
|
||||
|
||||
func SendRemoteNotification(notification *store.Notification) {
|
||||
// TODO send remote notification
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
// notify all cards of profile change
|
||||
@ -97,6 +134,7 @@ func SetProfileNotification(account *store.Account) {
|
||||
notification := &store.Notification{
|
||||
Node: card.Node,
|
||||
Module: APP_NOTIFYPROFILE,
|
||||
Guid: card.Guid,
|
||||
Token: card.OutToken,
|
||||
Revision: account.ProfileRevision,
|
||||
}
|
||||
@ -125,6 +163,7 @@ func SetContactArticleNotification(account *store.Account, card *store.Card) {
|
||||
notification := &store.Notification{
|
||||
Node: card.Node,
|
||||
Module: APP_NOTIFYARTICLE,
|
||||
Guid: card.Guid,
|
||||
Token: card.OutToken,
|
||||
Revision: account.ArticleRevision,
|
||||
}
|
||||
@ -149,6 +188,7 @@ func SetContactViewNotification(account *store.Account, card *store.Card) {
|
||||
notification := &store.Notification{
|
||||
Node: card.Node,
|
||||
Module: APP_NOTIFYVIEW,
|
||||
Guid: card.Guid,
|
||||
Token: card.OutToken,
|
||||
Revision: card.ViewRevision,
|
||||
}
|
||||
@ -172,6 +212,7 @@ func SetContactChannelNotification(account *store.Account, card *store.Card) {
|
||||
notification := &store.Notification{
|
||||
Node: card.Node,
|
||||
Module: APP_NOTIFYCHANNEL,
|
||||
Guid: card.Guid,
|
||||
Token: card.OutToken,
|
||||
Revision: account.ChannelRevision,
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ func AutoMigrate(db *gorm.DB) {
|
||||
type Notification struct {
|
||||
ID uint `gorm:"primaryKey;not null;unique;autoIncrement"`
|
||||
Node string `gorm:"not null"`
|
||||
Guid string `gorm:"not null"`
|
||||
Module string `gorm:"not null"`
|
||||
Token string `gorm:"not null"`
|
||||
Revision int64 `gorm:"not null"`
|
||||
|
Loading…
Reference in New Issue
Block a user