Skip to content

Commit aeb07cb

Browse files
authored
Merge pull request #41 from topcoder-platform/issus-125
Issues-229: refactored joining groups
2 parents 8726256 + 2898b93 commit aeb07cb

File tree

2 files changed

+77
-54
lines changed

2 files changed

+77
-54
lines changed

class.groups.plugin.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,9 @@ private function getMenuItemCssClassFromQuery($sender, $requestMethod){
822822
return $sender->ControllerName == 'groupscontroller' && Gdn::request()->get('filter') == $requestMethod ? ' Active' : '';
823823
}
824824

825+
public static function logMessage($message, $data =[], $file = __FILE__, $line = __LINE__) {
826+
logMessage($file, $line, 'GroupsPlugin', $message, json_encode($data) );
827+
}
825828
public static function log($message, $data= []) {
826829
if (c('Debug')) {
827830
Logger::event(

models/class.groupmodel.php

Lines changed: 74 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -830,19 +830,21 @@ public function checkPermission($userID,$groupID,$categoryID = null, $permission
830830
* @return bool|Gdn_DataSet|object|string
831831
*/
832832
public function join($GroupID, $UserID, $watched = true, $followed = true ){
833-
$results = [];
833+
GroupsPlugin::logMessage('!!!-----------------join:enter-----------------!!!', ['GroupID' => $GroupID, 'userID'=>$UserID, 'watched' => $watched, 'followed'=>$followed], __FILE__, __LINE__) ;
834834
$Fields = ['Role' => GroupModel::ROLE_MEMBER, 'GroupID' => $GroupID,'UserID' => $UserID, 'DateInserted' => Gdn_Format::toDateTime()];
835835
if( $this->SQL->getWhere('UserGroup', ['GroupID' => $GroupID,'UserID' => $UserID])->numRows() == 0) {
836836
$this->SQL->insert('UserGroup', $Fields);
837+
GroupsPlugin::logMessage('join:user was added in UserGroup', ['userID'=>$UserID, 'watched' => $watched, 'followed'=>$followed],__FILE__, __LINE__);
837838
if($followed === true) {
838-
$results['Followed'] = $this->followGroup($GroupID, $UserID);
839+
$this->followGroup($GroupID, $UserID);
839840
}
840841
if($watched === true) {
841-
$results['Watched'] = $this->watchGroup($GroupID, $UserID);
842+
$this->watchGroup($GroupID, $UserID);
842843
}
843844
$this->notifyJoinGroup($GroupID, $UserID);
844845
}
845-
return $results;
846+
GroupsPlugin::logMessage('join:exit', [],__FILE__, __LINE__);
847+
846848
}
847849

848850
/**
@@ -1248,110 +1250,128 @@ public function hasWatchedGroup($group) {
12481250

12491251
/**
12501252
* Follow all group's categories
1251-
*
1253+
* @param $group
1254+
* @param $userID
12521255
*/
12531256
public function followGroup($group, $userID) {
1254-
$result = [];
1257+
//TODO: Remove extra logging after testing
1258+
GroupsPlugin::logMessage('-----------------followGroup:enter------------', ['userID'=>$userID, 'group'=>$group],__FILE__, __LINE__);
12551259
if(is_numeric($group) && $group > 0) {
12561260
$group = $this->getByGroupID($group);
12571261
}
1258-
12591262
if($group->ChallengeID) {
1260-
// Remove existing UserCategory cache
1261-
// Before calculating the user-specific information on a category.
1262-
CategoryModel::clearUserCache($userID);
1263-
Gdn::cache()->remove("Follow_{$userID}");
1264-
$categoryModel = CategoryModel::instance();
1265-
$groupCategory = $categoryModel->getByCode($group->ChallengeID);
1266-
$parentCategoryID = val('CategoryID', $groupCategory);
1267-
$isFollowed = $categoryModel->follow($userID, $parentCategoryID, true);
1268-
$result[strval($parentCategoryID)] = $isFollowed;
1269-
$childrenCategories = CategoryModel::getChildren($parentCategoryID);
1270-
$categoryIDs = array_column($childrenCategories, 'CategoryID');
1263+
$categories = Gdn::sql()->getWhere('Category', ['GroupID' => $group->GroupID, 'DisplayAs' => 'Discussions'])->resultArray();
1264+
$categoryIDs = array_column($categories, 'CategoryID');
1265+
GroupsPlugin::logMessage('follow:allCatIDs', ['userID'=>$userID, 'catIds'=>$categoryIDs], __FILE__, __LINE__) ;
1266+
12711267
foreach($categoryIDs as $categoryID) {
1272-
$isFollowed = $categoryModel->follow($userID, $categoryID, true);
1273-
$result[strval($categoryID)] = $isFollowed;
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+
}
12741279
}
1275-
12761280
CategoryModel::clearUserCache($userID);
1281+
Gdn::cache()->remove("Follow_{$userID}");
12771282
}
1278-
return $result;
12791283
}
12801284

12811285
/**
12821286
* Unfollow all group's categories
1283-
*
1287+
* @param $group
1288+
* @param $userID
12841289
*/
12851290
public function unfollowGroup($group, $userID) {
1291+
GroupsPlugin::logMessage('-----------------unfollow:enter-----------------', ['userID'=>$userID, 'group'=>$group], __FILE__, __LINE__) ;
12861292
if(is_numeric($group) && $group > 0) {
12871293
$group = $this->getByGroupID($group);
12881294
}
12891295

12901296
if($group->ChallengeID) {
1291-
$categoryModel = new CategoryModel();
1292-
$groupCategory = $categoryModel->getByCode($group->ChallengeID);
1293-
$categories = CategoryModel::getSubtree($groupCategory->CategoryID, true);
1297+
$categories = Gdn::sql()->getWhere('Category', ['GroupID' => $group->GroupID, 'DisplayAs' => 'Discussions'])->resultArray();
12941298
$categoryIDs = array_column($categories, 'CategoryID');
1299+
GroupsPlugin::logMessage('unfollow:allCatIDs', ['userID'=>$userID, 'catIds'=>$categoryIDs], __FILE__, __LINE__) ;
12951300

12961301
foreach($categoryIDs as $categoryID) {
1297-
$categoryModel->follow($userID, $categoryID, false);
1302+
Gdn::sql()->update('UserCategory', [ 'Followed' => 0],
1303+
['UserID' => $userID,'CategoryID' => $categoryID])->put();
12981304
}
12991305

1306+
Gdn::cache()->remove("Follow_{$userID}");
13001307
CategoryModel::clearUserCache($userID);
13011308
}
13021309
}
13031310

13041311
/**
13051312
* Watch all group's categories
13061313
* @param $group
1314+
* @param $userID
13071315
*/
13081316
public function watchGroup($group, $userID) {
1309-
$results = [];
1317+
//TODO: Remove extra logging after testing
1318+
GroupsPlugin::logMessage('-----------------watch:enter-----------------', ['userID'=>$userID, 'group'=>$group], __FILE__, __LINE__) ;
1319+
13101320
if(is_numeric($group) && $group > 0) {
13111321
$group = $this->getByGroupID($group);
13121322
}
13131323

13141324
if($group->ChallengeID) {
1315-
// Remove existing UserCategory cache
1316-
// Before calculating the user-specific information on a category.
1317-
CategoryModel::clearUserCache($userID);
1318-
Gdn::cache()->remove("UserMeta_{$userID}");
1319-
1320-
$categoryModel = CategoryModel::instance();
1321-
$groupCategory = $categoryModel->getByCode($group->ChallengeID);
1322-
$parentCategoryID = val('CategoryID', $groupCategory);
1323-
$childrenCategories = CategoryModel::getChildren($parentCategoryID);
1324-
$categoryIDs = array_column($childrenCategories, 'CategoryID');
1325-
array_push($categoryIDs, $parentCategoryID);
1326-
$categoryModel->setCategoryMetaData($categoryIDs, $userID, 1);
1327-
$userMetaModel = new UserMetaModel();
1328-
$results = $userMetaModel->getUserMeta($userID);
1325+
$categories = Gdn::sql()->getWhere('Category', ['GroupID' => $group->GroupID, 'DisplayAs' => 'Discussions'])->resultArray();
1326+
$categoryIDs = array_column($categories, 'CategoryID');
1327+
GroupsPlugin::logMessage('watch:allCatIDs', ['userID'=>$userID, 'catIds'=>$categoryIDs], __FILE__, __LINE__) ;
1328+
// Don't use setCategoryMetaData due to cache
1329+
$metaKeys = ['Preferences.Email.NewComment.',
1330+
'Preferences.Email.NewDiscussion.',
1331+
'Preferences.Popup.NewComment.',
1332+
'Preferences.Popup.NewDiscussion.'];
1333+
foreach($categoryIDs as $categoryID) {
1334+
foreach ($metaKeys as $metaKey) {
1335+
Gdn::sql()->insert('UserMeta', [
1336+
'UserID' => $userID,
1337+
'Name' => $metaKey . $categoryID,
1338+
'Value' => 1
1339+
]);
1340+
}
1341+
}
13291342
CategoryModel::clearUserCache($userID);
1343+
$result = Gdn::cache()->remove("UserMeta_{$userID}");
1344+
GroupsPlugin::logMessage('watch:UserMetaCacheRemoved', ['userID'=>$userID, 'cacheRemoved'=>$result], __FILE__, __LINE__) ;
13301345
}
1331-
1332-
return $results;
13331346
}
13341347

13351348
/**
13361349
* Unwatch all group's categories
13371350
* @param $group
13381351
*/
13391352
public function unwatchGroup($group, $userID) {
1353+
GroupsPlugin::logMessage('-----------------unwatch:enter-----------------', ['userID'=>$userID, 'group'=>$group], __FILE__, __LINE__) ;
1354+
13401355
if(is_numeric($group) && $group > 0) {
13411356
$group = $this->getByGroupID($group);
13421357
}
1343-
13441358
if($group->ChallengeID) {
1345-
CategoryModel::clearUserCache($userID);
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+
}
13461374
Gdn::cache()->remove("UserMeta_{$userID}");
1347-
$categoryModel = new CategoryModel();
1348-
$groupCategory = $categoryModel->getByCode($group->ChallengeID);
1349-
$parentCategoryID = val('CategoryID', $groupCategory);
1350-
$childrenCategories = CategoryModel::getChildren($parentCategoryID);
1351-
$categoryIDs = array_column($childrenCategories, 'CategoryID');
1352-
array_push($categoryIDs, $parentCategoryID);
1353-
$categoryModel->setCategoryMetaData($categoryIDs, $userID, null);
1354-
13551375
CategoryModel::clearUserCache($userID);
13561376
}
13571377
}

0 commit comments

Comments
 (0)