From bed586bb05b444cc269ef761a16f6a9856a1a550 Mon Sep 17 00:00:00 2001 From: Chuan-Heng Hsiao Date: Wed, 2 Oct 2024 00:18:39 -0400 Subject: [PATCH] api.getUserPermInfo --- api/add_favorite_board.go | 2 +- api/create_article.go | 2 +- api/create_board.go | 2 +- api/create_comment.go | 2 +- api/delete_articles.go | 4 +-- api/edit_article.go | 2 +- api/get_article_blocks.go | 2 +- api/get_article_detail.go | 2 +- api/get_board_detail.go | 2 +- api/get_board_summary.go | 2 +- api/get_user_info.go | 43 ++++++++++++++----------- api/load_article_comments.go | 2 +- api/load_bottom_articles.go | 4 +-- api/load_class_boards.go | 2 +- api/load_general_articles.go | 4 +-- api/load_general_articles_by_keyword.go | 4 +-- api/reply_comments.go | 2 +- api/user_article_perm.go | 25 +++++++------- api/user_board_perm.go | 17 +++++----- api/user_perm_info.go | 26 +++++++++++++++ schema/user_perm_info.go | 2 ++ 21 files changed, 95 insertions(+), 58 deletions(-) create mode 100644 api/user_perm_info.go diff --git a/api/add_favorite_board.go b/api/add_favorite_board.go index 2e469ddd..af8288e4 100644 --- a/api/add_favorite_board.go +++ b/api/add_favorite_board.go @@ -48,7 +48,7 @@ func AddFavoriteBoard(remoteAddr string, userID bbs.UUserID, params interface{}, return nil, 500, err } - _, err = CheckUserBoardPermReadable(userID, boardID) + _, err = CheckUserBoardPermReadable(userID, boardID, c) if err != nil { return nil, 403, err } diff --git a/api/create_article.go b/api/create_article.go index 144f431c..66c23952 100644 --- a/api/create_article.go +++ b/api/create_article.go @@ -48,7 +48,7 @@ func CreateArticle(remoteAddr string, userID bbs.UUserID, params interface{}, pa return nil, 500, err } - err = CheckUserBoardPermPostable(userID, boardID) + err = CheckUserBoardPermPostable(userID, boardID, c) if err != nil { return nil, 403, err } diff --git a/api/create_board.go b/api/create_board.go index 32861ebe..9c682a5e 100644 --- a/api/create_board.go +++ b/api/create_board.go @@ -49,7 +49,7 @@ func CreateBoard(remoteAddr string, userID bbs.UUserID, params interface{}, path return nil, 400, ErrInvalidPath } - err = CheckUserBoardPermCreatable(userID) + err = CheckUserBoardPermCreatable(userID, c) if err != nil { return nil, 403, err } diff --git a/api/create_comment.go b/api/create_comment.go index 67649125..fa467a93 100644 --- a/api/create_comment.go +++ b/api/create_comment.go @@ -48,7 +48,7 @@ func CreateComment(remoteAddr string, userID bbs.UUserID, params interface{}, pa } articleID := thePath.FArticleID.ToArticleID() - err = CheckUserBoardPermPostable(userID, boardID) + err = CheckUserBoardPermPostable(userID, boardID, c) if err != nil { return nil, 403, err } diff --git a/api/delete_articles.go b/api/delete_articles.go index 8217e670..78b6a947 100644 --- a/api/delete_articles.go +++ b/api/delete_articles.go @@ -49,7 +49,7 @@ func DeleteArticles(remoteAddr string, userID bbs.UUserID, params interface{}, p return nil, 500, err } - userBoardPermReadable, err := CheckUserBoardPermReadable(userID, boardID) + userBoardPermReadable, err := CheckUserBoardPermReadable(userID, boardID, c) if err != nil { return nil, 403, err } @@ -59,7 +59,7 @@ func DeleteArticles(remoteAddr string, userID bbs.UUserID, params interface{}, p articleIDs = append(articleIDs, articleID.ToArticleID()) } - articlePermMap, err := CheckUserArticlesPermDeletable(userID, boardID, articleIDs, userBoardPermReadable) + articlePermMap, err := CheckUserArticlesPermDeletable(userID, boardID, articleIDs, userBoardPermReadable, c) if err != nil { return nil, 500, err } diff --git a/api/edit_article.go b/api/edit_article.go index 494818eb..2f1c66f3 100644 --- a/api/edit_article.go +++ b/api/edit_article.go @@ -59,7 +59,7 @@ func EditArticleDetail(remoteAddr string, userID bbs.UUserID, params interface{} articleID := thePath.FArticleID.ToArticleID() // check permission - err = CheckUserArticlePermEditable(userID, boardID, articleID, true) + err = CheckUserArticlePermEditable(userID, boardID, articleID, true, c) if err != nil { return nil, 403, err } diff --git a/api/get_article_blocks.go b/api/get_article_blocks.go index 2fd69c76..81db9664 100644 --- a/api/get_article_blocks.go +++ b/api/get_article_blocks.go @@ -77,7 +77,7 @@ func GetArticleBlocks(remoteAddr string, userID bbs.UUserID, params interface{}, articleID := thePath.FArticleID.ToArticleID() // check permission - err = CheckUserArticlePermReadable(userID, boardID, articleID, true) + err = CheckUserArticlePermReadable(userID, boardID, articleID, true, c) if err != nil { return nil, 403, err } diff --git a/api/get_article_detail.go b/api/get_article_detail.go index 5b81ba11..36e64928 100644 --- a/api/get_article_detail.go +++ b/api/get_article_detail.go @@ -69,7 +69,7 @@ func GetArticleDetail(remoteAddr string, userID bbs.UUserID, params interface{}, articleID := thePath.FArticleID.ToArticleID() // check permission - err = CheckUserArticlePermReadable(userID, boardID, articleID, true) + err = CheckUserArticlePermReadable(userID, boardID, articleID, true, c) if err != nil { return nil, 403, err } diff --git a/api/get_board_detail.go b/api/get_board_detail.go index d25feb85..addb17a2 100644 --- a/api/get_board_detail.go +++ b/api/get_board_detail.go @@ -44,7 +44,7 @@ func GetBoardDetail(remoteAddr string, userID bbs.UUserID, params interface{}, p } // is board-valid-user - _, err = CheckUserBoardPermReadable(userID, boardID) + _, err = CheckUserBoardPermReadable(userID, boardID, c) if err != nil { return nil, 403, err } diff --git a/api/get_board_summary.go b/api/get_board_summary.go index 7fe55e95..3cd36ee5 100644 --- a/api/get_board_summary.go +++ b/api/get_board_summary.go @@ -36,7 +36,7 @@ func GetBoardSummary(remoteAddr string, userID bbs.UUserID, params interface{}, return nil, 400, err } - _, err = CheckUserBoardPermReadable(userID, boardID) + _, err = CheckUserBoardPermReadable(userID, boardID, c) if err != nil { return nil, 403, err } diff --git a/api/get_user_info.go b/api/get_user_info.go index b51452e2..e5ccc7f6 100644 --- a/api/get_user_info.go +++ b/api/get_user_info.go @@ -116,11 +116,7 @@ func GetUserInfo(remoteAddr string, userID bbs.UUserID, params interface{}, path return nil, 400, ErrInvalidPath } - return tryGetUserInfo(userID, thePath.UserID, c) -} - -func tryGetUserInfo(userID bbs.UUserID, queryUserID bbs.UUserID, c *gin.Context) (result *GetUserInfoResult, statusCode int, err error) { - userPermInfo, err := schema.GetUserPermInfo(userID) + userPermInfo, err := getUserPermInfo(userID, c) if err != nil { return nil, 500, err } @@ -130,23 +126,12 @@ func tryGetUserInfo(userID bbs.UUserID, queryUserID bbs.UUserID, c *gin.Context) updateNanoTS := types.NowNanoTS() - // get backend data - var result_b pttbbsapi.GetUserResult - - urlMap := map[string]string{ - "uid": string(queryUserID), - } - url := utils.MergeURL(urlMap, pttbbsapi.GET_USER_R) - - statusCode, err = utils.BackendGet(c, url, nil, nil, &result_b) + userDetail, statusCode, err := tryGetUserInfo(userID, thePath.UserID, updateNanoTS, c) if err != nil { return nil, statusCode, err } - userDetail, err := deserializeUserDetailAndUpdateDB(result_b, updateNanoTS) - if err != nil { - return nil, 500, err - } + queryUserID := thePath.UserID userNewInfo, err := schema.GetUserNewInfo(queryUserID) if err != nil { @@ -168,6 +153,28 @@ func tryGetUserInfo(userID bbs.UUserID, queryUserID bbs.UUserID, c *gin.Context) return result, 200, nil } +func tryGetUserInfo(userID bbs.UUserID, queryUserID bbs.UUserID, updateNanoTS types.NanoTS, c *gin.Context) (userDetail *schema.UserDetail, statusCode int, err error) { + // get backend data + var result_b pttbbsapi.GetUserResult + + urlMap := map[string]string{ + "uid": string(queryUserID), + } + url := utils.MergeURL(urlMap, pttbbsapi.GET_USER_R) + + statusCode, err = utils.BackendGet(c, url, nil, nil, &result_b) + if err != nil { + return nil, statusCode, err + } + + userDetail, err = deserializeUserDetailAndUpdateDB(result_b, updateNanoTS) + if err != nil { + return nil, 500, err + } + + return userDetail, 0, nil +} + func NewUserInfoResult(userDetail_db *schema.UserDetail, userNewInfo_db *schema.UserNewInfo, userIDEmail_db *schema.UserIDEmail, userEmail_db *schema.UserEmail, userPermInfo *schema.UserPermInfo) (result *GetUserInfoResult) { if userNewInfo_db == nil { userNewInfo_db = &schema.UserNewInfo{} diff --git a/api/load_article_comments.go b/api/load_article_comments.go index 711303d2..101a1b32 100644 --- a/api/load_article_comments.go +++ b/api/load_article_comments.go @@ -58,7 +58,7 @@ func LoadArticleComments(remoteAddr string, userID bbs.UUserID, params interface articleID := thePath.FArticleID.ToArticleID() // check permission - err = CheckUserArticlePermReadable(userID, boardID, articleID, true) + err = CheckUserArticlePermReadable(userID, boardID, articleID, true, c) if err != nil { return nil, statusCode, err } diff --git a/api/load_bottom_articles.go b/api/load_bottom_articles.go index c67ed1bc..662db808 100644 --- a/api/load_bottom_articles.go +++ b/api/load_bottom_articles.go @@ -35,7 +35,7 @@ func LoadBottomArticles(remoteAddr string, userID bbs.UUserID, params interface{ return nil, 500, err } - userBoardPerm, err := CheckUserBoardPermReadable(userID, boardID) + userBoardPerm, err := CheckUserBoardPermReadable(userID, boardID, c) if err != nil { return nil, 403, err } @@ -50,7 +50,7 @@ func LoadBottomArticles(remoteAddr string, userID bbs.UUserID, params interface{ articleIDs[idx] = each.ArticleID } - articlePermEditableMap, articlePermDeletableMap, err := CheckUserArticlesPermEditableDeletable(userID, boardID, articleIDs, userBoardPerm) + articlePermEditableMap, articlePermDeletableMap, err := CheckUserArticlesPermEditableDeletable(userID, boardID, articleIDs, userBoardPerm, c) if err != nil { return nil, 500, err } diff --git a/api/load_class_boards.go b/api/load_class_boards.go index 85d59850..04778cb7 100644 --- a/api/load_class_boards.go +++ b/api/load_class_boards.go @@ -59,7 +59,7 @@ func LoadClassBoards(remoteAddr string, userID bbs.UUserID, params interface{}, } // is board-valid-user - _, err = CheckUserBoardPermReadable(userID, boardID) + _, err = CheckUserBoardPermReadable(userID, boardID, c) if err != nil { return nil, statusCode, err } diff --git a/api/load_general_articles.go b/api/load_general_articles.go index 9ef4fbed..466776c3 100644 --- a/api/load_general_articles.go +++ b/api/load_general_articles.go @@ -62,7 +62,7 @@ func LoadGeneralArticles(remoteAddr string, userID bbs.UUserID, params interface } // check board permission - userBoardPerm, err := CheckUserBoardPermReadable(userID, boardID) + userBoardPerm, err := CheckUserBoardPermReadable(userID, boardID, c) if err != nil { return nil, 403, err } @@ -79,7 +79,7 @@ func LoadGeneralArticles(remoteAddr string, userID bbs.UUserID, params interface } // check article permission - articlePermEditableMap, articlePermDeletableMap, err := CheckUserArticlesPermEditableDeletable(userID, boardID, articleIDs, userBoardPerm) + articlePermEditableMap, articlePermDeletableMap, err := CheckUserArticlesPermEditableDeletable(userID, boardID, articleIDs, userBoardPerm, c) if err != nil { return nil, 500, err } diff --git a/api/load_general_articles_by_keyword.go b/api/load_general_articles_by_keyword.go index e164b5da..88f97307 100644 --- a/api/load_general_articles_by_keyword.go +++ b/api/load_general_articles_by_keyword.go @@ -25,7 +25,7 @@ func LoadGeneralArticlesByKeyword(remoteAddr string, userID bbs.UUserID, params } // check board permission - userBoardPerm, err := CheckUserBoardPermReadable(userID, boardID) + userBoardPerm, err := CheckUserBoardPermReadable(userID, boardID, c) if err != nil { return nil, 403, err } @@ -55,7 +55,7 @@ func LoadGeneralArticlesByKeyword(remoteAddr string, userID bbs.UUserID, params } // check article permission - articlePermEditableMap, articlePermDeletableMap, err := CheckUserArticlesPermEditableDeletable(userID, boardID, articleIDs, userBoardPerm) + articlePermEditableMap, articlePermDeletableMap, err := CheckUserArticlesPermEditableDeletable(userID, boardID, articleIDs, userBoardPerm, c) if err != nil { return nil, 500, err } diff --git a/api/reply_comments.go b/api/reply_comments.go index 0178c966..c0fca79c 100644 --- a/api/reply_comments.go +++ b/api/reply_comments.go @@ -49,7 +49,7 @@ func ReplyComments(remoteAddr string, userID bbs.UUserID, params interface{}, pa articleID := thePath.FArticleID.ToArticleID() // check permission - err = CheckUserArticlePermReadable(userID, boardID, articleID, true) + err = CheckUserArticlePermReadable(userID, boardID, articleID, true, c) if err != nil { return nil, 403, err } diff --git a/api/user_article_perm.go b/api/user_article_perm.go index 97ba65ec..71404da3 100644 --- a/api/user_article_perm.go +++ b/api/user_article_perm.go @@ -3,14 +3,15 @@ package api import ( "github.com/Ptt-official-app/go-pttbbs/bbs" "github.com/Ptt-official-app/go-pttbbsweb/schema" + "github.com/gin-gonic/gin" ) // CheckUserArticlePermReadable // // Readable -func CheckUserArticlePermReadable(userID bbs.UUserID, boardID bbs.BBoardID, articleID bbs.ArticleID, isCheckBoard bool) (err error) { +func CheckUserArticlePermReadable(userID bbs.UUserID, boardID bbs.BBoardID, articleID bbs.ArticleID, isCheckBoard bool, c *gin.Context) (err error) { if isCheckBoard { - _, err = CheckUserBoardPermReadable(userID, boardID) + _, err = CheckUserBoardPermReadable(userID, boardID, c) if err != nil { return err } @@ -35,9 +36,9 @@ func CheckUserArticlePermReadable(userID bbs.UUserID, boardID bbs.BBoardID, arti // CheckUserArticlePermEditable // // Editable -func CheckUserArticlePermEditable(userID bbs.UUserID, boardID bbs.BBoardID, articleID bbs.ArticleID, isCheckBoard bool) (err error) { +func CheckUserArticlePermEditable(userID bbs.UUserID, boardID bbs.BBoardID, articleID bbs.ArticleID, isCheckBoard bool, c *gin.Context) (err error) { if isCheckBoard { - _, err = CheckUserBoardPermReadable(userID, boardID) + _, err = CheckUserBoardPermReadable(userID, boardID, c) if err != nil { return err } @@ -69,9 +70,9 @@ func checkUserArticlePermEditableCore(userID bbs.UUserID, boardID bbs.BBoardID, // CheckUserArticlesPermEditable // // articles Editable -func CheckUserArticlesPermEditable(userID bbs.UUserID, boardID bbs.BBoardID, articleIDs []bbs.ArticleID, userBoardPerm *UserBoardPermReadable) (articlePermMap map[bbs.ArticleID]error, err error) { +func CheckUserArticlesPermEditable(userID bbs.UUserID, boardID bbs.BBoardID, articleIDs []bbs.ArticleID, userBoardPerm *UserBoardPermReadable, c *gin.Context) (articlePermMap map[bbs.ArticleID]error, err error) { if userBoardPerm == nil { - userBoardPerm, err = CheckUserBoardPermReadable(userID, boardID) + userBoardPerm, err = CheckUserBoardPermReadable(userID, boardID, c) if err != nil { return nil, err } @@ -113,8 +114,8 @@ func checkUserArticlesPermEditableCore(userID bbs.UUserID, articlesPermInfo []*s // CheckUserArticlePermDeletable // // Deletable -func CheckUserArticlePermDeletable(userID bbs.UUserID, boardID bbs.BBoardID, articleID bbs.ArticleID) (err error) { - userBoardPermReadable, err := CheckUserBoardPermReadable(userID, boardID) +func CheckUserArticlePermDeletable(userID bbs.UUserID, boardID bbs.BBoardID, articleID bbs.ArticleID, c *gin.Context) (err error) { + userBoardPermReadable, err := CheckUserBoardPermReadable(userID, boardID, c) if err != nil { return err } @@ -153,9 +154,9 @@ func checkUserArticlePermDeletableCore(userID bbs.UUserID, boardID bbs.BBoardID, // CheckUserArticlePermDeletable // // Deletable -func CheckUserArticlesPermDeletable(userID bbs.UUserID, boardID bbs.BBoardID, articleIDs []bbs.ArticleID, userBoardPermReadable *UserBoardPermReadable) (articlePermMap map[bbs.ArticleID]error, err error) { +func CheckUserArticlesPermDeletable(userID bbs.UUserID, boardID bbs.BBoardID, articleIDs []bbs.ArticleID, userBoardPermReadable *UserBoardPermReadable, c *gin.Context) (articlePermMap map[bbs.ArticleID]error, err error) { if userBoardPermReadable == nil { - userBoardPermReadable, err = CheckUserBoardPermReadable(userID, boardID) + userBoardPermReadable, err = CheckUserBoardPermReadable(userID, boardID, c) if err != nil { return nil, err } @@ -200,9 +201,9 @@ func checkUserArticlesPermDeletableCore(userID bbs.UUserID, articlesPermInfo []* // CheckUserArticlesPermEditable // // articles Editable -func CheckUserArticlesPermEditableDeletable(userID bbs.UUserID, boardID bbs.BBoardID, articleIDs []bbs.ArticleID, userBoardPerm *UserBoardPermReadable) (articlePermEditableMap map[bbs.ArticleID]error, articlePermDeletableMap map[bbs.ArticleID]error, err error) { +func CheckUserArticlesPermEditableDeletable(userID bbs.UUserID, boardID bbs.BBoardID, articleIDs []bbs.ArticleID, userBoardPerm *UserBoardPermReadable, c *gin.Context) (articlePermEditableMap map[bbs.ArticleID]error, articlePermDeletableMap map[bbs.ArticleID]error, err error) { if userBoardPerm == nil { - userBoardPerm, err = CheckUserBoardPermReadable(userID, boardID) + userBoardPerm, err = CheckUserBoardPermReadable(userID, boardID, c) if err != nil { return nil, nil, err } diff --git a/api/user_board_perm.go b/api/user_board_perm.go index 01f37a05..7a0b6026 100644 --- a/api/user_board_perm.go +++ b/api/user_board_perm.go @@ -5,6 +5,7 @@ import ( "github.com/Ptt-official-app/go-pttbbs/ptttype" "github.com/Ptt-official-app/go-pttbbsweb/schema" "github.com/Ptt-official-app/go-pttbbsweb/types" + "github.com/gin-gonic/gin" ) type UserBoardPermReadable struct { @@ -30,8 +31,8 @@ var COOLDOWN_LIMIT = []*CooldownLimit{ } // CheckUserBoardPermCreatable -func CheckUserBoardPermCreatable(userID bbs.UUserID) (err error) { - userPermInfo, err := schema.GetUserPermInfo(userID) +func CheckUserBoardPermCreatable(userID bbs.UUserID, c *gin.Context) (err error) { + userPermInfo, err := getUserPermInfo(userID, c) if err != nil { return err } @@ -57,8 +58,8 @@ func checkUserBoardPermCreatableCore(userPermInfo *schema.UserPermInfo) (err err // CheckUserBoardPermReadable // // https://github.com/ptt/pttbbs/blob/master/mbbsd/board.c#L185 -func CheckUserBoardPermReadable(userID bbs.UUserID, boardID bbs.BBoardID) (userBoardPerm *UserBoardPermReadable, err error) { - userPermInfo, err := schema.GetUserPermInfo(userID) +func CheckUserBoardPermReadable(userID bbs.UUserID, boardID bbs.BBoardID, c *gin.Context) (userBoardPerm *UserBoardPermReadable, err error) { + userPermInfo, err := getUserPermInfo(userID, c) if err != nil { return nil, err } @@ -148,8 +149,8 @@ func checkUserBoardPermReadableCore(userID bbs.UUserID, boardID bbs.BBoardID, us // CheckUserBoardPermPostable // // https://github.com/ptt/pttbbs/blob/master/mbbsd/cache.c#L209 -func CheckUserBoardPermPostable(userID bbs.UUserID, boardID bbs.BBoardID) (err error) { - userPermInfo, err := schema.GetUserPermInfo(userID) +func CheckUserBoardPermPostable(userID bbs.UUserID, boardID bbs.BBoardID, c *gin.Context) (err error) { + userPermInfo, err := getUserPermInfo(userID, c) if err != nil { return err } @@ -260,8 +261,8 @@ func checkUserBoardPermPostableCore(userID bbs.UUserID, boardID bbs.BBoardID, us } // CheckUserBoardPermEditable -func CheckUserBoardPermEditable(userID bbs.UUserID, boardID bbs.BBoardID) (err error) { - userPermInfo, err := schema.GetUserPermInfo(userID) +func CheckUserBoardPermEditable(userID bbs.UUserID, boardID bbs.BBoardID, c *gin.Context) (err error) { + userPermInfo, err := getUserPermInfo(userID, c) if err != nil { return err } diff --git a/api/user_perm_info.go b/api/user_perm_info.go new file mode 100644 index 00000000..7ad6cdd0 --- /dev/null +++ b/api/user_perm_info.go @@ -0,0 +1,26 @@ +package api + +import ( + "github.com/Ptt-official-app/go-pttbbs/bbs" + "github.com/Ptt-official-app/go-pttbbsweb/schema" + "github.com/Ptt-official-app/go-pttbbsweb/types" + "github.com/gin-gonic/gin" +) + +func getUserPermInfo(userID bbs.UUserID, c *gin.Context) (userPermInfo *schema.UserPermInfo, err error) { + userPermInfo, err = schema.GetUserPermInfo(userID) + if err != nil { + return nil, err + } + if userPermInfo != nil { + return userPermInfo, nil + } + + updateNanoTS := types.NowNanoTS() + _, _, err = tryGetUserInfo(userID, userID, updateNanoTS, c) + if err != nil { + return nil, err + } + + return schema.GetUserPermInfo(userID) +} diff --git a/schema/user_perm_info.go b/schema/user_perm_info.go index c94c79fc..54b9c476 100644 --- a/schema/user_perm_info.go +++ b/schema/user_perm_info.go @@ -22,6 +22,8 @@ type UserPermInfo struct { CooldownNanoTS types.NanoTS `bson:"cooldown_nano_ts"` Posttime int `bson:"postitme"` + + UpdateNanoTS types.NanoTS `bson:"update_nano_ts"` } var (