⬆️ Upgrade to support the latest version Hydrogen Project standard
This commit is contained in:
100
pkg/internal/server/api/categories_api.go
Normal file
100
pkg/internal/server/api/categories_api.go
Normal file
@ -0,0 +1,100 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func listCategories(c *fiber.Ctx) error {
|
||||
categories, err := services.ListCategory()
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(categories)
|
||||
}
|
||||
|
||||
func newCategory(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
if user.PowerLevel <= 55 {
|
||||
return fiber.NewError(fiber.StatusForbidden, "require power level 55 to create categories")
|
||||
}
|
||||
|
||||
var data struct {
|
||||
Alias string `json:"alias" validate:"required"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
category, err := services.NewCategory(data.Alias, data.Name, data.Description)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(category)
|
||||
}
|
||||
|
||||
func editCategory(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
if user.PowerLevel <= 55 {
|
||||
return fiber.NewError(fiber.StatusForbidden, "require power level 55 to edit categories")
|
||||
}
|
||||
|
||||
id, _ := c.ParamsInt("categoryId", 0)
|
||||
category, err := services.GetCategoryWithID(uint(id))
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
var data struct {
|
||||
Alias string `json:"alias" validate:"required"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
category, err = services.EditCategory(category, data.Alias, data.Name, data.Description)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(category)
|
||||
}
|
||||
|
||||
func deleteCategory(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
if user.PowerLevel <= 55 {
|
||||
return fiber.NewError(fiber.StatusForbidden, "require power level 55 to delete categories")
|
||||
}
|
||||
|
||||
id, _ := c.ParamsInt("categoryId", 0)
|
||||
category, err := services.GetCategoryWithID(uint(id))
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
if err := services.DeleteCategory(category); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(category)
|
||||
}
|
49
pkg/internal/server/api/feed_api.go
Normal file
49
pkg/internal/server/api/feed_api.go
Normal file
@ -0,0 +1,49 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func listFeed(c *fiber.Ctx) error {
|
||||
take := c.QueryInt("take", 0)
|
||||
offset := c.QueryInt("offset", 0)
|
||||
realmId := c.QueryInt("realmId", 0)
|
||||
|
||||
tx := database.C
|
||||
if realmId > 0 {
|
||||
tx = services.FilterWithRealm(tx, uint(realmId))
|
||||
}
|
||||
|
||||
if len(c.Query("authorId")) > 0 {
|
||||
var author models.Account
|
||||
if err := database.C.Where(&models.Account{Name: c.Query("authorId")}).First(&author).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
tx = tx.Where("author_id = ?", author.ID)
|
||||
}
|
||||
|
||||
if len(c.Query("category")) > 0 {
|
||||
tx = services.FilterPostWithCategory(tx, c.Query("category"))
|
||||
}
|
||||
if len(c.Query("tag")) > 0 {
|
||||
tx = services.FilterPostWithTag(tx, c.Query("tag"))
|
||||
}
|
||||
|
||||
count, err := services.CountPost(tx)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
items, err := services.ListPost(tx, take, offset)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"count": count,
|
||||
"data": items,
|
||||
})
|
||||
}
|
32
pkg/internal/server/api/index.go
Normal file
32
pkg/internal/server/api/index.go
Normal file
@ -0,0 +1,32 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func MapAPIs(app *fiber.App) {
|
||||
api := app.Group("/api").Name("API")
|
||||
{
|
||||
api.Get("/users/me", getUserinfo)
|
||||
api.Get("/users/:accountId", getOthersInfo)
|
||||
|
||||
api.Get("/feed", listFeed)
|
||||
|
||||
posts := api.Group("/posts").Name("Posts API")
|
||||
{
|
||||
posts.Get("/", listPost)
|
||||
posts.Get("/:post", getPost)
|
||||
posts.Post("/", createPost)
|
||||
posts.Post("/:post/react", reactPost)
|
||||
posts.Put("/:postId", editPost)
|
||||
posts.Delete("/:postId", deletePost)
|
||||
|
||||
posts.Get("/:post/replies", listReplies)
|
||||
}
|
||||
|
||||
api.Get("/categories", listCategories)
|
||||
api.Post("/categories", newCategory)
|
||||
api.Put("/categories/:categoryId", editCategory)
|
||||
api.Delete("/categories/:categoryId", deleteCategory)
|
||||
}
|
||||
}
|
256
pkg/internal/server/api/posts_api.go
Normal file
256
pkg/internal/server/api/posts_api.go
Normal file
@ -0,0 +1,256 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
"github.com/samber/lo"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func getPost(c *fiber.Ctx) error {
|
||||
alias := c.Params("post")
|
||||
|
||||
item, err := services.GetPostWithAlias(alias)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
item.ReplyCount = services.CountPostReply(item.ID)
|
||||
item.ReactionCount = services.CountPostReactions(item.ID)
|
||||
item.ReactionList, err = services.ListPostReactions(item.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(item)
|
||||
}
|
||||
|
||||
func listPost(c *fiber.Ctx) error {
|
||||
take := c.QueryInt("take", 0)
|
||||
offset := c.QueryInt("offset", 0)
|
||||
realmId := c.QueryInt("realmId", 0)
|
||||
|
||||
tx := database.C
|
||||
if realmId > 0 {
|
||||
tx = services.FilterWithRealm(tx, uint(realmId))
|
||||
}
|
||||
|
||||
if len(c.Query("authorId")) > 0 {
|
||||
var author models.Account
|
||||
if err := database.C.Where(&models.Account{Name: c.Query("authorId")}).First(&author).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
tx = tx.Where("author_id = ?", author.ID)
|
||||
}
|
||||
|
||||
if len(c.Query("category")) > 0 {
|
||||
tx = services.FilterPostWithCategory(tx, c.Query("category"))
|
||||
}
|
||||
if len(c.Query("tag")) > 0 {
|
||||
tx = services.FilterPostWithTag(tx, c.Query("tag"))
|
||||
}
|
||||
|
||||
count, err := services.CountPost(tx)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
items, err := services.ListPost(tx, take, offset)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"count": count,
|
||||
"data": items,
|
||||
})
|
||||
}
|
||||
|
||||
func createPost(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureGrantedPerm(c, "CreateInteractivePost", true); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
var data struct {
|
||||
Alias string `json:"alias" form:"alias"`
|
||||
Content string `json:"content" form:"content" validate:"required,max=4096"`
|
||||
Tags []models.Tag `json:"tags" form:"tags"`
|
||||
Categories []models.Category `json:"categories" form:"categories"`
|
||||
Attachments []uint `json:"attachments" form:"attachments"`
|
||||
PublishedAt *time.Time `json:"published_at" form:"published_at"`
|
||||
RealmAlias string `json:"realm" form:"realm"`
|
||||
ReplyTo *uint `json:"reply_to" form:"reply_to"`
|
||||
RepostTo *uint `json:"repost_to" form:"repost_to"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
} else if len(data.Alias) == 0 {
|
||||
data.Alias = strings.ReplaceAll(uuid.NewString(), "-", "")
|
||||
}
|
||||
|
||||
for _, attachment := range data.Attachments {
|
||||
if !services.CheckAttachmentByIDExists(attachment, "i.attachment") {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("attachment %d not found", attachment))
|
||||
}
|
||||
}
|
||||
|
||||
item := models.Post{
|
||||
Alias: data.Alias,
|
||||
PublishedAt: data.PublishedAt,
|
||||
AuthorID: user.ID,
|
||||
Tags: data.Tags,
|
||||
Categories: data.Categories,
|
||||
Attachments: data.Attachments,
|
||||
Content: data.Content,
|
||||
}
|
||||
|
||||
if data.ReplyTo != nil {
|
||||
var replyTo models.Post
|
||||
if err := database.C.Where("id = ?", data.ReplyTo).First(&replyTo).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("related post was not found: %v", err))
|
||||
} else {
|
||||
item.ReplyID = &replyTo.ID
|
||||
}
|
||||
}
|
||||
if data.RepostTo != nil {
|
||||
var repostTo models.Post
|
||||
if err := database.C.Where("id = ?", data.RepostTo).First(&repostTo).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("related post was not found: %v", err))
|
||||
} else {
|
||||
item.RepostID = &repostTo.ID
|
||||
}
|
||||
}
|
||||
|
||||
if len(data.RealmAlias) > 0 {
|
||||
if realm, err := services.GetRealmWithAlias(data.RealmAlias); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else if _, err := services.GetRealmMember(realm.ExternalID, user.ExternalID); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("you aren't a part of related realm: %v", err))
|
||||
} else {
|
||||
item.RealmID = &realm.ID
|
||||
}
|
||||
}
|
||||
|
||||
item, err := services.NewPost(user, item)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(item)
|
||||
}
|
||||
|
||||
func editPost(c *fiber.Ctx) error {
|
||||
id, _ := c.ParamsInt("postId", 0)
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
var data struct {
|
||||
Alias string `json:"alias" form:"alias" validate:"required"`
|
||||
Content string `json:"content" form:"content" validate:"required,max=1024"`
|
||||
PublishedAt *time.Time `json:"published_at" form:"published_at"`
|
||||
Tags []models.Tag `json:"tags" form:"tags"`
|
||||
Categories []models.Category `json:"categories" form:"categories"`
|
||||
Attachments []uint `json:"attachments" form:"attachments"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var item models.Post
|
||||
if err := database.C.Where(models.Post{
|
||||
BaseModel: models.BaseModel{ID: uint(id)},
|
||||
AuthorID: user.ID,
|
||||
}).First(&item).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
for _, attachment := range data.Attachments {
|
||||
if !services.CheckAttachmentByIDExists(attachment, "i.attachment") {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("attachment %d not found", attachment))
|
||||
}
|
||||
}
|
||||
|
||||
item.Alias = data.Alias
|
||||
item.Content = data.Content
|
||||
item.PublishedAt = data.PublishedAt
|
||||
item.Tags = data.Tags
|
||||
item.Categories = data.Categories
|
||||
item.Attachments = data.Attachments
|
||||
|
||||
if item, err := services.EditPost(item); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
return c.JSON(item)
|
||||
}
|
||||
}
|
||||
|
||||
func deletePost(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
id, _ := c.ParamsInt("postId", 0)
|
||||
|
||||
var item models.Post
|
||||
if err := database.C.Where(models.Post{
|
||||
BaseModel: models.BaseModel{ID: uint(id)},
|
||||
AuthorID: user.ID,
|
||||
}).First(&item).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
if err := services.DeletePost(item); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
}
|
||||
|
||||
func reactPost(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
var data struct {
|
||||
Symbol string `json:"symbol" form:"symbol" validate:"required"`
|
||||
Attitude models.ReactionAttitude `json:"attitude" form:"attitude"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
reaction := models.Reaction{
|
||||
Symbol: data.Symbol,
|
||||
Attitude: data.Attitude,
|
||||
AccountID: user.ID,
|
||||
}
|
||||
|
||||
alias := c.Params("post")
|
||||
|
||||
var res models.Post
|
||||
if err := database.C.Where("alias = ?", alias).Select("id").First(&res).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to find post to react: %v", err))
|
||||
} else {
|
||||
reaction.PostID = &res.ID
|
||||
}
|
||||
|
||||
if positive, reaction, err := services.ReactPost(user, reaction); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
return c.Status(lo.Ternary(positive, fiber.StatusCreated, fiber.StatusNoContent)).JSON(reaction)
|
||||
}
|
||||
}
|
52
pkg/internal/server/api/replies_api.go
Normal file
52
pkg/internal/server/api/replies_api.go
Normal file
@ -0,0 +1,52 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func listReplies(c *fiber.Ctx) error {
|
||||
take := c.QueryInt("take", 0)
|
||||
offset := c.QueryInt("offset", 0)
|
||||
|
||||
tx := database.C
|
||||
var post models.Post
|
||||
if err := database.C.Where("alias = ?", c.Params("post")).First(&post).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to find post: %v", err))
|
||||
} else {
|
||||
tx = services.FilterPostReply(tx, post.ID)
|
||||
}
|
||||
|
||||
if len(c.Query("authorId")) > 0 {
|
||||
var author models.Account
|
||||
if err := database.C.Where(&models.Account{Name: c.Query("authorId")}).First(&author).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
tx = tx.Where("author_id = ?", author.ID)
|
||||
}
|
||||
|
||||
if len(c.Query("category")) > 0 {
|
||||
tx = services.FilterPostWithCategory(tx, c.Query("category"))
|
||||
}
|
||||
if len(c.Query("tag")) > 0 {
|
||||
tx = services.FilterPostWithTag(tx, c.Query("tag"))
|
||||
}
|
||||
|
||||
count, err := services.CountPost(tx)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
items, err := services.ListPost(tx, take, offset)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"count": count,
|
||||
"data": items,
|
||||
})
|
||||
}
|
37
pkg/internal/server/api/users_api.go
Normal file
37
pkg/internal/server/api/users_api.go
Normal file
@ -0,0 +1,37 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func getUserinfo(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
var data models.Account
|
||||
if err := database.C.
|
||||
Where(&models.Account{BaseModel: models.BaseModel{ID: user.ID}}).
|
||||
First(&data).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(data)
|
||||
}
|
||||
|
||||
func getOthersInfo(c *fiber.Ctx) error {
|
||||
accountId := c.Params("accountId")
|
||||
|
||||
var data models.Account
|
||||
if err := database.C.
|
||||
Where(&models.Account{Name: accountId}).
|
||||
First(&data).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(data)
|
||||
}
|
Reference in New Issue
Block a user