fix card link in notifications; card pointer was cached by template (#2096)

This commit is contained in:
Doug Lauder 2022-01-11 20:14:21 -05:00 committed by GitHub
parent 106a8e5b2e
commit c6714a76bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 12 deletions

View file

@ -20,9 +20,9 @@ import (
const (
// card change notifications.
defAddCardNotify = "{{.Authors | printAuthors \"unknown_user\" }} has added the card {{.NewBlock | makeLink}}\n"
defModifyCardNotify = "###### {{.Authors | printAuthors \"unknown_user\" }} has modified the card {{.Card | makeLink}}\n"
defDeleteCardNotify = "{{.Authors | printAuthors \"unknown_user\" }} has deleted the card {{.Card | makeLink}}\n"
defAddCardNotify = "{{.Authors | printAuthors \"unknown_user\" }} has added the card {{. | makeLink}}\n"
defModifyCardNotify = "###### {{.Authors | printAuthors \"unknown_user\" }} has modified the card {{. | makeLink}}\n"
defDeleteCardNotify = "{{.Authors | printAuthors \"unknown_user\" }} has deleted the card {{. | makeLink}}\n"
)
var (
@ -34,7 +34,7 @@ var (
// DiffConvOpts provides options when converting diffs to slack attachments.
type DiffConvOpts struct {
Language string
MakeCardLink func(block *model.Block) string
MakeCardLink func(block *model.Block, board *model.Block, card *model.Block) string
Logger *mlog.Logger
}
@ -49,11 +49,15 @@ func getTemplate(name string, opts DiffConvOpts, def string) (*template.Template
t = template.New(key)
if opts.MakeCardLink == nil {
opts.MakeCardLink = func(block *model.Block) string { return fmt.Sprintf("`%s`", block.Title) }
opts.MakeCardLink = func(block *model.Block, _ *model.Block, _ *model.Block) string {
return fmt.Sprintf("`%s`", block.Title)
}
}
myFuncs := template.FuncMap{
"getBoardDescription": getBoardDescription,
"makeLink": opts.MakeCardLink,
"makeLink": func(diff *Diff) string {
return opts.MakeCardLink(diff.NewBlock, diff.Board, diff.Card)
},
"stripNewlines": func(s string) string {
return strings.TrimSpace(strings.ReplaceAll(s, "\n", "¶ "))
},
@ -151,6 +155,13 @@ func cardDiff2SlackAttachment(cardDiff *Diff, opts DiffConvOpts) (*mm_model.Slac
// at this point new and old block are non-nil
opts.Logger.Debug("cardDiff2SlackAttachment",
mlog.String("board_id", cardDiff.Board.ID),
mlog.String("card_id", cardDiff.Card.ID),
mlog.String("new_block_id", cardDiff.NewBlock.ID),
mlog.String("old_block_id", cardDiff.OldBlock.ID),
)
buf.Reset()
if err := execTemplate(buf, "ModifyCardNotify", opts, defModifyCardNotify, cardDiff); err != nil {
return nil, fmt.Errorf("cannot write notification for card %s: %w", cardDiff.NewBlock.ID, err)

View file

@ -154,11 +154,6 @@ func (n *notifier) notifySubscribers(hint *model.NotificationHint) error {
return nil
}
n.logger.Debug("notifySubscribers - subscribers",
mlog.Any("hint", hint),
mlog.Int("sub_count", len(subs)),
)
// subs slice is sorted by `NotifiedAt`, therefore subs[0] contains the oldest NotifiedAt needed
oldestNotifiedAt := subs[0].NotifiedAt
@ -168,6 +163,13 @@ func (n *notifier) notifySubscribers(hint *model.NotificationHint) error {
return fmt.Errorf("could not get board & card for block %s: %w", hint.BlockID, err)
}
n.logger.Debug("notifySubscribers - subscribers",
mlog.Any("hint", hint),
mlog.String("board_id", board.ID),
mlog.String("card_id", card.ID),
mlog.Int("sub_count", len(subs)),
)
dg := &diffGenerator{
container: c,
board: board,
@ -198,7 +200,7 @@ func (n *notifier) notifySubscribers(hint *model.NotificationHint) error {
opts := DiffConvOpts{
Language: "en", // TODO: use correct language with i18n available on server.
MakeCardLink: func(block *model.Block) string {
MakeCardLink: func(block *model.Block, board *model.Block, card *model.Block) string {
return fmt.Sprintf("[%s](%s)", block.Title, utils.MakeCardLink(n.serverRoot, board.WorkspaceID, board.ID, card.ID))
},
Logger: n.logger,