Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions apis/hole/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"hash/fnv"
"io"
"math/rand"
"net/http"
Expand Down Expand Up @@ -964,10 +965,14 @@ func DeleteHole(c *fiber.Ctx) error {
func GenerateSummary(c *fiber.Ctx) error {
uid, _ := common.GetUserID(c)
if config.Config.WhiteListUserIds != nil && !slices.Contains(config.Config.WhiteListUserIds, uid) {
return c.Status(404).JSON(fiber.Map{
"code": 404,
"message": "Cannot GET " + c.Path(),
})
h := fnv.New32a()
h.Write([]byte(strconv.Itoa(uid)))
if int(h.Sum32())%100 >= int(100*config.Config.WhiteListRate) {
return c.Status(404).JSON(fiber.Map{
"code": 404,
"message": "Cannot GET " + c.Path(),
})
}
}
id, _ := c.ParamsInt("id")
forceRefresh := c.QueryBool("force_refresh")
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ var Config struct {
SummaryReplyBoundary3 int `env:"SUMMARY_REPLY_BOUNDARY_1" envDefault:"1000"`
SummaryLogLimit int `env:"SUMMARY_LOG_LIMIT" envDefault:"1000"`
WhiteListUserIds []int `env:"WHITE_LIST_USER_IDS"`
WhiteListRate float32 `env:"WHITE_LIST_RATE" envDefault:"1"`
MaxSummaryFloors int `env:"MAX_FLOORS_PER_HOLE" envDefault:"50"`
}

Expand Down
9 changes: 9 additions & 0 deletions models/hole.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models

import (
"fmt"
"strconv"
"time"

"golang.org/x/exp/maps"
Expand All @@ -13,6 +14,7 @@ import (
"gorm.io/gorm/clause"
"gorm.io/plugin/dbresolver"

"hash/fnv"
"treehole_next/config"
"treehole_next/utils"
)
Expand Down Expand Up @@ -293,6 +295,13 @@ func (holes Holes) Preprocess(c *fiber.Ctx) error {

// for users in whitelist or whitelist is empty, AISummaryAvailable is true,
hole.AISummaryAvailable = config.Config.WhiteListUserIds == nil || slices.Contains(config.Config.WhiteListUserIds, uid)
if !hole.AISummaryAvailable {
h := fnv.New32a()
h.Write([]byte(strconv.Itoa(uid)))
if int(h.Sum32())%100 < int(100*config.Config.WhiteListRate) {
hole.AISummaryAvailable = true
}
}
hole.AISummaryAvailable = hole.AISummaryAvailable && !(hole.Locked || hole.Hidden || hole.Frozen)
for _, tag := range hole.Tags {
if len(tag.Name) > 0 && tag.Name[0] == '*' {
Expand Down
Loading