Skip to content
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

Fix/issue439 userAPI #479

Merged
merged 20 commits into from
Dec 25, 2023
Merged

Fix/issue439 userAPI #479

merged 20 commits into from
Dec 25, 2023

Conversation

Nzt3-gh
Copy link
Contributor

@Nzt3-gh Nzt3-gh commented Oct 26, 2023

infra/traq/user.go のAPIを呼んでいる部分をgo traqを用いたコードに書き換えました。

@Nzt3-gh Nzt3-gh linked an issue Oct 26, 2023 that may be closed by this pull request
@ras0q
Copy link
Member

ras0q commented Oct 26, 2023

これを実行するとコードがフォーマットされるのでまずはこれを実行してほしいです:pray:
https://github.com/traPtitech/knoQ?tab=readme-ov-file#%E3%82%B3%E3%83%BC%E3%83%89%E3%83%95%E3%82%A9%E3%83%BC%E3%83%9E%E3%83%83%E3%83%88

Copy link
Member

@ras0q ras0q left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます!いくつかレビューしました!

Comment on lines 12 to 15
traqconf := traq.NewConfiguration()
conf := TraQDefaultConfig
traqconf.HTTPClient = conf.Client(context.Background(), token)
apiClient := traq.NewAPIClient(traqconf)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここは多分どのAPIも共通だと思うのでメソッドにしてよさそうです

conf := TraQDefaultConfig
traqconf.HTTPClient = conf.Client(context.Background(), token)
apiClient := traq.NewAPIClient(traqconf)
userDtail, _, err := apiClient.UserApi.GetUser(context.Background(), userID.String()).Execute()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここのcontextはAPIクライアントを作るときに使ったものと同じものを使ってほしいです!

utils/api.go Outdated

res, err := http.DefaultClient.Do(req)
res, err := apiClient.WebhookApi.PostWebhook(context.Background(), webhookID).XTRAQChannelId(channelID).XTRAQSignature(xTRAQSignature).Embed(int32(embed)).Body(message).Execute()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あまり行が長くなりそうなら分割すると見やすそうです

conf := TraQDefaultConfig
traqconf.HTTPClient = conf.Client(context.Background(), token)
apiClient := traq.NewAPIClient(traqconf)
userDtail, _, err := apiClient.UserApi.GetUser(context.Background(), userID.String()).Execute()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
userDtail, _, err := apiClient.UserApi.GetUser(context.Background(), userID.String()).Execute()
userDetail, _, err := apiClient.UserApi.GetUser(context.Background(), userID.String()).Execute()

Comment on lines 51 to 81
func convertMyUserdetailToUser(userdetail *traq.MyUserDetail) *traq.User {
user := new(traq.User)
err = json.Unmarshal(data, &user)
return user, err
user.Id = userdetail.Id
user.Name = userdetail.Name
user.DisplayName = userdetail.DisplayName
user.IconFileId = userdetail.IconFileId
user.Bot = userdetail.Bot
user.State = userdetail.State
user.UpdatedAt = userdetail.UpdatedAt
return user
}

func convertUserdetailToUser(userdetail *traq.UserDetail) *traq.User {
user := new(traq.User)
user.Id = userdetail.Id
user.Name = userdetail.Name
user.DisplayName = userdetail.DisplayName
user.IconFileId = userdetail.IconFileId
user.Bot = userdetail.Bot
user.State = userdetail.State
user.UpdatedAt = userdetail.UpdatedAt
return user
}
func convertUsersToUsers(users []traq.User) []*traq.User {
new_users := make([]*traq.User, len(users))
for i, _user := range users {
user := _user
new_users[i] = &user
}
return new_users
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これくらいならconvert関数かかなくてもよさそうな気がします
GetUsersの返り値も[]traq.Userにしちゃってよさそう

Copy link
Member

@ras0q ras0q left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

いくつかかきました!


"github.com/gofrs/uuid"
"github.com/traPtitech/go-traq"
"golang.org/x/oauth2"
)

func (repo *TraQRepository) GetUser(token *oauth2.Token, userID uuid.UUID) (*traq.User, error) {
URL := fmt.Sprintf("%s/users/%s", repo.URL, userID)
req, err := http.NewRequest(http.MethodGet, URL, nil)
apiClient := MakeApiClient(token)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここの引数でcontextを含めるとapiClientとリクエストで単一のcontextを使えるのでそうして欲しいです🙏

@@ -77,3 +78,11 @@ func (repo *TraQRepository) doRequest(token *oauth2.Token, req *http.Request) ([
}
return io.ReadAll(resp.Body)
}

func MakeApiClient(token *oauth2.Token, ctx context.Context) *traq.APIClient {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contextは第1引数に取るのが慣例なのでそうして欲しいです🙏

req, err := http.NewRequest(http.MethodGet, URL, nil)
ctx := context.TODO()
apiClient := MakeApiClient(token, ctx)
userDetail, _, err := apiClient.UserApi.GetUser(ctx, userID.String()).Execute()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

レスポンスのStatus Codeもチェックするといいと思います

Comment on lines 18 to 26
user := new(traq.User)
err = json.Unmarshal(data, &user)
user.Id = userDetail.Id
user.Name = userDetail.Name
user.DisplayName = userDetail.DisplayName
user.IconFileId = userDetail.IconFileId
user.Bot = userDetail.Bot
user.State = userDetail.State
user.UpdatedAt = userDetail.UpdatedAt
return user, err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

個人的には

user := traq.User {
  Id: userDetail.Id,
  // ...
}

で一気に書く方が見やすいと思います

utils/api.go Outdated

res, err := http.DefaultClient.Do(req)
res, err := apiClient.WebhookApi.PostWebhook(context.Background(), webhookID).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

動作変わらないのでどっちでもいいんですが他のところでcontext.TODO使ってくれてるので合わせると良さそう

utils/api.go Outdated
if channelID != "" {
req.Header.Set("X-TRAQ-Channel-Id", channelID)
}
xTRAQSignature := calcSignature(message, secret)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

定義から使用までが少し離れているのでPostWebhookの行の手前とかに定義すると読みやすいと思います

@@ -77,3 +78,11 @@ func (repo *TraQRepository) doRequest(token *oauth2.Token, req *http.Request) ([
}
return io.ReadAll(resp.Body)
}

func MakeApiClient(token *oauth2.Token, ctx context.Context) *traq.APIClient {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ApiよりもAPIの方が好まれるので直してもらいたいです🙏
ref: https://google.github.io/styleguide/go/decisions#initialisms

あとポインタを返すならMakeよりNewっぽい気がする(割とこの関数を頻繁につかうのでNewも100%しっくり来てるわけじゃないけどGetとかRetrieveもうーんって感じなのでNewでいいです)

@Nzt3-gh
Copy link
Contributor Author

Nzt3-gh commented Dec 20, 2023

修正したので確認をお願いします。

@Nzt3-gh Nzt3-gh marked this pull request as ready for review December 20, 2023 13:39
Copy link
Member

@ras0q ras0q left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

動作確認しました!よさそうです!

@Nzt3-gh Nzt3-gh merged commit 8715875 into main Dec 25, 2023
4 checks passed
@Nzt3-gh Nzt3-gh deleted the fix/issue439 branch December 25, 2023 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants