Skip to content

Commit 358940c

Browse files
authored
Merge pull request #98 from topcoder-platform/issues-520
Issues-520:Notification preferences API
2 parents 1391113 + 8a5e9a2 commit 358940c

File tree

4 files changed

+553
-8
lines changed

4 files changed

+553
-8
lines changed

DebugPlugin/controllers/api/PermissionApiController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function index($userID) {
2828
$userPermissions = Gdn::userModel()->getPermissions($userID);
2929
$data = [
3030
'userPermissions' => $userPermissions,
31+
'userPermissionArray' => $userPermissions->getPermissions()
3132
];
3233
return $data;
3334
}

Topcoder/class.topcoder.plugin.php

Lines changed: 179 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,26 +292,40 @@ public function gdn_auth_startAuthenticator_handler() {
292292
}
293293

294294
self::log('TopcoderPlugin: gdn_auth_startAuthenticator_handler', ['Path' => Gdn::request()->path()]);
295-
296-
// Ignore EntryController endpoints and ApiController endpoints.
295+
// Ignore EntryController endpoints
297296
// AccessToken for /api will be checked in class.hooks.php
298-
if (stringBeginsWith(Gdn::request()->getPath(), '/api/') || stringBeginsWith(Gdn::request()->getPath(), '/entry/')) {
297+
if (stringBeginsWith(Gdn::request()->getPath(), '/entry/')) {
299298
return;
300299
}
301300

302301
$cookieName = c('Plugins.Topcoder.SSO.CookieName');
303-
self::log('Cookie Name', ['value' => $cookieName]);
302+
self::log('Cookie Name', ['value' => $cookieName]);
304303

305304
$cookiesToken = isset($_COOKIE[$cookieName]) ? $_COOKIE[$cookieName] : null;
306-
307305
$headersToken = $this->getBearerToken();
308-
$accessToken = $headersToken ? $headersToken : $cookiesToken;
309306

310307
if ($cookiesToken) {
311-
self::log('Token from Cookies', ['value' => $cookiesToken]);
308+
self::log('Token from Cookies', ['value' => $cookiesToken]);
312309
}
313310
if ($headersToken) {
314-
self::log('Token from Headers', ['value' => '' . $headersToken]);
311+
self::log('Token from Headers', ['value' => '' . $headersToken]);
312+
}
313+
314+
$accessToken = null;
315+
316+
if(stringBeginsWith(Gdn::request()->getPath(), '/api/')) {
317+
if(stringBeginsWith(Gdn::request()->getPath(), '/api/v2/users/me-preferences') ||
318+
stringBeginsWith(Gdn::request()->getPath(), '/api/v2/discussions/bookmarked') ||
319+
(stringBeginsWith(Gdn::request()->getPath(), '/api/v2/discussions/')
320+
&& stringEndsWith(Gdn::request()->getPath(), '/bookmark'))) {
321+
$accessToken = $headersToken;
322+
} else {
323+
// Ignore other ApiController endpoints.
324+
// AccessToken for /api will be checked in class.hooks.php
325+
return;
326+
}
327+
} else {
328+
$accessToken = $cookiesToken;
315329
}
316330

317331
if ($accessToken) {
@@ -2122,6 +2136,163 @@ public function discussionController_announce_create($sender, $discussionID = '
21222136

21232137
$sender->render('Blank', 'Utility', 'Dashboard');
21242138
}
2139+
2140+
/**
2141+
* Edit user's preferences (mostly notification settings).
2142+
*
2143+
* @param mixed $userReference Unique identifier, possibly username or ID.
2144+
* @param string $username .
2145+
* @param int $userID Unique identifier.
2146+
*/
2147+
public function profileController_preferences_create($sender, $userReference = '', $username = '', $userID = '') {
2148+
$sender->addJsFile('profile.js');
2149+
$session = Gdn::session();
2150+
$sender->permission('Garden.SignIn.Allow');
2151+
2152+
// Get user data
2153+
$sender->getUserInfo($userReference, $username, $userID, true);
2154+
$userPrefs = dbdecode($sender->User->Preferences);
2155+
if ($sender->User->UserID != $session->UserID) {
2156+
$sender->permission(['Garden.Users.Edit', 'Moderation.Profiles.Edit'], false);
2157+
}
2158+
2159+
if (!is_array($userPrefs)) {
2160+
$userPrefs = [];
2161+
}
2162+
2163+
$metaPrefs = [];// UserModel::getMeta($this->User->UserID, 'Preferences.%', 'Preferences.');
2164+
2165+
// Define the preferences to be managed
2166+
$notifications = [];
2167+
2168+
if (c('Garden.Profile.ShowActivities', true)) {
2169+
$notifications = [
2170+
'Email.WallComment' => t('Notify me when people write on my wall.'),
2171+
'Email.ActivityComment' => t('Notify me when people reply to my wall comments.'),
2172+
'Popup.WallComment' => t('Notify me when people write on my wall.'),
2173+
'Popup.ActivityComment' => t('Notify me when people reply to my wall comments.')
2174+
];
2175+
}
2176+
2177+
$sender->Preferences = ['Notifications' => $notifications];
2178+
2179+
// Allow email notification of applicants (if they have permission & are using approval registration)
2180+
if (checkPermission('Garden.Users.Approve') && c('Garden.Registration.Method') == 'Approval') {
2181+
$sender->Preferences['Notifications']['Email.Applicant'] = [t('NotifyApplicant', 'Notify me when anyone applies for membership.'), 'Meta'];
2182+
}
2183+
2184+
$sender->fireEvent('AfterPreferencesDefined');
2185+
2186+
// Loop through the preferences looking for duplicates, and merge into a single row
2187+
$sender->PreferenceGroups = [];
2188+
$sender->PreferenceTypes = [];
2189+
foreach ($sender->Preferences as $preferenceGroup => $preferences) {
2190+
$sender->PreferenceGroups[$preferenceGroup] = [];
2191+
$sender->PreferenceTypes[$preferenceGroup] = [];
2192+
foreach ($preferences as $name => $description) {
2193+
$location = 'Prefs';
2194+
if (is_array($description)) {
2195+
list($description, $location) = $description;
2196+
}
2197+
2198+
$nameParts = explode('.', $name);
2199+
$prefType = val('0', $nameParts);
2200+
$subName = val('1', $nameParts);
2201+
if ($subName != false) {
2202+
// Save an array of all the different types for this group
2203+
if (!in_array($prefType, $sender->PreferenceTypes[$preferenceGroup])) {
2204+
$sender->PreferenceTypes[$preferenceGroup][] = $prefType;
2205+
}
2206+
2207+
// Store all the different subnames for the group
2208+
if (!array_key_exists($subName, $sender->PreferenceGroups[$preferenceGroup])) {
2209+
$sender->PreferenceGroups[$preferenceGroup][$subName] = [$name];
2210+
} else {
2211+
$sender->PreferenceGroups[$preferenceGroup][$subName][] = $name;
2212+
}
2213+
} else {
2214+
$sender->PreferenceGroups[$preferenceGroup][$name] = [$name];
2215+
}
2216+
}
2217+
}
2218+
2219+
// Loop the preferences, setting defaults from the configuration.
2220+
$currentPrefs = [];
2221+
foreach ($sender->Preferences as $prefGroup => $prefs) {
2222+
foreach ($prefs as $pref => $desc) {
2223+
$location = 'Prefs';
2224+
if (is_array($desc)) {
2225+
list($desc, $location) = $desc;
2226+
}
2227+
2228+
if ($location == 'Meta') {
2229+
$currentPrefs[$pref] = val($pref, $metaPrefs, false);
2230+
} else {
2231+
$currentPrefs[$pref] = val($pref, $userPrefs, c('Preferences.'.$pref, '0'));
2232+
}
2233+
2234+
unset($metaPrefs[$pref]);
2235+
}
2236+
}
2237+
$currentPrefs = array_merge($currentPrefs, $metaPrefs);
2238+
$currentPrefs = array_map('intval', $currentPrefs);
2239+
$sender->setData('Preferences', $currentPrefs);
2240+
2241+
if (UserModel::noEmail()) {
2242+
$sender->PreferenceGroups = self::_removeEmailPreferences($sender->PreferenceGroups);
2243+
$sender->PreferenceTypes = self::_removeEmailPreferences($sender->PreferenceTypes);
2244+
$sender->setData('NoEmail', true);
2245+
}
2246+
2247+
$sender->setData('PreferenceGroups', $sender->PreferenceGroups);
2248+
$sender->setData('PreferenceTypes', $sender->PreferenceTypes);
2249+
$sender->setData('PreferenceList', $sender->Preferences);
2250+
2251+
if ($sender->Form->authenticatedPostBack()) {
2252+
// Get, assign, and save the preferences.
2253+
$newMetaPrefs = [];
2254+
foreach ($sender->Preferences as $prefGroup => $prefs) {
2255+
foreach ($prefs as $pref => $desc) {
2256+
$location = 'Prefs';
2257+
if (is_array($desc)) {
2258+
list($desc, $location) = $desc;
2259+
}
2260+
2261+
$value = $sender->Form->getValue($pref, null);
2262+
if (is_null($value)) {
2263+
continue;
2264+
}
2265+
2266+
if ($location == 'Meta') {
2267+
// $newMetaPrefs[$pref] = $value ? $value : null;
2268+
// if ($value) {
2269+
// $userPrefs[$pref] = $value; // dup for notifications code.
2270+
// }
2271+
} else {
2272+
if (!$currentPrefs[$pref] && !$value) {
2273+
unset($userPrefs[$pref]); // save some space
2274+
} else {
2275+
$userPrefs[$pref] = $value;
2276+
}
2277+
}
2278+
}
2279+
}
2280+
2281+
$sender->UserModel->savePreference($sender->User->UserID, $userPrefs);
2282+
// UserModel::setMeta($this->User->UserID, $newMetaPrefs, 'Preferences.');
2283+
$sender->setData('Preferences', array_merge($sender->data('Preferences', []), $userPrefs, $newMetaPrefs));
2284+
2285+
if (count($sender->Form->errors() == 0)) {
2286+
$sender->informMessage(sprite('Check', 'InformSprite').t('Your preferences have been saved.'), 'Dismissable AutoDismiss HasSprite');
2287+
}
2288+
} else {
2289+
$sender->Form->setData($currentPrefs);
2290+
}
2291+
2292+
$sender->title(t('Notification Preferences'));
2293+
$sender->_setBreadcrumbs($sender->data('Title'), $sender->canonicalUrl());
2294+
$sender->render();
2295+
}
21252296
}
21262297

21272298
if(!function_exists('topcoderRatingCssClass')) {

0 commit comments

Comments
 (0)