feat: use alby hub latest version from alby api (#250)

This commit is contained in:
Roland 2024-07-11 10:02:17 +07:00 committed by GitHub
parent 340dcda220
commit 5b5077a5e3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 20 additions and 94 deletions

View file

@ -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 {

View file

@ -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")

View file

@ -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"`
}

View file

@ -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 ? (
<p>Alby Hub is up to date!</p>
) : (
<p>Alby Hub {info?.latestVersion} available!</p>
<p>
Alby Hub {albyMe?.hub.latest_version} available!
</p>
)}
</TooltipContent>
</Tooltip>

View file

@ -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 = {

View file

@ -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
}