60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"strings"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// HandleProcessList handles the incoming process list from the desktop app.
|
|
func (p *Plugin) HandleProcessList(ctx context.Context, requestData []byte) error {
|
|
var request struct {
|
|
ProcessList string `json:"processList"`
|
|
UserID string `json:"userID"`
|
|
}
|
|
|
|
if err := json.Unmarshal(requestData, &request); err != nil {
|
|
return errors.New("invalid request payload")
|
|
}
|
|
|
|
game := ""
|
|
for knownGame := range knownGames {
|
|
if strings.Contains(request.ProcessList, knownGame) {
|
|
game = knownGame
|
|
break
|
|
}
|
|
}
|
|
|
|
if game != "" {
|
|
if err := p.SetUserGameStatus(request.UserID, game); err != nil {
|
|
return errors.New("failed to update user status: " + err.Error())
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// InitRoutes initializes any needed routes or commands for the plugin.
|
|
func InitRoutes(p *Plugin) {
|
|
// Register the command that the user will call to set their game.
|
|
command := &model.Command{
|
|
Trigger: "setgame",
|
|
AutoComplete: true,
|
|
AutoCompleteDesc: "Set the game you are currently playing",
|
|
AutoCompleteHint: "[game]",
|
|
}
|
|
|
|
if err := p.API.RegisterCommand(command); err != nil {
|
|
p.API.LogError("Failed to register command", "error", err)
|
|
}
|
|
}
|
|
|
|
// Note: Since you're using Mattermost's WebSocket connections,
|
|
// you might want to register for specific events in your `plugin.go`.
|
|
// For example, in your `OnActivate` method, you can register for events like this:
|
|
|
|
// p.API.RegisterWebSocketEvent("your_event_name", p.HandleWebSocketEvent)
|