Switch from cookie to Bearer token auth and add CORS support - Semi finished

This commit is contained in:
2025-05-16 16:20:30 +03:30
parent 2e4b97e4bc
commit cb5149b7bc
8 changed files with 61 additions and 27 deletions
+28 -2
View File
@@ -60,11 +60,22 @@ func (ps *PostService) GetExistingAccountIds() []string {
ps.db.Model(&models.Account{}).Pluck("acc_id", &existingAccountIds)
return existingAccountIds
}
func (*PostService) GetNewPosts(existingPostIds []string, posts []models.Post) []models.Post {
var newPosts []models.Post = nil
for _, post := range posts {
if !arrayContains(existingPostIds, post.ID) && len(post.Attachments) > 0 && !post.Account.IsBot {
newPosts = append(newPosts, post)
var allImageMedia = true
for _, attachment := range post.Attachments {
if attachment.Type != "image" {
allImageMedia = false
break
}
} // Inefficient but anyways
if allImageMedia {
newPosts = append(newPosts, post)
}
}
}
return newPosts
@@ -94,9 +105,12 @@ func (ps *PostService) GetRandomPost() models.Post {
var post models.Post
ps.db.
Preload("Account").
Preload("Attachments").
Preload("Attachments", "approved = ?", true).
Order("RANDOM()").
First(&post)
if len(post.Attachments) > 0 {
post.Attachments = []models.MediaAttachment{post.Attachments[0]}
}
return post
}
@@ -112,6 +126,18 @@ func (ps *PostService) RejectMedia(mediaId string) bool {
Update("rejected", true).RowsAffected > 0
}
// Get a post which approve and rejet are false (For admin panel)
func (ps *PostService) GetMedia() models.MediaAttachment {
var media models.MediaAttachment
ps.db.Model(&models.MediaAttachment{}).
Where("approved = ?", false).
Where("rejected = ?", false).
Order("RANDOM()").
First(&media)
return media
}
func arrayContains(arr []string, str string) bool {
for _, a := range arr {
if a == str {