sending webmentions from editor

This commit is contained in:
Niko Abeler 2022-12-04 19:03:09 +01:00
parent 65fa4f6fb8
commit 51fd8cefe2
2 changed files with 76 additions and 0 deletions

View File

@ -1,8 +1,10 @@
package web
import (
"fmt"
"h4kor/owl-blogs"
"net/http"
"sync"
"time"
"github.com/julienschmidt/httprouter"
@ -262,6 +264,20 @@ func userEditorPostHandler(repo *owl.Repository) func(http.ResponseWriter, *http
// redirect to post
if !post.Meta().Draft {
// scan for webmentions
post.ScanForLinks()
webmentions := post.OutgoingWebmentions()
println("Found ", len(webmentions), " links")
wg := sync.WaitGroup{}
wg.Add(len(webmentions))
for _, mention := range post.OutgoingWebmentions() {
go func(mention owl.WebmentionOut) {
fmt.Printf("Sending webmention to %s", mention.Target)
defer wg.Done()
post.SendWebmention(mention)
}(mention)
}
wg.Wait()
http.Redirect(w, r, post.FullUrl(), http.StatusFound)
} else {
http.Redirect(w, r, user.EditorUrl(), http.StatusFound)

View File

@ -3,6 +3,8 @@ package web_test
import (
main "h4kor/owl-blogs/cmd/owl/web"
"h4kor/owl-blogs/test/assertions"
"h4kor/owl-blogs/test/mocks"
"io"
"net/http"
"net/http/httptest"
"net/url"
@ -11,6 +13,27 @@ import (
"testing"
)
type CountMockHttpClient struct {
InvokedGet int
InvokedPost int
InvokedPostForm int
}
func (c *CountMockHttpClient) Get(url string) (resp *http.Response, err error) {
c.InvokedGet++
return &http.Response{}, nil
}
func (c *CountMockHttpClient) Post(url, contentType string, body io.Reader) (resp *http.Response, err error) {
c.InvokedPost++
return &http.Response{}, nil
}
func (c *CountMockHttpClient) PostForm(url string, data url.Values) (resp *http.Response, err error) {
c.InvokedPostForm++
return &http.Response{}, nil
}
func TestLoginWrongPassword(t *testing.T) {
repo, user := getSingleUserTestRepo()
user.ResetPassword("testpassword")
@ -182,3 +205,40 @@ func TestEditorPostWithSessionNote(t *testing.T) {
assertions.AssertStatus(t, rr, http.StatusFound)
assertions.AssertEqual(t, rr.Header().Get("Location"), post.FullUrl())
}
func TestEditorSendsWebmentions(t *testing.T) {
repo, user := getSingleUserTestRepo()
repo.HttpClient = &CountMockHttpClient{}
repo.Parser = &mocks.MockHtmlParser{}
user.ResetPassword("testpassword")
mentioned_post, _ := user.CreateNewPost("test", false)
sessionId := user.CreateNewSession()
csrfToken := "test_csrf_token"
// Create Request and Response
form := url.Values{}
form.Add("type", "note")
form.Add("content", "[test]("+mentioned_post.FullUrl()+")")
form.Add("csrf_token", csrfToken)
req, err := http.NewRequest("POST", user.EditorUrl(), strings.NewReader(form.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(form.Encode())))
req.AddCookie(&http.Cookie{Name: "csrf_token", Value: csrfToken})
req.AddCookie(&http.Cookie{Name: "session", Value: sessionId})
assertions.AssertNoError(t, err, "Error creating request")
rr := httptest.NewRecorder()
router := main.SingleUserRouter(&repo)
router.ServeHTTP(rr, req)
posts, _ := user.AllPosts()
assertions.AssertEqual(t, len(posts), 2)
post := posts[0]
assertions.AssertLen(t, post.OutgoingWebmentions(), 1)
assertions.AssertStatus(t, rr, http.StatusFound)
assertions.AssertEqual(t, repo.HttpClient.(*CountMockHttpClient).InvokedPostForm, 1)
}