Changed project structure

This commit is contained in:
2025-05-11 20:43:56 +03:30
parent 8be6290635
commit 3e7f6b92d3
6 changed files with 4 additions and 4 deletions

View File

@@ -0,0 +1,59 @@
package helpers
import (
"CatsOfMastodonBotGo/internal/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) && len(post.Attachments) > 0 && !post.Account.IsBot {
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
}

View File

@@ -0,0 +1,43 @@
package helpers
import (
"CatsOfMastodonBotGo/internal/models"
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
)
func GetPosts(ctx context.Context, tag string, instance string) (error, []models.Post) {
var requestUrl = instance + "/api/v1/timelines/tag/" + tag + "?limit=40"
req, err := http.NewRequestWithContext(ctx, "GET", requestUrl, nil)
if err != nil {
log.Fatal(err)
return err, nil
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
return err, nil
}
if resp.StatusCode != 200 || strings.Split(strings.ToLower(resp.Header.Get("Content-Type")), ";")[0] != "application/json" {
log.Fatal("Status code:", resp.StatusCode, " Content-Type:", resp.Header.Get("Content-Type"))
return err, nil
}
var posts []models.Post = nil
err = json.NewDecoder(resp.Body).Decode(&posts)
if err != nil {
log.Fatal(err)
return err, nil
}
// defer: it basically means "do this later when the function returns"
defer resp.Body.Close()
if posts == nil {
return fmt.Errorf("no posts found for tag %s on instance %s", tag, instance), nil
}
return nil, posts
}

View File

@@ -0,0 +1,11 @@
package models
type Account struct {
AccId string `json:"id" gorm:"primaryKey"`
Username string `json:"username"`
Acct string `json:"acct"`
DisplayName string `json:"display_name"`
IsBot bool `json:"bot"`
Url string `json:"url"`
AvatarStatic string `json:"avatar_static"`
}

View File

@@ -0,0 +1,11 @@
package models
type MediaAttachment struct {
ID string `json:"id" gorm:"primaryKey"`
Type string `json:"type"`
Url string `json:"url"`
PreviewUrl string `json:"preview_url"`
RemoteUrl string `json:"remote_url"`
Approved bool `json:"-"`
PostID string // Foreign key to Post
}

9
internal/models/post.go Normal file
View File

@@ -0,0 +1,9 @@
package models
type Post struct {
ID string `json:"id" gorm:"primaryKey"`
Url string `json:"url"`
AccountID string // Foreign key field (must match Account.AccId)
Account Account `json:"account" gorm:"foreignKey:AccountID;references:AccId"`
Attachments []MediaAttachment `json:"media_attachments" gorm:"foreignKey:PostID;references:ID"`
}