A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. (PERSONAL FORK)
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Rewrite rhy to call github direct

Signed-off-by: Jacob Abel <jacobabel@nullpo.dev>

+99 -15
+21
kernel/util/cloud.go
··· 64 64 return northAmericaForumAssetsServer 65 65 } 66 66 67 + func GetGitHubApiEndpoint() string { 68 + return GitHubApiEndpoint 69 + } 70 + 71 + func GetGitHubSiyuanRepo() string { 72 + return GitHubSiyuanRepo 73 + } 74 + 75 + func GetGitHubBazaarRepo() string { 76 + return GitHubBazaarRepo 77 + } 78 + 79 + func GetGitHubBazaarBranch() string { 80 + return GitHubBazaarBranch 81 + } 82 + 67 83 const ( 68 84 chinaServer = "https://chinaServer.invalid" // 中国大陆云端服务地址,阿里云负载均衡,用于接口(数据同步文件上传、下载会走七牛云 OSS ChinaSyncServer) 69 85 chinaWebSocketServer = "wss://chinaWebSocketServer.invalid" // 中国大陆云端服务地址,阿里云负载均衡,用于接口(数据同步文件上传、下载会走七牛云 OSS ChinaSyncServer) ··· 78 94 northAmericaCloudAssetsServer = "https://northAmericaCloudAssetsServer.invalid/" // 北美云端图床服务地址,用于导出预览模式下订阅会员渲染图床 79 95 northAmericaAccountServer = "https://northAmericaAccountServer.invalid" // 流云服务地址,用于账号登录、分享发布帖子 80 96 northAmericaForumAssetsServer = "https://northAmericaForumAssetsServer.invalid/" // 北美云端图床服务地址,用于发布文章到社区 97 + 98 + GitHubApiEndpoint = "https://api.github.com" 99 + GitHubSiyuanRepo = "siyuan-note/siyuan" 100 + GitHubBazaarRepo = "siyuan-note/bazaar" 101 + GitHubBazaarBranch = "main" 81 102 82 103 BazaarStatServer = "https://bazaar.b3logfile.com" // 集市包统计服务地址,七牛云,全球 CDN 83 104 BazaarOSSServer = "https://oss.b3logfile.com" // 云端对象存储地址,七牛云,仅用于读取集市包,全球 CDN
+78 -15
kernel/util/rhy.go
··· 22 22 "sync" 23 23 "time" 24 24 25 + "github.com/imroc/req/v3" 25 26 "github.com/siyuan-note/httpclient" 26 27 "github.com/siyuan-note/logging" 27 28 ) 28 29 30 + 31 + var cachedBazaarResult = map[string]interface{}{} 32 + var bazaarResultCacheTime int64 33 + 34 + //var cachedReleaseResult = map[string]interface{}{} 35 + //var releaseResultCacheTime int64 36 + 37 + var aggregateResultCacheTime int64 38 + var rhyResultLock = sync.Mutex{} 29 39 var cachedRhyResult = map[string]interface{}{} 30 - var rhyResultCacheTime int64 31 - var rhyResultLock = sync.Mutex{} 40 + 41 + func NewGitHubApiRequest() *req.Request { 42 + return httpclient.NewCloudRequest30s().SetHeader("Accept", "application/vnd.github+json").SetHeader("X-GitHub-Api-Version", "2022-11-28") 43 + } 44 + 45 + // Query GitHub Releases for the Client 46 + //func GetReleaseResult(force bool, cacheDuration int64) (map[string]interface{}, error) { 47 + // 48 + // now := time.Now().Unix() 49 + // if cacheDuration >= now - releaseResultCacheTime && !force && 0 < len(cachedReleaseResult) { 50 + // return cachedRhyResult, nil 51 + // } 52 + // 53 + // request := NewGitHubApiRequest() 54 + // resp, err := request.SetSuccessResult(&cachedReleaseResult).Get(GetGitHubApiEndpoint() + "/repo/" + GetGitHubSiyuanRepo() + "/releases/tags/v" + Ver) 55 + // if err != nil { 56 + // logging.LogErrorf("GitHub Release: query failed: %s", err) 57 + // return nil, err 58 + // } 59 + // if 200 != resp.StatusCode { 60 + // msg := fmt.Sprintf("GitHub Release: query failed: %d", resp.StatusCode) 61 + // logging.LogErrorf(msg) 62 + // return nil, errors.New(msg) 63 + // } 64 + // releaseResultCacheTime = now 65 + // return cachedReleaseResult, nil 66 + //} 67 + 68 + // Query the GitHub Bazaar Repo's Main Branch 69 + func GetBazaarResult(force bool, cacheDuration int64) (map[string]interface{}, error) { 70 + 71 + now := time.Now().Unix() 72 + if cacheDuration >= now - bazaarResultCacheTime && !force && 0 < len(cachedBazaarResult) { 73 + return cachedBazaarResult, nil 74 + } 75 + 76 + uri := GetGitHubApiEndpoint() + "/repos/" + GetGitHubBazaarRepo() + "/branches/" + GetGitHubBazaarBranch() 77 + request := NewGitHubApiRequest() 78 + resp, err := request.SetSuccessResult(&cachedBazaarResult).Get(uri) 79 + if err != nil { 80 + logging.LogErrorf("GitHub Bazaar: query failed: %s", err) 81 + return nil, err 82 + } 83 + if 200 != resp.StatusCode { 84 + msg := fmt.Sprintf("GitHub Bazaar: query failed with code: %d", resp.StatusCode) 85 + logging.LogErrorf(msg) 86 + return nil, errors.New(msg) 87 + } 88 + bazaarResultCacheTime = now 89 + return cachedBazaarResult, nil 90 + } 32 91 92 + // Function Replaced to Directly Query Github. 33 93 func GetRhyResult(force bool) (map[string]interface{}, error) { 94 + 34 95 rhyResultLock.Lock() 35 96 defer rhyResultLock.Unlock() 36 97 ··· 38 99 if ContainerDocker == Container { 39 100 cacheDuration = int64(3600 * 24) 40 101 } 41 - 42 102 now := time.Now().Unix() 43 - if cacheDuration >= now-rhyResultCacheTime && !force && 0 < len(cachedRhyResult) { 103 + if cacheDuration >= now - aggregateResultCacheTime && !force && 0 < len(cachedBazaarResult) { 44 104 return cachedRhyResult, nil 45 105 } 46 106 47 - request := httpclient.NewCloudRequest30s() 48 - resp, err := request.SetSuccessResult(&cachedRhyResult).Get(GetCloudServer() + "/apis/siyuan/version?ver=" + Ver) 49 - if err != nil { 50 - logging.LogErrorf("get version info failed: %s", err) 51 - return nil, err 107 + //respRelease, errRelease := GetReleaseResult(force, cacheDuration) 108 + //if errRelease != nil { 109 + // nil, errRelease 110 + //} 111 + 112 + respBazaar, errBazaar := GetBazaarResult(force, cacheDuration) 113 + if errBazaar != nil { 114 + return nil, errBazaar 52 115 } 53 - if 200 != resp.StatusCode { 54 - msg := fmt.Sprintf("get rhy result failed: %d", resp.StatusCode) 55 - logging.LogErrorf(msg) 56 - return nil, errors.New(msg) 57 - } 58 - rhyResultCacheTime = now 116 + 117 + cachedRhyResult = make(map[string]interface{}) 118 + cachedRhyResult["bazaar"] = respBazaar["commit"].(map[string]interface{})["sha"] 119 + 120 + //aggregateResultCacheTime = min(releaseResultCacheTime, bazaarResultCacheTime) 121 + aggregateResultCacheTime = bazaarResultCacheTime 59 122 return cachedRhyResult, nil 60 123 } 61 124