mirror of https://github.com/jlelse/GoBlog
Use Golang toLower func for lower in queries, add webmention tests
This commit is contained in:
parent
695e552f50
commit
2bf98dddaa
28
database.go
28
database.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/gchaincl/sqlhooks/v2"
|
"github.com/gchaincl/sqlhooks/v2"
|
||||||
|
@ -63,25 +64,18 @@ func (a *goBlog) openDatabase(file string, logging bool) (*database, error) {
|
||||||
var dr driver.Driver = &sqlite.SQLiteDriver{
|
var dr driver.Driver = &sqlite.SQLiteDriver{
|
||||||
ConnectHook: func(c *sqlite.SQLiteConn) error {
|
ConnectHook: func(c *sqlite.SQLiteConn) error {
|
||||||
// Register functions
|
// Register functions
|
||||||
// Depends on app
|
for n, f := range map[string]interface{}{
|
||||||
if err := c.RegisterFunc("mdtext", a.renderText, true); err != nil {
|
"mdtext": a.renderText,
|
||||||
|
"tolocal": toLocalSafe,
|
||||||
|
"toutc": toUTCSafe,
|
||||||
|
"wordcount": wordCount,
|
||||||
|
"charcount": charCount,
|
||||||
|
"urlize": urlize,
|
||||||
|
"lowerx": strings.ToLower,
|
||||||
|
} {
|
||||||
|
if err := c.RegisterFunc(n, f, true); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Independent
|
|
||||||
if err := c.RegisterFunc("tolocal", toLocalSafe, true); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := c.RegisterFunc("toutc", toUTCSafe, true); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := c.RegisterFunc("wordcount", wordCount, true); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := c.RegisterFunc("charcount", charCount, true); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := c.RegisterFunc("urlize", urlize, true); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
|
|
|
@ -288,7 +288,7 @@ func buildPostsQuery(c *postsRequestConfig, selection string) (query string, arg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if c.taxonomy != nil && len(c.taxonomyValue) > 0 {
|
if c.taxonomy != nil && len(c.taxonomyValue) > 0 {
|
||||||
queryBuilder.WriteString(" and path in (select path from post_parameters where parameter = @taxname and lower(value) = lower(@taxval))")
|
queryBuilder.WriteString(" and path in (select path from post_parameters where parameter = @taxname and lowerx(value) = lowerx(@taxval))")
|
||||||
args = append(args, sql.Named("taxname", c.taxonomy.Name), sql.Named("taxval", c.taxonomyValue))
|
args = append(args, sql.Named("taxname", c.taxonomy.Name), sql.Named("taxval", c.taxonomyValue))
|
||||||
}
|
}
|
||||||
if len(c.sections) > 0 {
|
if len(c.sections) > 0 {
|
||||||
|
|
|
@ -89,7 +89,7 @@ func (a *goBlog) extractMention(r *http.Request) (*mention, error) {
|
||||||
|
|
||||||
func (db *database) webmentionExists(source, target string) bool {
|
func (db *database) webmentionExists(source, target string) bool {
|
||||||
result := 0
|
result := 0
|
||||||
row, err := db.queryRow("select exists(select 1 from webmentions where lower(source) = lower(@source) and lower(target) = lower(@target))", sql.Named("source", source), sql.Named("target", target))
|
row, err := db.queryRow("select exists(select 1 from webmentions where lowerx(source) = lowerx(@source) and lowerx(target) = lowerx(@target))", sql.Named("source", source), sql.Named("target", target))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -107,6 +107,23 @@ func (a *goBlog) createWebmention(source, target string) (err error) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *database) insertWebmention(m *mention, status webmentionStatus) error {
|
||||||
|
_, err := db.exec(
|
||||||
|
`
|
||||||
|
insert into webmentions (source, target, created, status, title, content, author)
|
||||||
|
values (@source, @target, @created, @status, @title, @content, @author)
|
||||||
|
`,
|
||||||
|
sql.Named("source", m.Source),
|
||||||
|
sql.Named("target", m.Target),
|
||||||
|
sql.Named("created", m.Created),
|
||||||
|
sql.Named("status", status),
|
||||||
|
sql.Named("title", m.Title),
|
||||||
|
sql.Named("content", m.Content),
|
||||||
|
sql.Named("author", m.Author),
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func (db *database) deleteWebmention(id int) error {
|
func (db *database) deleteWebmention(id int) error {
|
||||||
_, err := db.exec("delete from webmentions where id = @id", sql.Named("id", id))
|
_, err := db.exec("delete from webmentions where id = @id", sql.Named("id", id))
|
||||||
return err
|
return err
|
||||||
|
@ -146,7 +163,7 @@ func buildWebmentionsQuery(config *webmentionsRequestConfig) (query string, args
|
||||||
if config != nil {
|
if config != nil {
|
||||||
queryBuilder.WriteString("where 1")
|
queryBuilder.WriteString("where 1")
|
||||||
if config.target != "" {
|
if config.target != "" {
|
||||||
queryBuilder.WriteString(" and lower(target) = lower(@target)")
|
queryBuilder.WriteString(" and lowerx(target) = lowerx(@target)")
|
||||||
args = append(args, sql.Named("target", config.target))
|
args = append(args, sql.Named("target", config.target))
|
||||||
}
|
}
|
||||||
if config.status != "" {
|
if config.status != "" {
|
||||||
|
@ -154,8 +171,8 @@ func buildWebmentionsQuery(config *webmentionsRequestConfig) (query string, args
|
||||||
args = append(args, sql.Named("status", config.status))
|
args = append(args, sql.Named("status", config.status))
|
||||||
}
|
}
|
||||||
if config.sourcelike != "" {
|
if config.sourcelike != "" {
|
||||||
queryBuilder.WriteString(" and lower(source) like @sourcelike")
|
queryBuilder.WriteString(" and lowerx(source) like ('%' || lowerx(@sourcelike) || '%')")
|
||||||
args = append(args, sql.Named("sourcelike", "%"+strings.ToLower(config.sourcelike)+"%"))
|
args = append(args, sql.Named("sourcelike", config.sourcelike))
|
||||||
}
|
}
|
||||||
if config.id != 0 {
|
if config.id != 0 {
|
||||||
queryBuilder.WriteString(" and id = @id")
|
queryBuilder.WriteString(" and id = @id")
|
||||||
|
|
|
@ -110,11 +110,10 @@ func (a *goBlog) verifyMention(m *mention) error {
|
||||||
}
|
}
|
||||||
newStatus := webmentionStatusVerified
|
newStatus := webmentionStatusVerified
|
||||||
if a.db.webmentionExists(m.Source, m.Target) {
|
if a.db.webmentionExists(m.Source, m.Target) {
|
||||||
_, err = a.db.exec("update webmentions set status = @status, title = @title, content = @content, author = @author where lower(source) = lower(@source) and lower(target) = lower(@target)",
|
_, err = a.db.exec("update webmentions set status = @status, title = @title, content = @content, author = @author where lowerx(source) = lowerx(@source) and lowerx(target) = lowerx(@target)",
|
||||||
sql.Named("status", newStatus), sql.Named("title", m.Title), sql.Named("content", m.Content), sql.Named("author", m.Author), sql.Named("source", m.Source), sql.Named("target", m.Target))
|
sql.Named("status", newStatus), sql.Named("title", m.Title), sql.Named("content", m.Content), sql.Named("author", m.Author), sql.Named("source", m.Source), sql.Named("target", m.Target))
|
||||||
} else {
|
} else {
|
||||||
_, err = a.db.exec("insert into webmentions (source, target, created, status, title, content, author) values (@source, @target, @created, @status, @title, @content, @author)",
|
a.db.insertWebmention(m, newStatus)
|
||||||
sql.Named("source", m.Source), sql.Named("target", m.Target), sql.Named("created", m.Created), sql.Named("status", newStatus), sql.Named("title", m.Title), sql.Named("content", m.Content), sql.Named("author", m.Author))
|
|
||||||
a.sendNotification(fmt.Sprintf("New webmention from %s to %s", m.Source, m.Target))
|
a.sendNotification(fmt.Sprintf("New webmention from %s to %s", m.Source, m.Target))
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -0,0 +1,74 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_webmentions(t *testing.T) {
|
||||||
|
app := &goBlog{
|
||||||
|
cfg: &config{
|
||||||
|
Db: &configDb{
|
||||||
|
File: filepath.Join(t.TempDir(), "test.db"),
|
||||||
|
},
|
||||||
|
Server: &configServer{
|
||||||
|
PublicAddress: "https://example.com",
|
||||||
|
},
|
||||||
|
Blogs: map[string]*configBlog{
|
||||||
|
"en": {
|
||||||
|
Lang: "en",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
DefaultBlog: "en",
|
||||||
|
User: &configUser{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = app.initDatabase(false)
|
||||||
|
app.initComponents()
|
||||||
|
|
||||||
|
app.db.insertWebmention(&mention{
|
||||||
|
Source: "https://example.net/test",
|
||||||
|
Target: "https://example.com/täst",
|
||||||
|
Created: time.Now().Unix(),
|
||||||
|
Title: "Test-Title",
|
||||||
|
Content: "Test-Content",
|
||||||
|
Author: "Test-Author",
|
||||||
|
}, webmentionStatusVerified)
|
||||||
|
|
||||||
|
mentions, err := app.db.getWebmentions(&webmentionsRequestConfig{
|
||||||
|
sourcelike: "example.xyz",
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Len(t, mentions, 0)
|
||||||
|
|
||||||
|
count, err := app.db.countWebmentions(&webmentionsRequestConfig{
|
||||||
|
sourcelike: "example.net",
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, 1, count)
|
||||||
|
|
||||||
|
exists := app.db.webmentionExists("Https://Example.net/test", "Https://Example.com/TÄst")
|
||||||
|
assert.True(t, exists)
|
||||||
|
|
||||||
|
mentions = app.db.getWebmentionsByAddress("https://example.com/täst")
|
||||||
|
assert.Len(t, mentions, 0)
|
||||||
|
|
||||||
|
mentions, err = app.db.getWebmentions(&webmentionsRequestConfig{
|
||||||
|
sourcelike: "example.net",
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
if assert.Len(t, mentions, 1) {
|
||||||
|
|
||||||
|
app.db.approveWebmention(mentions[0].ID)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
mentions = app.db.getWebmentionsByAddress("https://example.com/täst")
|
||||||
|
assert.Len(t, mentions, 1)
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue