first retrieved webmention

This commit is contained in:
Niko Abeler 2023-08-09 20:36:44 +02:00
parent df5215d943
commit 723a6000bf
10 changed files with 95 additions and 24 deletions

View File

@ -109,7 +109,7 @@ func (s *WebmentionService) GetExistingWebmention(entryId string, source string,
}
for _, interaction := range inters {
if webm, ok := interaction.(*interactions.Webmention); ok {
m := webm.MetaData().(interactions.WebmentionMetaData)
m := webm.MetaData().(*interactions.WebmentionMetaData)
if m.Source == source && m.Target == target {
return webm, nil
}
@ -149,7 +149,7 @@ func (s *WebmentionService) ProcessWebmention(source string, target string) erro
Target: target,
Title: hEntry.Title,
}
webmention.SetMetaData(data)
webmention.SetMetaData(&data)
webmention.SetEntryID(entryId)
webmention.SetCreatedAt(time.Now())
err = s.InteractionRepository.Update(webmention)

View File

@ -64,7 +64,7 @@ func App(db infra.Database) *web.WebApp {
return web.NewWebApp(
entryService, entryRegister, binaryService,
authorService, siteConfigRepo, configRegister,
webmentionService,
webmentionService, interactionRepo,
)
}

View File

@ -103,13 +103,38 @@ func (repo *DefaultInteractionRepo) FindAll(entryId string) ([]model.Interaction
}
// FindById implements repository.InteractionRepository.
func (*DefaultInteractionRepo) FindById(id string) (model.Interaction, error) {
panic("unimplemented")
func (repo *DefaultInteractionRepo) FindById(id string) (model.Interaction, error) {
data := sqlInteraction{}
err := repo.db.Get(&data, "SELECT * FROM interactions WHERE id = ?", id)
if err != nil {
return nil, err
}
if data.Id == "" {
return nil, errors.New("interaction not found")
}
return repo.sqlInteractionToInteraction(data)
}
// Update implements repository.InteractionRepository.
func (*DefaultInteractionRepo) Update(interaction model.Interaction) error {
panic("unimplemented")
func (repo *DefaultInteractionRepo) Update(interaction model.Interaction) error {
exInter, _ := repo.FindById(interaction.ID())
if exInter == nil {
return errors.New("interaction not found")
}
_, err := repo.typeRegistry.TypeName(interaction)
if err != nil {
return errors.New("interaction type not registered")
}
var metaDataJson []byte
if interaction.MetaData() != nil {
metaDataJson, _ = json.Marshal(interaction.MetaData())
}
_, err = repo.db.Exec("UPDATE interactions SET entry_id = ?, meta_data = ? WHERE id = ?", interaction.EntryID(), metaDataJson, interaction.ID())
return err
}
func (repo *DefaultInteractionRepo) sqlInteractionToInteraction(interaction sqlInteraction) (model.Interaction, error) {

View File

@ -1,6 +1,10 @@
package interactions
import "owl-blogs/domain/model"
import (
"fmt"
"owl-blogs/domain/model"
"owl-blogs/render"
)
type Webmention struct {
model.InteractionBase
@ -14,7 +18,11 @@ type WebmentionMetaData struct {
}
func (i *Webmention) Content() model.InteractionContent {
return model.InteractionContent(i.meta.Source)
str, err := render.RenderTemplateToString("interaction/Webmention", i)
if err != nil {
fmt.Println(err)
}
return model.InteractionContent(str)
}
func (i *Webmention) MetaData() interface{} {

View File

@ -6,6 +6,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>{{template "title" .Data}} - {{ .SiteConfig.Title }}</title>
<meta property="og:title" content="{{template "title" .Data}}" />
<link rel="webmention" href="/webmention/" />
<link rel='stylesheet' href='/static/pico.min.css'>
<style>

View File

@ -0,0 +1,7 @@
<a href="{{.MetaData.Source}}">
{{if .MetaData.Title}}
{{.MetaData.Title}}
{{else}}
{{.MetaData.Source}}
{{end}}
</a>

View File

@ -39,6 +39,25 @@
</div>
{{if .Interactions}}
<br>
<br>
<br>
<hr>
<h4>
Interactions
</h4>
<ul>
{{range .Interactions}}
<li>
{{ .Content }}
</li>
{{end}}
</ul>
{{end}}
{{ if .LoggedIn }}
<br>
<br>

View File

@ -35,13 +35,14 @@ func NewWebApp(
configRepo repository.ConfigRepository,
configRegister *app.ConfigRegister,
webmentionService *app.WebmentionService,
interactionRepo repository.InteractionRepository,
) *WebApp {
app := fiber.New()
app.Use(middleware.NewUserMiddleware(authorService).Handle)
indexHandler := NewIndexHandler(entryService, configRepo)
listHandler := NewListHandler(entryService, configRepo)
entryHandler := NewEntryHandler(entryService, typeRegistry, authorService, configRepo)
entryHandler := NewEntryHandler(entryService, typeRegistry, authorService, configRepo, interactionRepo)
mediaHandler := NewMediaHandler(binService)
rssHandler := NewRSSHandler(entryService, configRepo)
loginHandler := NewLoginHandler(authorService, configRepo)

View File

@ -14,12 +14,14 @@ type EntryHandler struct {
entrySvc *app.EntryService
authorSvc *app.AuthorService
registry *app.EntryTypeRegistry
interactionRepo repository.InteractionRepository
}
type entryData struct {
Entry model.Entry
Author *model.Author
LoggedIn bool
Interactions []model.Interaction
}
func NewEntryHandler(
@ -27,12 +29,14 @@ func NewEntryHandler(
registry *app.EntryTypeRegistry,
authorService *app.AuthorService,
configRepo repository.ConfigRepository,
interactionRepo repository.InteractionRepository,
) *EntryHandler {
return &EntryHandler{
entrySvc: entryService,
authorSvc: authorService,
registry: registry,
configRepo: configRepo,
interactionRepo: interactionRepo,
}
}
@ -58,6 +62,8 @@ func (h *EntryHandler) Handle(c *fiber.Ctx) error {
author = &model.Author{}
}
inters, _ := h.interactionRepo.FindAll(entry.ID())
return render.RenderTemplateWithBase(
c,
getSiteConfig(h.configRepo),
@ -66,6 +72,7 @@ func (h *EntryHandler) Handle(c *fiber.Ctx) error {
Entry: entry,
Author: author,
LoggedIn: loggedIn,
Interactions: inters,
},
)
}

View File

@ -26,6 +26,9 @@ func (h *WebmentionHandler) Handle(c *fiber.Ctx) error {
target := c.FormValue("target")
source := c.FormValue("source")
println("target", target)
println("source", source)
if target == "" {
return c.Status(400).SendString("target is required")
}