64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
package helpers
|
|
|
|
import (
|
|
"CatsOfMastodonBotGo/models"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func AddMigrations(db *gorm.DB) {
|
|
db.AutoMigrate(&models.Post{}, &models.MediaAttachment{}, &models.Account{})
|
|
}
|
|
|
|
func GetExistingPostIds(db *gorm.DB) []string {
|
|
var existingPostIds []string
|
|
db.Model(&models.Post{}).Pluck("id", &existingPostIds)
|
|
return existingPostIds
|
|
}
|
|
|
|
func GetExistingAccountIds(db *gorm.DB) []string {
|
|
var existingAccountIds []string
|
|
db.Model(&models.Account{}).Pluck("acc_id", &existingAccountIds)
|
|
return existingAccountIds
|
|
}
|
|
|
|
func GetNewPosts(existingPostIds []string, posts []models.Post) []models.Post {
|
|
var newPosts []models.Post = nil
|
|
for _, post := range posts {
|
|
if !arrayContains(existingPostIds, post.ID) {
|
|
newPosts = append(newPosts, post)
|
|
}
|
|
}
|
|
return newPosts
|
|
}
|
|
|
|
func 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 InsertNewPosts(db *gorm.DB, newPosts []models.Post) int{
|
|
return int(db.Create(&newPosts).RowsAffected)
|
|
}
|
|
|
|
func InsertNewAccounts(db *gorm.DB, newAccounts []models.Account) int{
|
|
return int(db.Create(&newAccounts).RowsAffected)
|
|
}
|
|
|
|
func arrayContains(arr []string, str string) bool {
|
|
for _, a := range arr {
|
|
if a == str {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
|
|
|
|
|