From 5807660d0c762182b4e3305307591cc6be2caa59 Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Wed, 10 Jul 2024 22:32:50 +0700 Subject: [PATCH] feat: use alby hub latest version from alby api --- alby/models.go | 20 +++-- api/api.go | 1 - api/models.go | 1 - frontend/src/components/layouts/AppLayout.tsx | 9 ++- frontend/src/types.ts | 4 +- version/version.go | 79 ------------------- 6 files changed, 20 insertions(+), 94 deletions(-) diff --git a/alby/models.go b/alby/models.go index 625f8b4e..9f814f53 100644 --- a/alby/models.go +++ b/alby/models.go @@ -29,15 +29,19 @@ type AlbyPayRequest struct { Invoice string `json:"invoice"` } +type AlbyMeHub struct { + LatestVersion string `json:"latest_version"` +} type AlbyMe struct { - Identifier string `json:"identifier"` - NPub string `json:"nostr_pubkey"` - LightningAddress string `json:"lightning_address"` - Email string `json:"email"` - Name string `json:"name"` - Avatar string `json:"avatar"` - KeysendPubkey string `json:"keysend_pubkey"` - SharedNode bool `json:"shared_node"` + Identifier string `json:"identifier"` + NPub string `json:"nostr_pubkey"` + LightningAddress string `json:"lightning_address"` + Email string `json:"email"` + Name string `json:"name"` + Avatar string `json:"avatar"` + KeysendPubkey string `json:"keysend_pubkey"` + SharedNode bool `json:"shared_node"` + Hub AlbyMeHub `json:"hub"` } type AlbyBalance struct { diff --git a/api/api.go b/api/api.go index 51b2a789..38443aa1 100644 --- a/api/api.go +++ b/api/api.go @@ -580,7 +580,6 @@ func (api *api) GetInfo(ctx context.Context) (*InfoResponse, error) { info.AlbyAuthUrl = api.albyOAuthSvc.GetAuthUrl() info.OAuthRedirect = !api.cfg.GetEnv().IsDefaultClientId() info.Version = version.Tag - info.LatestVersion = version.GetLatestReleaseTag() albyUserIdentifier, err := api.albyOAuthSvc.GetUserIdentifier() if err != nil { logger.Logger.WithError(err).Error("Failed to get alby user identifier") diff --git a/api/models.go b/api/models.go index f922f9f6..baa5be0d 100644 --- a/api/models.go +++ b/api/models.go @@ -154,7 +154,6 @@ type InfoResponse struct { AlbyUserIdentifier string `json:"albyUserIdentifier"` AlbyAccountConnected bool `json:"albyAccountConnected"` Version string `json:"version"` - LatestVersion string `json:"latestVersion"` Network string `json:"network"` } diff --git a/frontend/src/components/layouts/AppLayout.tsx b/frontend/src/components/layouts/AppLayout.tsx index 96eb9eac..bde01f65 100644 --- a/frontend/src/components/layouts/AppLayout.tsx +++ b/frontend/src/components/layouts/AppLayout.tsx @@ -185,10 +185,9 @@ export default function AppLayout() { const upToDate = info?.version && - info.latestVersion && + albyMe?.hub.latest_version && info.version.startsWith("v") && - info.latestVersion.startsWith("v") && - info.version.substring(1) >= info.latestVersion.substring(1); + info.version.substring(1) >= albyMe?.hub.latest_version; return ( <> @@ -224,7 +223,9 @@ export default function AppLayout() { {upToDate ? (

Alby Hub is up to date!

) : ( -

Alby Hub {info?.latestVersion} available!

+

+ Alby Hub {albyMe?.hub.latest_version} available! +

)} diff --git a/frontend/src/types.ts b/frontend/src/types.ts index b7a7abbd..bc71726d 100644 --- a/frontend/src/types.ts +++ b/frontend/src/types.ts @@ -156,7 +156,6 @@ export interface InfoResponse { albyUserIdentifier: string; network?: Network; version: string; - latestVersion: string; } export type Network = "bitcoin" | "testnet" | "signet"; @@ -323,6 +322,9 @@ export type AlbyMe = { avatar: string; keysend_pubkey: string; shared_node: boolean; + hub: { + latest_version: string; + }; }; export type AlbyBalance = { diff --git a/version/version.go b/version/version.go index 8c897894..ec4069bc 100644 --- a/version/version.go +++ b/version/version.go @@ -1,82 +1,3 @@ package version -import ( - "encoding/json" - "io" - "net/http" - "time" - - "github.com/getAlby/hub/logger" - "github.com/sirupsen/logrus" -) - var Tag string = "" - -type githubRelease struct { - TagName string `json:"tag_name"` -} - -var latestRelease = "" -var lastVersionCheck = time.Time{} - -func GetLatestReleaseTag() string { - if latestRelease != "" && time.Since(lastVersionCheck) < 5*time.Minute { - return latestRelease - } - url := "https://api.github.com/repos/getAlby/hub/releases" - - client := http.Client{ - Timeout: time.Second * 10, - } - - req, err := http.NewRequest(http.MethodGet, url, nil) - if err != nil { - logger.Logger.WithError(err).WithFields(logrus.Fields{ - "url": url, - }).Error("Failed to create http request") - return "" - } - - res, err := client.Do(req) - if err != nil { - logger.Logger.WithError(err).WithFields(logrus.Fields{ - "url": url, - }).Error("Failed to send request") - return "" - } - - defer res.Body.Close() - - body, readErr := io.ReadAll(res.Body) - if readErr != nil { - logger.Logger.WithError(err).WithFields(logrus.Fields{ - "url": url, - }).Error("Failed to read response body") - return "" - } - - releases := []githubRelease{} - jsonErr := json.Unmarshal(body, &releases) - if jsonErr != nil { - logger.Logger.WithError(jsonErr).WithFields(logrus.Fields{ - "url": url, - }).Error("Failed to deserialize json") - return "" - } - - if len(releases) < 1 { - logger.Logger.Error("no github releases found") - return "" - } - - latestRelease = releases[0].TagName - - logger.Logger.WithFields(logrus.Fields{ - "latest": latestRelease, - "current": Tag, - }).Info("Found latest github release") - - lastVersionCheck = time.Now() - - return latestRelease -}