2021-04-09 18:04:04 +02:00
|
|
|
package recv
|
2021-04-07 17:31:23 +02:00
|
|
|
|
|
|
|
import (
|
2021-04-09 18:04:04 +02:00
|
|
|
"brainbaking.com/go-jamming/app/mf"
|
|
|
|
"brainbaking.com/go-jamming/common"
|
2021-04-18 15:39:29 +02:00
|
|
|
"brainbaking.com/go-jamming/db"
|
2021-04-09 18:04:04 +02:00
|
|
|
"brainbaking.com/go-jamming/rest"
|
2021-04-09 10:12:14 +02:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2021-04-07 17:31:23 +02:00
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
2021-04-08 12:50:15 +02:00
|
|
|
"willnorris.com/go/microformats"
|
2021-04-07 17:31:23 +02:00
|
|
|
)
|
|
|
|
|
2021-04-09 21:00:54 +02:00
|
|
|
// used as a "class" to iject dependencies, just to be able to test. Do NOT like htis.
|
|
|
|
// Is there a better way? e.g. in validate, I just pass rest.Client as an arg. Not great either.
|
2021-04-09 12:40:37 +02:00
|
|
|
type Receiver struct {
|
|
|
|
RestClient rest.Client
|
|
|
|
Conf *common.Config
|
2021-04-18 15:39:29 +02:00
|
|
|
Repo db.MentionRepo
|
2021-04-08 09:54:47 +02:00
|
|
|
}
|
|
|
|
|
2021-04-09 14:21:25 +02:00
|
|
|
func (recv *Receiver) Receive(wm mf.Mention) {
|
2021-04-13 09:10:32 +02:00
|
|
|
log.Info().Stringer("wm", wm).Msg("OK: looks valid")
|
2021-04-11 09:50:27 +02:00
|
|
|
_, body, geterr := recv.RestClient.GetBody(wm.Source)
|
2021-04-07 17:31:23 +02:00
|
|
|
|
|
|
|
if geterr != nil {
|
2021-04-13 09:10:32 +02:00
|
|
|
log.Warn().Err(geterr).Msg(" ABORT: invalid url")
|
2021-04-18 15:39:29 +02:00
|
|
|
recv.Repo.Delete(wm)
|
2021-04-07 17:31:23 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-08 09:54:47 +02:00
|
|
|
recv.processSourceBody(body, wm)
|
2021-04-07 17:31:23 +02:00
|
|
|
}
|
|
|
|
|
2021-04-09 14:21:25 +02:00
|
|
|
func (recv *Receiver) processSourceBody(body string, wm mf.Mention) {
|
2021-04-09 12:40:37 +02:00
|
|
|
if !strings.Contains(body, wm.Target) {
|
|
|
|
log.Warn().Str("target", wm.Target).Msg("ABORT: no mention of target found in html src of source!")
|
2021-04-08 12:50:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:21:25 +02:00
|
|
|
data := microformats.Parse(strings.NewReader(body), wm.SourceUrl())
|
2021-04-11 09:50:27 +02:00
|
|
|
indieweb := recv.convertBodyToIndiewebData(body, wm, mf.HEntry(data))
|
2021-04-09 12:40:37 +02:00
|
|
|
|
2021-04-18 15:39:29 +02:00
|
|
|
key, err := recv.Repo.Save(wm, indieweb)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Stringer("wm", wm).Msg("processSourceBody: failed to save json to db")
|
2021-04-13 09:10:32 +02:00
|
|
|
}
|
2021-04-18 15:39:29 +02:00
|
|
|
log.Info().Str("key", key).Msg("OK: Webmention processed.")
|
2021-04-09 12:40:37 +02:00
|
|
|
}
|
|
|
|
|
2021-04-09 14:21:25 +02:00
|
|
|
func (recv *Receiver) convertBodyToIndiewebData(body string, wm mf.Mention, hEntry *microformats.Microformat) *mf.IndiewebData {
|
2021-04-08 16:37:04 +02:00
|
|
|
if hEntry == nil {
|
2021-04-09 12:40:37 +02:00
|
|
|
return recv.parseBodyAsNonIndiewebSite(body, wm)
|
2021-04-08 16:37:04 +02:00
|
|
|
}
|
2021-04-09 12:40:37 +02:00
|
|
|
return recv.parseBodyAsIndiewebSite(hEntry, wm)
|
2021-04-08 16:37:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// see https://github.com/willnorris/microformats/blob/main/microformats.go
|
2021-04-09 14:21:25 +02:00
|
|
|
func (recv *Receiver) parseBodyAsIndiewebSite(hEntry *microformats.Microformat, wm mf.Mention) *mf.IndiewebData {
|
|
|
|
return &mf.IndiewebData{
|
2021-04-13 09:10:32 +02:00
|
|
|
Name: mf.Str(hEntry, "name"),
|
2021-04-09 14:21:25 +02:00
|
|
|
Author: mf.IndiewebAuthor{
|
2021-04-09 21:00:54 +02:00
|
|
|
Name: mf.DetermineAuthorName(hEntry),
|
2021-04-13 09:10:32 +02:00
|
|
|
Picture: mf.Str(mf.Prop(hEntry, "author"), "photo"),
|
2021-04-08 16:37:04 +02:00
|
|
|
},
|
2021-04-13 09:10:32 +02:00
|
|
|
Content: mf.Content(hEntry),
|
|
|
|
Url: mf.Url(hEntry, wm.Source),
|
|
|
|
Published: mf.Published(hEntry, recv.Conf.UtcOffset),
|
2021-04-09 21:00:54 +02:00
|
|
|
Source: wm.Source,
|
|
|
|
Target: wm.Target,
|
2021-04-13 09:10:32 +02:00
|
|
|
IndiewebType: mf.Type(hEntry),
|
2021-04-08 16:37:04 +02:00
|
|
|
}
|
2021-04-09 10:12:14 +02:00
|
|
|
}
|
|
|
|
|
2021-04-13 09:10:32 +02:00
|
|
|
var (
|
|
|
|
titleRegexp = regexp.MustCompile(`<title>(.*?)<\/title>`)
|
|
|
|
)
|
|
|
|
|
2021-04-09 14:21:25 +02:00
|
|
|
func (recv *Receiver) parseBodyAsNonIndiewebSite(body string, wm mf.Mention) *mf.IndiewebData {
|
2021-04-13 09:10:32 +02:00
|
|
|
title := nonIndiewebTitle(body, wm)
|
2021-04-09 14:21:25 +02:00
|
|
|
return &mf.IndiewebData{
|
|
|
|
Author: mf.IndiewebAuthor{
|
2021-04-09 12:40:37 +02:00
|
|
|
Name: wm.Source,
|
2021-04-09 10:12:14 +02:00
|
|
|
},
|
2021-04-09 21:00:54 +02:00
|
|
|
Name: title,
|
|
|
|
Content: title,
|
|
|
|
Published: mf.PublishedNow(recv.Conf.UtcOffset),
|
|
|
|
Url: wm.Source,
|
2021-04-13 09:10:32 +02:00
|
|
|
IndiewebType: mf.TypeMention,
|
2021-04-09 21:00:54 +02:00
|
|
|
Source: wm.Source,
|
|
|
|
Target: wm.Target,
|
2021-04-09 10:12:14 +02:00
|
|
|
}
|
2021-04-07 17:31:23 +02:00
|
|
|
}
|
2021-04-13 09:10:32 +02:00
|
|
|
|
|
|
|
func nonIndiewebTitle(body string, wm mf.Mention) string {
|
|
|
|
titleMatch := titleRegexp.FindStringSubmatch(body)
|
|
|
|
title := wm.Source
|
|
|
|
if titleMatch != nil {
|
|
|
|
title = titleMatch[1]
|
|
|
|
}
|
|
|
|
return title
|
|
|
|
}
|