diff --git a/dist/com.mattermost.gameStatusUpdate-0.2.11.tar.gz b/dist/com.mattermost.gameStatusUpdate-0.2.11.tar.gz index df8a1e9..762e1c1 100644 Binary files a/dist/com.mattermost.gameStatusUpdate-0.2.11.tar.gz and b/dist/com.mattermost.gameStatusUpdate-0.2.11.tar.gz differ diff --git a/dist/com.mattermost.gameStatusUpdate/server/dist/plugin-darwin-amd64 b/dist/com.mattermost.gameStatusUpdate/server/dist/plugin-darwin-amd64 index 39a7342..964c12a 100755 Binary files a/dist/com.mattermost.gameStatusUpdate/server/dist/plugin-darwin-amd64 and b/dist/com.mattermost.gameStatusUpdate/server/dist/plugin-darwin-amd64 differ diff --git a/dist/com.mattermost.gameStatusUpdate/server/dist/plugin-darwin-arm64 b/dist/com.mattermost.gameStatusUpdate/server/dist/plugin-darwin-arm64 index c15cad5..988320d 100755 Binary files a/dist/com.mattermost.gameStatusUpdate/server/dist/plugin-darwin-arm64 and b/dist/com.mattermost.gameStatusUpdate/server/dist/plugin-darwin-arm64 differ diff --git a/dist/com.mattermost.gameStatusUpdate/server/dist/plugin-linux-amd64 b/dist/com.mattermost.gameStatusUpdate/server/dist/plugin-linux-amd64 index 829faa0..5cfe0d7 100755 Binary files a/dist/com.mattermost.gameStatusUpdate/server/dist/plugin-linux-amd64 and b/dist/com.mattermost.gameStatusUpdate/server/dist/plugin-linux-amd64 differ diff --git a/dist/com.mattermost.gameStatusUpdate/server/dist/plugin-linux-arm64 b/dist/com.mattermost.gameStatusUpdate/server/dist/plugin-linux-arm64 index b0d1377..80ceb45 100755 Binary files a/dist/com.mattermost.gameStatusUpdate/server/dist/plugin-linux-arm64 and b/dist/com.mattermost.gameStatusUpdate/server/dist/plugin-linux-arm64 differ diff --git a/dist/com.mattermost.gameStatusUpdate/server/dist/plugin-windows-amd64.exe b/dist/com.mattermost.gameStatusUpdate/server/dist/plugin-windows-amd64.exe index b417312..2c94a5a 100755 Binary files a/dist/com.mattermost.gameStatusUpdate/server/dist/plugin-windows-amd64.exe and b/dist/com.mattermost.gameStatusUpdate/server/dist/plugin-windows-amd64.exe differ diff --git a/server/dist/plugin-darwin-amd64 b/server/dist/plugin-darwin-amd64 index 39a7342..964c12a 100755 Binary files a/server/dist/plugin-darwin-amd64 and b/server/dist/plugin-darwin-amd64 differ diff --git a/server/dist/plugin-darwin-arm64 b/server/dist/plugin-darwin-arm64 index c15cad5..988320d 100755 Binary files a/server/dist/plugin-darwin-arm64 and b/server/dist/plugin-darwin-arm64 differ diff --git a/server/dist/plugin-linux-amd64 b/server/dist/plugin-linux-amd64 index 829faa0..5cfe0d7 100755 Binary files a/server/dist/plugin-linux-amd64 and b/server/dist/plugin-linux-amd64 differ diff --git a/server/dist/plugin-linux-arm64 b/server/dist/plugin-linux-arm64 index b0d1377..80ceb45 100755 Binary files a/server/dist/plugin-linux-arm64 and b/server/dist/plugin-linux-arm64 differ diff --git a/server/dist/plugin-windows-amd64.exe b/server/dist/plugin-windows-amd64.exe index b417312..2c94a5a 100755 Binary files a/server/dist/plugin-windows-amd64.exe and b/server/dist/plugin-windows-amd64.exe differ diff --git a/server/plugin.go b/server/plugin.go index 727d006..53b5f39 100644 --- a/server/plugin.go +++ b/server/plugin.go @@ -33,8 +33,9 @@ var knownGames = map[string]bool{ func (p *Plugin) ReceiveProcessList(w http.ResponseWriter, r *http.Request) { var payload struct { ProcessList string `json:"processList"` - UserID string `json:"userID"` // Assuming you're sending userID from the desktop app + UserID string `json:"userID"` } + if err := json.NewDecoder(r.Body).Decode(&payload); err != nil { p.API.LogError("Failed to decode process list", "error", err.Error()) http.Error(w, "Invalid request payload", http.StatusBadRequest) @@ -57,26 +58,6 @@ func (p *Plugin) ReceiveProcessList(w http.ResponseWriter, r *http.Request) { } } -// GetActiveGame scans processes to determine the active game based on known game names -func (p *Plugin) GetActiveGame() (string, error) { - cmd := exec.Command("ps", "-e") - output, err := cmd.Output() - if err != nil { - return "", err - } - - lines := strings.Split(string(output), "\n") - for _, line := range lines { - for gameName := range knownGames { - if strings.Contains(line, gameName) { - return gameName, 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) @@ -85,6 +66,11 @@ func (p *Plugin) SetUserGameStatus(userID, game string) error { return appErr } + // Avoid unnecessary updates + if user.Props["custom_status"] == status { + return nil + } + customStatus := model.CustomStatus{ Emoji: "video_game", Text: status, @@ -123,6 +109,10 @@ func (p *Plugin) MonitorGameStatus(userID string) { if err := p.SetUserGameStatus(userID, game); err != nil { p.API.LogError("Failed to set user game status", "error", err.Error()) } + } else { + if err := p.SetUserGameStatus(userID, ""); err != nil { + p.API.LogError("Failed to clear user game status", "error", err.Error()) + } } time.Sleep(30 * time.Second) @@ -130,6 +120,26 @@ func (p *Plugin) MonitorGameStatus(userID string) { } } +// GetActiveGame scans processes to determine the active game based on known game names +func (p *Plugin) GetActiveGame() (string, error) { + cmd := exec.Command("tasklist") // Use tasklist for Windows + output, err := cmd.Output() + if err != nil { + return "", err + } + + lines := strings.Split(string(output), "\n") + for _, line := range lines { + for gameName := range knownGames { + if strings.Contains(line, gameName) { + return gameName, nil + } + } + } + + return "", nil +} + // OnActivate is called when the plugin is activated func (p *Plugin) OnActivate() error { config := p.getConfiguration() @@ -150,17 +160,21 @@ func (p *Plugin) OnActivate() error { // Register the HTTP handler for receiving process lists http.HandleFunc("/plugins/com.mattermost.gamestatusupdate/processlist", p.ReceiveProcessList) + + // Retrieve the current user ID + session, err := p.API.GetSession("current") // Use "current" to get the session for the logged-in user + if err != nil { + return fmt.Errorf("failed to get session: %w", err) // Handle the error appropriately + } + userID := session.UserId + + // Start monitoring game status for the current user + go p.MonitorGameStatus(userID) } return nil } -// OnDeactivate is called when the plugin is deactivated -func (p *Plugin) OnDeactivate() error { - close(p.stopChannel) - return nil -} - // HandleWebSocketEvent handles incoming WebSocket events. func (p *Plugin) HandleWebSocketEvent(event model.WebSocketEvent) { // Check if the event is of type user_status @@ -185,20 +199,18 @@ func (p *Plugin) HandleWebSocketEvent(event model.WebSocketEvent) { return } - // Continue with your logic to update the user's game status... + // Update the user's game status based on active game game, err := p.GetActiveGame() if err != nil { p.API.LogError("Failed to get active game", "error", err.Error()) return } - // Update the user's game status if game != "" { if err := p.SetUserGameStatus(data.UserID, game); err != nil { p.API.LogError("Failed to update user game status", "error", err.Error()) } } else { - // Clear the user's game status if no game is detected if err := p.SetUserGameStatus(data.UserID, ""); err != nil { p.API.LogError("Failed to clear user game status", "error", err.Error()) }