2023-11-06 20:49:02 +08:00
|
|
|
package versionchecker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
storepb "github.com/usememos/memos/proto/gen/store"
|
2023-11-06 22:33:12 +08:00
|
|
|
"github.com/usememos/memos/server/profile"
|
2023-11-06 20:49:02 +08:00
|
|
|
"github.com/usememos/memos/server/version"
|
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
// nolint
|
|
|
|
type VersionChecker struct {
|
2023-11-06 22:33:12 +08:00
|
|
|
Store *store.Store
|
|
|
|
Profile *profile.Profile
|
2023-11-06 20:49:02 +08:00
|
|
|
}
|
|
|
|
|
2023-11-06 22:33:12 +08:00
|
|
|
func NewVersionChecker(store *store.Store, profile *profile.Profile) *VersionChecker {
|
2023-11-06 20:49:02 +08:00
|
|
|
return &VersionChecker{
|
2023-11-06 22:33:12 +08:00
|
|
|
Store: store,
|
|
|
|
Profile: profile,
|
2023-11-06 20:49:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*VersionChecker) GetLatestVersion() (string, error) {
|
|
|
|
response, err := http.Get("https://www.usememos.com/api/version")
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "failed to make http request")
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
_, err = buf.ReadFrom(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "fail to read response body")
|
|
|
|
}
|
|
|
|
|
|
|
|
version := ""
|
|
|
|
if err = json.Unmarshal(buf.Bytes(), &version); err != nil {
|
|
|
|
return "", errors.Wrap(err, "fail to unmarshal get version response")
|
|
|
|
}
|
|
|
|
return version, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *VersionChecker) Check(ctx context.Context) {
|
|
|
|
latestVersion, err := c.GetLatestVersion()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-11-06 22:33:12 +08:00
|
|
|
if !version.IsVersionGreaterThan(latestVersion, version.GetCurrentVersion(c.Profile.Mode)) {
|
2023-11-06 20:49:02 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
versionUpdateActivityType := store.ActivityTypeVersionUpdate
|
|
|
|
list, err := c.Store.ListActivities(ctx, &store.FindActivity{
|
|
|
|
Type: &versionUpdateActivityType,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-06 22:33:12 +08:00
|
|
|
shouldNotify := true
|
|
|
|
if len(list) > 0 {
|
|
|
|
latestVersionUpdateActivity := list[0]
|
|
|
|
if latestVersionUpdateActivity.Payload != nil && version.IsVersionGreaterOrEqualThan(latestVersionUpdateActivity.Payload.VersionUpdate.Version, latestVersion) {
|
|
|
|
shouldNotify = false
|
|
|
|
}
|
2023-11-06 20:49:02 +08:00
|
|
|
}
|
2023-11-06 22:33:12 +08:00
|
|
|
|
|
|
|
if !shouldNotify {
|
2023-11-06 20:49:02 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create version update activity and inbox message.
|
|
|
|
activity := &store.Activity{
|
|
|
|
CreatorID: store.SystemBotID,
|
|
|
|
Type: store.ActivityTypeVersionUpdate,
|
|
|
|
Level: store.ActivityLevelInfo,
|
|
|
|
Payload: &storepb.ActivityPayload{
|
|
|
|
VersionUpdate: &storepb.ActivityVersionUpdatePayload{
|
|
|
|
Version: latestVersion,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if _, err := c.Store.CreateActivity(ctx, activity); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hostUserRole := store.RoleHost
|
|
|
|
users, err := c.Store.ListUsers(ctx, &store.FindUser{
|
|
|
|
Role: &hostUserRole,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(users) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hostUser := users[0]
|
|
|
|
if _, err := c.Store.CreateInbox(ctx, &store.Inbox{
|
|
|
|
SenderID: store.SystemBotID,
|
|
|
|
ReceiverID: hostUser.ID,
|
|
|
|
Status: store.UNREAD,
|
|
|
|
Message: &storepb.InboxMessage{
|
|
|
|
Type: storepb.InboxMessage_TYPE_VERSION_UPDATE,
|
|
|
|
ActivityId: &activity.ID,
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
fmt.Printf("failed to create inbox: %s\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *VersionChecker) Start(ctx context.Context) {
|
|
|
|
c.Check(ctx)
|
|
|
|
|
|
|
|
// Schedule checker every 8 hours.
|
|
|
|
ticker := time.NewTicker(8 * time.Hour)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Check(ctx)
|
|
|
|
}
|
|
|
|
}
|