Skip to content

Commit 0f08ff3

Browse files
committed
Issues-577: Watching feature for public forums
1 parent a19ba3a commit 0f08ff3

File tree

7 files changed

+110
-24
lines changed

7 files changed

+110
-24
lines changed

config/vanilla/bootstrap.before.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ function watchIcon($hasWatched = false, $title='') {
375375
* @param int $categoryID
376376
* @return string
377377
*/
378-
function watchButton($category) {
378+
function watchButton($category, $isHijackButton = true) {
379379
$output = ' ';
380380
$userID = Gdn::session()->UserID;
381381
if(is_numeric($category)) {
@@ -390,10 +390,15 @@ function watchButton($category) {
390390

391391
$text = $hasWatched ? t('Stop watching the category') : t('Watch the category');
392392
$icon = watchIcon($hasWatched, $text);
393+
$cssClasses = 'watchButton' . ($hasWatched ? ' isWatching': '');
394+
if($isHijackButton) {
395+
$cssClasses = 'Hijack '.$cssClasses;
396+
}
397+
393398
$output .= anchor(
394399
$icon,
395400
$hasWatched ? "/category/watched/{$categoryID}/" . Gdn::session()->transientKey() : "/category/watch/{$categoryID}/" . Gdn::session()->transientKey(),
396-
'Hijack watchButton' . ($hasWatched ? ' isWatching' : ''),
401+
$cssClasses,
397402
['title' => $text, 'aria-pressed' => $hasWatched ? 'true' : 'false', 'role' => 'button', 'tabindex' => '0']
398403
);
399404
}

vanilla/applications/vanilla/controllers/class.discussioncontroller.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,9 @@ public function dismissAnnouncement($discussionID = '') {
476476
*
477477
* @param int $DiscussionID Unique discussion ID.
478478
*/
479-
public function bookmark($DiscussionID = null) {
479+
public function bookmark($DiscussionID = null, $bookmarked=null, $tkey=null ) {
480480
// Make sure we are posting back.
481-
if (!$this->Request->isAuthenticatedPostBack()) {
481+
if (!$this->Request->isAuthenticatedPostBack() && !Gdn::session()->validateTransientKey($tkey)) {
482482
throw permissionException('Javascript');
483483
}
484484

@@ -491,7 +491,11 @@ public function bookmark($DiscussionID = null) {
491491
// Check the form to see if the data was posted.
492492
$Form = new Gdn_Form();
493493
$DiscussionID = $Form->getFormValue('DiscussionID', $DiscussionID);
494-
$Bookmark = $Form->getFormValue('Bookmark', null);
494+
// FIX: https://github.com/topcoder-platform/forums/issues/577
495+
// 0 - not bookmarked
496+
// 1 - bookmarked
497+
// 2 - unbookmarked, if Discussion's Author
498+
$Bookmark = $Form->getFormValue('Bookmark', $bookmarked);
495499
$UserID = $Form->getFormValue('UserID', $Session->UserID);
496500

497501
// Check the permission on the user.
@@ -511,10 +515,11 @@ public function bookmark($DiscussionID = null) {
511515
$Bookmark = $this->DiscussionModel->bookmark($DiscussionID, $UserID, $Bookmark);
512516

513517
// Set the new value for api calls and json targets.
518+
// FIX: https://github.com/topcoder-platform/forums/issues/577
514519
$this->setData([
515520
'UserID' => $UserID,
516521
'DiscussionID' => $DiscussionID,
517-
'Bookmarked' => (bool)$Bookmark
522+
'Bookmarked' => (int)$Bookmark
518523
]);
519524
setValue('Bookmarked', $Discussion, (int)$Bookmark);
520525

vanilla/applications/vanilla/models/class.commentmodel.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,7 @@ public function recordAdvancedNotications($activityModel, $activity, $discussion
13971397
}
13981398

13991399
$categoryID = val('CategoryID', $discussion);
1400+
$discussionID = val('DiscussionID', $discussion);
14001401

14011402
// Figure out the category that governs this notification preference.
14021403
$category = CategoryModel::categories($categoryID);
@@ -1414,6 +1415,13 @@ public function recordAdvancedNotications($activityModel, $activity, $discussion
14141415
$parentCategory = CategoryModel::categories($parentCategory["ParentCategoryID"]);
14151416
}
14161417

1418+
$discussionModel = new DiscussionModel();
1419+
// FIX: https://github.com/topcoder-platform/forums/issues/577
1420+
$removeUserIDs = array_column(
1421+
$discussionModel->getUnBookmarkUsers($discussionID)->resultArray(),
1422+
"UserID"
1423+
);
1424+
14171425
// Grab all of the users that need to be notified.
14181426
$data = $this->SQL
14191427
->whereIn('Name', ['Preferences.Email.NewComment.'.$category['CategoryID'], 'Preferences.Popup.NewComment.'.$category['CategoryID']])
@@ -1426,8 +1434,12 @@ public function recordAdvancedNotications($activityModel, $activity, $discussion
14261434
}
14271435

14281436
$userID = $row['UserID'];
1437+
1438+
if (in_array($userID, $removeUserIDs)) {
1439+
continue;
1440+
}
1441+
14291442
// Check user can still see the discussion.
1430-
$discussionModel = new DiscussionModel();
14311443
if (!$discussionModel->canView($discussion, $userID)) {
14321444
continue;
14331445
}

vanilla/applications/vanilla/models/class.discussionmodel.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,20 @@ public function getBookmarkUsers($discussionID) {
13391339
->where('Bookmarked', '1')
13401340
->get();
13411341
}
1342+
/**
1343+
* Gets all users who have unbookmarked the specified discussion.
1344+
*
1345+
*/
1346+
// FIX: https://github.com/topcoder-platform/forums/issues/577
1347+
public function getUnBookmarkUsers($discussionID) {
1348+
return $this->SQL
1349+
->select('UserID')
1350+
->from('UserDiscussion')
1351+
->where('DiscussionID', $discussionID)
1352+
->whereIn('Bookmarked', [0, 2])
1353+
->get();
1354+
}
1355+
13421356

13431357
/**
13441358
*
@@ -2872,8 +2886,8 @@ public function bookmark($discussionID, $userID, $bookmarked = null) {
28722886
$this->EventArguments['UserID'] = $userID;
28732887
$this->EventArguments['Bookmarked'] = $bookmarked;
28742888
$this->fireEvent('AfterBookmark');
2875-
2876-
return (bool)$bookmarked;
2889+
// FIX: https://github.com/topcoder-platform/forums/issues/577
2890+
return (int)$bookmarked;
28772891
}
28782892

28792893
/**

vanilla/applications/vanilla/views/discussion/index.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
if (!function_exists('WriteComment'))
44
include $this->fetchViewLocation('helper_functions', 'discussion');
55

6+
if (!function_exists('bookmarkButton'))
7+
include $this->fetchViewLocation('helper_functions', 'discussions');
8+
69
// Wrap the discussion related content in a div.
710
echo '<div class="MessageList Discussion">';
811

@@ -13,7 +16,8 @@
1316
echo '<div class="Options">';
1417

1518
$this->fireEvent('BeforeDiscussionOptions');
16-
writeBookmarkLink();
19+
//writeBookmarkLink();
20+
echo bookmarkButton($this->data('Discussion'));
1721
echo getDiscussionOptionsDropdown();
1822
writeAdminCheck();
1923

vanilla/applications/vanilla/views/discussions/helper_functions.php

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,59 @@ function bookmarkButton($discussion) {
7171
}
7272

7373
// Bookmark link
74-
$hasWatched = $discussion->Bookmarked == '1';
75-
$title = t($hasWatched ? 'Stop watching the discussion' : 'Watch the discussion');
76-
$icon = watchIcon($hasWatched, $title);
77-
return anchor(
78-
$icon,
79-
'/discussion/bookmark/'.$discussion->DiscussionID.'/'.Gdn::session()->transientKey(),
80-
'Hijack watchButton '.($hasWatched ? ' isWatching' : ''),
81-
['title' => $title]
82-
);
74+
// FIX : https://github.com/topcoder-platform/forums/issues/577
75+
// If User is watching a category then show it as bookmarked
76+
$categoryModel = new CategoryModel();
77+
$category = CategoryModel::categories($discussion->CategoryID);
78+
$groupID = val('GroupID', $category);
79+
// No changes for Challenge Forums
80+
if($groupID && $groupID > 0) {
81+
// New value should be opposite
82+
$hasWatched = $discussion->Bookmarked == 1;
83+
$newValue = $hasWatched ? 0 : 1;
84+
$title = t($hasWatched ? 'Stop watching the discussion' : 'Watch the discussion');
85+
$icon = watchIcon($hasWatched, $title);
86+
return anchor(
87+
$icon,
88+
'/discussion/bookmark/' . $discussion->DiscussionID . '/?tkey=' . Gdn::session()->transientKey() . '&bookmarked=' . $newValue,
89+
'Hijack watchButton ' . ($hasWatched ? ' isWatching' : ''),
90+
['title' => $title]
91+
);
92+
} else {
93+
$hasWatchedCategory = $categoryModel->hasWatched($discussion->CategoryID, Gdn::session()->UserID);
94+
$hasWatched = false;
95+
96+
// Author is added by default with Bookmarked = 0, Participated = 1
97+
$isAuthor = ($discussion->InsertUserID == Gdn::session()->UserID);
98+
99+
// If Watched Category: unwatched discussion
100+
if ($discussion->Bookmarked === null) {
101+
$hasWatched = $hasWatchedCategory;
102+
$newValue = $hasWatched === true ? 0 : 1;
103+
} else if ($discussion->Bookmarked == 0) {
104+
$hasWatched = false;
105+
if ($isAuthor) {
106+
$hasWatched = $hasWatchedCategory;
107+
$newValue = 2;
108+
} else {
109+
$newValue = 1;
110+
}
111+
} else if ($discussion->Bookmarked == 1) {
112+
$hasWatched = true;
113+
$newValue = $isAuthor? 2 : 0;
114+
} else if ($discussion->Bookmarked == 2) {
115+
$hasWatched = false;
116+
$newValue = 1;
117+
}
118+
$title = t($hasWatched ? 'Stop watching the discussion' : 'Watch the discussion');
119+
$icon = watchIcon($hasWatched, $title);
120+
return anchor(
121+
$icon,
122+
'/discussion/bookmark/' . $discussion->DiscussionID . '/?tkey=' . Gdn::session()->transientKey() . '&bookmarked=' . $newValue,
123+
'Hijack watchButton ' . ($hasWatched ? ' isWatching' : ''),
124+
['title' => $title]
125+
);
126+
}
83127
}
84128
}
85129

vanilla/applications/vanilla/views/discussions/index.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
include_once $this->fetchViewLocation('helper_functions', 'discussions', 'vanilla');
44
include_once $this->fetchViewLocation('helper_functions', 'categories', 'vanilla');
55

6-
echo '<h1 class="H HomepageTitle">'.
7-
adminCheck(NULL, ['', ' ']).
8-
$this->data('Title').
9-
followButton($this->data('Category.CategoryID')).'</h1>';
10-
//watchButton($this->data('Category.CategoryID')).
6+
// FIX: https://github.com/topcoder-platform/forums/issues/577
7+
$title = adminCheck(NULL, ['', ' ']).$this->data('Title');
8+
if(!$this->data('Category.GroupID')) {
9+
$title .= watchButton($this->data('Category.CategoryID'), false);
10+
}
11+
//followButton($this->data('Category.CategoryID'))
12+
echo '<h1 class="H HomepageTitle">'.$title.'</h1>';
1113

1214

1315
$Description = $this->data('Category.Description', $this->description());

0 commit comments

Comments
 (0)