@@ -21,25 +21,28 @@ package schema
2121
2222import (
2323 "github.com/apache/answer/internal/base/constant"
24+ "github.com/apache/answer/internal/base/reason"
2425 "github.com/apache/answer/internal/entity"
26+ "github.com/segmentfault/pacman/errors"
2527)
2628
2729// SimpleObjectInfo simple object info
2830type SimpleObjectInfo struct {
29- ObjectID string `json:"object_id"`
30- ObjectCreatorUserID string `json:"object_creator_user_id"`
31- QuestionID string `json:"question_id"`
32- QuestionStatus int `json:"question_status"`
33- QuestionShow int `json:"question_show"`
34- AnswerID string `json:"answer_id"`
35- AnswerStatus int `json:"answer_status"`
36- CommentID string `json:"comment_id"`
37- CommentStatus int `json:"comment_status"`
38- TagID string `json:"tag_id"`
39- TagStatus int `json:"tag_status"`
40- ObjectType string `json:"object_type"`
41- Title string `json:"title"`
42- Content string `json:"content"`
31+ ObjectID string `json:"object_id"`
32+ ObjectCreatorUserID string `json:"object_creator_user_id"`
33+ QuestionID string `json:"question_id"`
34+ QuestionCreatorUserID string `json:"question_creator_user_id"`
35+ QuestionStatus int `json:"question_status"`
36+ QuestionShow int `json:"question_show"`
37+ AnswerID string `json:"answer_id"`
38+ AnswerStatus int `json:"answer_status"`
39+ CommentID string `json:"comment_id"`
40+ CommentStatus int `json:"comment_status"`
41+ TagID string `json:"tag_id"`
42+ TagStatus int `json:"tag_status"`
43+ ObjectType string `json:"object_type"`
44+ Title string `json:"title"`
45+ Content string `json:"content"`
4346}
4447
4548// IsDeleted is deleted
@@ -57,6 +60,88 @@ func (s *SimpleObjectInfo) IsDeleted() bool {
5760 return false
5861}
5962
63+ func (s * SimpleObjectInfo ) CheckVisibility (userID string , isAdminModerator bool ) error {
64+ if s == nil {
65+ return errors .NotFound (reason .ObjectNotFound )
66+ }
67+ if s .isObjectRestricted () && ! s .canViewObject (userID , isAdminModerator ) {
68+ return errors .NotFound (s .objectNotFoundReason ())
69+ }
70+ if s .hasParentQuestion () && s .isParentQuestionRestricted () &&
71+ ! s .canViewParentQuestion (userID , isAdminModerator ) {
72+ return errors .NotFound (reason .QuestionNotFound )
73+ }
74+ return nil
75+ }
76+
77+ func (s * SimpleObjectInfo ) canViewObject (userID string , isAdminModerator bool ) bool {
78+ if isAdminModerator {
79+ return true
80+ }
81+ switch s .ObjectType {
82+ case constant .QuestionObjectType :
83+ return s .QuestionCreatorUserID == userID
84+ case constant .AnswerObjectType , constant .CommentObjectType , constant .TagObjectType :
85+ return s .ObjectCreatorUserID == userID
86+ default :
87+ return false
88+ }
89+ }
90+
91+ func (s * SimpleObjectInfo ) canViewParentQuestion (userID string , isAdminModerator bool ) bool {
92+ if isAdminModerator {
93+ return true
94+ }
95+ return s .QuestionCreatorUserID == userID
96+ }
97+
98+ func (s * SimpleObjectInfo ) hasParentQuestion () bool {
99+ switch s .ObjectType {
100+ case constant .AnswerObjectType , constant .CommentObjectType :
101+ return len (s .QuestionID ) > 0 && s .QuestionID != "0"
102+ default :
103+ return false
104+ }
105+ }
106+
107+ func (s * SimpleObjectInfo ) isObjectRestricted () bool {
108+ switch s .ObjectType {
109+ case constant .QuestionObjectType :
110+ return s .QuestionStatus == entity .QuestionStatusDeleted ||
111+ s .QuestionStatus == entity .QuestionStatusPending ||
112+ s .QuestionShow == entity .QuestionHide
113+ case constant .AnswerObjectType :
114+ return s .AnswerStatus == entity .AnswerStatusDeleted || s .AnswerStatus == entity .AnswerStatusPending
115+ case constant .CommentObjectType :
116+ return s .CommentStatus == entity .CommentStatusDeleted || s .CommentStatus == entity .CommentStatusPending
117+ case constant .TagObjectType :
118+ return s .TagStatus == entity .TagStatusDeleted
119+ default :
120+ return false
121+ }
122+ }
123+
124+ func (s * SimpleObjectInfo ) isParentQuestionRestricted () bool {
125+ return s .QuestionStatus == entity .QuestionStatusDeleted ||
126+ s .QuestionStatus == entity .QuestionStatusPending ||
127+ s .QuestionShow == entity .QuestionHide
128+ }
129+
130+ func (s * SimpleObjectInfo ) objectNotFoundReason () string {
131+ switch s .ObjectType {
132+ case constant .QuestionObjectType :
133+ return reason .QuestionNotFound
134+ case constant .AnswerObjectType :
135+ return reason .AnswerNotFound
136+ case constant .CommentObjectType :
137+ return reason .CommentNotFound
138+ case constant .TagObjectType :
139+ return reason .TagNotFound
140+ default :
141+ return reason .ObjectNotFound
142+ }
143+ }
144+
60145type UnreviewedRevisionInfoInfo struct {
61146 CreatedAt int64 `json:"created_at"`
62147 ObjectID string `json:"object_id"`
0 commit comments