changes to code to add automation and an application to test detection

This commit is contained in:
root 2024-09-20 07:42:34 -07:00
parent 53c81bb129
commit b23c129b79
13 changed files with 92 additions and 35 deletions

View file

@ -201,6 +201,11 @@ Setting the `MM_DEBUG` environment variable will invoke the debug builds. The si
* git remote add origin https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdate.git
* git push -u origin main
###
###
###
###
## DONT FORGET TO DO THIS BEFORE COMMITING CHANGES
* make clean
* make

View file

@ -4,9 +4,9 @@
"description": "This plugin serves as a starting point for writing a Mattermost plugin.",
"homepage_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdate",
"support_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdate/issues",
"release_notes_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdatereleases/tag/v0.0.3",
"release_notes_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdatereleases/tag/v0.0.4",
"icon_path": "assets/starter-template-icon.svg",
"version": "0.0.3",
"version": "0.0.4",
"min_server_version": "6.2.1",
"server": {
"executables": {

File diff suppressed because one or more lines are too long

View file

@ -18,9 +18,9 @@ const manifestStr = `
"description": "This plugin serves as a starting point for writing a Mattermost plugin.",
"homepage_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdate",
"support_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdate/issues",
"release_notes_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdatereleases/tag/v0.0.3",
"release_notes_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdatereleases/tag/v0.0.4",
"icon_path": "assets/starter-template-icon.svg",
"version": "0.0.3",
"version": "0.0.4",
"min_server_version": "6.2.1",
"server": {
"executables": {

View file

@ -6,6 +6,7 @@ import (
"os/exec"
"strings"
"sync"
"time"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/plugin"
@ -21,12 +22,15 @@ type Plugin struct {
// configuration is the active plugin configuration. Consult getConfiguration and
// setConfiguration for usage.
configuration *configuration
// stopChannel is used to stop the background process monitor when the plugin is deactivated.
stopChannel chan struct{}
}
// Known game processes
var knownGames = map[string]bool{
// Add known game processes here
"FortniteClient-Win64-Shipping.exe": true,
"OpenDental.exe": true,
"gameprocess": true,
}
@ -48,9 +52,66 @@ func (p *Plugin) GetActiveGame() (string, error) {
}
}
return "No game detected", nil
return "", nil
}
// SetUserGameStatus updates the user's custom status to reflect the game they are playing
func (p *Plugin) SetUserGameStatus(userID, game string) error {
status := fmt.Sprintf("playing: %s", game)
user, appErr := p.API.GetUser(userID)
if appErr != nil {
return appErr
}
// Serialize the custom status to JSON
customStatus := model.CustomStatus{
Emoji: "video_game",
Text: status,
Duration: "0", // Status remains until changed
}
customStatusJSON, err := json.Marshal(customStatus)
if err != nil {
return err
}
// Set the custom status in user.Props
user.Props = map[string]string{
"custom_status": string(customStatusJSON),
}
if _, appErr := p.API.UpdateUser(user); appErr != nil {
return appErr
}
return nil
}
// MonitorGameStatus continuously monitors running processes and updates the user's status
func (p *Plugin) MonitorGameStatus(userID string) {
for {
select {
case <-p.stopChannel:
return
default:
game, err := p.GetActiveGame()
if err != nil {
p.API.LogError("Failed to scan processes", "error", err.Error())
continue
}
if game != "" {
if err := p.SetUserGameStatus(userID, game); err != nil {
p.API.LogError("Failed to set user game status", "error", err.Error())
}
}
// Sleep for a defined interval before scanning again
time.Sleep(30 * time.Second) // Scan every 30 seconds (can be configurable)
}
}
}
// ExecuteCommand handles the `/setgame` command to manually set a game
func (p *Plugin) ExecuteCommand(c *plugin.Context, command *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
split := strings.Fields(command.Command)
@ -72,45 +133,27 @@ func (p *Plugin) ExecuteCommand(c *plugin.Context, command *model.CommandArgs) (
game = strings.Join(split[1:], " ")
}
status := fmt.Sprintf("Playing: %s", game)
user, appErr := p.API.GetUser(command.UserId)
if appErr != nil {
return nil, appErr
}
// Serialize the custom status to JSON
customStatus := model.CustomStatus{
Emoji: "video_game",
Text: status,
Duration: "0", // Optional: Time duration the status will stay
}
customStatusJSON, err := json.Marshal(customStatus)
err = p.SetUserGameStatus(command.UserId, game)
if err != nil {
return nil, &model.AppError{Message: "Failed to serialize custom status", StatusCode: 500}
}
// Set the custom status in user.Props
user.Props = map[string]string{
"custom_status": string(customStatusJSON),
}
if _, appErr := p.API.UpdateUser(user); appErr != nil {
return nil, appErr
return nil, &model.AppError{Message: "Failed to set game status", StatusCode: 500}
}
return &model.CommandResponse{
Text: fmt.Sprintf("Your status has been updated to: %s", status),
Text: fmt.Sprintf("your status has been updated to: %s", game),
}, nil
default:
return &model.CommandResponse{}, nil
}
}
// OnActivate is called when the plugin is activated
func (p *Plugin) OnActivate() error {
p.stopChannel = make(chan struct{})
command := &model.Command{
Trigger: "setgame",
AutoComplete: true,
AutoCompleteDesc: "Set the game you are currently playing",
AutoCompleteDesc: "set the game you are currently playing",
AutoCompleteHint: "[game]",
}
@ -118,5 +161,14 @@ func (p *Plugin) OnActivate() error {
return err
}
// Start background process monitoring for all users
go p.MonitorGameStatus("user_id_here") // Replace with actual user ID(s)
return nil
}
// OnDeactivate is called when the plugin is deactivated
func (p *Plugin) OnDeactivate() error {
close(p.stopChannel)
return nil
}

2
webapp/dist/main.js vendored

File diff suppressed because one or more lines are too long

View file

@ -7,9 +7,9 @@ const manifest = JSON.parse(`
"description": "This plugin serves as a starting point for writing a Mattermost plugin.",
"homepage_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdate",
"support_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdate/issues",
"release_notes_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdatereleases/tag/v0.0.3",
"release_notes_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdatereleases/tag/v0.0.4",
"icon_path": "assets/starter-template-icon.svg",
"version": "0.0.3",
"version": "0.0.4",
"min_server_version": "6.2.1",
"server": {
"executables": {