runner.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Package version provides a runner to check the latest version of the application.
  2. package version
  3. import (
  4. "bytes"
  5. "context"
  6. "encoding/json"
  7. "net/http"
  8. "time"
  9. "github.com/pkg/errors"
  10. "golang.org/x/exp/slog"
  11. storepb "github.com/usememos/memos/proto/gen/store"
  12. "github.com/usememos/memos/server/profile"
  13. "github.com/usememos/memos/server/version"
  14. "github.com/usememos/memos/store"
  15. )
  16. type Runner struct {
  17. Store *store.Store
  18. Profile *profile.Profile
  19. }
  20. func NewRunner(store *store.Store, profile *profile.Profile) *Runner {
  21. return &Runner{
  22. Store: store,
  23. Profile: profile,
  24. }
  25. }
  26. // Schedule checker every 8 hours.
  27. const runnerInterval = time.Hour * 8
  28. func (r *Runner) Run(ctx context.Context) {
  29. ticker := time.NewTicker(runnerInterval)
  30. defer ticker.Stop()
  31. for {
  32. select {
  33. case <-ticker.C:
  34. r.RunOnce(ctx)
  35. case <-ctx.Done():
  36. return
  37. }
  38. }
  39. }
  40. func (r *Runner) RunOnce(ctx context.Context) {
  41. r.Check(ctx)
  42. }
  43. func (r *Runner) Check(ctx context.Context) {
  44. latestVersion, err := r.GetLatestVersion()
  45. if err != nil {
  46. return
  47. }
  48. if !version.IsVersionGreaterThan(latestVersion, version.GetCurrentVersion(r.Profile.Mode)) {
  49. return
  50. }
  51. versionUpdateActivityType := store.ActivityTypeVersionUpdate
  52. list, err := r.Store.ListActivities(ctx, &store.FindActivity{
  53. Type: &versionUpdateActivityType,
  54. })
  55. if err != nil {
  56. return
  57. }
  58. shouldNotify := true
  59. if len(list) > 0 {
  60. latestVersionUpdateActivity := list[0]
  61. if latestVersionUpdateActivity.Payload != nil && version.IsVersionGreaterOrEqualThan(latestVersionUpdateActivity.Payload.VersionUpdate.Version, latestVersion) {
  62. shouldNotify = false
  63. }
  64. }
  65. if !shouldNotify {
  66. return
  67. }
  68. // Create version update activity and inbox message.
  69. activity := &store.Activity{
  70. CreatorID: store.SystemBotID,
  71. Type: store.ActivityTypeVersionUpdate,
  72. Level: store.ActivityLevelInfo,
  73. Payload: &storepb.ActivityPayload{
  74. VersionUpdate: &storepb.ActivityVersionUpdatePayload{
  75. Version: latestVersion,
  76. },
  77. },
  78. }
  79. if _, err := r.Store.CreateActivity(ctx, activity); err != nil {
  80. return
  81. }
  82. hostUserRole := store.RoleHost
  83. users, err := r.Store.ListUsers(ctx, &store.FindUser{
  84. Role: &hostUserRole,
  85. })
  86. if err != nil {
  87. return
  88. }
  89. if len(users) == 0 {
  90. return
  91. }
  92. hostUser := users[0]
  93. if _, err := r.Store.CreateInbox(ctx, &store.Inbox{
  94. SenderID: store.SystemBotID,
  95. ReceiverID: hostUser.ID,
  96. Status: store.UNREAD,
  97. Message: &storepb.InboxMessage{
  98. Type: storepb.InboxMessage_VERSION_UPDATE,
  99. ActivityId: &activity.ID,
  100. },
  101. }); err != nil {
  102. slog.Error("failed to create inbox", slog.String("error", err.Error()))
  103. }
  104. }
  105. func (*Runner) GetLatestVersion() (string, error) {
  106. response, err := http.Get("https://www.usememos.com/api/version")
  107. if err != nil {
  108. return "", errors.Wrap(err, "failed to make http request")
  109. }
  110. defer response.Body.Close()
  111. buf := &bytes.Buffer{}
  112. _, err = buf.ReadFrom(response.Body)
  113. if err != nil {
  114. return "", errors.Wrap(err, "fail to read response body")
  115. }
  116. version := ""
  117. if err = json.Unmarshal(buf.Bytes(), &version); err != nil {
  118. return "", errors.Wrap(err, "fail to unmarshal get version response")
  119. }
  120. return version, nil
  121. }