diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 6eb7413..63da3dc 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,8 +4,12 @@
-
-
+
+
+
+
+
+
@@ -151,8 +155,6 @@
-
-
@@ -176,7 +178,9 @@
-
+
+
+
true
diff --git a/pkg/internal/models/profiles.go b/pkg/internal/models/profiles.go
index 1aa5452..8c873a8 100644
--- a/pkg/internal/models/profiles.go
+++ b/pkg/internal/models/profiles.go
@@ -10,6 +10,7 @@ type AccountProfile struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Experience uint64 `json:"experience"`
+ LastSeenAt *time.Time `json:"last_seen_at"`
Birthday *time.Time `json:"birthday"`
AccountID uint `json:"account_id"`
}
diff --git a/pkg/internal/server/api/statuses_api.go b/pkg/internal/server/api/statuses_api.go
index b4293e4..5649d89 100644
--- a/pkg/internal/server/api/statuses_api.go
+++ b/pkg/internal/server/api/statuses_api.go
@@ -2,6 +2,7 @@ package api
import (
"fmt"
+ "git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
@@ -13,9 +14,11 @@ import (
func getStatus(c *fiber.Ctx) error {
alias := c.Params("alias")
- user, err := services.GetAccountWithName(alias)
- if err != nil {
- return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("account not found: %s", alias))
+ var user models.Account
+ if err := database.C.Where(models.Account{
+ 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)
@@ -24,6 +27,7 @@ func getStatus(c *fiber.Ctx) error {
return c.JSON(fiber.Map{
"status": lo.Ternary(err == nil, &status, nil),
+ "last_seen_at": user.Profile.LastSeenAt,
"is_disturbable": disturbable,
"is_online": online,
})
diff --git a/pkg/internal/services/accounts.go b/pkg/internal/services/accounts.go
index e49f341..ab764ca 100644
--- a/pkg/internal/services/accounts.go
+++ b/pkg/internal/services/accounts.go
@@ -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
+}
diff --git a/pkg/internal/services/connections.go b/pkg/internal/services/connections.go
index f38ad3a..87484e9 100644
--- a/pkg/internal/services/connections.go
+++ b/pkg/internal/services/connections.go
@@ -28,4 +28,10 @@ func ClientUnregister(user models.Account, conn *websocket.Conn) {
}
delete(wsConn[user.ID], conn)
wsMutex.Unlock()
+
+ if status, err := GetStatus(user.ID); err != nil || !status.IsInvisible {
+ if len(wsConn[user.ID]) == 0 {
+ _ = SetAccountLastSeen(user.ID)
+ }
+ }
}