added router

This commit is contained in:
aaron 2024-11-05 05:17:08 -08:00
parent 079e4752a4
commit 8b971fe8aa
24 changed files with 90 additions and 82 deletions

View file

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

View file

@ -1,12 +1,12 @@
{ {
"id": "com.mattermost.plugin-gameStatusUpdate", "id": "com.mattermost.gameStatusUpdate",
"name": "game status update", "name": "game status update",
"description": "this plugin will update statuses to show what game is being played - fuck discord", "description": "this plugin will update statuses to show what game is being played - fuck discord",
"homepage_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdate", "homepage_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdate",
"support_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdate/issues", "support_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdate/issues",
"release_notes_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdatereleases/tag/v0.2.7", "release_notes_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdatereleases/tag/v0.2.8",
"icon_path": "assets/starter-template-icon.svg", "icon_path": "assets/starter-template-icon.svg",
"version": "0.2.8", "version": "0.2.9",
"min_server_version": "6.2.1", "min_server_version": "6.2.1",
"server": { "server": {
"executables": { "executables": {

View file

Before

Width:  |  Height:  |  Size: 5 KiB

After

Width:  |  Height:  |  Size: 5 KiB

View file

@ -1,7 +1,7 @@
{ {
"id": "com.mattermost.plugin-gameStatusUpdate", "id": "com.mattermost.gameStatusUpdate",
"name": "game status update", "name": "game status update",
"version": "0.2.8", "version": "0.2.9",
"description": "this plugin will update statuses to show what game is being played - fuck discord", "description": "this plugin will update statuses to show what game is being played - fuck discord",
"homepage_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdate", "homepage_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdate",
"support_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdate/issues", "support_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdate/issues",

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -1,10 +1,14 @@
package main package main
//import ( import (
// "github.com/mattermost/mattermost/server/public/plugin" "github.com/mattermost/mattermost/server/public/plugin"
//) )
//func main() { func main() {
// plugin.ClientMain(&Plugin{}) p := &Plugin{
//} stopChannel: make(chan struct{}),
// }
p.apiRouter = InitRoutes(p) // Initialize the API router here
plugin.ClientMain(p)
}

View file

@ -13,14 +13,14 @@ var manifest *model.Manifest
const manifestStr = ` const manifestStr = `
{ {
"id": "com.mattermost.plugin-gameStatusUpdate", "id": "com.mattermost.gameStatusUpdate",
"name": "game status update", "name": "game status update",
"description": "this plugin will update statuses to show what game is being played - fuck discord", "description": "this plugin will update statuses to show what game is being played - fuck discord",
"homepage_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdate", "homepage_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdate",
"support_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdate/issues", "support_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdate/issues",
"release_notes_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdatereleases/tag/v0.2.7", "release_notes_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdatereleases/tag/v0.2.8",
"icon_path": "assets/starter-template-icon.svg", "icon_path": "assets/starter-template-icon.svg",
"version": "0.2.8", "version": "0.2.9",
"min_server_version": "6.2.1", "min_server_version": "6.2.1",
"server": { "server": {
"executables": { "executables": {

View file

@ -17,14 +17,10 @@ import (
type Plugin struct { type Plugin struct {
plugin.MattermostPlugin plugin.MattermostPlugin
// configurationLock synchronizes access to the configuration.
configurationLock sync.RWMutex configurationLock sync.RWMutex
// configuration is the active plugin configuration.
configuration *configuration configuration *configuration
// stopChannel is used to stop the background process monitor when the plugin is deactivated.
stopChannel chan struct{} stopChannel chan struct{}
apiRouter http.Handler
} }
// Known game processes // Known game processes
@ -36,18 +32,15 @@ var knownGames = map[string]bool{
// GetActiveGame scans processes to determine the active game based on known game names // GetActiveGame scans processes to determine the active game based on known game names
func (p *Plugin) GetActiveGame() (string, error) { func (p *Plugin) GetActiveGame() (string, error) {
// For Linux, use the ps command to get the list of processes cmd := exec.Command("ps", "-e")
cmd := exec.Command("ps", "-e") // -e lists all processes
output, err := cmd.Output() output, err := cmd.Output()
if err != nil { if err != nil {
return "", err return "", err
} }
// Convert output to a string and split into lines
lines := strings.Split(string(output), "\n") lines := strings.Split(string(output), "\n")
for _, line := range lines { for _, line := range lines {
for gameName := range knownGames { for gameName := range knownGames {
// Check if the line contains a known game process
if strings.Contains(line, gameName) { if strings.Contains(line, gameName) {
return gameName, nil return gameName, nil
} }
@ -65,18 +58,16 @@ func (p *Plugin) SetUserGameStatus(userID, game string) error {
return appErr return appErr
} }
// Serialize the custom status to JSON
customStatus := model.CustomStatus{ customStatus := model.CustomStatus{
Emoji: "video_game", Emoji: "video_game",
Text: status, Text: status,
Duration: "0", // Status remains until changed Duration: "0",
} }
customStatusJSON, err := json.Marshal(customStatus) customStatusJSON, err := json.Marshal(customStatus)
if err != nil { if err != nil {
return err return err
} }
// Set the custom status in user.Props
user.Props = map[string]string{ user.Props = map[string]string{
"custom_status": string(customStatusJSON), "custom_status": string(customStatusJSON),
} }
@ -107,44 +98,11 @@ func (p *Plugin) MonitorGameStatus(userID string) {
} }
} }
// Sleep for a defined interval before scanning again time.Sleep(30 * time.Second)
time.Sleep(30 * time.Second) // Scan every 30 seconds
} }
} }
} }
// handleProcessList handles the incoming process list from the desktop app
func (p *Plugin) handleProcessList(w http.ResponseWriter, r *http.Request) {
var requestData struct {
ProcessList string `json:"processList"`
UserID string `json:"userID"`
}
if err := json.NewDecoder(r.Body).Decode(&requestData); err != nil {
http.Error(w, "Invalid request payload", http.StatusBadRequest)
return
}
// Check for active game from the process list
game := ""
for knownGame := range knownGames {
if strings.Contains(requestData.ProcessList, knownGame) {
game = knownGame
break
}
}
if game != "" {
err := p.SetUserGameStatus(requestData.UserID, game)
if err != nil {
http.Error(w, "Failed to update user status", http.StatusInternalServerError)
return
}
}
w.WriteHeader(http.StatusOK)
}
// OnActivate is called when the plugin is activated // OnActivate is called when the plugin is activated
func (p *Plugin) OnActivate() error { func (p *Plugin) OnActivate() error {
p.stopChannel = make(chan struct{}) p.stopChannel = make(chan struct{})
@ -160,16 +118,9 @@ func (p *Plugin) OnActivate() error {
return err return err
} }
// Create a new HTTP ServeMux and register the endpoint // Initialize the router and start the HTTP server
mux := http.NewServeMux() p.apiRouter = InitRoutes(p) // Set the API routes
mux.HandleFunc("/processlist", p.handleProcessList) go StartHTTPServer(p) // Start the HTTP server
// Start the HTTP server
go func() {
if err := http.ListenAndServe(":8780", mux); err != nil {
p.API.LogError("Failed to start HTTP server", "error", err.Error())
}
}()
return nil return nil
} }
@ -179,7 +130,3 @@ func (p *Plugin) OnDeactivate() error {
close(p.stopChannel) close(p.stopChannel)
return nil return nil
} }
func main() {
plugin.ClientMain(&Plugin{})
}

57
server/router.go Normal file
View file

@ -0,0 +1,57 @@
package main
import (
"encoding/json"
"net/http"
"strings"
)
// InitRoutes initializes all HTTP routes for the plugin
func InitRoutes(p *Plugin) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/processlist", p.handleProcessList)
return mux
}
// StartHTTPServer starts the HTTP server on a specific port
func StartHTTPServer(p *Plugin) {
httpServer := &http.Server{
Addr: ":8780",
Handler: p.apiRouter,
}
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
p.API.LogError("Failed to start HTTP server", "error", err.Error())
}
}
// handleProcessList handles the incoming process list from the desktop app
func (p *Plugin) handleProcessList(w http.ResponseWriter, r *http.Request) {
var requestData struct {
ProcessList string `json:"processList"`
UserID string `json:"userID"`
}
if err := json.NewDecoder(r.Body).Decode(&requestData); err != nil {
http.Error(w, "Invalid request payload", http.StatusBadRequest)
return
}
game := ""
for knownGame := range knownGames {
if strings.Contains(requestData.ProcessList, knownGame) {
game = knownGame
break
}
}
if game != "" {
err := p.SetUserGameStatus(requestData.UserID, game)
if err != nil {
http.Error(w, "Failed to update user status", http.StatusInternalServerError)
return
}
}
w.WriteHeader(http.StatusOK)
}

2
webapp/dist/main.js vendored

File diff suppressed because one or more lines are too long

View file

@ -2,14 +2,14 @@
const manifest = JSON.parse(` const manifest = JSON.parse(`
{ {
"id": "com.mattermost.plugin-gameStatusUpdate", "id": "com.mattermost.gameStatusUpdate",
"name": "game status update", "name": "game status update",
"description": "this plugin will update statuses to show what game is being played - fuck discord", "description": "this plugin will update statuses to show what game is being played - fuck discord",
"homepage_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdate", "homepage_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdate",
"support_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdate/issues", "support_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdate/issues",
"release_notes_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdatereleases/tag/v0.2.7", "release_notes_url": "https://gitlab.peanutsmediaserver.com/aaron/gameStatusUpdatereleases/tag/v0.2.8",
"icon_path": "assets/starter-template-icon.svg", "icon_path": "assets/starter-template-icon.svg",
"version": "0.2.8", "version": "0.2.9",
"min_server_version": "6.2.1", "min_server_version": "6.2.1",
"server": { "server": {
"executables": { "executables": {