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

View File

@ -26,27 +26,30 @@ func SetPushEvent(w http.ResponseWriter, r *http.Request) {
return
}
if code, err := SendPushEvent(card.Account, event); err != nil {
ErrResponse(w, code, err);
return;
}
SendPushEvent(card.Account, event)
WriteResponse(w, nil)
}
//SendPushEvent delivers notification to clients
func SendPushEvent(account store.Account, event string) (int, error) {
func SendPushEvent(account store.Account, event string) {
messages := []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 {
return http.StatusInternalServerError, err
// check if server supports push
if getBoolConfigValue(CNFPushSupported, true) != true {
return
}
// send push notification for each
for _, message := range messages {
PrintMsg(message);
// get all sessions supporting push for specified event
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();
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
const APPNotifyChannel = "channel"
//APPPushNotify config for push notifications
const APPPushNotify = "push"
//APPNotifyView config for notification name for view
const APPNotifyView = "view"

View File

@ -77,6 +77,8 @@ func sendLocalNotification(notification *store.Notification) {
if err := NotifyViewRevision(&card, notification.Revision); err != nil {
ErrMsg(err)
}
} else if notification.Module == APPPushNotify {
SendPushEvent(card.Account, notification.Event)
} else {
LogMsg("unknown notification type")
}
@ -86,24 +88,37 @@ func sendRemoteNotification(notification *store.Notification) {
var module string
if notification.Module == APPNotifyProfile {
module = "profile"
module = "profile/revision"
} else if notification.Module == APPNotifyArticle {
module = "article"
module = "article/revision"
} else if notification.Module == APPNotifyChannel {
module = "channel"
module = "channel/revision"
} else if notification.Module == APPNotifyView {
module = "view"
module = "view/revision"
} else if notification.Module == APPPushNotify {
module = "notification"
} 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
var body []byte
var err error
if module == "notification" {
body, err = json.Marshal(notification.Event)
if err != nil {
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))
if err != nil {
ErrMsg(err)
@ -228,3 +243,28 @@ func SetContactChannelNotification(account *store.Account, card *store.Card) {
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) {
db, err := gorm.Open(sqlite.Open(path), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
Logger: logger.Default.LogMode(logger.Info),
})
if err != nil {
panic("failed to connect database")

View File

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