You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
funcmoderateHandler(c echo.Context) error {
ctx:=c.Request().Context()
deferc.Request().Body.Close()
iferr:=verifyUserSession(c); err!=nil {
returnerr
}
livestreamID, err:=strconv.Atoi(c.Param("livestream_id"))
iferr!=nil {
returnecho.NewHTTPError(http.StatusBadRequest, "livestream_id in path must be integer")
}
// error already checkedsess, _:=session.Get(defaultSessionIDKey, c)
// existence already checkeduserID:=sess.Values[defaultUserIDKey].(int64)
varreq*ModerateRequestiferr:=json.NewDecoder(c.Request().Body).Decode(&req); err!=nil {
returnecho.NewHTTPError(http.StatusBadRequest, "failed to decode the request body as json")
}
tx, err:=dbConn.BeginTxx(ctx, nil)
iferr!=nil {
returnecho.NewHTTPError(http.StatusInternalServerError, "failed to begin transaction: "+err.Error())
}
defertx.Rollback()
// 配信者自身の配信に対するmoderateなのかを検証varownedLivestreams []LivestreamModeliferr:=tx.SelectContext(ctx, &ownedLivestreams, "SELECT * FROM livestreams WHERE id = ? AND user_id = ?", livestreamID, userID); err!=nil {
returnecho.NewHTTPError(http.StatusInternalServerError, "failed to get livestreams: "+err.Error())
}
iflen(ownedLivestreams) ==0 {
returnecho.NewHTTPError(http.StatusBadRequest, "A streamer can't moderate livestreams that other streamers own")
}
rs, err:=tx.NamedExecContext(ctx, "INSERT INTO ng_words(user_id, livestream_id, word, created_at) VALUES (:user_id, :livestream_id, :word, :created_at)", &NGWord{
UserID: int64(userID),
LivestreamID: int64(livestreamID),
Word: req.NGWord,
CreatedAt: time.Now().Unix(),
})
iferr!=nil {
returnecho.NewHTTPError(http.StatusInternalServerError, "failed to insert new NG word: "+err.Error())
}
wordID, err:=rs.LastInsertId()
iferr!=nil {
returnecho.NewHTTPError(http.StatusInternalServerError, "failed to get last inserted NG word id: "+err.Error())
}
varngwords []*NGWordiferr:=tx.SelectContext(ctx, &ngwords, "SELECT * FROM ng_words WHERE livestream_id = ?", livestreamID); err!=nil {
returnecho.NewHTTPError(http.StatusInternalServerError, "failed to get NG words: "+err.Error())
}
// NGワードにヒットする過去の投稿も全削除するfor_, ngword:=rangengwords {
// ライブコメント一覧取得varlivecomments []*LivecommentModeliferr:=tx.SelectContext(ctx, &livecomments, "SELECT * FROM livecomments"); err!=nil {
returnecho.NewHTTPError(http.StatusInternalServerError, "failed to get livecomments: "+err.Error())
}
for_, livecomment:=rangelivecomments {
query:=` DELETE FROM livecomments WHERE id = ? AND livestream_id = ? AND (SELECT COUNT(*) FROM (SELECT ? AS text) AS texts INNER JOIN (SELECT CONCAT('%', ?, '%') AS pattern) AS patterns ON texts.text LIKE patterns.pattern) >= 1; `if_, err:=tx.ExecContext(ctx, query, livecomment.ID, livestreamID, livecomment.Comment, ngword.Word); err!=nil {
returnecho.NewHTTPError(http.StatusInternalServerError, "failed to delete old livecomments that hit spams: "+err.Error())
}
}
}
iferr:=tx.Commit(); err!=nil {
returnecho.NewHTTPError(http.StatusInternalServerError, "failed to commit: "+err.Error())
}
returnc.JSON(http.StatusCreated, map[string]interface{}{
"word_id": wordID,
})
}
The text was updated successfully, but these errors were encountered:
ochicop-isucon13/home/isucon/webapp/go/livecomment_handler.go
Line 331 in 71aa8b4
The text was updated successfully, but these errors were encountered: