Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions alby/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 0 additions & 1 deletion api/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
9 changes: 5 additions & 4 deletions frontend/src/components/layouts/AppLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<>
Expand Down Expand Up @@ -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>
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ export interface InfoResponse {
albyUserIdentifier: string;
network?: Network;
version: string;
latestVersion: string;
}

export type Network = "bitcoin" | "testnet" | "signet";
Expand Down Expand Up @@ -323,6 +322,9 @@ export type AlbyMe = {
avatar: string;
keysend_pubkey: string;
shared_node: boolean;
hub: {
latest_version: string;
};
};

export type AlbyBalance = {
Expand Down
79 changes: 0 additions & 79 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading