@@ -35,6 +35,10 @@ class TopcoderPlugin extends Gdn_Plugin {
35
35
const CACHE_TOPCODER_KEY_TOPCODER_ROLE_RESOURCES = 'topcoder.roleresources ' ;
36
36
const CACHE_TOPCODER_KEY_TOPCODER_CHALLENGE_RESOURCES = 'topcoder.challenge.{ChallengeID}.resources ' ;
37
37
38
+ const CACHE_DEFAULT_EXPIRY_TIME = 60 *60 *3 ; //The default expiration time in Memcached is in seconds, 10800 = 3 hours
39
+ const CACHE_TOPCODER_PROFILE_EXPIRY_TIME = 60 *60 *24 *7 ; // 1 week
40
+ const CACHE_ONE_DAY_EXPIRY_TIME = 60 *60 *24 ; // 1 day
41
+
38
42
const ROLE_TYPE_TOPCODER = 'topcoder ' ;
39
43
const ROLE_TOPCODER_CONNECT_ADMIN = 'Connect Admin ' ;
40
44
const ROLE_TOPCODER_ADMINISTRATOR = 'administrator ' ;
@@ -679,6 +683,21 @@ public function base_afterSignIn_handler($sender, $args) {
679
683
}
680
684
self ::log ('base_afterSignIn_handler ' , ['Session Permissions ' => Gdn::session ()->getPermissionsArray ()]);
681
685
686
+ if (Gdn_Cache::activeEnabled ()) {
687
+ $ currentUser = Gdn::session ()->User ;
688
+ $ lastVisit = val ('DateLastActive ' , $ currentUser , false );
689
+ if ($ lastVisit ) {
690
+ $ seconds = now () - Gdn_Format::toTimestamp ($ lastVisit );
691
+ if ($ seconds > self ::CACHE_ONE_DAY_EXPIRY_TIME ) { // Update the current User once a day
692
+ // remove from Topcoder cache by UserID and Topcoder handle
693
+ self ::removeTopcoderUserFromCache ($ currentUser ->UserID );
694
+ self ::removeUserFromTopcoderCache ($ currentUser ->Name );
695
+ // update cache
696
+ self ::getTopcoderUserFromTopcoderCache ($ currentUser ->Name );
697
+ self ::getTopcoderUserFromCache ($ currentUser ->UserID );
698
+ }
699
+ }
700
+ }
682
701
}
683
702
684
703
/**
@@ -1362,7 +1381,7 @@ public function getRoleResources() {
1362
1381
private static function topcoderRoleResourcesCache ($ roleResources ) {
1363
1382
return Gdn::cache ()->store (self ::CACHE_TOPCODER_KEY_TOPCODER_ROLE_RESOURCES ,
1364
1383
$ roleResources , [
1365
- Gdn_Cache::FEATURE_EXPIRY => 3600
1384
+ Gdn_Cache::FEATURE_EXPIRY => self :: CACHE_ONE_DAY_EXPIRY_TIME
1366
1385
]);
1367
1386
}
1368
1387
@@ -1426,9 +1445,20 @@ public function getChallengeResources($challengeId) {
1426
1445
return $ challengeResources ;
1427
1446
}
1428
1447
1448
+ $ expirationTime = self ::CACHE_DEFAULT_EXPIRY_TIME ;
1449
+ $ challenge = self ::loadChallenge ($ challengeId );
1450
+ if ($ challenge && count ($ challenge ) > 0 ) {
1451
+ // Set expiration time for Challenge roles
1452
+ $ endDate = strtotime ($ challenge [0 ]->endDate );
1453
+ $ startDate = strtotime ($ challenge [0 ]->startDate );
1454
+ // $duration = $endDate > -1 && $startDate > -1 ? $endDate - $startDate: 0;
1455
+ // archived
1456
+ $ isEnded = $ endDate > -1 && now () - $ endDate > 0 ;
1457
+ $ expirationTime = $ isEnded ? self ::CACHE_DEFAULT_EXPIRY_TIME : self ::CACHE_ONE_DAY_EXPIRY_TIME ;
1458
+ }
1429
1459
$ challengeResources = self ::loadChallengeResources ($ challengeId );
1430
1460
if (Gdn_Cache::activeEnabled () && $ challengeResources ) {
1431
- self ::topcoderChallengeResourcesCache ( $ challengeId , $ challengeResources );
1461
+ self ::topcoderChallengeResourcesCache ( $ challengeId , $ challengeResources, $ expirationTime );
1432
1462
}
1433
1463
return $ challengeResources ;
1434
1464
}
@@ -1454,10 +1484,10 @@ private static function getChallengeResourcesFromCache($challengeID) {
1454
1484
return $ challengeResources ;
1455
1485
}
1456
1486
1457
- private static function topcoderChallengeResourcesCache ($ challengeID , $ challengeResources ) {
1487
+ private static function topcoderChallengeResourcesCache ($ challengeID , $ challengeResources, $ expirationTime = self :: CACHE_DEFAULT_EXPIRY_TIME ) {
1458
1488
$ challengeKey = formatString (self ::CACHE_TOPCODER_KEY_TOPCODER_CHALLENGE_RESOURCES , ['ChallengeID ' => $ challengeID ]);
1459
1489
return Gdn::cache ()->store ($ challengeKey , $ challengeResources , [
1460
- Gdn_Cache::FEATURE_EXPIRY => 3600
1490
+ Gdn_Cache::FEATURE_EXPIRY => $ expirationTime
1461
1491
]);
1462
1492
}
1463
1493
@@ -1490,6 +1520,35 @@ private static function loadChallengeResources($challengeId) {
1490
1520
return null ;
1491
1521
}
1492
1522
1523
+ /**
1524
+ * Load Topcoder Challenge by Challenge ID
1525
+ * @param $challengeId
1526
+ * @return mixed|null
1527
+ */
1528
+ private static function loadChallenge ($ challengeId ) {
1529
+ $ token = TopcoderPlugin::getM2MToken ();
1530
+ if ($ token ) {
1531
+ $ challengeURI = c ('Plugins.Topcoder.ChallengeApiURI ' , '/v5/challenges/ ' );
1532
+ $ topcoderChallengeApiUrl = c ('Plugins.Topcoder.BaseApiURL ' ) . $ challengeURI ;
1533
+ $ options = array ('http ' => array (
1534
+ 'method ' => 'GET ' ,
1535
+ 'header ' => 'Authorization: Bearer ' .$ token
1536
+ ));
1537
+ $ context = stream_context_create ($ options );
1538
+ $ data = file_get_contents ($ topcoderChallengeApiUrl . '?challengeId= ' . $ challengeId , false , $ context );
1539
+ if ($ data === false ) {
1540
+ // Handle errors (e.g. 404 and others)
1541
+ self ::log ('Couldn \'t get challenge: no token ' , ['headers ' => json_encode ($ http_response_header )]);
1542
+ logMessage (__FILE__ , __LINE__ , 'TopcoderPlugin ' , 'loadChallenge ' , "Couldn't load Topcoder challenge " .json_encode ($ http_response_header ));
1543
+ return null ;
1544
+ }
1545
+
1546
+ return json_decode ($ data );
1547
+ }
1548
+ self ::log ('Couldn \'t load challenge: no token ' , []);
1549
+ return null ;
1550
+ }
1551
+
1493
1552
/**
1494
1553
* Get a Topcoder Roles
1495
1554
*
@@ -1795,7 +1854,7 @@ private static function topcoderUserCache($userFields) {
1795
1854
$ userID = val ('UserID ' , $ userFields );
1796
1855
$ userKey = formatString (self ::CACHE_KEY_TOPCODER_PROFILE , ['UserID ' => $ userID ]);
1797
1856
$ cached = $ cached & Gdn::cache ()->store ($ userKey , $ userFields , [
1798
- Gdn_Cache::FEATURE_EXPIRY => 3600
1857
+ Gdn_Cache::FEATURE_EXPIRY => self :: CACHE_TOPCODER_PROFILE_EXPIRY_TIME
1799
1858
]);
1800
1859
return $ cached ;
1801
1860
}
@@ -1826,7 +1885,26 @@ public static function getTopcoderUserByHandle($topcoderHandle) {
1826
1885
return $ topcoderUser ;
1827
1886
}
1828
1887
1888
+ private static function removeTopcoderUserFromCache ($ userID ) {
1889
+ if (!Gdn_Cache::activeEnabled ()) {
1890
+ return false ;
1891
+ }
1892
+
1893
+ $ handleKey = formatString (self ::CACHE_KEY_TOPCODER_PROFILE , ['UserID ' => $ userID ]);
1894
+ return Gdn::cache ()->remove ($ handleKey );
1895
+ }
1896
+
1829
1897
// This cache includes Topcoder which might not exist in Vanilla
1898
+ private static function removeUserFromTopcoderCache ($ topcoderHandle ) {
1899
+ if (!Gdn_Cache::activeEnabled ()) {
1900
+ return false ;
1901
+ }
1902
+
1903
+ $ handleKey = formatString (self ::CACHE_TOPCODER_KEY_TOPCODER_PROFILE , ['Handle ' => $ topcoderHandle ]);
1904
+ return Gdn::cache ()->remove ($ handleKey );
1905
+ }
1906
+
1907
+ // This cache includes Topcoder which might not exist in Vanilla
1830
1908
private static function getTopcoderUserFromTopcoderCache ($ topcoderHandle ) {
1831
1909
if (!Gdn_Cache::activeEnabled ()) {
1832
1910
return false ;
@@ -1887,7 +1965,7 @@ private static function topcoderUserTopcoderCache($userFields) {
1887
1965
$ handle = val ('Handle ' , $ userFields );
1888
1966
$ userKey = formatString (self ::CACHE_TOPCODER_KEY_TOPCODER_PROFILE , ['Handle ' => $ handle ]);
1889
1967
$ cached = $ cached & Gdn::cache ()->store ($ userKey , $ userFields , [
1890
- Gdn_Cache::FEATURE_EXPIRY => 3600
1968
+ Gdn_Cache::FEATURE_EXPIRY => self :: CACHE_TOPCODER_PROFILE_EXPIRY_TIME
1891
1969
]);
1892
1970
return $ cached ;
1893
1971
}
@@ -2163,6 +2241,7 @@ function userAnchor($user, $cssClass = null, $options = null) {
2163
2241
2164
2242
Gdn::controller ()->EventArguments ['User ' ] = $ user ;
2165
2243
Gdn::controller ()->EventArguments ['IsTopcoderAdmin ' ] =$ isTopcoderAdmin ;
2244
+ Gdn::controller ()->EventArguments ['HideRoles ' ] = val ('HideRoles ' , $ options , false );
2166
2245
Gdn::controller ()->EventArguments ['Text ' ] =& $ text ;
2167
2246
Gdn::controller ()->EventArguments ['Attributes ' ] =& $ attributes ;
2168
2247
Gdn::controller ()->fireEvent ('UserAnchor ' );
0 commit comments