Update to use commons package
- Rename module from publisher to publish - Change all shared.* references to commons.* - Use commons.Item, commons.Feed, etc from shared library Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
module github.com/1440news/publisher
|
||||
module github.com/1440news/publish
|
||||
|
||||
go 1.24.0
|
||||
|
||||
|
||||
@@ -8,11 +8,11 @@ import (
|
||||
)
|
||||
|
||||
// scanItems scans rows into Item structs
|
||||
func scanItems(rows pgx.Rows) ([]*shared.Item, error) {
|
||||
var items []*shared.Item
|
||||
func scanItems(rows pgx.Rows) ([]*commons.Item, error) {
|
||||
var items []*commons.Item
|
||||
|
||||
for rows.Next() {
|
||||
item := &shared.Item{}
|
||||
item := &commons.Item{}
|
||||
var guid, title, link, description, content, author *string
|
||||
var pubDate, updatedAt, publishedAt *interface{}
|
||||
var publishedUri *string
|
||||
@@ -32,13 +32,13 @@ func scanItems(rows pgx.Rows) ([]*shared.Item, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
item.GUID = shared.StringValue(guid)
|
||||
item.Title = shared.StringValue(title)
|
||||
item.Link = shared.StringValue(link)
|
||||
item.Description = shared.StringValue(description)
|
||||
item.Content = shared.StringValue(content)
|
||||
item.Author = shared.StringValue(author)
|
||||
item.PublishedUri = shared.StringValue(publishedUri)
|
||||
item.GUID = commons.StringValue(guid)
|
||||
item.Title = commons.StringValue(title)
|
||||
item.Link = commons.StringValue(link)
|
||||
item.Description = commons.StringValue(description)
|
||||
item.Content = commons.StringValue(content)
|
||||
item.Author = commons.StringValue(author)
|
||||
item.PublishedUri = commons.StringValue(publishedUri)
|
||||
|
||||
if pubDate != nil {
|
||||
if t, ok := (*pubDate).(interface{ Time() interface{} }); ok {
|
||||
@@ -49,9 +49,9 @@ func scanItems(rows pgx.Rows) ([]*shared.Item, error) {
|
||||
}
|
||||
|
||||
if enclosureUrl != nil && *enclosureUrl != "" {
|
||||
item.Enclosure = &shared.Enclosure{
|
||||
item.Enclosure = &commons.Enclosure{
|
||||
URL: *enclosureUrl,
|
||||
Type: shared.StringValue(enclosureType),
|
||||
Type: commons.StringValue(enclosureType),
|
||||
}
|
||||
if enclosureLength != nil {
|
||||
item.Enclosure.Length = *enclosureLength
|
||||
|
||||
@@ -16,7 +16,7 @@ func main() {
|
||||
fmt.Println("Starting publisher service...")
|
||||
|
||||
// Open database connection
|
||||
db, err := shared.OpenDatabase("")
|
||||
db, err := commons.OpenDatabase("")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to connect to database: %v", err)
|
||||
}
|
||||
@@ -82,14 +82,14 @@ func loadEnvFile(filename string) {
|
||||
|
||||
// PublisherService manages publishing items to AT Protocol PDS
|
||||
type PublisherService struct {
|
||||
db *shared.DB
|
||||
db *commons.DB
|
||||
publisher *Publisher
|
||||
pdsHost string
|
||||
feedPassword string
|
||||
}
|
||||
|
||||
// NewPublisherService creates a new publisher service
|
||||
func NewPublisherService(db *shared.DB, pdsHost, feedPassword string) *PublisherService {
|
||||
func NewPublisherService(db *commons.DB, pdsHost, feedPassword string) *PublisherService {
|
||||
return &PublisherService{
|
||||
db: db,
|
||||
publisher: NewPublisher(pdsHost),
|
||||
@@ -184,7 +184,7 @@ func (s *PublisherService) publishFeedItems(feedURL, account string) {
|
||||
}
|
||||
|
||||
// GetUnpublishedItems returns unpublished items for a feed
|
||||
func (s *PublisherService) GetUnpublishedItems(feedURL string, limit int) ([]*shared.Item, error) {
|
||||
func (s *PublisherService) GetUnpublishedItems(feedURL string, limit int) ([]*commons.Item, error) {
|
||||
rows, err := s.db.Query(`
|
||||
SELECT feed_url, guid, title, link, description, content, author, pub_date, discovered_at, updated_at,
|
||||
enclosure_url, enclosure_type, enclosure_length, image_urls, tags,
|
||||
|
||||
+1
-1
@@ -234,7 +234,7 @@ func formatTagsForPost(tags []string, textOffset int) (string, []BskyFacet) {
|
||||
}
|
||||
|
||||
// PublishItem posts a feed item to the PDS
|
||||
func (p *Publisher) PublishItem(session *PDSSession, item *shared.Item) (string, error) {
|
||||
func (p *Publisher) PublishItem(session *PDSSession, item *commons.Item) (string, error) {
|
||||
if item.GUID == "" && item.Link == "" {
|
||||
return "", fmt.Errorf("item has no GUID or link, cannot publish")
|
||||
}
|
||||
|
||||
@@ -49,10 +49,10 @@ func (s *PublisherService) handleEnablePublish(w http.ResponseWriter, r *http.Re
|
||||
return
|
||||
}
|
||||
|
||||
feedURL = shared.NormalizeURL(feedURL)
|
||||
feedURL = commons.NormalizeURL(feedURL)
|
||||
|
||||
if account == "" {
|
||||
account = shared.DeriveHandleFromFeed(feedURL)
|
||||
account = commons.DeriveHandleFromFeed(feedURL)
|
||||
if account == "" {
|
||||
http.Error(w, "could not derive account handle from URL", http.StatusBadRequest)
|
||||
return
|
||||
@@ -82,7 +82,7 @@ func (s *PublisherService) handleDisablePublish(w http.ResponseWriter, r *http.R
|
||||
return
|
||||
}
|
||||
|
||||
feedURL = shared.NormalizeURL(feedURL)
|
||||
feedURL = commons.NormalizeURL(feedURL)
|
||||
|
||||
if err := s.SetPublishStatus(feedURL, "skip", ""); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@@ -110,7 +110,7 @@ func (s *PublisherService) handleSetPublishStatus(w http.ResponseWriter, r *http
|
||||
return
|
||||
}
|
||||
|
||||
feedURL = shared.NormalizeURL(feedURL)
|
||||
feedURL = commons.NormalizeURL(feedURL)
|
||||
|
||||
result := map[string]interface{}{
|
||||
"url": feedURL,
|
||||
@@ -119,7 +119,7 @@ func (s *PublisherService) handleSetPublishStatus(w http.ResponseWriter, r *http
|
||||
|
||||
if status == "pass" {
|
||||
if account == "" {
|
||||
account = shared.DeriveHandleFromFeed(feedURL)
|
||||
account = commons.DeriveHandleFromFeed(feedURL)
|
||||
}
|
||||
result["account"] = account
|
||||
}
|
||||
@@ -228,7 +228,7 @@ func (s *PublisherService) handlePublishCandidates(w http.ResponseWriter, r *htt
|
||||
Category: f.Category,
|
||||
SourceHost: f.DomainHost,
|
||||
ItemCount: f.ItemCount,
|
||||
DerivedHandle: shared.DeriveHandleFromFeed(f.URL),
|
||||
DerivedHandle: commons.DeriveHandleFromFeed(f.URL),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -262,7 +262,7 @@ func (s *PublisherService) handleUnpublishedItems(w http.ResponseWriter, r *http
|
||||
}
|
||||
|
||||
if items == nil {
|
||||
items = []*shared.Item{}
|
||||
items = []*commons.Item{}
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
@@ -446,7 +446,7 @@ func (s *PublisherService) handleDeriveHandle(w http.ResponseWriter, r *http.Req
|
||||
return
|
||||
}
|
||||
|
||||
handle := shared.DeriveHandleFromFeed(feedURL)
|
||||
handle := commons.DeriveHandleFromFeed(feedURL)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
@@ -508,19 +508,19 @@ func (s *PublisherService) handleRefreshProfiles(w http.ResponseWriter, r *http.
|
||||
// Database helper methods
|
||||
|
||||
func (s *PublisherService) SetPublishStatus(feedURL, status, account string) error {
|
||||
feedURL = shared.NormalizeURL(feedURL)
|
||||
feedURL = commons.NormalizeURL(feedURL)
|
||||
|
||||
if status == "pass" && account == "" {
|
||||
account = shared.DeriveHandleFromFeed(feedURL)
|
||||
account = commons.DeriveHandleFromFeed(feedURL)
|
||||
}
|
||||
|
||||
_, err := s.db.Exec(`
|
||||
UPDATE feeds SET publish_status = $1, publish_account = $2 WHERE url = $3
|
||||
`, status, shared.NullableString(account), feedURL)
|
||||
`, status, commons.NullableString(account), feedURL)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *PublisherService) GetFeedsByPublishStatus(status string) ([]*shared.Feed, error) {
|
||||
func (s *PublisherService) GetFeedsByPublishStatus(status string) ([]*commons.Feed, error) {
|
||||
rows, err := s.db.Query(`
|
||||
SELECT url, type, category, title, description, language, site_url,
|
||||
discovered_at, last_checked_at, next_check_at, last_build_date,
|
||||
@@ -541,7 +541,7 @@ func (s *PublisherService) GetFeedsByPublishStatus(status string) ([]*shared.Fee
|
||||
return scanFeeds(rows)
|
||||
}
|
||||
|
||||
func (s *PublisherService) GetPublishCandidates(limit int) ([]*shared.Feed, error) {
|
||||
func (s *PublisherService) GetPublishCandidates(limit int) ([]*commons.Feed, error) {
|
||||
rows, err := s.db.Query(`
|
||||
SELECT url, type, category, title, description, language, site_url,
|
||||
discovered_at, last_checked_at, next_check_at, last_build_date,
|
||||
@@ -572,7 +572,7 @@ func (s *PublisherService) GetUnpublishedItemCount(feedURL string) (int, error)
|
||||
return count, err
|
||||
}
|
||||
|
||||
func (s *PublisherService) GetItemByGUID(feedURL, guid string) (*shared.Item, error) {
|
||||
func (s *PublisherService) GetItemByGUID(feedURL, guid string) (*commons.Item, error) {
|
||||
items, err := s.db.Query(`
|
||||
SELECT feed_url, guid, title, link, description, content, author, pub_date, discovered_at, updated_at,
|
||||
enclosure_url, enclosure_type, enclosure_length, image_urls, tags,
|
||||
@@ -620,11 +620,11 @@ func (s *PublisherService) RefreshAllProfiles(publisher *Publisher, feedPassword
|
||||
continue
|
||||
}
|
||||
|
||||
displayName := shared.StringValue(title)
|
||||
displayName := commons.StringValue(title)
|
||||
if displayName == "" {
|
||||
displayName = account
|
||||
}
|
||||
desc := stripHTML(shared.StringValue(description))
|
||||
desc := stripHTML(commons.StringValue(description))
|
||||
if desc == "" {
|
||||
desc = "News feed via 1440.news"
|
||||
}
|
||||
@@ -639,9 +639,9 @@ func (s *PublisherService) RefreshAllProfiles(publisher *Publisher, feedPassword
|
||||
}
|
||||
|
||||
var avatar *BlobRef
|
||||
faviconSource := shared.StringValue(siteURL)
|
||||
faviconSource := commons.StringValue(siteURL)
|
||||
if faviconSource == "" {
|
||||
faviconSource = shared.StringValue(sourceHost)
|
||||
faviconSource = commons.StringValue(sourceHost)
|
||||
}
|
||||
if faviconSource != "" {
|
||||
faviconData, mimeType, err := FetchFaviconBytes(faviconSource)
|
||||
@@ -659,11 +659,11 @@ func (s *PublisherService) RefreshAllProfiles(publisher *Publisher, feedPassword
|
||||
}
|
||||
|
||||
// scanFeeds helper
|
||||
func scanFeeds(rows interface{ Next() bool; Scan(...interface{}) error; Err() error }) ([]*shared.Feed, error) {
|
||||
var feeds []*shared.Feed
|
||||
func scanFeeds(rows interface{ Next() bool; Scan(...interface{}) error; Err() error }) ([]*commons.Feed, error) {
|
||||
var feeds []*commons.Feed
|
||||
|
||||
for rows.Next() {
|
||||
feed := &shared.Feed{}
|
||||
feed := &commons.Feed{}
|
||||
var feedType, category, title, description, language, siteURL *string
|
||||
var lastCheckedAt, nextCheckAt, lastBuildDate *interface{}
|
||||
var etag, lastModified, lastError, sourceURL, domainTLD *string
|
||||
@@ -685,19 +685,19 @@ func scanFeeds(rows interface{ Next() bool; Scan(...interface{}) error; Err() er
|
||||
continue
|
||||
}
|
||||
|
||||
feed.Type = shared.StringValue(feedType)
|
||||
feed.Category = shared.StringValue(category)
|
||||
feed.Title = shared.StringValue(title)
|
||||
feed.Description = shared.StringValue(description)
|
||||
feed.Language = shared.StringValue(language)
|
||||
feed.SiteURL = shared.StringValue(siteURL)
|
||||
feed.ETag = shared.StringValue(etag)
|
||||
feed.LastModified = shared.StringValue(lastModified)
|
||||
feed.LastError = shared.StringValue(lastError)
|
||||
feed.SourceURL = shared.StringValue(sourceURL)
|
||||
feed.DomainTLD = shared.StringValue(domainTLD)
|
||||
feed.PublishStatus = shared.StringValue(publishStatus)
|
||||
feed.PublishAccount = shared.StringValue(publishAccount)
|
||||
feed.Type = commons.StringValue(feedType)
|
||||
feed.Category = commons.StringValue(category)
|
||||
feed.Title = commons.StringValue(title)
|
||||
feed.Description = commons.StringValue(description)
|
||||
feed.Language = commons.StringValue(language)
|
||||
feed.SiteURL = commons.StringValue(siteURL)
|
||||
feed.ETag = commons.StringValue(etag)
|
||||
feed.LastModified = commons.StringValue(lastModified)
|
||||
feed.LastError = commons.StringValue(lastError)
|
||||
feed.SourceURL = commons.StringValue(sourceURL)
|
||||
feed.DomainTLD = commons.StringValue(domainTLD)
|
||||
feed.PublishStatus = commons.StringValue(publishStatus)
|
||||
feed.PublishAccount = commons.StringValue(publishAccount)
|
||||
|
||||
feeds = append(feeds, feed)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user