2023-08-08 18:17:04 +00:00
|
|
|
package app
|
|
|
|
|
2023-08-08 19:32:24 +00:00
|
|
|
import (
|
2023-08-11 14:11:59 +00:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2023-08-08 19:32:24 +00:00
|
|
|
"owl-blogs/app/owlhttp"
|
|
|
|
"owl-blogs/app/repository"
|
2023-08-11 14:11:59 +00:00
|
|
|
"owl-blogs/domain/model"
|
2023-08-08 19:32:24 +00:00
|
|
|
"owl-blogs/interactions"
|
|
|
|
"time"
|
|
|
|
)
|
2023-08-08 18:17:04 +00:00
|
|
|
|
|
|
|
type WebmentionService struct {
|
2024-02-25 13:01:55 +00:00
|
|
|
siteConfigService *SiteConfigService
|
2023-08-08 18:17:04 +00:00
|
|
|
InteractionRepository repository.InteractionRepository
|
|
|
|
EntryRepository repository.EntryRepository
|
2023-08-08 19:32:24 +00:00
|
|
|
Http owlhttp.HttpClient
|
|
|
|
}
|
|
|
|
|
2023-08-08 18:17:04 +00:00
|
|
|
func NewWebmentionService(
|
2024-02-25 13:01:55 +00:00
|
|
|
siteConfigService *SiteConfigService,
|
2023-08-08 18:17:04 +00:00
|
|
|
interactionRepository repository.InteractionRepository,
|
|
|
|
entryRepository repository.EntryRepository,
|
2023-08-08 19:32:24 +00:00
|
|
|
http owlhttp.HttpClient,
|
2023-08-11 13:52:14 +00:00
|
|
|
bus *EventBus,
|
2023-08-08 18:17:04 +00:00
|
|
|
) *WebmentionService {
|
2023-08-11 13:52:14 +00:00
|
|
|
svc := &WebmentionService{
|
2024-02-25 13:01:55 +00:00
|
|
|
siteConfigService: siteConfigService,
|
2023-08-08 18:17:04 +00:00
|
|
|
InteractionRepository: interactionRepository,
|
|
|
|
EntryRepository: entryRepository,
|
2023-08-08 19:32:24 +00:00
|
|
|
Http: http,
|
|
|
|
}
|
2023-08-11 13:52:14 +00:00
|
|
|
bus.Subscribe(svc)
|
|
|
|
return svc
|
2023-08-08 19:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WebmentionService) GetExistingWebmention(entryId string, source string, target string) (*interactions.Webmention, error) {
|
|
|
|
inters, err := s.InteractionRepository.FindAll(entryId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, interaction := range inters {
|
|
|
|
if webm, ok := interaction.(*interactions.Webmention); ok {
|
2023-08-09 18:36:44 +00:00
|
|
|
m := webm.MetaData().(*interactions.WebmentionMetaData)
|
2023-08-08 19:32:24 +00:00
|
|
|
if m.Source == source && m.Target == target {
|
|
|
|
return webm, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WebmentionService) ProcessWebmention(source string, target string) error {
|
|
|
|
resp, err := s.Http.Get(source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-11 13:52:14 +00:00
|
|
|
hEntry, err := ParseHEntry(resp)
|
2023-08-08 19:32:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
entryId := UrlToEntryId(target)
|
|
|
|
_, err = s.EntryRepository.FindById(entryId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
webmention, err := s.GetExistingWebmention(entryId, source, target)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if webmention != nil {
|
2023-08-09 18:10:51 +00:00
|
|
|
data := interactions.WebmentionMetaData{
|
2023-08-08 19:32:24 +00:00
|
|
|
Source: source,
|
|
|
|
Target: target,
|
|
|
|
Title: hEntry.Title,
|
|
|
|
}
|
2023-08-09 18:36:44 +00:00
|
|
|
webmention.SetMetaData(&data)
|
2023-08-08 19:32:24 +00:00
|
|
|
webmention.SetEntryID(entryId)
|
|
|
|
webmention.SetCreatedAt(time.Now())
|
|
|
|
err = s.InteractionRepository.Update(webmention)
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
webmention = &interactions.Webmention{}
|
2023-08-09 18:10:51 +00:00
|
|
|
data := interactions.WebmentionMetaData{
|
2023-08-08 19:32:24 +00:00
|
|
|
Source: source,
|
|
|
|
Target: target,
|
|
|
|
Title: hEntry.Title,
|
|
|
|
}
|
2023-08-09 18:10:51 +00:00
|
|
|
webmention.SetMetaData(&data)
|
2023-08-08 19:32:24 +00:00
|
|
|
webmention.SetEntryID(entryId)
|
|
|
|
webmention.SetCreatedAt(time.Now())
|
|
|
|
err = s.InteractionRepository.Create(webmention)
|
|
|
|
return err
|
2023-08-08 18:17:04 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-11 14:11:59 +00:00
|
|
|
|
|
|
|
func (s *WebmentionService) ScanForLinks(entry model.Entry) ([]string, error) {
|
|
|
|
content := string(entry.Content())
|
|
|
|
return ParseLinksFromString(content)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WebmentionService) FullEntryUrl(entry model.Entry) string {
|
2024-02-25 13:01:55 +00:00
|
|
|
siteConfig, _ := s.siteConfigService.GetSiteConfig()
|
2023-08-11 14:11:59 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|