all database operations now take a context.Context, which is propagated through from sources
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
package htmlcheck
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
@@ -81,6 +82,7 @@ const hyphApos = "-'"
|
||||
|
||||
// htmlCheckerImpl is the implementation of the HTML checker.
|
||||
type htmlCheckerImpl struct {
|
||||
ctx context.Context // request context, as instances are generally per-request
|
||||
config *HTMLCheckerConfig // pointer to configuration
|
||||
started bool // has checker been started?
|
||||
finished bool // has checker been finished?
|
||||
@@ -147,13 +149,14 @@ func (ht *htmlCheckerImpl) copyOutputFilters(dest []outputFilter, source []strin
|
||||
*/
|
||||
|
||||
/* AmNewHTMLChecker creates a new HTML Checker object.
|
||||
* Parametrers:
|
||||
* Parameters:
|
||||
* ctx - Standard Go context value.
|
||||
* configName - Name of the configuration to use.
|
||||
* Returns:
|
||||
* New HTML checker reference.
|
||||
* Standard Go error status.
|
||||
*/
|
||||
func AmNewHTMLChecker(configName string) (HTMLChecker, error) {
|
||||
func AmNewHTMLChecker(ctx context.Context, configName string) (HTMLChecker, error) {
|
||||
config, ok := configsRegistry[configName]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("configuration %s not found", configName)
|
||||
@@ -166,6 +169,7 @@ func AmNewHTMLChecker(configName string) (HTMLChecker, error) {
|
||||
}
|
||||
}
|
||||
rc := htmlCheckerImpl{
|
||||
ctx: ctx,
|
||||
config: config,
|
||||
started: false,
|
||||
finished: false,
|
||||
@@ -486,7 +490,7 @@ func (ht *htmlCheckerImpl) emitFromStartOfTempBuffer(nrunes int) {
|
||||
// attemptRewrite attempts to apply a list of rewriters on the text, returning the first one that matches.
|
||||
func (ht *htmlCheckerImpl) attemptRewrite(rewriters []rewriter, data string) *markupData {
|
||||
for _, r := range rewriters {
|
||||
rc := r.Rewrite(data, ht)
|
||||
rc := r.Rewrite(ht.ctx, data, ht)
|
||||
if rc != nil {
|
||||
return rc
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
package htmlcheck
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"os"
|
||||
|
||||
@@ -76,12 +77,13 @@ func (rw *spellingRewriter) Name() string {
|
||||
|
||||
/* Rewrite rewrites the given string data and adds markup before and after if needed.
|
||||
* Parameters:
|
||||
* ctx - Standard Go error status.
|
||||
* data - The data to be rewritten.
|
||||
* svc - Services interface we can use.
|
||||
* Returns:
|
||||
* Pointer to markup data, or nil.
|
||||
*/
|
||||
func (rw *spellingRewriter) Rewrite(data string, svc rewriterServices) *markupData {
|
||||
func (rw *spellingRewriter) Rewrite(ctx context.Context, data string, svc rewriterServices) *markupData {
|
||||
if rw.dict.CheckWord(data) {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
package htmlcheck
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"math"
|
||||
"regexp"
|
||||
@@ -90,12 +91,13 @@ func (rw *emoticonRewriter) Name() string {
|
||||
|
||||
/* Rewrite rewrites the given string data and adds markup before and after if needed.
|
||||
* Parameters:
|
||||
* ctx - Standard Go context value.
|
||||
* data - The data to be rewritten.
|
||||
* svc - Services interface we can use.
|
||||
* Returns:
|
||||
* Pointer to markup data, or nil.
|
||||
*/
|
||||
func (rw *emoticonRewriter) Rewrite(data string, svc rewriterServices) *markupData {
|
||||
func (rw *emoticonRewriter) Rewrite(ctx context.Context, data string, svc rewriterServices) *markupData {
|
||||
pos := math.MaxInt
|
||||
for _, c := range rw.prefixChars {
|
||||
foo := strings.IndexByte(data, c)
|
||||
@@ -163,12 +165,13 @@ func (rw *emoticonTagRewriter) Name() string {
|
||||
|
||||
/* Rewrite rewrites the given string data and adds markup before and after if needed.
|
||||
* Parameters:
|
||||
* ctx - Standard Go context value.
|
||||
* data - The data to be rewritten.
|
||||
* svc - Services interface we can use.
|
||||
* Returns:
|
||||
* Pointer to markup data, or nil.
|
||||
*/
|
||||
func (rw *emoticonTagRewriter) Rewrite(data string, svc rewriterServices) *markupData {
|
||||
func (rw *emoticonTagRewriter) Rewrite(ctx context.Context, data string, svc rewriterServices) *markupData {
|
||||
m := rw.re.FindStringSubmatch(data)
|
||||
if m == nil {
|
||||
return nil
|
||||
|
||||
+10
-8
@@ -10,6 +10,7 @@
|
||||
package htmlcheck
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/mail"
|
||||
"net/url"
|
||||
@@ -41,7 +42,7 @@ type rewriterServices interface {
|
||||
// rewriter is the interface for components that rewrite source text and place markup around it.
|
||||
type rewriter interface {
|
||||
Name() string
|
||||
Rewrite(string, rewriterServices) *markupData
|
||||
Rewrite(context.Context, string, rewriterServices) *markupData
|
||||
}
|
||||
|
||||
// rewriterRegistry contains a list of all rewriters.
|
||||
@@ -72,7 +73,7 @@ func (rw *emailRewriter) Name() string {
|
||||
* Returns:
|
||||
* Pointer to markup data, or nil.
|
||||
*/
|
||||
func (rw *emailRewriter) Rewrite(data string, svc rewriterServices) *markupData {
|
||||
func (rw *emailRewriter) Rewrite(ctx context.Context, data string, svc rewriterServices) *markupData {
|
||||
_, err := mail.ParseAddress(data)
|
||||
if err != nil {
|
||||
return nil
|
||||
@@ -154,7 +155,7 @@ func buildPostLink(decoded, context *database.PostLinkData) string {
|
||||
* Returns:
|
||||
* Pointer to markup data, or nil.
|
||||
*/
|
||||
func (rw *postLinkRewriter) Rewrite(data string, svc rewriterServices) *markupData {
|
||||
func (rw *postLinkRewriter) Rewrite(ctx context.Context, data string, svc rewriterServices) *markupData {
|
||||
q := svc.rewriterContextValue("PostLinkDecoderContext")
|
||||
if q == nil {
|
||||
return nil
|
||||
@@ -165,7 +166,7 @@ func (rw *postLinkRewriter) Rewrite(data string, svc rewriterServices) *markupDa
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
err = mydata.VerifyNames()
|
||||
err = mydata.VerifyNames(ctx)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
@@ -205,17 +206,18 @@ func (rw *userLinkRewriter) Name() string {
|
||||
|
||||
/* Rewrite rewrites the given string data and adds markup before and after if needed.
|
||||
* Parameters:
|
||||
* ctx - Standard Go context value.
|
||||
* data - The data to be rewritten.
|
||||
* svc - Services interface we can use.
|
||||
* Returns:
|
||||
* Pointer to markup data, or nil.
|
||||
*/
|
||||
func (rw *userLinkRewriter) Rewrite(data string, svc rewriterServices) *markupData {
|
||||
func (rw *userLinkRewriter) Rewrite(ctx context.Context, data string, svc rewriterServices) *markupData {
|
||||
if data == "" || len(data) > 64 || !database.AmIsValidAmsterdamID(data) {
|
||||
return nil
|
||||
}
|
||||
|
||||
user, err := database.AmGetUserByName(data, nil)
|
||||
user, err := database.AmGetUserByName(ctx, data, nil)
|
||||
if err != nil || user == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -258,8 +260,8 @@ func (rw *countingRewriter) Name() string {
|
||||
* Returns:
|
||||
* Pointer to markup data, or nil.
|
||||
*/
|
||||
func (rw *countingRewriter) Rewrite(data string, svc rewriterServices) *markupData {
|
||||
rc := rw.inner.Rewrite(data, svc)
|
||||
func (rw *countingRewriter) Rewrite(ctx context.Context, data string, svc rewriterServices) *markupData {
|
||||
rc := rw.inner.Rewrite(ctx, data, svc)
|
||||
if rc != nil && !rc.rescan {
|
||||
rw.count++
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
package htmlcheck
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
@@ -72,12 +73,13 @@ func (rw *urlRewriter) Name() string {
|
||||
|
||||
/* Rewrite rewrites the given string data and adds markup before and after if needed.
|
||||
* Parameters:
|
||||
* ctx - Standard Go context value.
|
||||
* data - The data to be rewritten.
|
||||
* svc - Services interface we can use.
|
||||
* Returns:
|
||||
* Pointer to markup data, or nil.
|
||||
*/
|
||||
func (rw *urlRewriter) Rewrite(data string, svc rewriterServices) *markupData {
|
||||
func (rw *urlRewriter) Rewrite(ctx context.Context, data string, svc rewriterServices) *markupData {
|
||||
for _, ue := range urlElements {
|
||||
s := ue.eval(data)
|
||||
if s != "" {
|
||||
|
||||
Reference in New Issue
Block a user