diff --git a/pkg/models/accounts.go b/pkg/models/accounts.go index fe22f8d..0c32fa5 100644 --- a/pkg/models/accounts.go +++ b/pkg/models/accounts.go @@ -8,18 +8,19 @@ import "time" type Account struct { BaseModel - Name string `json:"name"` - Nick string `json:"nick"` - Avatar string `json:"avatar"` - Description string `json:"description"` - EmailAddress string `json:"email_address"` - PowerLevel int `json:"power_level"` - Posts []Post `json:"posts" gorm:"foreignKey:AuthorID"` - Attachments []Attachment `json:"attachments" gorm:"foreignKey:AuthorID"` - LikedPosts []PostLike `json:"liked_posts"` - DislikedPosts []PostDislike `json:"disliked_posts"` - Realms []Realm `json:"realms"` - ExternalID uint `json:"external_id"` + Name string `json:"name"` + Nick string `json:"nick"` + Avatar string `json:"avatar"` + Description string `json:"description"` + EmailAddress string `json:"email_address"` + PowerLevel int `json:"power_level"` + Posts []Post `json:"posts" gorm:"foreignKey:AuthorID"` + Attachments []Attachment `json:"attachments" gorm:"foreignKey:AuthorID"` + LikedPosts []PostLike `json:"liked_posts"` + DislikedPosts []PostDislike `json:"disliked_posts"` + RealmIdentities []RealmMember `json:"identities"` + Realms []Realm `json:"realms"` + ExternalID uint `json:"external_id"` } type AccountMembership struct { diff --git a/pkg/models/realms.go b/pkg/models/realms.go index 370d87b..9520098 100644 --- a/pkg/models/realms.go +++ b/pkg/models/realms.go @@ -3,8 +3,17 @@ package models type Realm struct { BaseModel - Name string `json:"name"` - Description string `json:"description"` - Posts []Post `json:"posts"` - AccountID uint `json:"account_id"` + Name string `json:"name"` + Description string `json:"description"` + Posts []Post `json:"posts"` + Members []RealmMember `json:"members"` + IsPublic bool `json:"is_public"` + AccountID uint `json:"account_id"` +} + +type RealmMember struct { + BaseModel + + RealmID uint `json:"realm_id"` + AccountID uint `json:"account_id"` } diff --git a/pkg/server/realms_api.go b/pkg/server/realms_api.go index e48c969..1c2f62f 100644 --- a/pkg/server/realms_api.go +++ b/pkg/server/realms_api.go @@ -49,13 +49,14 @@ func createRealm(c *fiber.Ctx) error { var data struct { Name string `json:"name" validate:"required"` Description string `json:"description"` + IsPublic bool `json:"is_public"` } if err := BindAndValidate(c, &data); err != nil { return err } - realm, err := services.NewRealm(user, data.Name, data.Description) + realm, err := services.NewRealm(user, data.Name, data.Description, data.IsPublic) if err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } @@ -63,6 +64,40 @@ func createRealm(c *fiber.Ctx) error { return c.JSON(realm) } +func inviteRealm(c *fiber.Ctx) error { + user := c.Locals("principal").(models.Account) + realmId, _ := c.ParamsInt("realmId", 0) + + var data struct { + AccountID uint `json:"account_id" validate:"required"` + } + + if err := BindAndValidate(c, &data); err != nil { + return err + } + + var realm models.Realm + if err := database.C.Where(&models.Realm{ + BaseModel: models.BaseModel{ID: uint(realmId)}, + AccountID: user.ID, + }).First(&realm).Error; err != nil { + return fiber.NewError(fiber.StatusNotFound, err.Error()) + } + + var account models.Account + if err := database.C.Where(&models.Account{ + BaseModel: models.BaseModel{ID: uint(realmId)}, + }).First(&account).Error; err != nil { + return fiber.NewError(fiber.StatusNotFound, err.Error()) + } + + if err := services.InviteRealmMember(account, realm); err != nil { + return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } else { + return c.SendStatus(fiber.StatusOK) + } +} + func editRealm(c *fiber.Ctx) error { user := c.Locals("principal").(models.Account) id, _ := c.ParamsInt("realmId", 0) @@ -70,6 +105,7 @@ func editRealm(c *fiber.Ctx) error { var data struct { Name string `json:"name" validate:"required"` Description string `json:"description"` + IsPublic bool `json:"is_public"` } if err := BindAndValidate(c, &data); err != nil { @@ -84,7 +120,7 @@ func editRealm(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusNotFound, err.Error()) } - realm, err := services.EditRealm(realm, data.Name, data.Description) + realm, err := services.EditRealm(realm, data.Name, data.Description, data.IsPublic) if err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } diff --git a/pkg/server/startup.go b/pkg/server/startup.go index 0edae32..999ecd4 100644 --- a/pkg/server/startup.go +++ b/pkg/server/startup.go @@ -80,6 +80,7 @@ func NewServer() { api.Get("/realms/me", auth, listOwnedRealm) api.Get("/realms/:realmId", getRealm) api.Post("/realms", auth, createRealm) + api.Post("/realms/:realmId/invite", auth, inviteRealm) api.Put("/realms/:realmId", auth, editRealm) api.Delete("/realms/:realmId", auth, deleteRealm) } diff --git a/pkg/services/posts.go b/pkg/services/posts.go index cfc4e6f..3750e2c 100644 --- a/pkg/services/posts.go +++ b/pkg/services/posts.go @@ -151,6 +151,15 @@ func NewPost( var realmId *uint if realm != nil { + if !realm.IsPublic { + var member models.RealmMember + if err := database.C.Where(&models.RealmMember{ + RealmID: *realmId, + AccountID: user.ID, + }).First(&member).Error; err != nil { + return post, fmt.Errorf("you aren't a part of that realm") + } + } realmId = &realm.ID } diff --git a/pkg/services/realms.go b/pkg/services/realms.go index 9057f56..4dbefcf 100644 --- a/pkg/services/realms.go +++ b/pkg/services/realms.go @@ -23,11 +23,15 @@ func ListRealmWithUser(user models.Account) ([]models.Realm, error) { return realms, nil } -func NewRealm(user models.Account, name, description string) (models.Realm, error) { +func NewRealm(user models.Account, name, description string, isPublic bool) (models.Realm, error) { realm := models.Realm{ Name: name, Description: description, AccountID: user.ID, + IsPublic: isPublic, + Members: []models.RealmMember{ + {AccountID: user.ID}, + }, } err := database.C.Save(&realm).Error @@ -35,9 +39,21 @@ func NewRealm(user models.Account, name, description string) (models.Realm, erro return realm, err } -func EditRealm(realm models.Realm, name, description string) (models.Realm, error) { +func InviteRealmMember(user models.Account, target models.Realm) error { + member := models.RealmMember{ + RealmID: target.ID, + AccountID: user.ID, + } + + err := database.C.Save(&member).Error + + return err +} + +func EditRealm(realm models.Realm, name, description string, isPublic bool) (models.Realm, error) { realm.Name = name realm.Description = description + realm.IsPublic = isPublic err := database.C.Save(&realm).Error