-
Notifications
You must be signed in to change notification settings - Fork 2
feat: api foundation #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
realtlos
wants to merge
21
commits into
main
Choose a base branch
from
feat/api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
d760ca4
feat: add platform config structs
realtlos 96d9d29
feat: add data routes and db methods
realtlos a42728d
feat: add settings and command write routes
realtlos 7a334d0
feat: add oauth platform auth routes
realtlos 3502d41
fix: lint in db package
realtlos 0d05f92
fix: suppress lint errors in existing files
realtlos fcb9a53
fix: address clanker comments
realtlos 8dd3e3e
fix: stop leaking NATS connections per request
realtlos d5a5a72
refactor: consolidate channel auth into shared middleware helper
realtlos c3178d0
fix NATS subject names and add initialized log
realtlos d4f72c2
remove bench flag causing gotestfmt panic
realtlos 5dd255e
fix: use native go test
realtlos a06a16e
refactor: revert gotestfmt changes
realtlos f135244
fix: address copilot comments
realtlos b4e1b56
fix: lint
realtlos fbf7cc9
Merge remote-tracking branch 'origin/main' into feat/api
realtlos 15dde7e
fix: make command/channel settings platform aware
realtlos ae630b1
fix: lint
realtlos 70547af
fix: lint please
realtlos e63f530
fix: remove block-start/end blank lines to satisfy whitespace linter
realtlos b12f625
fix: format loops.go with gofumpt v0.9.2
realtlos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| // Package del contains routes for http.MethodDelete requests. | ||
| package del | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "slices" | ||
| "time" | ||
|
|
||
| "github.com/Potat-Industries/potat-api/api" | ||
| "github.com/Potat-Industries/potat-api/api/middleware" | ||
| "github.com/Potat-Industries/potat-api/common" | ||
| "github.com/Potat-Industries/potat-api/common/db" | ||
| "github.com/Potat-Industries/potat-api/common/logger" | ||
| ) | ||
|
|
||
| func init() { | ||
| api.SetRoute(api.Route{ | ||
| Path: "/channel/command-settings", | ||
| Method: http.MethodDelete, | ||
| Handler: deleteCommandSettings, | ||
| UseAuth: true, | ||
| }) | ||
| } | ||
|
|
||
| func deleteCommandSettings(writer http.ResponseWriter, request *http.Request) { //nolint:cyclop | ||
| start := time.Now() | ||
|
|
||
| user, ok := request.Context().Value(middleware.AuthedUser).(*common.User) | ||
| if !ok || user == nil { | ||
| api.GenericResponse(writer, http.StatusUnauthorized, common.GenericResponse[any]{ | ||
| Errors: &[]common.ErrorMessage{{Message: "Unauthorized"}}, | ||
| }, start) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| postgres, pgOK := request.Context().Value(middleware.PostgresKey).(*db.PostgresClient) | ||
| if !pgOK { | ||
| logger.Error.Println("Postgres client not found in context") | ||
| api.GenericResponse(writer, http.StatusInternalServerError, common.GenericResponse[any]{ | ||
| Errors: &[]common.ErrorMessage{{Message: "Internal Server Error"}}, | ||
| }, start) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| channelID := request.URL.Query().Get("id") | ||
| if channelID == "" { | ||
| for _, conn := range user.Connections { | ||
| if conn.Platform == common.TWITCH { | ||
| channelID = conn.UserID | ||
|
|
||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| command := request.URL.Query().Get("command") | ||
|
|
||
| if channelID == "" { | ||
| api.GenericResponse(writer, http.StatusBadRequest, common.GenericResponse[any]{ | ||
| Errors: &[]common.ErrorMessage{{Message: "channel id is required"}}, | ||
| }, start) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| if !isDelAuthorized(request, user, channelID, postgres) { | ||
| api.GenericResponse(writer, http.StatusForbidden, common.GenericResponse[any]{ | ||
| Errors: &[]common.ErrorMessage{{Message: "Forbidden"}}, | ||
| }, start) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| // Without a specific command name, this is a no-op (not yet implemented upstream). | ||
| if command == "" { | ||
| api.GenericResponse(writer, http.StatusOK, common.GenericResponse[any]{ | ||
| Data: &[]any{}, | ||
| }, start) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| if err := postgres.ResetCommandSettings(request.Context(), channelID, command); err != nil { | ||
| logger.Error.Printf("Error resetting command settings: %v", err) | ||
| api.GenericResponse(writer, http.StatusInternalServerError, common.GenericResponse[any]{ | ||
| Errors: &[]common.ErrorMessage{{Message: "Failed to reset command settings"}}, | ||
| }, start) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| api.GenericResponse(writer, http.StatusOK, common.GenericResponse[any]{ | ||
| Data: &[]any{}, | ||
| }, start) | ||
| } | ||
|
|
||
| func isDelAuthorized( | ||
| request *http.Request, | ||
| user *common.User, | ||
| channelID string, | ||
| postgres *db.PostgresClient, | ||
| ) bool { | ||
| if user.Level >= int(common.ADMIN) { | ||
| return true | ||
| } | ||
|
|
||
| var twitchID string | ||
| for _, conn := range user.Connections { | ||
| if conn.Platform == common.TWITCH { | ||
| twitchID = conn.UserID | ||
|
|
||
| break | ||
| } | ||
| } | ||
|
|
||
| if twitchID == channelID { | ||
| return true | ||
| } | ||
|
|
||
| ambassadors, err := postgres.GetChannelAmbassadors(request.Context(), channelID, common.TWITCH) | ||
| if err != nil { | ||
| return false | ||
| } | ||
|
|
||
| return slices.Contains(ambassadors, twitchID) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.