sending webmentions again

This commit is contained in:
Niko Abeler 2023-08-11 16:11:59 +02:00
parent 08678e2697
commit ad8cbbf556
2 changed files with 66 additions and 5 deletions

View File

@ -1,25 +1,32 @@
package app package app
import ( import (
"fmt"
"net/url"
"owl-blogs/app/owlhttp" "owl-blogs/app/owlhttp"
"owl-blogs/app/repository" "owl-blogs/app/repository"
"owl-blogs/config"
"owl-blogs/domain/model"
"owl-blogs/interactions" "owl-blogs/interactions"
"time" "time"
) )
type WebmentionService struct { type WebmentionService struct {
ConfigRepo repository.ConfigRepository
InteractionRepository repository.InteractionRepository InteractionRepository repository.InteractionRepository
EntryRepository repository.EntryRepository EntryRepository repository.EntryRepository
Http owlhttp.HttpClient Http owlhttp.HttpClient
} }
func NewWebmentionService( func NewWebmentionService(
configRepo repository.ConfigRepository,
interactionRepository repository.InteractionRepository, interactionRepository repository.InteractionRepository,
entryRepository repository.EntryRepository, entryRepository repository.EntryRepository,
http owlhttp.HttpClient, http owlhttp.HttpClient,
bus *EventBus, bus *EventBus,
) *WebmentionService { ) *WebmentionService {
svc := &WebmentionService{ svc := &WebmentionService{
ConfigRepo: configRepo,
InteractionRepository: interactionRepository, InteractionRepository: interactionRepository,
EntryRepository: entryRepository, EntryRepository: entryRepository,
Http: http, Http: http,
@ -90,3 +97,57 @@ func (s *WebmentionService) ProcessWebmention(source string, target string) erro
return err 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)
}

View File

@ -47,7 +47,7 @@ func App(db infra.Database) *web.WebApp {
entryRepo := infra.NewEntryRepository(db, entryRegister) entryRepo := infra.NewEntryRepository(db, entryRegister)
binRepo := infra.NewBinaryFileRepo(db) binRepo := infra.NewBinaryFileRepo(db)
authorRepo := infra.NewDefaultAuthorRepo(db) authorRepo := infra.NewDefaultAuthorRepo(db)
siteConfigRepo := infra.NewConfigRepo(db) configRepo := infra.NewConfigRepo(db)
interactionRepo := infra.NewInteractionRepo(db, interactionRegister) interactionRepo := infra.NewInteractionRepo(db, interactionRegister)
// Create External Services // Create External Services
@ -59,21 +59,21 @@ func App(db infra.Database) *web.WebApp {
// Create Services // Create Services
entryService := app.NewEntryService(entryRepo, eventBus) entryService := app.NewEntryService(entryRepo, eventBus)
binaryService := app.NewBinaryFileService(binRepo) binaryService := app.NewBinaryFileService(binRepo)
authorService := app.NewAuthorService(authorRepo, siteConfigRepo) authorService := app.NewAuthorService(authorRepo, configRepo)
webmentionService := app.NewWebmentionService( webmentionService := app.NewWebmentionService(
interactionRepo, entryRepo, httpClient, eventBus, configRepo, interactionRepo, entryRepo, httpClient, eventBus,
) )
// plugins // plugins
plugings.NewEcho(eventBus) plugings.NewEcho(eventBus)
plugings.RegisterInstagram( plugings.RegisterInstagram(
siteConfigRepo, configRegister, binaryService, eventBus, configRepo, configRegister, binaryService, eventBus,
) )
// Create WebApp // Create WebApp
return web.NewWebApp( return web.NewWebApp(
entryService, entryRegister, binaryService, entryService, entryRegister, binaryService,
authorService, siteConfigRepo, configRegister, authorService, configRepo, configRegister,
webmentionService, interactionRepo, webmentionService, interactionRepo,
) )