1+ <?php
2+
3+ use Garden \Web \Exception \ClientException ;
4+
5+ /**
6+ * Topcoder API Controller for the `/topcoder` resource.
7+ */
8+ class TopcoderApiController extends AbstractApiController{
9+
10+ /** @var UserMetaModel */
11+ private $ userMetaModel ;
12+ /** @var UserMetaModel */
13+ private $ categoryModel ;
14+ /**
15+ * TopcoderApiController constructor.
16+ *
17+ * @param UserMetaModel $userMetaModel
18+ * @param CategoryModel $categoryModel
19+ */
20+ public function __construct (UserMetaModel $ userMetaModel , CategoryModel $ categoryModel ) {
21+ $ this ->userMetaModel = $ userMetaModel ;
22+ $ this ->categoryModel = $ categoryModel ;
23+ }
24+
25+ /**
26+ * Lookup a single category by its numeric ID
27+ *
28+ * @param int $id The category ID
29+ * @throws NotFoundException if the category cannot be found.
30+ * @return array
31+ */
32+ private function category ($ id ) {
33+ $ category = CategoryModel::categories ($ id );
34+ if (empty ($ category )) {
35+ throw new NotFoundException ('Category ' );
36+ }
37+ return $ category ;
38+ }
39+
40+ /**
41+ * Add the "watch" status on a category for the user.
42+ *
43+ * @param int $id The target category's ID.
44+ * @param $userId The target user's ID.
45+ * @param array $body
46+ * @return array
47+ * @throws ClientException
48+ * @throws \Garden\Schema\ValidationException
49+ * @throws \Garden\Web\Exception\HttpException
50+ * @throws \Vanilla\Exception\PermissionException
51+ */
52+ public function put_watch ($ userId ,$ id , array $ body ) {
53+ $ this ->permission ('Garden.SignIn.Allow ' );
54+ $ schema = ['watched:b ' => 'The category-watched status for the user. ' ];
55+ $ in = $ this ->schema ($ schema , 'in ' );
56+ $ out = $ this ->schema ($ schema , 'out ' );
57+ $ body = $ in ->validate ($ body );
58+ $ newEmailCommentKey = 'Preferences.Email.NewComment. ' .$ id ;
59+ $ newEmailDiscussionKey = 'Preferences.Email.NewDiscussion. ' .$ id ;
60+ $ newPopupCommentKey = 'Preferences.Popup.NewComment. ' .$ id ;
61+ $ newPopupDiscussionKey = 'Preferences.Popup.NewDiscussion. ' .$ id ;
62+ $ isDiscussionFollowed = count ($ this ->userMetaModel ->getUserMeta ($ userId ,$ newEmailDiscussionKey )) > 0 ;
63+
64+ // Is this a new watch?
65+ if ($ body ['watched ' ] && !$ isDiscussionFollowed ) {
66+ $ category = $ this ->category ($ id );
67+ $ this ->permission ('Vanilla.Discussions.View ' , $ category ['PermissionCategoryID ' ]);
68+ }
69+ // null is used to remove data
70+ $ watched = $ body ['watched ' ] ? 1 : null ;
71+ $ this ->userMetaModel ->setUserMeta ($ userId , $ newEmailCommentKey , $ watched );
72+ $ this ->userMetaModel ->setUserMeta ($ userId , $ newEmailDiscussionKey , $ watched );
73+ $ this ->userMetaModel ->setUserMeta ($ userId , $ newPopupCommentKey , $ watched );
74+ $ this ->userMetaModel ->setUserMeta ($ userId , $ newPopupDiscussionKey , $ watched );
75+
76+ $ result = $ out ->validate ([
77+ 'watched ' => count ($ this ->userMetaModel ->getUserMeta ($ userId ,$ newEmailDiscussionKey )) > 0
78+ ]);
79+ return $ result ;
80+ }
81+ }
0 commit comments