Refactor app structure: move models to domain, centralize config and database init - TODO: add jwt
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"CatsOfMastodonBotGo/internal/models"
|
||||
"CatsOfMastodonBotGo/internal/database"
|
||||
"CatsOfMastodonBotGo/internal/domain"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
@@ -16,12 +17,14 @@ type PostService struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
var PostServiceInstance *PostService
|
||||
|
||||
// Constructor
|
||||
func NewPostService(db *gorm.DB) *PostService {
|
||||
return &PostService{db: db}
|
||||
func InitPostService() {
|
||||
PostServiceInstance = &PostService{db: database.Gorm}
|
||||
}
|
||||
|
||||
func (*PostService) GetPostsFromApi(ctx context.Context, tag string, instance string) (error, []models.Post) {
|
||||
func (*PostService) GetPostsFromApi(ctx context.Context, tag string, instance string) (error, []domain.Post) {
|
||||
var requestUrl = instance + "/api/v1/timelines/tag/" + tag + "?limit=40"
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", requestUrl, nil)
|
||||
if err != nil {
|
||||
@@ -36,7 +39,7 @@ func (*PostService) GetPostsFromApi(ctx context.Context, tag string, instance st
|
||||
return fmt.Errorf("Status code:", resp.StatusCode, " Content-Type:", resp.Header.Get("Content-Type")), nil
|
||||
}
|
||||
|
||||
var posts []models.Post = nil
|
||||
var posts []domain.Post = nil
|
||||
err = json.NewDecoder(resp.Body).Decode(&posts)
|
||||
if err != nil {
|
||||
return err, nil
|
||||
@@ -51,19 +54,19 @@ func (*PostService) GetPostsFromApi(ctx context.Context, tag string, instance st
|
||||
|
||||
func (ps *PostService) GetExistingPostIds() []string {
|
||||
var existingPostIds []string
|
||||
ps.db.Model(&models.Post{}).Pluck("id", &existingPostIds)
|
||||
ps.db.Model(&domain.Post{}).Pluck("id", &existingPostIds)
|
||||
return existingPostIds
|
||||
}
|
||||
|
||||
func (ps *PostService) GetExistingAccountIds() []string {
|
||||
var existingAccountIds []string
|
||||
ps.db.Model(&models.Account{}).Pluck("acc_id", &existingAccountIds)
|
||||
ps.db.Model(&domain.Account{}).Pluck("acc_id", &existingAccountIds)
|
||||
return existingAccountIds
|
||||
}
|
||||
|
||||
|
||||
func (*PostService) GetNewPosts(existingPostIds []string, posts []models.Post) []models.Post {
|
||||
var newPosts []models.Post = nil
|
||||
func (*PostService) GetNewPosts(existingPostIds []string, posts []domain.Post) []domain.Post {
|
||||
var newPosts []domain.Post = nil
|
||||
for _, post := range posts {
|
||||
if !arrayContains(existingPostIds, post.ID) && len(post.Attachments) > 0 && !post.Account.IsBot {
|
||||
var allImageMedia = true
|
||||
@@ -81,8 +84,8 @@ func (*PostService) GetNewPosts(existingPostIds []string, posts []models.Post) [
|
||||
return newPosts
|
||||
}
|
||||
|
||||
func (*PostService) GetNewAccounts(existingAccountIds []string, posts []models.Post) []models.Account {
|
||||
var newAccounts []models.Account = nil
|
||||
func (*PostService) GetNewAccounts(existingAccountIds []string, posts []domain.Post) []domain.Account {
|
||||
var newAccounts []domain.Account = nil
|
||||
for _, post := range posts {
|
||||
if !arrayContains(existingAccountIds, post.Account.AccId) {
|
||||
newAccounts = append(newAccounts, post.Account)
|
||||
@@ -91,45 +94,45 @@ func (*PostService) GetNewAccounts(existingAccountIds []string, posts []models.P
|
||||
return newAccounts
|
||||
}
|
||||
|
||||
func (ps *PostService) InsertNewPosts(newPosts []models.Post) int {
|
||||
func (ps *PostService) InsertNewPosts(newPosts []domain.Post) int {
|
||||
return int(ps.db.Create(&newPosts).RowsAffected)
|
||||
}
|
||||
|
||||
func (ps *PostService) InsertNewAccounts(newAccounts []models.Account) int {
|
||||
func (ps *PostService) InsertNewAccounts(newAccounts []domain.Account) int {
|
||||
return int(ps.db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&newAccounts).RowsAffected)
|
||||
}
|
||||
|
||||
// From this point on, its for the api endpoints
|
||||
|
||||
func (ps *PostService) GetRandomPost() models.Post {
|
||||
var post models.Post
|
||||
func (ps *PostService) GetRandomPost() domain.Post {
|
||||
var post domain.Post
|
||||
ps.db.
|
||||
Preload("Account").
|
||||
Preload("Attachments", "approved = ?", true).
|
||||
Order("RANDOM()").
|
||||
First(&post)
|
||||
if len(post.Attachments) > 0 {
|
||||
post.Attachments = []models.MediaAttachment{post.Attachments[0]}
|
||||
post.Attachments = []domain.MediaAttachment{post.Attachments[0]}
|
||||
}
|
||||
return post
|
||||
}
|
||||
|
||||
func (ps *PostService) ApproveMedia(mediaId string) bool {
|
||||
return ps.db.Model(&models.MediaAttachment{}).
|
||||
return ps.db.Model(&domain.MediaAttachment{}).
|
||||
Where("id = ?", mediaId).
|
||||
Update("approved", true).RowsAffected > 0
|
||||
}
|
||||
|
||||
func (ps *PostService) RejectMedia(mediaId string) bool {
|
||||
return ps.db.Model(&models.MediaAttachment{}).
|
||||
return ps.db.Model(&domain.MediaAttachment{}).
|
||||
Where("id = ?", mediaId).
|
||||
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{}).
|
||||
func (ps *PostService) GetMedia() domain.MediaAttachment {
|
||||
var media domain.MediaAttachment
|
||||
ps.db.Model(&domain.MediaAttachment{}).
|
||||
Where("approved = ?", false).
|
||||
Where("rejected = ?", false).
|
||||
Order("RANDOM()").
|
||||
|
||||
Reference in New Issue
Block a user