website/pkg/models/forum_quota.go
Noah Petherbridge b12390563e Forum Creation Quotas
Add minimum quotas for users to earn the ability to create custom forums.

The entry requirements that could earn the first forum include:
1. Having a Certified account status for at least 45 days.
2. Having written 10 posts or replies in the forums.

Additional quota is granted in increasing difficulty based on the count of
forum posts created.

Other changes:

* Admin view of Manage Forums can filter for official/community.
* "Certified Since" now shown on profile pages.
* Update FAQ page for Forums feature.
2024-08-23 22:56:40 -07:00

68 lines
1.7 KiB
Go

package models
import (
"time"
"code.nonshy.com/nonshy/website/pkg/config"
"code.nonshy.com/nonshy/website/pkg/log"
)
// Functions dealing with quota allowances for user-created forums.
// ComputeForumQuota returns a count of how many user-created forums this user is allowed to own.
//
// The forum quota slowly increases over time and quantity of forum posts written by this user.
func ComputeForumQuota(user *User) int64 {
var (
credits int64
numPosts = CountCommentsByUser(user, "threads")
)
// Get the user's certification date. They get one credit for having a long standing
// certified status on their account.
if certSince, err := user.CertifiedSince(); err != nil {
return 0
} else if time.Since(certSince) > config.UserForumQuotaCertLifetimeDays {
credits++
}
// Take their number of posts and compute their quota.
var schedule int64
for _, schedule = range config.UserForumQuotaCommentCountSchedule {
if numPosts > schedule {
credits++
numPosts -= schedule
}
}
// If they still have posts, repeat the final schedule per credit.
for numPosts > schedule {
credits++
numPosts -= schedule
}
return credits
}
// ForumQuotaRemaining computes the amount of additional forums the current user can create.
func (u *User) ForumQuotaRemaining() int64 {
var (
quota = ComputeForumQuota(u)
owned = CountOwnedUserForums(u)
)
return quota - owned
}
// CountOwnedUserForums returns the total number of user forums owned by the given user.
func CountOwnedUserForums(user *User) int64 {
var count int64
result := DB.Model(&Forum{}).Where(
"owner_id = ? AND (category='' OR category IS NULL)",
user.ID,
).Count(&count)
if result.Error != nil {
log.Error("CountOwnedUserForums(%d): %s", user.ID, result.Error)
}
return count
}