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
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 52 additions & 38 deletions infra/traq/user.go
Original file line number Diff line number Diff line change
@@ -1,67 +1,81 @@
package traq

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"context"

"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)
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も共通だと思うのでメソッドにしてよさそうです

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クライアントを作るときに使ったものと同じものを使ってほしいです!

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()

if err != nil {
return nil, err
}
data, err := repo.doRequest(token, req)
if err != nil {
return nil, err
}

user := new(traq.User)
err = json.Unmarshal(data, &user)
user := convertUserdetailToUser(userDtail)
return user, err
}

func (repo *TraQRepository) GetUsers(token *oauth2.Token, includeSuspended bool) ([]*traq.User, error) {
URL, err := url.Parse(fmt.Sprintf("%s/users", repo.URL))
if err != nil {
return nil, err
}
q := URL.Query()
q.Set("include-suspended", strconv.FormatBool(includeSuspended))
URL.RawQuery = q.Encode()
req, err := http.NewRequest(http.MethodGet, URL.String(), nil)
if err != nil {
return nil, err
}
data, err := repo.doRequest(token, req)
traqconf := traq.NewConfiguration()
conf := TraQDefaultConfig
traqconf.HTTPClient = conf.Client(context.Background(), token)
apiClient := traq.NewAPIClient(traqconf)
users, _, err := apiClient.UserApi.GetUsers(context.Background()).IncludeSuspended(includeSuspended).Execute()
if err != nil {
return nil, err
}

users := make([]*traq.User, 0)
err = json.Unmarshal(data, &users)
return users, err
res_users := convertUsersToUsers(users)
return res_users, err
}

func (repo *TraQRepository) GetUserMe(token *oauth2.Token) (*traq.User, error) {
URL := fmt.Sprintf("%s/users/me", repo.URL)
req, err := http.NewRequest(http.MethodGet, URL, nil)
if err != nil {
return nil, err
}
data, err := repo.doRequest(token, req)
traqconf := traq.NewConfiguration()
conf := TraQDefaultConfig
traqconf.HTTPClient = conf.Client(context.Background(), token)
client := traq.NewAPIClient(traqconf)

data, _, err := client.MeApi.GetMe(context.Background()).Execute()
if err != nil {
return nil, err
}
user := convertMyUserdetailToUser(data)
return user, err
}

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にしちゃってよさそう

33 changes: 8 additions & 25 deletions utils/api.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,32 @@
package utils

import (
"context"
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"errors"
"net/http"
"net/url"
"path"
"strconv"
"strings"

"github.com/labstack/echo/v4"
"github.com/traPtitech/go-traq"
)

const baseURL = "https://q.trap.jp/api/v3"


// RequestWebhook q.trap/jp にメッセージを送信します。
func RequestWebhook(message, secret, channelID, webhookID string, embed int) error {
u, err := url.Parse(baseURL + "/webhooks")
if err != nil {
return err
}
u.Path = path.Join(u.Path, webhookID)
query := u.Query()
query.Set("embed", strconv.Itoa(embed))
u.RawQuery = query.Encode()

req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(message))
if err != nil {
return err
}
req.Header.Set(echo.HeaderContentType, echo.MIMETextPlain)
req.Header.Set("X-TRAQ-Signature", calcSignature(message, secret))
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の行の手前とかに定義すると読みやすいと思います

configuration := traq.NewConfiguration()
apiClient := traq.NewAPIClient(configuration)

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.

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

if err != nil {
return err
}
if res.StatusCode >= 400 {
return errors.New(http.StatusText(res.StatusCode))
}

return nil
}

Expand Down