Skip to content

Commit

Permalink
@mention support (mattermost-community#1147)
Browse files Browse the repository at this point in the history
  • Loading branch information
wiggin77 authored Sep 13, 2021
1 parent 20aafbc commit 8425f53
Show file tree
Hide file tree
Showing 23 changed files with 1,209 additions and 81 deletions.
33 changes: 33 additions & 0 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -748,3 +748,36 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

---

## wiggin77/merror

This product contains 'merror' by GitHub user 'wiggin77'.

Multiple Error aggregator for Go.

* HOMEPAGE:
* https://github.com/wiggin77/merror

* LICENSE: MIT

MIT License

Copyright (c) 2018 wiggin77

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
12 changes: 11 additions & 1 deletion linux/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,17 @@ func runServer(port int) (*server.Server, error) {
return nil, err
}

server, err := server.New(config, sessionToken, db, logger, "", nil)
params := server.Params{
Cfg: config,
SingleUserToken: sessionToken,
DBStore: db,
Logger: logger,
ServerID: "",
WSAdapter: nil,
NotifyBackends: nil,
}

server, err := server.New(params)
if err != nil {
fmt.Println("ERROR INITIALIZING THE SERVER", err)
return nil, err
Expand Down
61 changes: 61 additions & 0 deletions mattermost-plugin/server/notifications.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"fmt"

"github.com/mattermost/focalboard/server/services/notify"
"github.com/mattermost/focalboard/server/services/notify/notifymentions"
"github.com/mattermost/focalboard/server/services/notify/plugindelivery"

pluginapi "github.com/mattermost/mattermost-plugin-api"

"github.com/mattermost/mattermost-server/v6/model"

"github.com/mattermost/mattermost-server/v6/shared/mlog"
)

const (
botUsername = "boards"
botDisplayname = "Boards"
botDescription = "Created by Boards plugin."
)

func createMentionsNotifyBackend(client *pluginapi.Client, serverRoot string, logger *mlog.Logger) (notify.Backend, error) {
bot := &model.Bot{
Username: botUsername,
DisplayName: botDisplayname,
Description: botDescription,
}
botID, err := client.Bot.EnsureBot(bot)
if err != nil {
return nil, fmt.Errorf("failed to ensure %s bot: %w", botDisplayname, err)
}

pluginAPI := &pluginAPIAdapter{client: client}

delivery := plugindelivery.New(botID, serverRoot, pluginAPI)

backend := notifymentions.New(delivery, logger)

return backend, nil
}

type pluginAPIAdapter struct {
client *pluginapi.Client
}

func (da *pluginAPIAdapter) GetDirectChannel(userID1, userID2 string) (*model.Channel, error) {
return da.client.Channel.GetDirect(userID1, userID2)
}

func (da *pluginAPIAdapter) CreatePost(post *model.Post) error {
return da.client.Post.CreatePost(post)
}

func (da *pluginAPIAdapter) GetUserByID(userID string) (*model.User, error) {
return da.client.User.Get(userID)
}

func (da *pluginAPIAdapter) GetUserByUsername(name string) (*model.User, error) {
return da.client.User.GetByUsername(name)
}
18 changes: 17 additions & 1 deletion mattermost-plugin/server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/mattermost/focalboard/server/auth"
"github.com/mattermost/focalboard/server/server"
"github.com/mattermost/focalboard/server/services/config"
"github.com/mattermost/focalboard/server/services/notify"
"github.com/mattermost/focalboard/server/services/store"
"github.com/mattermost/focalboard/server/services/store/mattermostauthlayer"
"github.com/mattermost/focalboard/server/services/store/sqlstore"
Expand Down Expand Up @@ -132,7 +133,22 @@ func (p *Plugin) OnActivate() error {

p.wsPluginAdapter = ws.NewPluginAdapter(p.API, auth.New(cfg, db))

server, err := server.New(cfg, "", db, logger, serverID, p.wsPluginAdapter)
mentionsBackend, err := createMentionsNotifyBackend(client, cfg.ServerRoot, logger)
if err != nil {
return fmt.Errorf("error creating mentions notifications backend: %w", err)
}

params := server.Params{
Cfg: cfg,
SingleUserToken: "",
DBStore: db,
Logger: logger,
ServerID: serverID,
WSAdapter: p.wsPluginAdapter,
NotifyBackends: []notify.Backend{mentionsBackend},
}

server, err := server.New(params)
if err != nil {
fmt.Println("ERROR INITIALIZING THE SERVER", err)
return err
Expand Down
48 changes: 26 additions & 22 deletions server/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/mattermost/focalboard/server/auth"
"github.com/mattermost/focalboard/server/services/config"
"github.com/mattermost/focalboard/server/services/metrics"
"github.com/mattermost/focalboard/server/services/notify"
"github.com/mattermost/focalboard/server/services/store"
"github.com/mattermost/focalboard/server/services/webhook"
"github.com/mattermost/focalboard/server/ws"
Expand All @@ -14,34 +15,37 @@ import (
)

type Services struct {
Auth *auth.Auth
Store store.Store
FilesBackend filestore.FileBackend
Webhook *webhook.Client
Metrics *metrics.Metrics
Logger *mlog.Logger
Auth *auth.Auth
Store store.Store
FilesBackend filestore.FileBackend
Webhook *webhook.Client
Metrics *metrics.Metrics
Notifications *notify.Service
Logger *mlog.Logger
}

type App struct {
config *config.Configuration
store store.Store
auth *auth.Auth
wsAdapter ws.Adapter
filesBackend filestore.FileBackend
webhook *webhook.Client
metrics *metrics.Metrics
logger *mlog.Logger
config *config.Configuration
store store.Store
auth *auth.Auth
wsAdapter ws.Adapter
filesBackend filestore.FileBackend
webhook *webhook.Client
metrics *metrics.Metrics
notifications *notify.Service
logger *mlog.Logger
}

func New(config *config.Configuration, wsAdapter ws.Adapter, services Services) *App {
return &App{
config: config,
store: services.Store,
auth: services.Auth,
wsAdapter: wsAdapter,
filesBackend: services.FilesBackend,
webhook: services.Webhook,
metrics: services.Metrics,
logger: services.Logger,
config: config,
store: services.Store,
auth: services.Auth,
wsAdapter: wsAdapter,
filesBackend: services.FilesBackend,
webhook: services.Webhook,
metrics: services.Metrics,
notifications: services.Notifications,
logger: services.Logger,
}
}
98 changes: 91 additions & 7 deletions server/app/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package app

import (
"github.com/mattermost/focalboard/server/model"
"github.com/mattermost/focalboard/server/services/notify"
"github.com/mattermost/focalboard/server/services/store"

"github.com/mattermost/mattermost-server/v6/shared/mlog"
)

func (a *App) GetBlocks(c store.Container, parentID string, blockType string) ([]model.Block, error) {
Expand Down Expand Up @@ -30,40 +33,64 @@ func (a *App) GetParentID(c store.Container, blockID string) (string, error) {
}

func (a *App) PatchBlock(c store.Container, blockID string, blockPatch *model.BlockPatch, userID string) error {
err := a.store.PatchBlock(c, blockID, blockPatch, userID)
oldBlock, err := a.store.GetBlock(c, blockID)
if err != nil {
return nil
}

err = a.store.PatchBlock(c, blockID, blockPatch, userID)
if err != nil {
return err
}

a.metrics.IncrementBlocksPatched(1)
block, err := a.store.GetBlock(c, blockID)
if err != nil {
return nil
}
a.wsAdapter.BroadcastBlockChange(c.WorkspaceID, *block)
go a.webhook.NotifyUpdate(*block)
go func() {
a.webhook.NotifyUpdate(*block)
a.notifyBlockChanged(notify.Update, c, block, oldBlock, userID)
}()
return nil
}

func (a *App) InsertBlock(c store.Container, block model.Block, userID string) error {
err := a.store.InsertBlock(c, &block, userID)
if err == nil {
a.wsAdapter.BroadcastBlockChange(c.WorkspaceID, block)
a.metrics.IncrementBlocksInserted(1)
go func() {
a.webhook.NotifyUpdate(block)
a.notifyBlockChanged(notify.Add, c, &block, nil, userID)
}()
}
return err
}

func (a *App) InsertBlocks(c store.Container, blocks []model.Block, userID string) error {
needsNotify := make([]model.Block, 0, len(blocks))
for i := range blocks {
err := a.store.InsertBlock(c, &blocks[i], userID)
if err != nil {
return err
}

needsNotify = append(needsNotify, blocks[i])

a.wsAdapter.BroadcastBlockChange(c.WorkspaceID, blocks[i])
a.metrics.IncrementBlocksInserted(len(blocks))
go a.webhook.NotifyUpdate(blocks[i])
a.metrics.IncrementBlocksInserted(1)
}

go func() {
for _, b := range needsNotify {
block := b
a.webhook.NotifyUpdate(block)
a.notifyBlockChanged(notify.Add, c, &block, nil, userID)
}
}()

return nil
}

Expand All @@ -80,7 +107,7 @@ func (a *App) GetAllBlocks(c store.Container) ([]model.Block, error) {
}

func (a *App) DeleteBlock(c store.Container, blockID string, modifiedBy string) error {
parentID, err := a.GetParentID(c, blockID)
block, err := a.store.GetBlock(c, blockID)
if err != nil {
return err
}
Expand All @@ -90,12 +117,69 @@ func (a *App) DeleteBlock(c store.Container, blockID string, modifiedBy string)
return err
}

a.wsAdapter.BroadcastBlockDelete(c.WorkspaceID, blockID, parentID)
a.wsAdapter.BroadcastBlockDelete(c.WorkspaceID, blockID, block.ParentID)
a.metrics.IncrementBlocksDeleted(1)

go func() {
a.notifyBlockChanged(notify.Update, c, block, block, modifiedBy)
}()
return nil
}

func (a *App) GetBlockCountsByType() (map[string]int64, error) {
return a.store.GetBlockCountsByType()
}

func (a *App) notifyBlockChanged(action notify.Action, c store.Container, block *model.Block, oldBlock *model.Block, userID string) {
if a.notifications == nil {
return
}

// find card and board for the changed block.
board, card, err := a.getBoardAndCard(c, block)
if err != nil {
a.logger.Error("Error notifying for block change; cannot determine board or card", mlog.Err(err))
return
}

evt := notify.BlockChangeEvent{
Action: action,
Workspace: c.WorkspaceID,
Board: board,
Card: card,
BlockChanged: block,
BlockOld: oldBlock,
UserID: userID,
}
a.notifications.BlockChanged(evt)
}

const (
maxSearchDepth = 50
)

// getBoardAndCard returns the first parent of type `card` and first parent of type `board` for the specified block.
// `board` and/or `card` may return nil without error if the block does not belong to a board or card.
func (a *App) getBoardAndCard(c store.Container, block *model.Block) (board *model.Block, card *model.Block, err error) {
var count int // don't let invalid blocks hierarchy cause infinite loop.
iter := block
for {
count++
if board == nil && iter.Type == "board" {
board = iter
}

if card == nil && iter.Type == "card" {
card = iter
}

if iter.ParentID == "" || (board != nil && card != nil) || count > maxSearchDepth {
break
}

iter, err = a.store.GetBlock(c, iter.ParentID)
if err != nil {
return board, card, err
}
}
return board, card, nil
}
1 change: 1 addition & 0 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.7.0
github.com/wiggin77/merror v1.0.3
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
Loading

0 comments on commit 8425f53

Please sign in to comment.