Cherry pick to main for add board link in bot notification (#3272)

Co-authored-by: Varghese Jose <varghese.jose@tutanota.com>
Co-authored-by: Hosted Weblate <hosted@weblate.org>
Co-authored-by: jprusch <rs@schaeferbarthold.de>
This commit is contained in:
Rajat Dabade 2022-06-24 12:39:04 +05:30 committed by GitHub
parent ca922dbc1e
commit 853c2950f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 9 deletions

View file

@ -21,7 +21,7 @@ import (
const ( const (
// card change notifications. // card change notifications.
defAddCardNotify = "{{.Authors | printAuthors \"unknown_user\" }} has added the 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" defModifyCardNotify = "###### {{.Authors | printAuthors \"unknown_user\" }} has modified the card {{. | makeLink}} on the board {{. | makeBoardLink}}\n"
defDeleteCardNotify = "{{.Authors | printAuthors \"unknown_user\" }} has deleted the card {{. | makeLink}}\n" defDeleteCardNotify = "{{.Authors | printAuthors \"unknown_user\" }} has deleted the card {{. | makeLink}}\n"
) )
@ -33,9 +33,10 @@ var (
// DiffConvOpts provides options when converting diffs to slack attachments. // DiffConvOpts provides options when converting diffs to slack attachments.
type DiffConvOpts struct { type DiffConvOpts struct {
Language string Language string
MakeCardLink func(block *model.Block, board *model.Board, card *model.Block) string MakeCardLink func(block *model.Block, board *model.Board, card *model.Block) string
Logger *mlog.Logger MakeBoardLink func(board *model.Board) string
Logger *mlog.Logger
} }
// getTemplate returns a new or cached named template based on the language specified. // getTemplate returns a new or cached named template based on the language specified.
@ -53,11 +54,20 @@ func getTemplate(name string, opts DiffConvOpts, def string) (*template.Template
return fmt.Sprintf("`%s`", block.Title) return fmt.Sprintf("`%s`", block.Title)
} }
} }
if opts.MakeBoardLink == nil {
opts.MakeBoardLink = func(board *model.Board) string {
return fmt.Sprintf("`%s`", board.Title)
}
}
myFuncs := template.FuncMap{ myFuncs := template.FuncMap{
"getBoardDescription": getBoardDescription, "getBoardDescription": getBoardDescription,
"makeLink": func(diff *Diff) string { "makeLink": func(diff *Diff) string {
return opts.MakeCardLink(diff.NewBlock, diff.Board, diff.Card) return opts.MakeCardLink(diff.NewBlock, diff.Board, diff.Card)
}, },
"makeBoardLink": func(diff *Diff) string {
return opts.MakeBoardLink(diff.Board)
},
"stripNewlines": func(s string) string { "stripNewlines": func(s string) string {
return strings.TrimSpace(strings.ReplaceAll(s, "\n", "¶ ")) return strings.TrimSpace(strings.ReplaceAll(s, "\n", "¶ "))
}, },

View file

@ -204,6 +204,9 @@ func (n *notifier) notifySubscribers(hint *model.NotificationHint) error {
MakeCardLink: func(block *model.Block, board *model.Board, card *model.Block) string { MakeCardLink: func(block *model.Block, board *model.Board, card *model.Block) string {
return fmt.Sprintf("[%s](%s)", block.Title, utils.MakeCardLink(n.serverRoot, board.TeamID, board.ID, card.ID)) return fmt.Sprintf("[%s](%s)", block.Title, utils.MakeCardLink(n.serverRoot, board.TeamID, board.ID, card.ID))
}, },
MakeBoardLink: func(board *model.Board) string {
return fmt.Sprintf("[%s](%s)", board.Title, utils.MakeBoardLink(n.serverRoot, board.TeamID, board.ID))
},
Logger: n.logger, Logger: n.logger,
} }

View file

@ -24,11 +24,12 @@ func (pd *PluginDelivery) MentionDeliver(mentionedUser *mm_model.User, extract s
return "", fmt.Errorf("cannot get direct channel: %w", err) return "", fmt.Errorf("cannot get direct channel: %w", err)
} }
link := utils.MakeCardLink(pd.serverRoot, evt.Board.TeamID, evt.Board.ID, evt.Card.ID) link := utils.MakeCardLink(pd.serverRoot, evt.Board.TeamID, evt.Board.ID, evt.Card.ID)
boardLink := utils.MakeBoardLink(pd.serverRoot, evt.Board.TeamID, evt.Board.ID)
post := &mm_model.Post{ post := &mm_model.Post{
UserId: pd.botID, UserId: pd.botID,
ChannelId: channel.Id, ChannelId: channel.Id,
Message: formatMessage(author.Username, extract, evt.Card.Title, link, evt.BlockChanged), Message: formatMessage(author.Username, extract, evt.Card.Title, link, evt.BlockChanged, boardLink, evt.Board.Title),
} }
return mentionedUser.Id, pd.api.CreatePost(post) return mentionedUser.Id, pd.api.CreatePost(post)
} }

View file

@ -11,14 +11,14 @@ import (
const ( const (
// TODO: localize these when i18n is available. // TODO: localize these when i18n is available.
defCommentTemplate = "@%s mentioned you in a comment on the card [%s](%s)\n> %s" defCommentTemplate = "@%s mentioned you in a comment on the card [%s](%s) in board [%s](%s)\n> %s"
defDescriptionTemplate = "@%s mentioned you in the card [%s](%s)\n> %s" defDescriptionTemplate = "@%s mentioned you in the card [%s](%s) in board [%s](%s)\n> %s"
) )
func formatMessage(author string, extract string, card string, link string, block *model.Block) string { func formatMessage(author string, extract string, card string, link string, block *model.Block, boardLink string, board string) string {
template := defDescriptionTemplate template := defDescriptionTemplate
if block.Type == model.TypeComment { if block.Type == model.TypeComment {
template = defCommentTemplate template = defCommentTemplate
} }
return fmt.Sprintf(template, author, card, link, extract) return fmt.Sprintf(template, author, card, link, board, boardLink, extract)
} }

View file

@ -9,3 +9,7 @@ import "fmt"
func MakeCardLink(serverRoot string, teamID string, boardID string, cardID string) string { func MakeCardLink(serverRoot string, teamID string, boardID string, cardID string) string {
return fmt.Sprintf("%s/team/%s/%s/0/%s", serverRoot, teamID, boardID, cardID) return fmt.Sprintf("%s/team/%s/%s/0/%s", serverRoot, teamID, boardID, cardID)
} }
func MakeBoardLink(serverRoot string, teamID string, board string) string {
return fmt.Sprintf("%s/team/%s/%s", serverRoot, teamID, board)
}