|
| 1 | +package sync |
| 2 | + |
| 3 | +import ( |
| 4 | + "log/slog" |
| 5 | + |
| 6 | + "github.com/theandrew168/bloggulus/backend/feed" |
| 7 | + "github.com/theandrew168/bloggulus/backend/fetch" |
| 8 | + "github.com/theandrew168/bloggulus/backend/model" |
| 9 | + "github.com/theandrew168/bloggulus/backend/repository" |
| 10 | + "github.com/theandrew168/bloggulus/backend/timeutil" |
| 11 | +) |
| 12 | + |
| 13 | +// UpdateCacheHeaders updates the ETag and Last-Modified headers for a blog if they have changed. |
| 14 | +func UpdateCacheHeaders(blog *model.Blog, response fetch.FetchFeedResponse) bool { |
| 15 | + headersChanged := false |
| 16 | + if response.ETag != "" && response.ETag != blog.ETag() { |
| 17 | + headersChanged = true |
| 18 | + blog.SetETag(response.ETag) |
| 19 | + } |
| 20 | + |
| 21 | + if response.LastModified != "" && response.LastModified != blog.LastModified() { |
| 22 | + headersChanged = true |
| 23 | + blog.SetLastModified(response.LastModified) |
| 24 | + } |
| 25 | + |
| 26 | + return headersChanged |
| 27 | +} |
| 28 | + |
| 29 | +type ComparePostsResult struct { |
| 30 | + PostsToCreate []*model.Post |
| 31 | + PostsToUpdate []*model.Post |
| 32 | +} |
| 33 | + |
| 34 | +// ComparePosts compares a list of known posts to a list of feed posts and returns |
| 35 | +// a list of posts to create and a list of posts to update. |
| 36 | +func ComparePosts(blog *model.Blog, knownPosts []*model.Post, feedPosts []feed.Post) (ComparePostsResult, error) { |
| 37 | + // Create a map of URLs to posts for quick lookups. |
| 38 | + knownPostsByURL := make(map[string]*model.Post) |
| 39 | + for _, post := range knownPosts { |
| 40 | + knownPostsByURL[post.URL()] = post |
| 41 | + } |
| 42 | + |
| 43 | + var postsToCreate []*model.Post |
| 44 | + var postsToUpdate []*model.Post |
| 45 | + |
| 46 | + // Compare each post in the feed to the posts in the database. |
| 47 | + for _, feedPost := range feedPosts { |
| 48 | + knownPost, ok := knownPostsByURL[feedPost.URL] |
| 49 | + if !ok { |
| 50 | + // The post is new so we need to create it. |
| 51 | + postToCreate, err := model.NewPost( |
| 52 | + blog, |
| 53 | + feedPost.URL, |
| 54 | + feedPost.Title, |
| 55 | + feedPost.Content, |
| 56 | + feedPost.PublishedAt, |
| 57 | + ) |
| 58 | + if err != nil { |
| 59 | + return ComparePostsResult{}, err |
| 60 | + } |
| 61 | + |
| 62 | + postsToCreate = append(postsToCreate, postToCreate) |
| 63 | + } else { |
| 64 | + // The post already exists but we might need to update it. |
| 65 | + knownPostShouldBeUpdated := false |
| 66 | + |
| 67 | + // Check if the post's title has changed. |
| 68 | + if feedPost.Title != "" && feedPost.Title != knownPost.Title() { |
| 69 | + knownPost.SetTitle(feedPost.Title) |
| 70 | + knownPostShouldBeUpdated = true |
| 71 | + } |
| 72 | + |
| 73 | + // Check if the post's content has changed. |
| 74 | + if feedPost.Content != "" && feedPost.Content != knownPost.Content() { |
| 75 | + knownPost.SetContent(feedPost.Content) |
| 76 | + knownPostShouldBeUpdated = true |
| 77 | + } |
| 78 | + |
| 79 | + // Check if the post's publishedAt date has changed. |
| 80 | + if feedPost.PublishedAt != knownPost.PublishedAt() { |
| 81 | + knownPost.SetPublishedAt(feedPost.PublishedAt) |
| 82 | + knownPostShouldBeUpdated = true |
| 83 | + } |
| 84 | + |
| 85 | + // If any post data has changed, add it to the list of posts to update. |
| 86 | + if knownPostShouldBeUpdated { |
| 87 | + postsToUpdate = append(postsToUpdate, knownPost) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + result := ComparePostsResult{ |
| 93 | + PostsToCreate: postsToCreate, |
| 94 | + PostsToUpdate: postsToUpdate, |
| 95 | + } |
| 96 | + return result, nil |
| 97 | +} |
| 98 | + |
| 99 | +func SyncNewBlog(repo *repository.Repository, feedFetcher fetch.FeedFetcher, feedURL string) error { |
| 100 | + // Make an unconditional fetch for the blog's feed. |
| 101 | + req := fetch.FetchFeedRequest{ |
| 102 | + URL: feedURL, |
| 103 | + } |
| 104 | + resp, err := feedFetcher.FetchFeed(req) |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + |
| 109 | + // No feed data from a new blog is an error. |
| 110 | + if resp.Feed == "" { |
| 111 | + return fetch.ErrUnreachableFeed |
| 112 | + } |
| 113 | + |
| 114 | + feedBlog, err := feed.Parse(feedURL, resp.Feed) |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + // Create a new blog based on the feed data. |
| 120 | + blog, err := model.NewBlog( |
| 121 | + feedBlog.FeedURL, |
| 122 | + feedBlog.SiteURL, |
| 123 | + feedBlog.Title, |
| 124 | + resp.ETag, |
| 125 | + resp.LastModified, |
| 126 | + timeutil.Now(), |
| 127 | + ) |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + |
| 132 | + err = repo.Blog().Create(blog) |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + |
| 137 | + err = SyncPosts(repo, blog, feedBlog.Posts) |
| 138 | + if err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + |
| 142 | + return nil |
| 143 | +} |
| 144 | + |
| 145 | +func SyncExistingBlog(repo *repository.Repository, feedFetcher fetch.FeedFetcher, blog *model.Blog) error { |
| 146 | + // Make a conditional fetch for the blog's feed. |
| 147 | + req := fetch.FetchFeedRequest{ |
| 148 | + URL: blog.FeedURL(), |
| 149 | + ETag: blog.ETag(), |
| 150 | + LastModified: blog.LastModified(), |
| 151 | + } |
| 152 | + resp, err := feedFetcher.FetchFeed(req) |
| 153 | + if err != nil { |
| 154 | + return err |
| 155 | + } |
| 156 | + |
| 157 | + // Update the blog's cache headers if they have changed. |
| 158 | + headersChanged := UpdateCacheHeaders(blog, resp) |
| 159 | + if headersChanged { |
| 160 | + err = repo.Blog().Update(blog) |
| 161 | + if err != nil { |
| 162 | + return err |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + if resp.Feed == "" { |
| 167 | + slog.Info("skipping blog (no feed content)", "title", blog.Title(), "id", blog.ID()) |
| 168 | + return nil |
| 169 | + } |
| 170 | + |
| 171 | + feedBlog, err := feed.Parse(blog.FeedURL(), resp.Feed) |
| 172 | + if err != nil { |
| 173 | + return err |
| 174 | + } |
| 175 | + |
| 176 | + err = SyncPosts(repo, blog, feedBlog.Posts) |
| 177 | + if err != nil { |
| 178 | + return err |
| 179 | + } |
| 180 | + |
| 181 | + return nil |
| 182 | +} |
| 183 | + |
| 184 | +func SyncPosts(repo *repository.Repository, blog *model.Blog, feedPosts []feed.Post) error { |
| 185 | + // List all known posts for the current blog. |
| 186 | + knownPosts, err := repo.Post().ListByBlog(blog) |
| 187 | + if err != nil { |
| 188 | + return err |
| 189 | + } |
| 190 | + |
| 191 | + // Compare the known posts to the feed posts. |
| 192 | + result, err := ComparePosts(blog, knownPosts, feedPosts) |
| 193 | + if err != nil { |
| 194 | + return err |
| 195 | + } |
| 196 | + |
| 197 | + // Create any posts that are new. |
| 198 | + for _, post := range result.PostsToCreate { |
| 199 | + err = repo.Post().Create(post) |
| 200 | + if err != nil { |
| 201 | + slog.Warn("failed to create post", "url", post.URL(), "error", err.Error()) |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + // Update any posts that have changed. |
| 206 | + for _, post := range result.PostsToUpdate { |
| 207 | + err = repo.Post().Update(post) |
| 208 | + if err != nil { |
| 209 | + slog.Warn("failed to update post", "url", post.URL(), "error", err.Error()) |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + return nil |
| 214 | +} |
0 commit comments