2022-11-14 06:18:54 +00:00
|
|
|
package databag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"databag/internal/store"
|
|
|
|
"net/http"
|
2022-11-15 06:25:15 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2022-11-14 06:18:54 +00:00
|
|
|
)
|
|
|
|
|
2022-11-15 06:25:15 +00:00
|
|
|
type Payload struct {
|
|
|
|
Title string `json:"title"`
|
|
|
|
Body string `json:"body"`
|
2023-04-20 00:10:09 +00:00
|
|
|
Sound string `json:"sound"`
|
2022-11-15 06:25:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Message struct {
|
|
|
|
Notification Payload `json:"notification"`
|
|
|
|
To string `json:"to"`
|
2022-11-14 06:18:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//AddPushEvent notify account of event to push notify
|
|
|
|
func SetPushEvent(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
card, code, err := ParamContactToken(r, false)
|
|
|
|
if err != nil {
|
|
|
|
ErrResponse(w, code, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var event string
|
|
|
|
if err := ParseRequest(r, w, &event); err != nil {
|
|
|
|
ErrResponse(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-15 05:08:20 +00:00
|
|
|
SendPushEvent(card.Account, event)
|
2022-11-14 07:27:15 +00:00
|
|
|
WriteResponse(w, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
//SendPushEvent delivers notification to clients
|
2022-11-15 05:08:20 +00:00
|
|
|
func SendPushEvent(account store.Account, event string) {
|
2022-11-14 07:27:15 +00:00
|
|
|
|
2022-11-15 05:08:20 +00:00
|
|
|
// check if server supports push
|
|
|
|
if getBoolConfigValue(CNFPushSupported, true) != true {
|
|
|
|
return
|
2022-11-14 06:18:54 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 05:08:20 +00:00
|
|
|
// get all sessions supporting push for specified event
|
2023-06-08 05:50:49 +00:00
|
|
|
rows, err := store.DB.Table("sessions").Select("sessions.push_token, sessions.push_type, 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();
|
2022-11-15 05:08:20 +00:00
|
|
|
if err != nil {
|
|
|
|
ErrMsg(err);
|
|
|
|
return
|
|
|
|
}
|
2023-04-22 22:59:33 +00:00
|
|
|
|
|
|
|
tokens := make(map[string]bool)
|
2022-11-15 05:08:20 +00:00
|
|
|
for rows.Next() {
|
|
|
|
var pushToken string
|
2023-06-08 05:50:49 +00:00
|
|
|
var pushType string
|
2022-11-15 05:08:20 +00:00
|
|
|
var messageTitle string
|
|
|
|
var messageBody string
|
2023-04-22 22:59:33 +00:00
|
|
|
|
2023-06-08 05:50:49 +00:00
|
|
|
rows.Scan(&pushToken, &pushType, &messageTitle, &messageBody)
|
2024-01-17 04:44:31 +00:00
|
|
|
if pushToken == "" || pushToken == "null" {
|
|
|
|
continue;
|
|
|
|
}
|
2022-11-15 06:25:15 +00:00
|
|
|
|
2023-04-22 22:59:33 +00:00
|
|
|
if _, exists := tokens[pushToken]; !exists {
|
|
|
|
tokens[pushToken] = true;
|
2022-11-15 06:25:15 +00:00
|
|
|
|
2023-06-08 05:50:49 +00:00
|
|
|
if pushType == "up" {
|
|
|
|
message := []byte(messageTitle);
|
|
|
|
req, err := http.NewRequest(http.MethodPost, pushToken, bytes.NewBuffer(message))
|
|
|
|
if err != nil {
|
|
|
|
ErrMsg(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
ErrMsg(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
ErrMsg(errors.New("failed to push notification"));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
url := "https://fcm.googleapis.com/fcm/send"
|
|
|
|
payload := Payload{ Title: messageTitle, Body: messageBody, Sound: "default" };
|
|
|
|
message := Message{ Notification: payload, To: pushToken };
|
|
|
|
|
|
|
|
body, err := json.Marshal(message)
|
|
|
|
if err != nil {
|
|
|
|
ErrMsg(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(body))
|
|
|
|
if err != nil {
|
|
|
|
ErrMsg(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
req.Header.Set("Authorization", "key=AAAAkgDXt8c:APA91bEjH67QpUWU6uAfCIXLqm0kf6AdPNVICZPCcWbmgW9NGYIErAxMDTy4LEbe4ik93Ho4Z-AJNIhr6nXXKC9qKmyKkkYHJWAEVH47_FXBQV6rsoi9ZB_oiuV66XKKAy1V40GmvfaX")
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
ErrMsg(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
ErrMsg(errors.New("failed to push notification"));
|
|
|
|
}
|
2023-04-22 22:59:33 +00:00
|
|
|
}
|
2022-11-15 06:25:15 +00:00
|
|
|
}
|
2022-11-14 06:18:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|