Skip to content

Commit

Permalink
livestram関連エンドポイント修正
Browse files Browse the repository at this point in the history
  • Loading branch information
tukeJonny committed Nov 1, 2023
1 parent 830b698 commit 94f6aa9
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 16 deletions.
43 changes: 42 additions & 1 deletion bench/isupipe/client_livecomment.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ type ModerateRequest struct {
NGWord string `json:"ng_word"`
}

type NGWord struct {
ID int64 `json:"id"`
UserID int64 `json:"user_id"`
LivestreamID int64 `json:"livestream_id"`
Word string `json:"word"`
CreatedAt int64 `json:"created_at"`
}

func (c *Client) GetLivecomments(ctx context.Context, livestreamID int, opts ...ClientOption) ([]Livecomment, error) {
var (
defaultStatusCode = http.StatusOK
Expand Down Expand Up @@ -120,7 +128,40 @@ func (c *Client) GetLivecommentReports(ctx context.Context, livestreamID int, op
}

// FIXME: 実装
// func (c *Client) GetNgwords(ctx context.Context)
func (c *Client) GetNgwords(ctx context.Context, livestreamID int64, opts ...ClientOption) ([]*NGWord, error) {
var (
defaultStatusCode = http.StatusOK
o = newClientOptions(defaultStatusCode, opts...)
)

urlPath := fmt.Sprintf("/api/livestream/%d/ngwords", livestreamID)
req, err := c.agent.NewRequest(http.MethodPost, urlPath, nil)
if err != nil {
return nil, bencherror.NewInternalError(err)
}

resp, err := sendRequest(ctx, c.agent, req)
if err != nil {
return nil, err
}
defer func() {
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}()

if resp.StatusCode != o.wantStatusCode {
return nil, bencherror.NewHttpStatusError(req, o.wantStatusCode, resp.StatusCode)
}

var ngwords []*NGWord
if resp.StatusCode == defaultStatusCode {
if err := json.NewDecoder(resp.Body).Decode(&ngwords); err != nil {
return nil, bencherror.NewHttpResponseError(err, req)
}
}

return ngwords, nil
}

func (c *Client) PostLivecomment(ctx context.Context, livestreamID int, r *PostLivecommentRequest, opts ...ClientOption) (*PostLivecommentResponse, error) {
var (
Expand Down
43 changes: 39 additions & 4 deletions bench/isupipe/client_livestream.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (c *Client) GetLivestream(
return nil
}

func (c *Client) GetLivestreams(
func (c *Client) SearchLivestreams(
ctx context.Context,
opts ...ClientOption,
) ([]*Livestream, error) {
Expand All @@ -77,7 +77,7 @@ func (c *Client) GetLivestreams(
o = newClientOptions(defaultStatusCode, opts...)
)

req, err := c.agent.NewRequest(http.MethodGet, "/api/livestream", nil)
req, err := c.agent.NewRequest(http.MethodGet, "/api/livestream/search", nil)
if err != nil {
return nil, bencherror.NewInternalError(err)
}
Expand Down Expand Up @@ -105,7 +105,42 @@ func (c *Client) GetLivestreams(
return livestreams, nil
}

func (c *Client) GetLivestreamsByTag(
// 自分のライブ配信一覧取得
func (c *Client) GetLivestreams(ctx context.Context, opts ...ClientOption) ([]*Livestream, error) {
var (
defaultStatusCode = http.StatusOK
o = newClientOptions(defaultStatusCode, opts...)
)

req, err := c.agent.NewRequest(http.MethodGet, "/api/livestream", nil)
if err != nil {
return nil, bencherror.NewInternalError(err)
}

resp, err := sendRequest(ctx, c.agent, req)
if err != nil {
return nil, err
}
defer func() {
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}()

if resp.StatusCode != o.wantStatusCode {
return nil, bencherror.NewHttpStatusError(req, o.wantStatusCode, resp.StatusCode)
}

var livestreams []*Livestream
if resp.StatusCode == defaultStatusCode {
if err := json.NewDecoder(resp.Body).Decode(&livestreams); err != nil {
return nil, bencherror.NewHttpResponseError(err, req)
}
}

return livestreams, nil
}

func (c *Client) SearchLivestreamsByTag(
ctx context.Context,
tag string,
opts ...ClientOption,
Expand All @@ -115,7 +150,7 @@ func (c *Client) GetLivestreamsByTag(
o = newClientOptions(defaultStatusCode, opts...)
)

req, err := c.agent.NewRequest(http.MethodGet, "/api/livestream", nil)
req, err := c.agent.NewRequest(http.MethodGet, "/api/livestream/search", nil)
if err != nil {
return bencherror.NewInternalError(err)
}
Expand Down
2 changes: 1 addition & 1 deletion bench/scenario/core_pretest.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func Pretest(ctx context.Context, client *isupipe.Client) error {
return err
}

if err := client.GetLivestreamsByTag(ctx, "椅子" /* tag name */); err != nil {
if err := client.SearchLivestreamsByTag(ctx, "椅子" /* tag name */); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions bench/scenario/visit_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func VisitTop(ctx context.Context, client *isupipe.Client) error {
// FIXME: プロフィールアイコン取得

// FIXME: 10件程度ライブストリーム取得
_, err := client.GetLivestreams(ctx)
_, err := client.SearchLivestreams(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -41,7 +41,7 @@ func VisitLivestreamAdmin(ctx context.Context, client *isupipe.Client) error {

// ライブコメント一覧取得
// FIXME: 自分のライブストリーム一覧を取ってくる必要がある
_, err := client.GetLivestreams(ctx)
_, err := client.SearchLivestreams(ctx)
if err != nil {
return err
}
Expand Down
51 changes: 44 additions & 7 deletions webapp/go/livestream_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func reserveLivestreamHandler(c echo.Context) error {
return c.JSON(http.StatusCreated, livestream)
}

func getLivestreamsHandler(c echo.Context) error {
func searchLivestreamsHandler(c echo.Context) error {
ctx := c.Request().Context()
keyTagName := c.QueryParam("tag")

Expand All @@ -210,13 +210,9 @@ func getLivestreamsHandler(c echo.Context) error {
}
defer tx.Rollback()

// 複数件取得
var livestreamModels []*LivestreamModel
if keyTagName == "" {
if err := tx.SelectContext(ctx, &livestreamModels, "SELECT * FROM livestreams"); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
} else {
if c.QueryParam("tag") != "" {
// タグによる取得
var tagIDList []int
if err := tx.SelectContext(ctx, &tagIDList, "SELECT id FROM tags WHERE name = ?", keyTagName); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
Expand All @@ -239,6 +235,20 @@ func getLivestreamsHandler(c echo.Context) error {

livestreamModels = append(livestreamModels, &ls)
}
} else {
// 検索条件なし
query := `SELECT * FROM livestreams`
if c.QueryParam("limit") != "" {
limit, err := strconv.Atoi(c.QueryParam("limit"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
query += fmt.Sprintf(" LIMIT %d", limit)
}

if err := tx.SelectContext(ctx, &livestreamModels, query); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
}

livestreams := make([]Livestream, len(livestreamModels))
Expand All @@ -257,6 +267,33 @@ func getLivestreamsHandler(c echo.Context) error {
return c.JSON(http.StatusOK, livestreams)
}

func getUserLivestreamsHandler(c echo.Context) error {
ctx := c.Request().Context()

tx, err := dbConn.BeginTxx(ctx, nil)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
defer tx.Rollback()

sess, err := session.Get(defaultSessionIDKey, c)
if err != nil {
return echo.NewHTTPError(http.StatusUnauthorized, err.Error())
}

userID, ok := sess.Values[defaultUserIDKey].(int64)
if !ok {
return echo.NewHTTPError(http.StatusUnauthorized, "failed to find user-id from session")
}

var livestreams []*LivestreamModel
if err := tx.SelectContext(ctx, &livestreams, "SELECT * FROM livestreams WHERE user_id = ?", userID); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

return c.JSON(http.StatusOK, livestreams)
}

// viewerテーブルの廃止
func enterLivestreamHandler(c echo.Context) error {
ctx := c.Request().Context()
Expand Down
3 changes: 2 additions & 1 deletion webapp/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ func main() {
// reserve livestream
e.POST("/api/livestream/reservation", reserveLivestreamHandler)
// list livestream
e.GET("/api/livestream", getLivestreamsHandler)
e.GET("/api/livestream/search", searchLivestreamsHandler)
e.GET("/api/livestream", getUserLivestreamsHandler)
// get livestream
e.GET("/api/livestream/:livestream_id", getLivestreamHandler)
// get polling livecomment timeline
Expand Down

0 comments on commit 94f6aa9

Please sign in to comment.