mirror of https://github.com/usememos/memos
chore: remove version update activity
parent
983323ceef
commit
0a429a899f
@ -1,140 +0,0 @@
|
||||
// Package version provides a runner to check the latest version of the application.
|
||||
package version
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/exp/slog"
|
||||
|
||||
storepb "github.com/usememos/memos/proto/gen/store"
|
||||
"github.com/usememos/memos/server/profile"
|
||||
"github.com/usememos/memos/server/version"
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
type Runner struct {
|
||||
Store *store.Store
|
||||
Profile *profile.Profile
|
||||
}
|
||||
|
||||
func NewRunner(store *store.Store, profile *profile.Profile) *Runner {
|
||||
return &Runner{
|
||||
Store: store,
|
||||
Profile: profile,
|
||||
}
|
||||
}
|
||||
|
||||
// Schedule checker every 8 hours.
|
||||
const runnerInterval = time.Hour * 8
|
||||
|
||||
func (r *Runner) Run(ctx context.Context) {
|
||||
ticker := time.NewTicker(runnerInterval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
r.RunOnce(ctx)
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Runner) RunOnce(ctx context.Context) {
|
||||
r.Check(ctx)
|
||||
}
|
||||
|
||||
func (r *Runner) Check(ctx context.Context) {
|
||||
latestVersion, err := r.GetLatestVersion()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if !version.IsVersionGreaterThan(latestVersion, version.GetCurrentVersion(r.Profile.Mode)) {
|
||||
return
|
||||
}
|
||||
|
||||
versionUpdateActivityType := store.ActivityTypeVersionUpdate
|
||||
list, err := r.Store.ListActivities(ctx, &store.FindActivity{
|
||||
Type: &versionUpdateActivityType,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
shouldNotify := true
|
||||
if len(list) > 0 {
|
||||
latestVersionUpdateActivity := list[0]
|
||||
if latestVersionUpdateActivity.Payload != nil && version.IsVersionGreaterOrEqualThan(latestVersionUpdateActivity.Payload.VersionUpdate.Version, latestVersion) {
|
||||
shouldNotify = false
|
||||
}
|
||||
}
|
||||
|
||||
if !shouldNotify {
|
||||
return
|
||||
}
|
||||
|
||||
// Create version update activity and inbox message.
|
||||
activity := &store.Activity{
|
||||
CreatorID: store.SystemBotID,
|
||||
Type: store.ActivityTypeVersionUpdate,
|
||||
Level: store.ActivityLevelInfo,
|
||||
Payload: &storepb.ActivityPayload{
|
||||
VersionUpdate: &storepb.ActivityVersionUpdatePayload{
|
||||
Version: latestVersion,
|
||||
},
|
||||
},
|
||||
}
|
||||
if _, err := r.Store.CreateActivity(ctx, activity); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
hostUserRole := store.RoleHost
|
||||
users, err := r.Store.ListUsers(ctx, &store.FindUser{
|
||||
Role: &hostUserRole,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if len(users) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
hostUser := users[0]
|
||||
if _, err := r.Store.CreateInbox(ctx, &store.Inbox{
|
||||
SenderID: store.SystemBotID,
|
||||
ReceiverID: hostUser.ID,
|
||||
Status: store.UNREAD,
|
||||
Message: &storepb.InboxMessage{
|
||||
Type: storepb.InboxMessage_VERSION_UPDATE,
|
||||
ActivityId: &activity.ID,
|
||||
},
|
||||
}); err != nil {
|
||||
slog.Error("failed to create inbox", slog.String("error", err.Error()))
|
||||
}
|
||||
}
|
||||
|
||||
func (*Runner) GetLatestVersion() (string, error) {
|
||||
response, err := http.Get("https://www.usememos.com/api/version")
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to make http request")
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
_, err = buf.ReadFrom(response.Body)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "fail to read response body")
|
||||
}
|
||||
|
||||
version := ""
|
||||
if err = json.Unmarshal(buf.Bytes(), &version); err != nil {
|
||||
return "", errors.Wrap(err, "fail to unmarshal get version response")
|
||||
}
|
||||
return version, nil
|
||||
}
|
@ -1 +0,0 @@
|
||||
package version
|
@ -1,101 +0,0 @@
|
||||
import { Tooltip } from "@mui/joy";
|
||||
import { ArrowUpIcon, InboxIcon } from "lucide-react";
|
||||
import { useEffect, useState } from "react";
|
||||
import toast from "react-hot-toast";
|
||||
import { activityServiceClient } from "@/grpcweb";
|
||||
import { activityNamePrefix } from "@/store/v1";
|
||||
import { userStore } from "@/store/v2";
|
||||
import { Activity } from "@/types/proto/api/v1/activity_service";
|
||||
import { Inbox, Inbox_Status } from "@/types/proto/api/v1/inbox_service";
|
||||
import { cn } from "@/utils";
|
||||
import { useTranslate } from "@/utils/i18n";
|
||||
|
||||
interface Props {
|
||||
inbox: Inbox;
|
||||
}
|
||||
|
||||
const VersionUpdateMessage = ({ inbox }: Props) => {
|
||||
const t = useTranslate();
|
||||
const [activity, setActivity] = useState<Activity | undefined>(undefined);
|
||||
|
||||
useEffect(() => {
|
||||
if (!inbox.activityId) {
|
||||
return;
|
||||
}
|
||||
|
||||
(async () => {
|
||||
const activity = await activityServiceClient.getActivity({
|
||||
name: `${activityNamePrefix}${inbox.activityId}`,
|
||||
});
|
||||
setActivity(activity);
|
||||
})();
|
||||
}, [inbox.activityId]);
|
||||
|
||||
const handleNavigate = () => {
|
||||
if (!activity?.payload?.versionUpdate?.version) {
|
||||
return;
|
||||
}
|
||||
|
||||
window.open(`https://github.com/usememos/memos/releases/tag/v${activity?.payload?.versionUpdate?.version}`);
|
||||
if (inbox.status === Inbox_Status.UNREAD) {
|
||||
handleArchiveMessage(true);
|
||||
}
|
||||
};
|
||||
|
||||
const handleArchiveMessage = async (silence = false) => {
|
||||
await userStore.updateInbox(
|
||||
{
|
||||
name: inbox.name,
|
||||
status: Inbox_Status.ARCHIVED,
|
||||
},
|
||||
["status"],
|
||||
);
|
||||
if (!silence) {
|
||||
toast.success(t("message.archived-successfully"));
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="w-full flex flex-row justify-start items-start gap-3">
|
||||
<div
|
||||
className={cn(
|
||||
"shrink-0 mt-2 p-2 rounded-full border",
|
||||
inbox.status === Inbox_Status.UNREAD
|
||||
? "border-blue-600 text-blue-600 bg-blue-50 dark:bg-zinc-800"
|
||||
: "border-gray-500 text-gray-500 bg-gray-50 dark:bg-zinc-800",
|
||||
)}
|
||||
>
|
||||
<Tooltip title={"Update"} placement="bottom">
|
||||
<ArrowUpIcon className="w-4 sm:w-5 h-auto" />
|
||||
</Tooltip>
|
||||
</div>
|
||||
<div
|
||||
className={cn(
|
||||
"border w-full p-2 px-3 rounded-lg flex flex-col justify-start items-start gap-1 dark:border-zinc-700 hover:bg-gray-100 dark:hover:bg-zinc-700",
|
||||
inbox.status !== Inbox_Status.UNREAD && "opacity-60",
|
||||
)}
|
||||
>
|
||||
<div className="w-full flex flex-row justify-between items-center">
|
||||
<span className="text-sm text-gray-500">{inbox.createTime?.toLocaleString()}</span>
|
||||
<div>
|
||||
{inbox.status === Inbox_Status.UNREAD && (
|
||||
<Tooltip title={t("common.archive")} placement="top">
|
||||
<InboxIcon className="w-4 h-auto cursor-pointer text-gray-400 hover:text-blue-600" onClick={() => handleArchiveMessage()} />
|
||||
</Tooltip>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<p
|
||||
className="text-base leading-tight cursor-pointer text-gray-500 dark:text-gray-400 hover:underline hover:text-blue-600"
|
||||
onClick={handleNavigate}
|
||||
>
|
||||
{t("inbox.version-update", {
|
||||
version: activity?.payload?.versionUpdate?.version,
|
||||
})}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default VersionUpdateMessage;
|
Loading…
Reference in New Issue