Last seen at

This commit is contained in:
LittleSheep 2024-06-26 20:28:12 +08:00
parent 8c2649e29d
commit b1f6cf8f6e
5 changed files with 34 additions and 8 deletions

14
.idea/workspace.xml generated
View File

@ -4,8 +4,12 @@
<option name="autoReloadType" value="ALL" /> <option name="autoReloadType" value="ALL" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":sparkles: Status system"> <list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Fix online condition">
<change beforePath="$PROJECT_DIR$/pkg/internal/services/statuses.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/statuses.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/models/profiles.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/models/profiles.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/statuses_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/statuses_api.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/accounts.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/accounts.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/connections.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/connections.go" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -151,8 +155,6 @@
</option> </option>
</component> </component>
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<MESSAGE value=":sparkles: Consul registration" />
<MESSAGE value=":wastebasket: Remove HTTP provision to consul" />
<MESSAGE value=":sparkles: Drop direct connection and uses consul" /> <MESSAGE value=":sparkles: Drop direct connection and uses consul" />
<MESSAGE value=":technologist: Add the server side Hyper SDK" /> <MESSAGE value=":technologist: Add the server side Hyper SDK" />
<MESSAGE value=":recycle: Improve code structure and much easier to read&#10;:bug: Fix auth middleware" /> <MESSAGE value=":recycle: Improve code structure and much easier to read&#10;:bug: Fix auth middleware" />
@ -176,7 +178,9 @@
<MESSAGE value=":bug: Authenticate wrong payload hotfix" /> <MESSAGE value=":bug: Authenticate wrong payload hotfix" />
<MESSAGE value=":sparkles: Can pick up mfa request" /> <MESSAGE value=":sparkles: Can pick up mfa request" />
<MESSAGE value=":sparkles: Status system" /> <MESSAGE value=":sparkles: Status system" />
<option name="LAST_COMMIT_MESSAGE" value=":sparkles: Status system" /> <MESSAGE value=":bug: Fix status expired in cache" />
<MESSAGE value=":bug: Fix online condition" />
<option name="LAST_COMMIT_MESSAGE" value=":bug: Fix online condition" />
</component> </component>
<component name="VgoProject"> <component name="VgoProject">
<settings-migrated>true</settings-migrated> <settings-migrated>true</settings-migrated>

View File

@ -10,6 +10,7 @@ type AccountProfile struct {
FirstName string `json:"first_name"` FirstName string `json:"first_name"`
LastName string `json:"last_name"` LastName string `json:"last_name"`
Experience uint64 `json:"experience"` Experience uint64 `json:"experience"`
LastSeenAt *time.Time `json:"last_seen_at"`
Birthday *time.Time `json:"birthday"` Birthday *time.Time `json:"birthday"`
AccountID uint `json:"account_id"` AccountID uint `json:"account_id"`
} }

View File

@ -2,6 +2,7 @@ package api
import ( import (
"fmt" "fmt"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models" "git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts" "git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services" "git.solsynth.dev/hydrogen/passport/pkg/internal/services"
@ -13,9 +14,11 @@ import (
func getStatus(c *fiber.Ctx) error { func getStatus(c *fiber.Ctx) error {
alias := c.Params("alias") alias := c.Params("alias")
user, err := services.GetAccountWithName(alias) var user models.Account
if err != nil { if err := database.C.Where(models.Account{
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("account not found: %s", alias)) Name: alias,
}).Preload("Profile").First(&user).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("account not found: %s", err))
} }
status, err := services.GetStatus(user.ID) status, err := services.GetStatus(user.ID)
@ -24,6 +27,7 @@ func getStatus(c *fiber.Ctx) error {
return c.JSON(fiber.Map{ return c.JSON(fiber.Map{
"status": lo.Ternary(err == nil, &status, nil), "status": lo.Ternary(err == nil, &status, nil),
"last_seen_at": user.Profile.LastSeenAt,
"is_disturbable": disturbable, "is_disturbable": disturbable,
"is_online": online, "is_online": online,
}) })

View File

@ -179,3 +179,14 @@ func RecycleUnConfirmAccount() {
} }
} }
} }
func SetAccountLastSeen(uid uint) error {
var profile models.AccountProfile
if err := database.C.Where("account_id = ?", uid).First(&profile).Error; err != nil {
return err
}
profile.LastSeenAt = lo.ToPtr(time.Now())
return database.C.Save(&profile).Error
}

View File

@ -28,4 +28,10 @@ func ClientUnregister(user models.Account, conn *websocket.Conn) {
} }
delete(wsConn[user.ID], conn) delete(wsConn[user.ID], conn)
wsMutex.Unlock() wsMutex.Unlock()
if status, err := GetStatus(user.ID); err != nil || !status.IsInvisible {
if len(wsConn[user.ID]) == 0 {
_ = SetAccountLastSeen(user.ID)
}
}
} }