pushing contact events

This commit is contained in:
Roland Osborne 2022-11-14 21:08:20 -08:00
parent 160429671e
commit c1617fe9a0
6 changed files with 78 additions and 25 deletions

View File

@ -99,6 +99,9 @@ func SetOpenMessage(w http.ResponseWriter, r *http.Request) {
ErrResponse(w, http.StatusInternalServerError, err) ErrResponse(w, http.StatusInternalServerError, err)
return return
} }
// push event
SendPushEvent(account, "contact.updateCard");
} else { } else {
// update profile if revision changed // update profile if revision changed
@ -154,6 +157,9 @@ func SetOpenMessage(w http.ResponseWriter, r *http.Request) {
ErrResponse(w, http.StatusInternalServerError, err) ErrResponse(w, http.StatusInternalServerError, err)
return return
} }
// push event
SendPushEvent(account, "contact.addCard");
} }
status := &ContactStatus{ status := &ContactStatus{

View File

@ -26,27 +26,30 @@ func SetPushEvent(w http.ResponseWriter, r *http.Request) {
return return
} }
if code, err := SendPushEvent(card.Account, event); err != nil { SendPushEvent(card.Account, event)
ErrResponse(w, code, err);
return;
}
WriteResponse(w, nil) WriteResponse(w, nil)
} }
//SendPushEvent delivers notification to clients //SendPushEvent delivers notification to clients
func SendPushEvent(account store.Account, event string) (int, error) { func SendPushEvent(account store.Account, event string) {
messages := []push{} // check if server supports push
if err := store.DB.Model(&store.Session{}).Select("sessions.push_token, push_events.message_title, push_events.message_body").Joins("left join push_events on push_events.session_id = session.id").Where("sessions.account_id = ? AND session.push_enabled = ? AND push_events.event = ?", account.ID, true, event).Scan(messages).Error; err != nil { if getBoolConfigValue(CNFPushSupported, true) != true {
return http.StatusInternalServerError, err return
} }
// send push notification for each // get all sessions supporting push for specified event
for _, message := range messages { rows, err := store.DB.Table("sessions").Select("sessions.push_token, push_events.message_title, push_events.message_body").Joins("left join push_events on push_events.session_id = sessions.id").Where("sessions.account_id = ? AND sessions.push_enabled = ? AND push_events.event = ?", account.GUID, true, event).Rows();
PrintMsg(message); if err != nil {
ErrMsg(err);
return
}
for rows.Next() {
PrintMsg("IN ROW");
var pushToken string
var messageTitle string
var messageBody string
rows.Scan(&pushToken, &messageTitle, &messageBody)
} }
return http.StatusOK, nil
} }

View File

@ -75,6 +75,9 @@ const APPNotifyArticle = "article"
//APPNotifyChannel config for notification name for channel //APPNotifyChannel config for notification name for channel
const APPNotifyChannel = "channel" const APPNotifyChannel = "channel"
//APPPushNotify config for push notifications
const APPPushNotify = "push"
//APPNotifyView config for notification name for view //APPNotifyView config for notification name for view
const APPNotifyView = "view" const APPNotifyView = "view"

View File

@ -77,6 +77,8 @@ func sendLocalNotification(notification *store.Notification) {
if err := NotifyViewRevision(&card, notification.Revision); err != nil { if err := NotifyViewRevision(&card, notification.Revision); err != nil {
ErrMsg(err) ErrMsg(err)
} }
} else if notification.Module == APPPushNotify {
SendPushEvent(card.Account, notification.Event)
} else { } else {
LogMsg("unknown notification type") LogMsg("unknown notification type")
} }
@ -86,24 +88,37 @@ func sendRemoteNotification(notification *store.Notification) {
var module string var module string
if notification.Module == APPNotifyProfile { if notification.Module == APPNotifyProfile {
module = "profile" module = "profile/revision"
} else if notification.Module == APPNotifyArticle { } else if notification.Module == APPNotifyArticle {
module = "article" module = "article/revision"
} else if notification.Module == APPNotifyChannel { } else if notification.Module == APPNotifyChannel {
module = "channel" module = "channel/revision"
} else if notification.Module == APPNotifyView { } else if notification.Module == APPNotifyView {
module = "view" module = "view/revision"
} else if notification.Module == APPPushNotify {
module = "notification"
} else { } else {
LogMsg("unknown notification type") LogMsg("unknown notification type")
return return
} }
body, err := json.Marshal(notification.Revision) var body []byte
if err != nil { var err error
ErrMsg(err) if module == "notification" {
return body, err = json.Marshal(notification.Event)
} if err != nil {
url := "https://" + notification.Node + "/contact/" + module + "/revision?contact=" + notification.GUID + "." + notification.Token ErrMsg(err)
return
}
} else {
body, err = json.Marshal(notification.Revision)
if err != nil {
ErrMsg(err)
return
}
}
url := "https://" + notification.Node + "/contact/" + module + "?contact=" + notification.GUID + "." + notification.Token
req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(body)) req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(body))
if err != nil { if err != nil {
ErrMsg(err) ErrMsg(err)
@ -228,3 +243,28 @@ func SetContactChannelNotification(account *store.Account, card *store.Card) {
notify <- notification notify <- notification
} }
} }
//SetContactPushNotification notifies contacts of push event
func SetContactPushNotification(card *store.Card, event string) {
if card.Status != APPCardConnected {
return
}
// add new notification for card
notification := &store.Notification{
Node: card.Node,
Module: APPPushNotify,
GUID: card.GUID,
Token: card.OutToken,
Revision: 0,
Event: event,
};
if res := store.DB.Save(notification).Error; res != nil {
ErrMsg(res)
} else {
notify <- notification
}
}

View File

@ -10,7 +10,7 @@ var DB *gorm.DB;
func SetPath(path string) { func SetPath(path string) {
db, err := gorm.Open(sqlite.Open(path), &gorm.Config{ db, err := gorm.Open(sqlite.Open(path), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent), Logger: logger.Default.LogMode(logger.Info),
}) })
if err != nil { if err != nil {
panic("failed to connect database") panic("failed to connect database")

View File

@ -35,6 +35,7 @@ type Notification struct {
Module string `gorm:"not null"` Module string `gorm:"not null"`
Token string `gorm:"not null"` Token string `gorm:"not null"`
Revision int64 `gorm:"not null"` Revision int64 `gorm:"not null"`
Event string
} }
type Config struct { type Config struct {