Files
gamegroup2/backend/pb_hooks/main.go
T

160 lines
4.3 KiB
Go
Raw Normal View History

2026-04-17 14:16:56 +08:00
package main
import (
"log"
"github.com/pocketbase/pocketbase"
2026-04-17 14:17:54 +08:00
"github.com/pocketbase/pocketbase/apis"
2026-04-17 14:16:56 +08:00
"github.com/pocketbase/pocketbase/core"
)
// isGroupMember checks if a user is a member of a group
func isGroupMember(app *pocketbase.PocketBase, groupId string, userId string) (bool, error) {
group, err := app.Dao().FindRecordById("groups", groupId)
if err != nil {
return false, err
}
members := group.GetStringSlice("members")
for _, member := range members {
if member == userId {
return true, nil
}
}
return false, nil
}
// isGroupOwner checks if a user is the owner of a group
func isGroupOwner(app *pocketbase.PocketBase, groupId string, userId string) (bool, error) {
group, err := app.Dao().FindRecordById("groups", groupId)
if err != nil {
return false, err
}
return group.GetString("owner") == userId, nil
}
func main() {
app := pocketbase.New()
// Groups API Rules
2026-04-17 14:17:54 +08:00
app.OnRecordBeforeCreateRequest("groups").Add(func(e *core.RecordCreateEvent) error {
authRecord, _ := e.HttpContext.AuthRecord()
if authRecord == nil {
return apis.NewForbiddenError("需要登录", nil)
}
2026-04-17 14:16:56 +08:00
// Set the owner to the current user
2026-04-17 14:17:54 +08:00
e.Record.Set("owner", authRecord.Id)
2026-04-17 14:16:56 +08:00
// Initialize members array with the owner
2026-04-17 14:17:54 +08:00
e.Record.Set("members", []string{authRecord.Id})
return nil
2026-04-17 14:16:56 +08:00
})
2026-04-17 14:17:54 +08:00
app.OnRecordBeforeUpdateRequest("groups").Add(func(e *core.RecordUpdateEvent) error {
authRecord, _ := e.HttpContext.AuthRecord()
if authRecord == nil {
return apis.NewForbiddenError("需要登录", nil)
}
2026-04-17 14:16:56 +08:00
// Only owner can update the group
2026-04-17 14:17:54 +08:00
isOwner, err := isGroupOwner(app, e.Record.Id, authRecord.Id)
2026-04-17 14:16:56 +08:00
if err != nil || !isOwner {
2026-04-17 14:17:54 +08:00
return apis.NewForbiddenError("只有群组所有者可以更新群组", nil)
2026-04-17 14:16:56 +08:00
}
2026-04-17 14:17:54 +08:00
return nil
2026-04-17 14:16:56 +08:00
})
2026-04-17 14:17:54 +08:00
app.OnRecordBeforeDeleteRequest("groups").Add(func(e *core.RecordDeleteEvent) error {
authRecord, _ := e.HttpContext.AuthRecord()
if authRecord == nil {
return apis.NewForbiddenError("需要登录", nil)
}
2026-04-17 14:16:56 +08:00
// Only owner can delete the group
2026-04-17 14:17:54 +08:00
isOwner, err := isGroupOwner(app, e.Record.Id, authRecord.Id)
2026-04-17 14:16:56 +08:00
if err != nil || !isOwner {
2026-04-17 14:17:54 +08:00
return apis.NewForbiddenError("只有群组所有者可以删除群组", nil)
2026-04-17 14:16:56 +08:00
}
2026-04-17 14:17:54 +08:00
return nil
2026-04-17 14:16:56 +08:00
})
// Team Sessions API Rules
2026-04-17 14:17:54 +08:00
app.OnRecordBeforeCreateRequest("team_sessions").Add(func(e *core.RecordCreateEvent) error {
authRecord, _ := e.HttpContext.AuthRecord()
if authRecord == nil {
return apis.NewForbiddenError("需要登录", nil)
}
2026-04-17 14:16:56 +08:00
groupId := e.Record.GetString("group")
// Check if user is a member of the group
2026-04-17 14:17:54 +08:00
isMember, err := isGroupMember(app, groupId, authRecord.Id)
2026-04-17 14:16:56 +08:00
if err != nil || !isMember {
2026-04-17 14:17:54 +08:00
return apis.NewForbiddenError("只有群组成员可以创建团队会话", nil)
2026-04-17 14:16:56 +08:00
}
2026-04-17 14:17:54 +08:00
return nil
2026-04-17 14:16:56 +08:00
})
// Invitations API Rules
2026-04-17 14:17:54 +08:00
app.OnRecordBeforeCreateRequest("invitations").Add(func(e *core.RecordCreateEvent) error {
authRecord, _ := e.HttpContext.AuthRecord()
if authRecord == nil {
return apis.NewForbiddenError("需要登录", nil)
}
2026-04-17 14:16:56 +08:00
groupId := e.Record.GetString("group")
// Only group owner can create invitations
2026-04-17 14:17:54 +08:00
isOwner, err := isGroupOwner(app, groupId, authRecord.Id)
2026-04-17 14:16:56 +08:00
if err != nil || !isOwner {
2026-04-17 14:17:54 +08:00
return apis.NewForbiddenError("只有群组所有者可以创建邀请", nil)
2026-04-17 14:16:56 +08:00
}
// Set status to pending
e.Record.Set("status", "pending")
2026-04-17 14:17:54 +08:00
return nil
2026-04-17 14:16:56 +08:00
})
2026-04-17 14:17:54 +08:00
app.OnRecordAfterCreateRequest("invitations").Add(func(e *core.RecordCreateEvent) error {
2026-04-17 14:16:56 +08:00
// Send real-time notification to the invited user
message := map[string]interface{}{
"action": "invitation",
"data": map[string]interface{}{
"id": e.Record.Id,
"group": e.Record.GetString("group"),
"invited_by": e.Record.GetString("invited_by"),
"status": e.Record.GetString("status"),
"created": e.Record.Created.Time(),
},
}
// Broadcast to the invited user's channel
2026-04-17 14:17:54 +08:00
if err := app.Subscriptions().Broadcast("invitations", message); err != nil {
2026-04-17 14:16:56 +08:00
log.Printf("Error broadcasting invitation: %v", err)
}
2026-04-17 14:17:54 +08:00
return nil
2026-04-17 14:16:56 +08:00
})
// Real-time subscription rules
2026-04-17 14:17:54 +08:00
app.OnRecordAfterAuthWithTokenRequest().Add(func(e *core.RecordAuthEvent) error {
// Subscribe to invitations channel and user's groups channel
app.Subscriptions().Subscribe(e.HttpContext.Response(), []string{
2026-04-17 14:16:56 +08:00
"invitations",
"groups:" + e.Record.Id,
"team_sessions",
})
2026-04-17 14:17:54 +08:00
return nil
2026-04-17 14:16:56 +08:00
})
2026-04-17 14:17:54 +08:00
if err := app.Start(); err != nil {
log.Fatal(err)
}
2026-04-17 14:16:56 +08:00
}