2021-04-07 17:31:23 +02:00
|
|
|
|
|
|
|
package webmention
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-04-08 12:16:33 +02:00
|
|
|
"os"
|
|
|
|
"crypto/md5"
|
2021-04-07 17:31:23 +02:00
|
|
|
|
2021-04-08 12:16:33 +02:00
|
|
|
"github.com/wgroeneveld/go-jamming/common"
|
2021-04-08 09:54:47 +02:00
|
|
|
"github.com/wgroeneveld/go-jamming/rest"
|
2021-04-07 17:31:23 +02:00
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type webmention struct {
|
|
|
|
source string
|
|
|
|
target string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *webmention) String() string {
|
|
|
|
return fmt.Sprintf("source: %s, target: %s", wm.source, wm.target)
|
|
|
|
}
|
|
|
|
|
2021-04-08 12:16:33 +02:00
|
|
|
func (wm *webmention) asPath(conf *common.Config) string {
|
|
|
|
filename := fmt.Sprintf("%x", md5.Sum([]byte("source=" + wm.source + ",target=" + wm.target)))
|
|
|
|
domain, _ := conf.FetchDomain(wm.target)
|
|
|
|
return conf.DataPath + "/" + domain + "/" + filename + ".json"
|
|
|
|
}
|
|
|
|
|
2021-04-08 09:54:47 +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.
|
|
|
|
type receiver struct {
|
|
|
|
restClient rest.Client
|
2021-04-08 12:16:33 +02:00
|
|
|
conf *common.Config
|
2021-04-08 09:54:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (recv *receiver) receive(wm webmention) {
|
2021-04-07 17:31:23 +02:00
|
|
|
log.Info().Str("webmention", wm.String()).Msg("OK: looks valid")
|
2021-04-08 09:54:47 +02:00
|
|
|
body, geterr := recv.restClient.GetBody(wm.source)
|
2021-04-07 17:31:23 +02:00
|
|
|
|
|
|
|
if geterr != nil {
|
|
|
|
log.Warn().Str("source", wm.source).Msg(" ABORT: invalid url")
|
2021-04-08 09:54:47 +02:00
|
|
|
recv.deletePossibleOlderWebmention(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-08 09:54:47 +02:00
|
|
|
func (recv *receiver) deletePossibleOlderWebmention(wm webmention) {
|
2021-04-08 12:16:33 +02:00
|
|
|
os.Remove(wm.asPath(recv.conf))
|
2021-04-07 17:31:23 +02:00
|
|
|
}
|
|
|
|
|
2021-04-08 09:54:47 +02:00
|
|
|
func (recv *receiver) processSourceBody(body string, wm webmention) {
|
2021-04-07 17:31:23 +02:00
|
|
|
|
|
|
|
}
|