2024-07-25 14:02:26 +00:00
|
|
|
package api
|
2024-07-25 14:12:00 +00:00
|
|
|
|
|
|
|
import (
|
2024-08-18 17:49:27 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"gorm.io/datatypes"
|
2024-07-25 14:12:00 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
|
2024-08-06 15:45:58 +00:00
|
|
|
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/services"
|
2024-07-25 14:12:00 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func listAttachment(c *fiber.Ctx) error {
|
|
|
|
take := c.QueryInt("take", 0)
|
|
|
|
offset := c.QueryInt("offset", 0)
|
|
|
|
|
|
|
|
if take > 100 {
|
|
|
|
take = 100
|
|
|
|
}
|
|
|
|
|
2024-08-02 19:22:45 +00:00
|
|
|
tx := database.C
|
2024-07-25 14:12:00 +00:00
|
|
|
|
2024-08-06 17:02:03 +00:00
|
|
|
needQuery := true
|
|
|
|
|
2024-08-06 15:45:58 +00:00
|
|
|
var result = make([]models.Attachment, take)
|
2024-08-18 13:10:10 +00:00
|
|
|
var idxList []string
|
2024-08-06 15:45:58 +00:00
|
|
|
|
2024-08-02 13:52:32 +00:00
|
|
|
if len(c.Query("id")) > 0 {
|
2024-08-18 13:10:10 +00:00
|
|
|
var pendingQueryId []string
|
2024-08-06 15:45:58 +00:00
|
|
|
idx := strings.Split(c.Query("id"), ",")
|
|
|
|
for p, raw := range idx {
|
2024-08-18 13:10:10 +00:00
|
|
|
idxList = append(idxList, raw)
|
|
|
|
if val, ok := services.GetAttachmentCache(raw); ok {
|
2024-08-06 15:45:58 +00:00
|
|
|
result[p] = val
|
|
|
|
} else {
|
2024-08-18 13:10:10 +00:00
|
|
|
pendingQueryId = append(pendingQueryId, raw)
|
2024-08-06 15:45:58 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-18 13:10:10 +00:00
|
|
|
tx = tx.Where("rid IN ?", pendingQueryId)
|
2024-08-06 17:02:03 +00:00
|
|
|
needQuery = len(pendingQueryId) > 0
|
2024-08-02 19:22:45 +00:00
|
|
|
} else {
|
|
|
|
// Do sort this when doesn't filter by the id
|
|
|
|
// Because the sort will mess up the result
|
|
|
|
tx = tx.Order("created_at DESC")
|
2024-08-18 17:49:27 +00:00
|
|
|
|
|
|
|
// Do not expose un-public indexable attachments
|
|
|
|
prefix := viper.GetString("database.prefix")
|
|
|
|
tx = tx.
|
|
|
|
Joins(fmt.Sprintf("JOIN %sattachment_pools ON %sattachment_pools.id = %sattachments.pool_id", prefix, prefix, prefix)).
|
|
|
|
Where(datatypes.JSONQuery(fmt.Sprintf("%sattachment_pools.config", prefix)).Equals(true, "public_indexable"))
|
2024-08-02 13:52:32 +00:00
|
|
|
}
|
|
|
|
|
2024-07-26 08:42:16 +00:00
|
|
|
if len(c.Query("author")) > 0 {
|
|
|
|
var author models.Account
|
|
|
|
if err := database.C.Where("name = ?", c.Query("author")).First(&author).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
|
|
|
tx = tx.Where("account_id = ?", author.ID)
|
|
|
|
}
|
2024-07-25 14:12:00 +00:00
|
|
|
}
|
2024-07-26 08:32:01 +00:00
|
|
|
|
2024-08-18 17:49:27 +00:00
|
|
|
if pools := c.Query("pools"); len(pools) > 0 {
|
|
|
|
prefix := viper.GetString("database.prefix")
|
|
|
|
poolAliases := strings.Split(pools, ",")
|
|
|
|
tx = tx.
|
|
|
|
Joins(fmt.Sprintf("JOIN %sattachment_pools ON %sattachment_pools.id = %sattachments.pool_id", prefix, prefix, prefix)).
|
|
|
|
Where(fmt.Sprintf("%sattachment_pools.alias IN ?", prefix), poolAliases)
|
2024-07-25 14:12:00 +00:00
|
|
|
}
|
|
|
|
|
2024-08-11 08:21:16 +00:00
|
|
|
if original := c.QueryBool("original", false); original {
|
|
|
|
tx = tx.Where("ref_id IS NULL")
|
|
|
|
}
|
|
|
|
|
2024-07-25 14:12:00 +00:00
|
|
|
var count int64
|
|
|
|
countTx := tx
|
|
|
|
if err := countTx.Model(&models.Attachment{}).Count(&count).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-08-06 17:02:03 +00:00
|
|
|
if needQuery {
|
|
|
|
var out []models.Attachment
|
|
|
|
if err := tx.Offset(offset).Limit(take).Preload("Account").Find(&out).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(idxList) == 0 {
|
|
|
|
result = out
|
|
|
|
} else {
|
|
|
|
for _, item := range out {
|
|
|
|
for p, id := range idxList {
|
2024-08-18 13:10:10 +00:00
|
|
|
if item.Rid == id {
|
2024-08-06 17:02:03 +00:00
|
|
|
result[p] = item
|
|
|
|
}
|
2024-08-06 15:45:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-06 17:02:03 +00:00
|
|
|
for _, item := range result {
|
2024-08-18 11:53:50 +00:00
|
|
|
services.CacheAttachment(item)
|
2024-08-06 17:02:03 +00:00
|
|
|
}
|
|
|
|
|
2024-07-25 14:12:00 +00:00
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"count": count,
|
2024-08-06 15:45:58 +00:00
|
|
|
"data": result,
|
2024-07-25 14:12:00 +00:00
|
|
|
})
|
|
|
|
}
|