Skip to content

Commit

Permalink
Adding the new RHS panel (#83)
Browse files Browse the repository at this point in the history
* Adding the initial implemention of the RHS

* WIP

* Initial fully working prototype

* Adding tiny improvements

* Enabling backward compatibility

* Some improvements

* Polishing the PR

* Splitting things a bit to make the components smaller and more organized

* Fixing a bug on creation of new posts

* Some other improvements

* Setting the right autocompletion texts the buttons in the new chat

* Fixing tiny bug on showing the 'new chat' link

* Fixing linter error

* Addressing PR review comments

* Fixing linter error

* Adding sepreate API for fetching AI bot threads.

---------

Co-authored-by: Christopher Speller <[email protected]>
  • Loading branch information
jespino and crspeller authored Nov 20, 2023
1 parent 9beecbb commit 36596fa
Show file tree
Hide file tree
Showing 15 changed files with 925 additions and 139 deletions.
48 changes: 48 additions & 0 deletions server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package main
import (
"net/http"

sq "github.com/Masterminds/squirrel"
"github.com/gin-gonic/gin"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/plugin"
"github.com/pkg/errors"
)

const (
Expand All @@ -18,6 +21,8 @@ func (p *Plugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Req
router.Use(p.ginlogger)
router.Use(p.MattermostAuthorizationRequired)

router.GET("/ai_threads", p.handleGetAIThreads)

postRouter := router.Group("/post/:postid")
postRouter.Use(p.postAuthorizationRequired)
postRouter.POST("/react", p.handleReact)
Expand Down Expand Up @@ -62,3 +67,46 @@ func (p *Plugin) MattermostAuthorizationRequired(c *gin.Context) {
return
}
}

func (p *Plugin) handleGetAIThreads(c *gin.Context) {
userID := c.GetHeader("Mattermost-User-Id")

botDMChannel, err := p.pluginAPI.Channel.GetDirect(userID, p.botid)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, errors.Wrap(err, "unable to get DM with AI bot"))
return
}

// Extra permissions checks are not totally nessiary since a user should always have permission to read their own DMs
if !p.pluginAPI.User.HasPermissionToChannel(userID, botDMChannel.Id, model.PermissionReadChannel) {
c.AbortWithError(http.StatusForbidden, errors.New("user doesn't have permission to read channel"))
return
}

var posts []struct {
ID string
Message string
ReplyCount int
UpdateAt int64
}
if err := p.doQuery(&posts, p.builder.
Select(
"p.Id",
"p.Message",
"(SELECT COUNT(*) FROM Posts WHERE Posts.RootId = p.Id AND DeleteAt = 0) AS ReplyCount",
"p.UpdateAt",
).
From("Posts as p").
Where(sq.Eq{"ChannelID": botDMChannel.Id}).
Where(sq.Eq{"RootId": ""}).
Where(sq.Eq{"DeleteAt": 0}).
OrderBy("CreateAt DESC").
Limit(60).
Offset(0),
); err != nil {
c.AbortWithError(http.StatusInternalServerError, errors.Wrap(err, "failed to get posts for bot DM"))
return
}

c.JSON(http.StatusOK, posts)
}
Loading

0 comments on commit 36596fa

Please sign in to comment.