✨ Status system
This commit is contained in:
parent
38ee30f455
commit
132d301aeb
19
.idea/workspace.xml
generated
19
.idea/workspace.xml
generated
@ -4,16 +4,15 @@
|
||||
<option name="autoReloadType" value="ALL" />
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Authenticate wrong payload hotfix">
|
||||
<change beforePath="$PROJECT_DIR$/.idea/dataSources/74bcf3ef-a2b9-435b-b9e5-f32902a33b25.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/dataSources/74bcf3ef-a2b9-435b-b9e5-f32902a33b25.xml" afterDir="false" />
|
||||
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":sparkles: Can pick up mfa request">
|
||||
<change afterPath="$PROJECT_DIR$/pkg/internal/server/api/statuses_api.go" afterDir="false" />
|
||||
<change 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/server/api/auth_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/auth_api.go" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/pkg/internal/models/statuses.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/models/statuses.go" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/index.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/index.go" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/web/src/layouts/master.vue" beforeDir="false" afterPath="$PROJECT_DIR$/web/src/layouts/master.vue" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/web/src/layouts/user-center.vue" beforeDir="false" afterPath="$PROJECT_DIR$/web/src/layouts/user-center.vue" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/web/src/router/index.ts" beforeDir="false" afterPath="$PROJECT_DIR$/web/src/router/index.ts" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/web/src/views/auth/sign-in.vue" beforeDir="false" afterPath="$PROJECT_DIR$/web/src/views/auth/sign-in.vue" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/web/src/views/personalize.vue" beforeDir="false" afterPath="$PROJECT_DIR$/web/src/views/personalize.vue" 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/factors.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/factors.go" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/pkg/internal/services/notifications.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/notifications.go" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
@ -151,7 +150,6 @@
|
||||
</option>
|
||||
</component>
|
||||
<component name="VcsManagerConfiguration">
|
||||
<MESSAGE value=":fire: Remove ws connected does not push notify feature" />
|
||||
<MESSAGE value=":sparkles: Able to read current user's realm profile" />
|
||||
<MESSAGE value=":sparkles: Consul registration" />
|
||||
<MESSAGE value=":wastebasket: Remove HTTP provision to consul" />
|
||||
@ -176,7 +174,8 @@
|
||||
<MESSAGE value=":bug: Fix frontend" />
|
||||
<MESSAGE value=":card_file_box: Add the status model" />
|
||||
<MESSAGE value=":bug: Authenticate wrong payload hotfix" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value=":bug: Authenticate wrong payload hotfix" />
|
||||
<MESSAGE value=":sparkles: Can pick up mfa request" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value=":sparkles: Can pick up mfa request" />
|
||||
</component>
|
||||
<component name="VgoProject">
|
||||
<settings-migrated>true</settings-migrated>
|
||||
|
@ -1,5 +1,7 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type StatusAttitude = uint8
|
||||
|
||||
const (
|
||||
@ -16,5 +18,6 @@ type Status struct {
|
||||
Attitude StatusAttitude `json:"attitude"`
|
||||
IsNoDisturb bool `json:"is_no_disturb"`
|
||||
IsInvisible bool `json:"is_invisible"`
|
||||
ClearAt *time.Time `json:"clear_at"`
|
||||
AccountID uint `json:"account_id"`
|
||||
}
|
||||
|
@ -35,6 +35,8 @@ func MapAPIs(app *fiber.App) {
|
||||
|
||||
me.Post("/confirm", doRegisterConfirm)
|
||||
|
||||
me.Post("/status", setStatus)
|
||||
|
||||
friends := me.Group("/friends").Name("Friends")
|
||||
{
|
||||
friends.Get("/", listFriendship)
|
||||
@ -49,6 +51,7 @@ func MapAPIs(app *fiber.App) {
|
||||
directory := api.Group("/users/:alias").Name("User Directory")
|
||||
{
|
||||
directory.Get("/", getOtherUserinfo)
|
||||
directory.Get("/status", getStatus)
|
||||
}
|
||||
|
||||
api.Post("/users", doRegister)
|
||||
|
66
pkg/internal/server/api/statuses_api.go
Normal file
66
pkg/internal/server/api/statuses_api.go
Normal file
@ -0,0 +1,66 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"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"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/samber/lo"
|
||||
"time"
|
||||
)
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
status, err := services.GetStatus(user.ID)
|
||||
disturbable := services.GetStatusDisturbable(user.ID) == nil
|
||||
online := services.GetStatusOnline(user.ID) == nil
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"status": lo.Ternary(err == nil, &status, nil),
|
||||
"is_disturbable": disturbable,
|
||||
"is_online": online,
|
||||
})
|
||||
}
|
||||
|
||||
func setStatus(c *fiber.Ctx) error {
|
||||
user := c.Locals("user").(models.Account)
|
||||
if err := exts.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Type string `json:"type" validate:"required"`
|
||||
Label string `json:"label" validate:"required"`
|
||||
Attitude uint `json:"attitude" validate:"required"`
|
||||
IsNoDisturb bool `json:"is_no_disturb"`
|
||||
IsInvisible bool `json:"is_invisible"`
|
||||
ClearAt *time.Time `json:"clear_at"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &req); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
status := models.Status{
|
||||
Type: req.Type,
|
||||
Label: req.Label,
|
||||
Attitude: models.StatusAttitude(req.Attitude),
|
||||
IsNoDisturb: req.IsNoDisturb,
|
||||
IsInvisible: req.IsInvisible,
|
||||
ClearAt: req.ClearAt,
|
||||
AccountID: user.ID,
|
||||
}
|
||||
|
||||
if status, err := services.NewStatus(user, status); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
return c.JSON(status)
|
||||
}
|
||||
}
|
@ -25,6 +25,17 @@ func GetAccount(id uint) (models.Account, error) {
|
||||
return account, nil
|
||||
}
|
||||
|
||||
func GetAccountWithName(alias string) (models.Account, error) {
|
||||
var account models.Account
|
||||
if err := database.C.Where(models.Account{
|
||||
Name: alias,
|
||||
}).First(&account).Error; err != nil {
|
||||
return account, err
|
||||
}
|
||||
|
||||
return account, nil
|
||||
}
|
||||
|
||||
func LookupAccount(probe string) (models.Account, error) {
|
||||
var account models.Account
|
||||
if err := database.C.Where(models.Account{Name: probe}).First(&account).Error; err == nil {
|
||||
|
@ -2,6 +2,7 @@ package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/samber/lo"
|
||||
"strings"
|
||||
|
||||
@ -81,7 +82,8 @@ func GetFactorCode(factor models.AuthFactor) (bool, error) {
|
||||
subject := fmt.Sprintf("[%s] Login verification code", viper.GetString("name"))
|
||||
content := fmt.Sprintf(EmailPasswordTemplate, user.Name, factor.Secret, viper.GetString("maintainer"))
|
||||
if err := SendMail(user.GetPrimaryEmail().Content, subject, content); err != nil {
|
||||
return true, err
|
||||
log.Warn().Err(err).Uint("factor", factor.ID).Msg("Failed to delivery one-time-password via mail...")
|
||||
return true, nil
|
||||
}
|
||||
return true, nil
|
||||
|
||||
|
@ -65,7 +65,10 @@ func PushNotification(notification models.Notification) error {
|
||||
}.Marshal())
|
||||
}
|
||||
|
||||
// TODO Detect the push notification is turned off (still push when IsForcePush is on)
|
||||
// Skip push notify
|
||||
if GetStatusDisturbable(notification.RecipientID) != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var subscribers []models.NotificationSubscriber
|
||||
if err := database.C.Where(&models.NotificationSubscriber{
|
||||
|
59
pkg/internal/services/statuses.go
Normal file
59
pkg/internal/services/statuses.go
Normal file
@ -0,0 +1,59 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
|
||||
"time"
|
||||
)
|
||||
|
||||
var statusCache = make(map[uint]models.Status)
|
||||
|
||||
func NewStatus(user models.Account, status models.Status) (models.Status, error) {
|
||||
if err := database.C.Save(&status).Error; err != nil {
|
||||
return status, err
|
||||
} else {
|
||||
statusCache[user.ID] = status
|
||||
}
|
||||
return status, nil
|
||||
}
|
||||
|
||||
func GetStatus(uid uint) (models.Status, error) {
|
||||
if status, ok := statusCache[uid]; ok {
|
||||
return status, nil
|
||||
}
|
||||
var status models.Status
|
||||
if err := database.C.
|
||||
Where("account_id = ?", uid).
|
||||
Where("clear_at < ?", time.Now()).
|
||||
First(&status).Error; err != nil {
|
||||
return status, err
|
||||
} else {
|
||||
statusCache[uid] = status
|
||||
}
|
||||
return status, nil
|
||||
}
|
||||
|
||||
func GetStatusDisturbable(uid uint) error {
|
||||
status, err := GetStatus(uid)
|
||||
isOnline := wsConn[uid] == nil || len(wsConn[uid]) < 0
|
||||
if isOnline && err != nil {
|
||||
return nil
|
||||
} else if err == nil && status.IsNoDisturb {
|
||||
return fmt.Errorf("do not disturb")
|
||||
} else {
|
||||
return fmt.Errorf("offline")
|
||||
}
|
||||
}
|
||||
|
||||
func GetStatusOnline(uid uint) error {
|
||||
status, err := GetStatus(uid)
|
||||
isOnline := wsConn[uid] == nil || len(wsConn[uid]) < 0
|
||||
if isOnline && err != nil {
|
||||
return nil
|
||||
} else if err == nil && status.IsInvisible {
|
||||
return fmt.Errorf("invisible")
|
||||
} else {
|
||||
return fmt.Errorf("offline")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user