Fix MariaDB RANDOM() eror

This commit is contained in:
2025-07-25 12:28:07 +03:30
parent b818726b1b
commit 714443156e

View File

@@ -1,6 +1,7 @@
package services
import (
"CatsOfMastodonBotGo/internal/config"
"CatsOfMastodonBotGo/internal/database"
"CatsOfMastodonBotGo/internal/domain"
"context"
@@ -105,11 +106,15 @@ func (ps *PostService) InsertNewAccounts(newAccounts []domain.Account) int {
func (ps *PostService) GetRandomPost() domain.Post {
var post domain.Post
orderExpr := "RANDOM()" // sqlite
if config.Config.DBEngine != "sqlite" {
orderExpr = "RAND()" // mariadb/mysql
}
ps.db.
Preload("Account").
Preload("Attachments", "Approved = ?", true).
Where("exists (select 1 from media_attachments where post_id = posts.id and approved = ?)", true).
Order("RANDOM()").
Order(orderExpr).
First(&post)
if len(post.Attachments) > 0 {
post.Attachments = []domain.MediaAttachment{post.Attachments[0]}
@@ -132,10 +137,14 @@ func (ps *PostService) RejectMedia(mediaId string) bool {
// Get a post which approve and rejet are false (For admin panel)
func (ps *PostService) GetMedia() domain.MediaAttachment {
var media domain.MediaAttachment
orderExpr := "RANDOM()" // sqlite
if config.Config.DBEngine != "sqlite" {
orderExpr = "RAND()" // mariadb/mysql
}
ps.db.Model(&domain.MediaAttachment{}).
Where("approved = ?", false).
Where("rejected = ?", false).
Order("RANDOM()").
Order(orderExpr).
First(&media)
return media
}