From ad8cbbf5560f3b0868ef253dce235112dfcc43f6 Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Fri, 11 Aug 2023 16:11:59 +0200 Subject: [PATCH] sending webmentions again --- app/webmention_service.go | 61 +++++++++++++++++++++++++++++++++++++++ cmd/owl/main.go | 10 +++---- 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/app/webmention_service.go b/app/webmention_service.go index 47a5ee7..ba2c2c0 100644 --- a/app/webmention_service.go +++ b/app/webmention_service.go @@ -1,25 +1,32 @@ package app import ( + "fmt" + "net/url" "owl-blogs/app/owlhttp" "owl-blogs/app/repository" + "owl-blogs/config" + "owl-blogs/domain/model" "owl-blogs/interactions" "time" ) type WebmentionService struct { + ConfigRepo repository.ConfigRepository InteractionRepository repository.InteractionRepository EntryRepository repository.EntryRepository Http owlhttp.HttpClient } func NewWebmentionService( + configRepo repository.ConfigRepository, interactionRepository repository.InteractionRepository, entryRepository repository.EntryRepository, http owlhttp.HttpClient, bus *EventBus, ) *WebmentionService { svc := &WebmentionService{ + ConfigRepo: configRepo, InteractionRepository: interactionRepository, EntryRepository: entryRepository, Http: http, @@ -90,3 +97,57 @@ func (s *WebmentionService) ProcessWebmention(source string, target string) erro return err } } + +func (s *WebmentionService) ScanForLinks(entry model.Entry) ([]string, error) { + content := string(entry.Content()) + return ParseLinksFromString(content) +} + +func (s *WebmentionService) FullEntryUrl(entry model.Entry) string { + siteConfig := model.SiteConfig{} + s.ConfigRepo.Get(config.SITE_CONFIG, &siteConfig) + + url, _ := url.JoinPath( + siteConfig.FullUrl, + fmt.Sprintf("/posts/%s/", entry.ID()), + ) + return url +} + +func (s *WebmentionService) SendWebmention(entry model.Entry) error { + links, err := s.ScanForLinks(entry) + if err != nil { + return err + } + for _, target := range links { + resp, err := s.Http.Get(target) + if err != nil { + continue + } + endpoint, err := GetWebmentionEndpoint(resp) + if err != nil { + continue + } + payload := url.Values{} + payload.Set("source", s.FullEntryUrl(entry)) + payload.Set("target", target) + _, err = s.Http.PostForm(endpoint, payload) + if err != nil { + continue + } + println("Send webmention for target", target) + } + return nil +} + +func (s *WebmentionService) NotifyEntryCreated(entry model.Entry) { + s.SendWebmention(entry) +} + +func (s *WebmentionService) NotifyEntryUpdated(entry model.Entry) { + s.SendWebmention(entry) +} + +func (s *WebmentionService) NotifyEntryDeleted(entry model.Entry) { + s.SendWebmention(entry) +} diff --git a/cmd/owl/main.go b/cmd/owl/main.go index 4fb3cf8..39bea94 100644 --- a/cmd/owl/main.go +++ b/cmd/owl/main.go @@ -47,7 +47,7 @@ func App(db infra.Database) *web.WebApp { entryRepo := infra.NewEntryRepository(db, entryRegister) binRepo := infra.NewBinaryFileRepo(db) authorRepo := infra.NewDefaultAuthorRepo(db) - siteConfigRepo := infra.NewConfigRepo(db) + configRepo := infra.NewConfigRepo(db) interactionRepo := infra.NewInteractionRepo(db, interactionRegister) // Create External Services @@ -59,21 +59,21 @@ func App(db infra.Database) *web.WebApp { // Create Services entryService := app.NewEntryService(entryRepo, eventBus) binaryService := app.NewBinaryFileService(binRepo) - authorService := app.NewAuthorService(authorRepo, siteConfigRepo) + authorService := app.NewAuthorService(authorRepo, configRepo) webmentionService := app.NewWebmentionService( - interactionRepo, entryRepo, httpClient, eventBus, + configRepo, interactionRepo, entryRepo, httpClient, eventBus, ) // plugins plugings.NewEcho(eventBus) plugings.RegisterInstagram( - siteConfigRepo, configRegister, binaryService, eventBus, + configRepo, configRegister, binaryService, eventBus, ) // Create WebApp return web.NewWebApp( entryService, entryRegister, binaryService, - authorService, siteConfigRepo, configRegister, + authorService, configRepo, configRegister, webmentionService, interactionRepo, )