Add namespace sharing events

This commit is contained in:
kolaente 2021-01-31 22:20:25 +01:00
parent e8f2d577c2
commit 405f3771b1
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 54 additions and 8 deletions

View File

@ -129,3 +129,23 @@ type ListSharedWithTeamEvent struct {
func (l *ListSharedWithTeamEvent) TopicName() string { func (l *ListSharedWithTeamEvent) TopicName() string {
return "list.shared.team" return "list.shared.team"
} }
type NamespaceSharedWithUserEvent struct {
Namespace *Namespace
User *user.User
Doer *user.User
}
func (n *NamespaceSharedWithUserEvent) TopicName() string {
return "namespace.shared.user"
}
type NamespaceSharedWithTeamEvent struct {
Namespace *Namespace
Team *Team
Doer *user.User
}
func (n *NamespaceSharedWithTeamEvent) TopicName() string {
return "namespace.shared.team"
}

View File

@ -17,6 +17,8 @@
package models package models
import ( import (
"code.vikunja.io/api/pkg/events"
user2 "code.vikunja.io/api/pkg/user"
"time" "time"
"code.vikunja.io/web" "code.vikunja.io/web"
@ -71,15 +73,15 @@ func (tn *TeamNamespace) Create(s *xorm.Session, a web.Auth) (err error) {
} }
// Check if the team exists // Check if the team exists
_, err = GetTeamByID(s, tn.TeamID) team, err := GetTeamByID(s, tn.TeamID)
if err != nil { if err != nil {
return return err
} }
// Check if the namespace exists // Check if the namespace exists
_, err = GetNamespaceByID(s, tn.NamespaceID) namespace, err := GetNamespaceByID(s, tn.NamespaceID)
if err != nil { if err != nil {
return return err
} }
// Check if the team already has access to the namespace // Check if the team already has access to the namespace
@ -96,7 +98,19 @@ func (tn *TeamNamespace) Create(s *xorm.Session, a web.Auth) (err error) {
// Insert the new team // Insert the new team
_, err = s.Insert(tn) _, err = s.Insert(tn)
return if err != nil {
return err
}
doer, err := user2.GetFromAuth(a)
if err != nil {
return err
}
return events.Publish(&NamespaceSharedWithTeamEvent{
Namespace: namespace,
Team: team,
Doer: doer,
})
} }
// Delete deletes a team <-> namespace relation based on the namespace & team id // Delete deletes a team <-> namespace relation based on the namespace & team id

View File

@ -17,6 +17,7 @@
package models package models
import ( import (
"code.vikunja.io/api/pkg/events"
"time" "time"
user2 "code.vikunja.io/api/pkg/user" user2 "code.vikunja.io/api/pkg/user"
@ -75,7 +76,7 @@ func (nu *NamespaceUser) Create(s *xorm.Session, a web.Auth) (err error) {
} }
// Check if the namespace exists // Check if the namespace exists
l, err := GetNamespaceByID(s, nu.NamespaceID) n, err := GetNamespaceByID(s, nu.NamespaceID)
if err != nil { if err != nil {
return return
} }
@ -89,7 +90,7 @@ func (nu *NamespaceUser) Create(s *xorm.Session, a web.Auth) (err error) {
// Check if the user already has access or is owner of that namespace // Check if the user already has access or is owner of that namespace
// We explicitly DO NOT check for teams here // We explicitly DO NOT check for teams here
if l.OwnerID == nu.UserID { if n.OwnerID == nu.UserID {
return ErrUserAlreadyHasNamespaceAccess{UserID: nu.UserID, NamespaceID: nu.NamespaceID} return ErrUserAlreadyHasNamespaceAccess{UserID: nu.UserID, NamespaceID: nu.NamespaceID}
} }
@ -105,8 +106,19 @@ func (nu *NamespaceUser) Create(s *xorm.Session, a web.Auth) (err error) {
// Insert user <-> namespace relation // Insert user <-> namespace relation
_, err = s.Insert(nu) _, err = s.Insert(nu)
if err != nil {
return err
}
return doer, err := user2.GetFromAuth(a)
if err != nil {
return err
}
return events.Publish(&NamespaceSharedWithUserEvent{
Namespace: n,
User: user,
Doer: doer,
})
} }
// Delete deletes a namespace <-> user relation // Delete deletes a namespace <-> user relation