2020-12-04 13:10:32 +01:00
|
|
|
package hub
|
2020-10-04 22:22:53 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"runtime"
|
|
|
|
"time"
|
2020-10-08 08:52:03 +02:00
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2020-10-04 22:22:53 +02:00
|
|
|
)
|
|
|
|
|
2020-12-04 13:10:32 +01:00
|
|
|
var FeedbackURL = ServiceURL + "/%s/feedback"
|
2020-10-04 22:22:53 +02:00
|
|
|
|
|
|
|
type Feedback struct {
|
|
|
|
Category string `json:"Category"`
|
2020-10-05 07:40:11 +02:00
|
|
|
Subject string `json:"Subject"`
|
2020-10-04 22:22:53 +02:00
|
|
|
Message string `json:"Message"`
|
|
|
|
UserName string `json:"UserName"`
|
|
|
|
UserEmail string `json:"UserEmail"`
|
|
|
|
UserAgent string `json:"UserAgent"`
|
2020-10-05 07:40:11 +02:00
|
|
|
ApiKey string `json:"ApiKey"`
|
|
|
|
ClientVersion string `json:"ClientVersion"`
|
2020-12-05 06:21:16 +01:00
|
|
|
ClientSerial string `json:"ClientSerial"`
|
2020-10-05 07:40:11 +02:00
|
|
|
ClientOS string `json:"ClientOS"`
|
|
|
|
ClientArch string `json:"ClientArch"`
|
|
|
|
ClientCPU int `json:"ClientCPU"`
|
2020-10-04 22:22:53 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 13:10:32 +01:00
|
|
|
// NewFeedback creates a new hub feedback instance.
|
2020-12-05 06:21:16 +01:00
|
|
|
func NewFeedback(version, serial string) *Feedback {
|
2020-10-04 22:22:53 +02:00
|
|
|
return &Feedback{
|
|
|
|
ClientVersion: version,
|
2020-12-05 06:21:16 +01:00
|
|
|
ClientSerial: serial,
|
2020-10-04 22:22:53 +02:00
|
|
|
ClientOS: runtime.GOOS,
|
|
|
|
ClientArch: runtime.GOARCH,
|
|
|
|
ClientCPU: runtime.NumCPU(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) SendFeedback(f form.Feedback) (err error) {
|
2020-12-05 06:21:16 +01:00
|
|
|
feedback := NewFeedback(c.Version, c.Serial)
|
2020-10-04 22:22:53 +02:00
|
|
|
feedback.Category = f.Category
|
2021-09-23 23:46:17 +02:00
|
|
|
feedback.Subject = txt.Shorten(f.Message, 50, "...")
|
2020-10-04 22:22:53 +02:00
|
|
|
feedback.Message = f.Message
|
|
|
|
feedback.UserName = f.UserName
|
|
|
|
feedback.UserEmail = f.UserEmail
|
|
|
|
feedback.UserAgent = f.UserAgent
|
2020-10-05 07:40:11 +02:00
|
|
|
feedback.ApiKey = c.Key
|
2020-10-04 22:22:53 +02:00
|
|
|
|
|
|
|
client := &http.Client{Timeout: 60 * time.Second}
|
|
|
|
url := fmt.Sprintf(FeedbackURL, c.Key)
|
|
|
|
method := http.MethodPost
|
2021-11-20 19:14:00 +01:00
|
|
|
|
2020-10-04 22:22:53 +02:00
|
|
|
var req *http.Request
|
|
|
|
|
2020-10-08 08:52:03 +02:00
|
|
|
log.Debugf("sending feedback to %s", ApiHost())
|
2020-10-04 22:22:53 +02:00
|
|
|
|
|
|
|
if j, err := json.Marshal(feedback); err != nil {
|
|
|
|
return err
|
|
|
|
} else if req, err = http.NewRequest(method, url, bytes.NewReader(j)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("Accept-Language", f.UserLocales)
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
|
|
|
|
var r *http.Response
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
r, err = client.Do(req)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if r.StatusCode >= 400 {
|
2020-10-08 08:52:03 +02:00
|
|
|
err = fmt.Errorf("sending feedback to %s failed (error %d)", ApiHost(), r.StatusCode)
|
2020-10-04 22:22:53 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewDecoder(r.Body).Decode(c)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|