🧑💻 Improve DX by extending authkit
This commit is contained in:
44
pkg/authkit/audit.go
Normal file
44
pkg/authkit/audit.go
Normal file
@ -0,0 +1,44 @@
|
||||
package authkit
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"git.solsynth.dev/hypernet/passport/pkg/proto"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func AddEvent(nx *nex.Conn, userId uint, action, target, ip, ua string) error {
|
||||
conn, err := nx.GetClientGrpcConn(nex.ServiceTypeAuth)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get auth service client: %v", err)
|
||||
}
|
||||
_, err = proto.NewAuditServiceClient(conn).RecordEvent(nil, &proto.RecordEventRequest{
|
||||
UserId: uint64(userId),
|
||||
Action: action,
|
||||
Target: target,
|
||||
Ip: ip,
|
||||
UserAgent: ua,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func AddEventExt(nx *nex.Conn, action, target string, c *fiber.Ctx) error {
|
||||
user, ok := c.Locals("nex_user").(*sec.UserInfo)
|
||||
if !ok {
|
||||
return fmt.Errorf("failed to get user info, make sure you call this method behind the ContextMiddleware")
|
||||
}
|
||||
|
||||
conn, err := nx.GetClientGrpcConn(nex.ServiceTypeAuth)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get auth service client: %v", err)
|
||||
}
|
||||
_, err = proto.NewAuditServiceClient(conn).RecordEvent(nil, &proto.RecordEventRequest{
|
||||
UserId: uint64(user.ID),
|
||||
Action: action,
|
||||
Target: target,
|
||||
Ip: c.IP(),
|
||||
UserAgent: c.Get(fiber.HeaderUserAgent),
|
||||
})
|
||||
return err
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hypernet/pusher/pkg/pushkit"
|
||||
"gorm.io/datatypes"
|
||||
"time"
|
||||
)
|
||||
@ -20,8 +21,29 @@ type Notification struct {
|
||||
AccountID uint `json:"account_id"`
|
||||
|
||||
ReadAt *time.Time `json:"read_at"`
|
||||
}
|
||||
|
||||
IsRealtime bool `json:"is_realtime" gorm:"-"`
|
||||
func (v Notification) EncodeToPushkit() pushkit.Notification {
|
||||
return pushkit.Notification{
|
||||
Topic: v.Topic,
|
||||
Title: v.Title,
|
||||
Subtitle: v.Subtitle,
|
||||
Body: v.Body,
|
||||
Metadata: v.Metadata,
|
||||
Priority: v.Priority,
|
||||
}
|
||||
}
|
||||
|
||||
func NewNotificationFromPushkit(pk pushkit.Notification) Notification {
|
||||
return Notification{
|
||||
Topic: pk.Topic,
|
||||
Title: pk.Title,
|
||||
Subtitle: pk.Subtitle,
|
||||
Body: pk.Body,
|
||||
Metadata: pk.Metadata,
|
||||
Priority: pk.Priority,
|
||||
SenderID: nil,
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
|
47
pkg/authkit/notify.go
Normal file
47
pkg/authkit/notify.go
Normal file
@ -0,0 +1,47 @@
|
||||
package authkit
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
||||
"git.solsynth.dev/hypernet/passport/pkg/proto"
|
||||
"git.solsynth.dev/hypernet/pusher/pkg/pushkit"
|
||||
"github.com/goccy/go-json"
|
||||
)
|
||||
|
||||
func NotifyUser(nx *nex.Conn, userId uint64, notify pushkit.Notification, unsaved ...bool) error {
|
||||
conn, err := nx.GetClientGrpcConn(nex.ServiceTypeAuth)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get auth service client: %v", err)
|
||||
}
|
||||
raw, _ := json.Marshal(notify)
|
||||
if len(unsaved) == 0 {
|
||||
unsaved = append(unsaved, false)
|
||||
}
|
||||
_, err = proto.NewNotifyServiceClient(conn).NotifyUser(nil, &proto.NotifyUserRequest{
|
||||
UserId: userId,
|
||||
Notify: &proto.NotifyInfo{
|
||||
Unsaved: unsaved[0],
|
||||
Data: raw,
|
||||
},
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func NotifyUserBatch(nx *nex.Conn, userId []uint64, notify pushkit.Notification, unsaved ...bool) error {
|
||||
conn, err := nx.GetClientGrpcConn(nex.ServiceTypeAuth)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get auth service client: %v", err)
|
||||
}
|
||||
raw, _ := json.Marshal(notify)
|
||||
if len(unsaved) == 0 {
|
||||
unsaved = append(unsaved, false)
|
||||
}
|
||||
_, err = proto.NewNotifyServiceClient(conn).NotifyUserBatch(nil, &proto.NotifyUserBatchRequest{
|
||||
UserId: userId,
|
||||
Notify: &proto.NotifyInfo{
|
||||
Unsaved: unsaved[0],
|
||||
Data: raw,
|
||||
},
|
||||
})
|
||||
return err
|
||||
}
|
Reference in New Issue
Block a user