68 lines
1.7 KiB
Go
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
|
||
|
}
|