66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
package services
|
|
|
|
import (
|
|
"CatsOfMastodonBotGo/internal/models"
|
|
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
type PostService struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// Constructor
|
|
func NewPostService(db *gorm.DB) *PostService {
|
|
return &PostService{db: db}
|
|
}
|
|
|
|
func (ps *PostService) GetExistingPostIds() []string {
|
|
var existingPostIds []string
|
|
ps.db.Model(&models.Post{}).Pluck("id", &existingPostIds)
|
|
return existingPostIds
|
|
}
|
|
|
|
func (ps *PostService) GetExistingAccountIds() []string {
|
|
var existingAccountIds []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)
|
|
}
|
|
}
|
|
return newPosts
|
|
}
|
|
|
|
func (*PostService) GetNewAccounts(existingAccountIds []string, posts []models.Post) []models.Account {
|
|
var newAccounts []models.Account = nil
|
|
for _, post := range posts {
|
|
if !arrayContains(existingAccountIds, post.Account.AccId) {
|
|
newAccounts = append(newAccounts, post.Account)
|
|
}
|
|
}
|
|
return newAccounts
|
|
}
|
|
|
|
func (ps *PostService) InsertNewPosts(newPosts []models.Post) int {
|
|
return int(ps.db.Create(&newPosts).RowsAffected)
|
|
}
|
|
|
|
func (ps *PostService) InsertNewAccounts(newAccounts []models.Account) int {
|
|
return int(ps.db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&newAccounts).RowsAffected)
|
|
}
|
|
|
|
func arrayContains(arr []string, str string) bool {
|
|
for _, a := range arr {
|
|
if a == str {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|