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 all 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
9 changes: 9 additions & 0 deletions infra/traq/traq.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"net/url"

"github.com/traPtitech/go-traq"
"github.com/traPtitech/knoQ/utils/random"
"golang.org/x/oauth2"
)
Expand Down Expand Up @@ -77,3 +78,11 @@ func (repo *TraQRepository) doRequest(token *oauth2.Token, req *http.Request) ([
}
return io.ReadAll(resp.Body)
}

func NewAPIClient(ctx context.Context, token *oauth2.Token) *traq.APIClient {
traqconf := traq.NewConfiguration()
conf := TraQDefaultConfig
traqconf.HTTPClient = conf.Client(ctx, token)
apiClient := traq.NewAPIClient(traqconf)
return apiClient
}
65 changes: 34 additions & 31 deletions infra/traq/user.go
Original file line number Diff line number Diff line change
@@ -1,67 +1,70 @@
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)
ctx := context.TODO()
apiClient := NewAPIClient(ctx, token)
userDetail, resp, err := apiClient.UserApi.GetUser(ctx, userID.String()).Execute()
if err != nil {
return nil, err
}
data, err := repo.doRequest(token, req)
err = handleStatusCode(resp.StatusCode)
if err != nil {
return nil, err
}

user := new(traq.User)
err = json.Unmarshal(data, &user)
return user, err
user := traq.User{
Id: userDetail.Id,
Name: userDetail.Name,
DisplayName: userDetail.DisplayName,
IconFileId: userDetail.IconFileId,
Bot: userDetail.Bot,
State: userDetail.State,
UpdatedAt: userDetail.UpdatedAt,
}
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))
func (repo *TraQRepository) GetUsers(token *oauth2.Token, includeSuspended bool) ([]traq.User, error) {
ctx := context.TODO()
apiClient := NewAPIClient(ctx, token)
users, resp, err := apiClient.UserApi.GetUsers(ctx).IncludeSuspended(includeSuspended).Execute()
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)
err = handleStatusCode(resp.StatusCode)
if err != nil {
return nil, err
}
data, err := repo.doRequest(token, req)
if err != nil {
return nil, err
}

users := make([]*traq.User, 0)
err = json.Unmarshal(data, &users)
return 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)
ctx := context.TODO()
apiClient := NewAPIClient(ctx, token)
userDetail, resp, err := apiClient.MeApi.GetMe(ctx).Execute()
if err != nil {
return nil, err
}
data, err := repo.doRequest(token, req)
err = handleStatusCode(resp.StatusCode)
if err != nil {
return nil, err
}
user := traq.User{
Id: userDetail.Id,
Name: userDetail.Name,
DisplayName: userDetail.DisplayName,
IconFileId: userDetail.IconFileId,
Bot: userDetail.Bot,
State: userDetail.State,
UpdatedAt: userDetail.UpdatedAt,
}

user := new(traq.User)
err = json.Unmarshal(data, &user)
return user, err
return &user, err
}
7 changes: 4 additions & 3 deletions repository/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,11 @@ func (repo *Repository) IsPrevilege(info *domain.ConInfo) bool {
return user.Privilege
}

func traQUserMap(users []*traq.User) map[uuid.UUID]*traq.User {
func traQUserMap(users []traq.User) map[uuid.UUID]*traq.User {
userMap := make(map[uuid.UUID]*traq.User)
for _, user := range users {
userMap[uuid.Must(uuid.FromString(user.GetId()))] = user
for _, u := range users {
user:=u
userMap[uuid.Must(uuid.FromString(user.GetId()))] = &user
}
return userMap
}
Expand Down
35 changes: 9 additions & 26 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)
}
configuration := traq.NewConfiguration()
apiClient := traq.NewAPIClient(configuration)

res, err := http.DefaultClient.Do(req)
xTRAQSignature := calcSignature(message, secret)
res, err := apiClient.WebhookApi.PostWebhook(context.TODO(), webhookID).
XTRAQChannelId(channelID).XTRAQSignature(xTRAQSignature).
Embed(int32(embed)).Body(message).Execute()
if err != nil {
return err
}
if res.StatusCode >= 400 {
return errors.New(http.StatusText(res.StatusCode))
}

return nil
}

Expand Down