Skip to content

Commit be298a3

Browse files
authored
Merge pull request #42 from topcoder-platform/issus-125
Issues-229: supporting followed\watched for existing group members, Issues-165
2 parents aeb07cb + 6c8837d commit be298a3

File tree

2 files changed

+29
-80
lines changed

2 files changed

+29
-80
lines changed

controllers/class.groupcontroller.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ public function delete($GroupID = false) {
176176
* @param int|bool $groupID
177177
*/
178178
public function edit($groupID = false) {
179+
Gdn_Theme::section('Group');
179180
$Group = false;
180181
if($groupID) {
181182
$Group = $this->GroupModel->getByGroupID($groupID);

models/class.groupmodel.php

Lines changed: 28 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -827,23 +827,15 @@ public function checkPermission($userID,$groupID,$categoryID = null, $permission
827827
* @param $UserID
828828
* @param bool $watched
829829
* @param bool $followed
830-
* @return bool|Gdn_DataSet|object|string
831830
*/
832831
public function join($GroupID, $UserID, $watched = true, $followed = true ){
833-
GroupsPlugin::logMessage('!!!-----------------join:enter-----------------!!!', ['GroupID' => $GroupID, 'userID'=>$UserID, 'watched' => $watched, 'followed'=>$followed], __FILE__, __LINE__) ;
834832
$Fields = ['Role' => GroupModel::ROLE_MEMBER, 'GroupID' => $GroupID,'UserID' => $UserID, 'DateInserted' => Gdn_Format::toDateTime()];
835833
if( $this->SQL->getWhere('UserGroup', ['GroupID' => $GroupID,'UserID' => $UserID])->numRows() == 0) {
836834
$this->SQL->insert('UserGroup', $Fields);
837-
GroupsPlugin::logMessage('join:user was added in UserGroup', ['userID'=>$UserID, 'watched' => $watched, 'followed'=>$followed],__FILE__, __LINE__);
838-
if($followed === true) {
839-
$this->followGroup($GroupID, $UserID);
840-
}
841-
if($watched === true) {
842-
$this->watchGroup($GroupID, $UserID);
843-
}
844835
$this->notifyJoinGroup($GroupID, $UserID);
845836
}
846-
GroupsPlugin::logMessage('join:exit', [],__FILE__, __LINE__);
837+
$this->followGroup($GroupID, $UserID, $followed);
838+
$this->watchGroup($GroupID, $UserID, $watched);
847839

848840
}
849841

@@ -1252,30 +1244,22 @@ public function hasWatchedGroup($group) {
12521244
* Follow all group's categories
12531245
* @param $group
12541246
* @param $userID
1247+
* @param bool $followed
12551248
*/
1256-
public function followGroup($group, $userID) {
1257-
//TODO: Remove extra logging after testing
1258-
GroupsPlugin::logMessage('-----------------followGroup:enter------------', ['userID'=>$userID, 'group'=>$group],__FILE__, __LINE__);
1249+
public function followGroup($group, $userID, $followed = true ) {
12591250
if(is_numeric($group) && $group > 0) {
12601251
$group = $this->getByGroupID($group);
12611252
}
12621253
if($group->ChallengeID) {
12631254
$categories = Gdn::sql()->getWhere('Category', ['GroupID' => $group->GroupID, 'DisplayAs' => 'Discussions'])->resultArray();
12641255
$categoryIDs = array_column($categories, 'CategoryID');
1265-
GroupsPlugin::logMessage('follow:allCatIDs', ['userID'=>$userID, 'catIds'=>$categoryIDs], __FILE__, __LINE__) ;
12661256

12671257
foreach($categoryIDs as $categoryID) {
1268-
try {
1269-
Gdn::sql()->insert(
1270-
'UserCategory',
1271-
['Followed' => 1, 'UserID' => $userID, 'CategoryID' => $categoryID]
1272-
);
1273-
} catch (Exception $e) {
1274-
Gdn::sql()->update('UserCategory',
1275-
['Followed' => 1],
1276-
['UserID' => $userID, 'CategoryID' => $categoryID]
1277-
)->put();
1278-
}
1258+
$this->SQL->replace(
1259+
'UserCategory',
1260+
['Followed' => (int)$followed],
1261+
['UserID' => $userID, 'CategoryID' => $categoryID]
1262+
);
12791263
}
12801264
CategoryModel::clearUserCache($userID);
12811265
Gdn::cache()->remove("Follow_{$userID}");
@@ -1288,92 +1272,56 @@ public function followGroup($group, $userID) {
12881272
* @param $userID
12891273
*/
12901274
public function unfollowGroup($group, $userID) {
1291-
GroupsPlugin::logMessage('-----------------unfollow:enter-----------------', ['userID'=>$userID, 'group'=>$group], __FILE__, __LINE__) ;
1292-
if(is_numeric($group) && $group > 0) {
1293-
$group = $this->getByGroupID($group);
1294-
}
1295-
1296-
if($group->ChallengeID) {
1297-
$categories = Gdn::sql()->getWhere('Category', ['GroupID' => $group->GroupID, 'DisplayAs' => 'Discussions'])->resultArray();
1298-
$categoryIDs = array_column($categories, 'CategoryID');
1299-
GroupsPlugin::logMessage('unfollow:allCatIDs', ['userID'=>$userID, 'catIds'=>$categoryIDs], __FILE__, __LINE__) ;
1300-
1301-
foreach($categoryIDs as $categoryID) {
1302-
Gdn::sql()->update('UserCategory', [ 'Followed' => 0],
1303-
['UserID' => $userID,'CategoryID' => $categoryID])->put();
1304-
}
1305-
1306-
Gdn::cache()->remove("Follow_{$userID}");
1307-
CategoryModel::clearUserCache($userID);
1308-
}
1275+
$this->followGroup($group, $userID, false);
13091276
}
13101277

13111278
/**
13121279
* Watch all group's categories
13131280
* @param $group
13141281
* @param $userID
1282+
* @param int $watched 1 - watch, 0/null - unwatch
13151283
*/
1316-
public function watchGroup($group, $userID) {
1317-
//TODO: Remove extra logging after testing
1318-
GroupsPlugin::logMessage('-----------------watch:enter-----------------', ['userID'=>$userID, 'group'=>$group], __FILE__, __LINE__) ;
1319-
1284+
public function watchGroup($group, $userID, $watched = true) {
13201285
if(is_numeric($group) && $group > 0) {
13211286
$group = $this->getByGroupID($group);
13221287
}
13231288

13241289
if($group->ChallengeID) {
13251290
$categories = Gdn::sql()->getWhere('Category', ['GroupID' => $group->GroupID, 'DisplayAs' => 'Discussions'])->resultArray();
13261291
$categoryIDs = array_column($categories, 'CategoryID');
1327-
GroupsPlugin::logMessage('watch:allCatIDs', ['userID'=>$userID, 'catIds'=>$categoryIDs], __FILE__, __LINE__) ;
1328-
// Don't use setCategoryMetaData due to cache
1292+
// Don't use setCategoryMetaData due to cache
13291293
$metaKeys = ['Preferences.Email.NewComment.',
13301294
'Preferences.Email.NewDiscussion.',
13311295
'Preferences.Popup.NewComment.',
13321296
'Preferences.Popup.NewDiscussion.'];
13331297
foreach($categoryIDs as $categoryID) {
13341298
foreach ($metaKeys as $metaKey) {
1335-
Gdn::sql()->insert('UserMeta', [
1336-
'UserID' => $userID,
1337-
'Name' => $metaKey . $categoryID,
1338-
'Value' => 1
1339-
]);
1299+
if($watched) {
1300+
$this->SQL->replace(
1301+
'UserMeta',
1302+
[ 'Value' => (int)$watched],
1303+
[ 'UserID' => $userID, 'Name' => $metaKey . $categoryID,]
1304+
);
1305+
} else {
1306+
Gdn::sql()->delete('UserMeta', [
1307+
'UserID' => $userID,
1308+
'Name' => $metaKey . $categoryID
1309+
]);
1310+
}
13401311
}
13411312
}
13421313
CategoryModel::clearUserCache($userID);
1343-
$result = Gdn::cache()->remove("UserMeta_{$userID}");
1344-
GroupsPlugin::logMessage('watch:UserMetaCacheRemoved', ['userID'=>$userID, 'cacheRemoved'=>$result], __FILE__, __LINE__) ;
1314+
Gdn::cache()->remove("UserMeta_{$userID}");
13451315
}
13461316
}
13471317

13481318
/**
13491319
* Unwatch all group's categories
13501320
* @param $group
1321+
* @param $userID
13511322
*/
13521323
public function unwatchGroup($group, $userID) {
1353-
GroupsPlugin::logMessage('-----------------unwatch:enter-----------------', ['userID'=>$userID, 'group'=>$group], __FILE__, __LINE__) ;
1354-
1355-
if(is_numeric($group) && $group > 0) {
1356-
$group = $this->getByGroupID($group);
1357-
}
1358-
if($group->ChallengeID) {
1359-
$categories = Gdn::sql()->getWhere('Category', ['GroupID' => $group->GroupID, 'DisplayAs' => 'Discussions'])->resultArray();
1360-
$categoryIDs = array_column($categories, 'CategoryID');
1361-
// Don't use setCategoryMetaData due to cache
1362-
$metaKeys = ['Preferences.Email.NewComment.',
1363-
'Preferences.Email.NewDiscussion.',
1364-
'Preferences.Popup.NewComment.',
1365-
'Preferences.Popup.NewDiscussion.'];
1366-
foreach($categoryIDs as $categoryID) {
1367-
foreach ($metaKeys as $metaKey) {
1368-
Gdn::sql()->delete('UserMeta', [
1369-
'UserID' => $userID,
1370-
'Name' => $metaKey . $categoryID
1371-
]);
1372-
}
1373-
}
1374-
Gdn::cache()->remove("UserMeta_{$userID}");
1375-
CategoryModel::clearUserCache($userID);
1376-
}
1324+
$this->watchGroup($group,$userID,false);
13771325
}
13781326

13791327

0 commit comments

Comments
 (0)