This repository was archived by the owner on May 21, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 46
API: Add Post API #256
Open
AnnsAnns
wants to merge
14
commits into
animenotifier:go
Choose a base branch
from
AnnsAnns:API
base: go
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
API: Add Post API #256
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
78d5129
Add Token Generation
c850bfc
Add basic setup
100839d
Find User based on token
419b1bd
Fix CI integration
3797dfa
Parse JSON fully [Skip CI]
cec1bd4
Fix CI
ca62937
Resolve requested changes
AnnsAnns a447994
Start work on AnimeList updating [Will break CI]
AnnsAnns b6ce3ba
Merge branch 'go' of https://github.com/animenotifier/notify.moe into…
73cdc7a
Merge branch 'API' of https://github.com/tumGER/notify.moe into API
f1dc5d9
Reimplement tokenapi.AnimeUpdate() using animeListEntry struct
6a9ec46
Fully implement animeListItem imports [Not Tested]
504a53d
Fix listimporters, remove unnecessary conversion
34fa434
Add token to privacy function
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
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
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
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
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
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
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,35 @@ | ||
| package tokenapi | ||
|
|
||
| import ( | ||
| "errors" | ||
|
|
||
| "github.com/animenotifier/notify.moe/arn" | ||
| ) | ||
|
|
||
| type AnimeParameters struct { | ||
| AnimeName string | ||
|
AnnsAnns marked this conversation as resolved.
Outdated
|
||
| AnimeID string | ||
| AnimeEpisode int | ||
| AnimeRating arn.AnimeRatingCount | ||
| } | ||
|
|
||
| func AnimeUpdate(request *TokenRequest) error { | ||
| parameters := &AnimeParameters{} | ||
| animeJSON := request.JSON.Get("anime") | ||
|
|
||
| parameters.AnimeName = animeJSON.Get("name").String() | ||
| parameters.AnimeID = animeJSON.Get("id").String() | ||
| parameters.AnimeEpisode = int(animeJSON.Get("episode").Int()) | ||
|
|
||
| if parameters.AnimeName == "" && parameters.AnimeID == "" { | ||
| return errors.New("Neither ID nor Name of the anime has been supplied") | ||
| } | ||
|
|
||
| rating := animeJSON.Get("ratings") | ||
| parameters.AnimeRating.Overall = int(rating.Get("overall").Int()) | ||
| parameters.AnimeRating.Story = int(rating.Get("story").Int()) | ||
| parameters.AnimeRating.Visuals = int(rating.Get("visuals").Int()) | ||
| parameters.AnimeRating.Soundtrack = int(rating.Get("soundtrack").Int()) | ||
|
|
||
| return nil | ||
| } | ||
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,18 @@ | ||
| package tokenapi | ||
|
|
||
| import ( | ||
| "github.com/akyoto/uuid" | ||
| "github.com/animenotifier/notify.moe/arn" | ||
| ) | ||
|
|
||
| func GetUserFromToken(token uuid.UUID) *arn.User { | ||
| user := &arn.User{} | ||
|
|
||
| for user = range arn.StreamUsers() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could replace this O(n) loop with an O(1) lookup using a new collection in the DB ("TokenToUser") later on. Otherwise this can become a huge bottleneck.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I originally wanted to implement this in a better way but I'm honestly too unfamiliar with nano so I didn't want to touch what I was unsure about. |
||
| if user.APIToken == token { | ||
| break | ||
| } | ||
| } | ||
|
|
||
| return user | ||
| } | ||
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,18 @@ | ||
| package tokenapi | ||
|
|
||
| import ( | ||
| "os" | ||
|
|
||
| "github.com/aerogo/aero" | ||
| "github.com/aerogo/log" | ||
| ) | ||
|
|
||
| // Install installs all authentication routes in the application. | ||
| func Install(app *aero.Application) { | ||
| authLog := log.New() | ||
| authLog.AddWriter(os.Stdout) | ||
| authLog.AddWriter(log.File("logs/tokenapi.log")) | ||
|
|
||
| // Login | ||
| Main(app, authLog) | ||
| } |
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,50 @@ | ||
| package tokenapi | ||
|
|
||
| import ( | ||
| "github.com/aerogo/aero" | ||
| "github.com/aerogo/log" | ||
| "github.com/akyoto/uuid" | ||
| "github.com/animenotifier/notify.moe/arn" | ||
| "github.com/tidwall/gjson" | ||
|
AnnsAnns marked this conversation as resolved.
|
||
|
|
||
| "net/http" | ||
| ) | ||
|
|
||
| type TokenRequest struct { | ||
| Token uuid.UUID | ||
| User arn.User | ||
|
|
||
| JSON gjson.Result | ||
| } | ||
|
|
||
| func Main(app *aero.Application, log *log.Log) { | ||
| app.Post("/tokenapi", func(ctx aero.Context) error { | ||
| response, err := ctx.Request().Body().String() | ||
| if err != nil { | ||
|
AnnsAnns marked this conversation as resolved.
|
||
| return ctx.Error(http.StatusBadRequest, "Couldn't get body", err) | ||
| } | ||
| if !gjson.Valid(response) { | ||
| return ctx.Error(http.StatusBadRequest, "Couldn't parse JSON") | ||
| } | ||
|
|
||
| request := &TokenRequest{} | ||
| request.JSON = gjson.Parse(response) | ||
|
|
||
| token, err := uuid.Parse(request.JSON.Get("token").String()) | ||
| if err != nil { | ||
| return ctx.Error(http.StatusBadRequest, "Couldn't parse token", err) | ||
| } | ||
| request.Token = token | ||
| request.User = *GetUserFromToken(token) | ||
|
|
||
| action := request.JSON.Get("action").String() | ||
| if action == "UpdateAnime" { | ||
| err := AnimeUpdate(request) | ||
| if err != nil { | ||
| return ctx.Error(http.StatusBadRequest, "Error updating anime:", err) | ||
| } | ||
| } | ||
|
|
||
| return ctx.Error(http.StatusAccepted, "Processed request for", request.User.CleanNick()) | ||
| }) | ||
| } | ||
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.