Fix MariaDB RANDOM() eror
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"CatsOfMastodonBotGo/internal/config"
|
||||||
"CatsOfMastodonBotGo/internal/database"
|
"CatsOfMastodonBotGo/internal/database"
|
||||||
"CatsOfMastodonBotGo/internal/domain"
|
"CatsOfMastodonBotGo/internal/domain"
|
||||||
"context"
|
"context"
|
||||||
@@ -105,11 +106,15 @@ func (ps *PostService) InsertNewAccounts(newAccounts []domain.Account) int {
|
|||||||
|
|
||||||
func (ps *PostService) GetRandomPost() domain.Post {
|
func (ps *PostService) GetRandomPost() domain.Post {
|
||||||
var post domain.Post
|
var post domain.Post
|
||||||
|
orderExpr := "RANDOM()" // sqlite
|
||||||
|
if config.Config.DBEngine != "sqlite" {
|
||||||
|
orderExpr = "RAND()" // mariadb/mysql
|
||||||
|
}
|
||||||
ps.db.
|
ps.db.
|
||||||
Preload("Account").
|
Preload("Account").
|
||||||
Preload("Attachments", "Approved = ?", true).
|
Preload("Attachments", "Approved = ?", true).
|
||||||
Where("exists (select 1 from media_attachments where post_id = posts.id and approved = ?)", true).
|
Where("exists (select 1 from media_attachments where post_id = posts.id and approved = ?)", true).
|
||||||
Order("RANDOM()").
|
Order(orderExpr).
|
||||||
First(&post)
|
First(&post)
|
||||||
if len(post.Attachments) > 0 {
|
if len(post.Attachments) > 0 {
|
||||||
post.Attachments = []domain.MediaAttachment{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)
|
// Get a post which approve and rejet are false (For admin panel)
|
||||||
func (ps *PostService) GetMedia() domain.MediaAttachment {
|
func (ps *PostService) GetMedia() domain.MediaAttachment {
|
||||||
var media domain.MediaAttachment
|
var media domain.MediaAttachment
|
||||||
|
orderExpr := "RANDOM()" // sqlite
|
||||||
|
if config.Config.DBEngine != "sqlite" {
|
||||||
|
orderExpr = "RAND()" // mariadb/mysql
|
||||||
|
}
|
||||||
ps.db.Model(&domain.MediaAttachment{}).
|
ps.db.Model(&domain.MediaAttachment{}).
|
||||||
Where("approved = ?", false).
|
Where("approved = ?", false).
|
||||||
Where("rejected = ?", false).
|
Where("rejected = ?", false).
|
||||||
Order("RANDOM()").
|
Order(orderExpr).
|
||||||
First(&media)
|
First(&media)
|
||||||
return media
|
return media
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user