Skip to content

Commit b07f1ef

Browse files
authored
Merge pull request #88 from topcoder-platform/issues-475_2
Issues 475-2, 558
2 parents 55a7110 + b6d8087 commit b07f1ef

File tree

2 files changed

+92
-8
lines changed

2 files changed

+92
-8
lines changed

Topcoder/class.topcoder.plugin.php

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ class TopcoderPlugin extends Gdn_Plugin {
3535
const CACHE_TOPCODER_KEY_TOPCODER_ROLE_RESOURCES = 'topcoder.roleresources';
3636
const CACHE_TOPCODER_KEY_TOPCODER_CHALLENGE_RESOURCES = 'topcoder.challenge.{ChallengeID}.resources';
3737

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+
3842
const ROLE_TYPE_TOPCODER = 'topcoder';
3943
const ROLE_TOPCODER_CONNECT_ADMIN = 'Connect Admin';
4044
const ROLE_TOPCODER_ADMINISTRATOR = 'administrator';
@@ -679,6 +683,21 @@ public function base_afterSignIn_handler($sender, $args) {
679683
}
680684
self::log('base_afterSignIn_handler', ['Session Permissions' => Gdn::session()->getPermissionsArray()]);
681685

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+
}
682701
}
683702

684703
/**
@@ -1362,7 +1381,7 @@ public function getRoleResources() {
13621381
private static function topcoderRoleResourcesCache($roleResources) {
13631382
return Gdn::cache()->store(self::CACHE_TOPCODER_KEY_TOPCODER_ROLE_RESOURCES,
13641383
$roleResources, [
1365-
Gdn_Cache::FEATURE_EXPIRY => 3600
1384+
Gdn_Cache::FEATURE_EXPIRY => self::CACHE_ONE_DAY_EXPIRY_TIME
13661385
]);
13671386
}
13681387

@@ -1426,9 +1445,20 @@ public function getChallengeResources($challengeId) {
14261445
return $challengeResources;
14271446
}
14281447

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+
}
14291459
$challengeResources = self::loadChallengeResources($challengeId);
14301460
if(Gdn_Cache::activeEnabled() && $challengeResources) {
1431-
self::topcoderChallengeResourcesCache( $challengeId, $challengeResources);
1461+
self::topcoderChallengeResourcesCache( $challengeId, $challengeResources, $expirationTime);
14321462
}
14331463
return $challengeResources;
14341464
}
@@ -1454,10 +1484,10 @@ private static function getChallengeResourcesFromCache($challengeID) {
14541484
return $challengeResources;
14551485
}
14561486

1457-
private static function topcoderChallengeResourcesCache($challengeID, $challengeResources) {
1487+
private static function topcoderChallengeResourcesCache($challengeID, $challengeResources, $expirationTime = self::CACHE_DEFAULT_EXPIRY_TIME) {
14581488
$challengeKey = formatString(self::CACHE_TOPCODER_KEY_TOPCODER_CHALLENGE_RESOURCES, ['ChallengeID' => $challengeID]);
14591489
return Gdn::cache()->store($challengeKey , $challengeResources, [
1460-
Gdn_Cache::FEATURE_EXPIRY => 3600
1490+
Gdn_Cache::FEATURE_EXPIRY => $expirationTime
14611491
]);
14621492
}
14631493

@@ -1490,6 +1520,35 @@ private static function loadChallengeResources($challengeId) {
14901520
return null;
14911521
}
14921522

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+
14931552
/**
14941553
* Get a Topcoder Roles
14951554
*
@@ -1795,7 +1854,7 @@ private static function topcoderUserCache($userFields) {
17951854
$userID = val('UserID', $userFields);
17961855
$userKey = formatString(self::CACHE_KEY_TOPCODER_PROFILE, ['UserID' => $userID]);
17971856
$cached = $cached & Gdn::cache()->store($userKey, $userFields, [
1798-
Gdn_Cache::FEATURE_EXPIRY => 3600
1857+
Gdn_Cache::FEATURE_EXPIRY => self::CACHE_TOPCODER_PROFILE_EXPIRY_TIME
17991858
]);
18001859
return $cached;
18011860
}
@@ -1826,7 +1885,26 @@ public static function getTopcoderUserByHandle($topcoderHandle) {
18261885
return $topcoderUser;
18271886
}
18281887

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+
18291897
// 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
18301908
private static function getTopcoderUserFromTopcoderCache($topcoderHandle) {
18311909
if(!Gdn_Cache::activeEnabled()) {
18321910
return false;
@@ -1887,7 +1965,7 @@ private static function topcoderUserTopcoderCache($userFields) {
18871965
$handle = val('Handle', $userFields);
18881966
$userKey = formatString(self::CACHE_TOPCODER_KEY_TOPCODER_PROFILE, ['Handle' => $handle]);
18891967
$cached = $cached & Gdn::cache()->store($userKey, $userFields, [
1890-
Gdn_Cache::FEATURE_EXPIRY => 3600
1968+
Gdn_Cache::FEATURE_EXPIRY => self::CACHE_TOPCODER_PROFILE_EXPIRY_TIME
18911969
]);
18921970
return $cached;
18931971
}

Voting/class.voting.plugin.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,8 @@ function formattedPScore($score)
346346
}
347347

348348
if (!function_exists('generateVoterBox')) {
349-
function generateVoterBox($id, $VoteType, $pScore, $nScore, $currentUserVote)
350-
{
349+
function generateVoterBox($id, $VoteType, $pScore, $nScore, $currentUserVote) {
350+
351351
$cssClassVoteUp = 'SpriteVoteUp';
352352
$cssClassVoteDown = 'SpriteVoteDown';
353353
if($currentUserVote > 0) {
@@ -359,6 +359,12 @@ function generateVoterBox($id, $VoteType, $pScore, $nScore, $currentUserVote)
359359
$voterBoxID = 'Voter_' . $VoteType . '_' . $id;
360360
$voteUpUrl = '/discussion/vote' . strtolower($VoteType) . '/' . $id . '/voteup/' . Gdn::session()->TransientKey() . '/';
361361
$voteDownUrl = '/discussion/vote' . strtolower($VoteType) . '/' . $id . '/votedown/' . Gdn::session()->TransientKey() . '/';
362+
363+
if (!Gdn::session()->IsValid()) {
364+
$voteUpUrl = Gdn::Authenticator()->SignInUrl(Gdn::controller()->SelfUrl);
365+
$voteDownUrl = Gdn::Authenticator()->SignInUrl(Gdn::controller()->SelfUrl);
366+
}
367+
362368
$result = '<span id="' . $voterBoxID . '" class="Voter">';
363369
$result .= Anchor(Wrap('', 'span', array('class' => 'icon ' . $cssClassVoteUp, 'rel' => 'nofollow')), $voteUpUrl, 'VoteUp');
364370
$counts = formattedPScore($pScore);

0 commit comments

Comments
 (0)