This commit is contained in:
Liang Ding 2022-07-14 10:39:53 +08:00
parent d1fb78ebb1
commit f825fefa43
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
8 changed files with 96 additions and 22 deletions

View file

@ -21,6 +21,8 @@ import (
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
@ -150,6 +152,8 @@ func AutoRefreshUser() {
if !subscriptionExpirationReminded {
subscriptionExpirationReminded = true
go func() {
defer util.Recover()
if "ios" == util.Container {
return
}
@ -170,13 +174,65 @@ func AutoRefreshUser() {
}()
}
if nil != Conf.User {
time.Sleep(3 * time.Minute)
go func() {
defer util.Recover()
if nil != Conf.User {
RefreshUser(Conf.User.UserToken)
time.Sleep(3 * time.Minute)
if nil != Conf.User {
RefreshUser(Conf.User.UserToken)
}
subscriptionExpirationReminded = false
}
subscriptionExpirationReminded = false
}
}()
go func() {
defer util.Recover()
time.Sleep(1 * time.Minute)
announcementConf := filepath.Join(util.HomeDir, ".config", "siyuan", "announcement.json")
var existingAnnouncements, newAnnouncements []*Announcement
if gulu.File.IsExist(announcementConf) {
data, err := os.ReadFile(announcementConf)
if nil != err {
util.LogErrorf("read announcement conf failed: %s", err)
return
}
if err = gulu.JSON.UnmarshalJSON(data, &existingAnnouncements); nil != err {
util.LogErrorf("unmarshal announcement conf failed: %s", err)
return
}
}
for _, announcement := range GetAnnouncements() {
var exist bool
for _, existingAnnouncement := range existingAnnouncements {
if announcement.Id == existingAnnouncement.Id {
exist = true
break
}
}
if !exist {
existingAnnouncements = append(existingAnnouncements, announcement)
newAnnouncements = append(newAnnouncements, announcement)
}
}
data, err := gulu.JSON.MarshalJSON(existingAnnouncements)
if nil != err {
util.LogErrorf("marshal announcement conf failed: %s", err)
return
}
if err = os.WriteFile(announcementConf, data, 0644); nil != err {
util.LogErrorf("write announcement conf failed: %s", err)
return
}
for _, newAnnouncement := range newAnnouncements {
util.PushMsg(fmt.Sprintf(Conf.Language(11), newAnnouncement.URL, newAnnouncement.Title), 0)
}
}()
<-refreshUserTicker.C
}
}

View file

@ -27,15 +27,33 @@ var (
checkUpdateLock = &sync.Mutex{}
)
func CheckUpdate(showMsg bool) {
if !showMsg {
type Announcement struct {
Id string `json:"id"`
Title string `json:"title"`
URL string `json:"url"`
}
func GetAnnouncements() (ret []*Announcement) {
result, err := util.GetRhyResult(false)
if nil != err {
util.LogErrorf("get ")
return
}
if "ios" == util.Container {
if showMsg {
util.PushMsg(Conf.Language(36), 5000)
}
announcements := result["announcement"].([]interface{})
for _, announcement := range announcements {
ann := announcement.(map[string]interface{})
ret = append(ret, &Announcement{
Id: ann["id"].(string),
Title: ann["title"].(string),
URL: ann["url"].(string),
})
}
return
}
func CheckUpdate(showMsg bool) {
if !showMsg {
return
}