Skip to content

v1.6 #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Apr 18, 2021
Merged

v1.6 #93

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions DebugPlugin/controllers/api/CacheApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,79 @@ public function get_flush() {
return false;
}
}

public function get_extendedstats(array $query) {
$this->permission('Garden.Settings.Manage');
$in = $this->schema([
'type:s' => 'The type of statistics to fetch(stats, detail, cachedump, slabs, items, sizes)',
'slabid:i?' => 'Slab ID',
'limit:i?' => 'Limit the number of entries to dump'
], 'in')->setDescription('Get server statistics');

$query = $in->validate($query);
$type = $query['type'];
$slabID = $query['slabid'];
$limit = $query['limit'];

if(Gdn_Cache::activeCache()) {
$pos = strrpos(getenv('MEMCACHED_SERVER'), ':');
$server = substr(getenv('MEMCACHED_SERVER'), 0, $pos);
$port = substr(getenv('MEMCACHED_SERVER'), $pos+1);
switch ($type) {
case 'slabs':
return $this->sendMemcacheCommand($server, $port,'stats slabs');
case 'stats':
return $this->sendMemcacheCommand($server, $port,'stats');
case 'items':
return $this->sendMemcacheCommand($server, $port,'stats items');
case 'sizes':
return $this->sendMemcacheCommand($server, $port,'stats sizes');
case 'detail_on':
return $this->sendMemcacheCommand($server, $port,'stats detail on');
case 'detail_off':
return $this->sendMemcacheCommand($server, $port,'stats detail off');
case 'detail_dump':
return $this->sendMemcacheCommand($server, $port,'stats detail dump');
case 'cachedump':
if(!$slabID) {
return 'Missing slabid';
}
$limit = isset($limit)? $limit:100;
return $this->sendMemcacheCommand($server, $port,'stats cachedump '.$slabID.' '.$limit);
default:
return 'Not supported';
}
} else {
return 'Cached disabled';
}
}

function sendMemcacheCommand($server,$port,$command){

$s = @fsockopen($server,$port);
if (!$s){
die("Cant connect to:".$server.':'.$port);
}

fwrite($s, $command."\r\n");

$buf='';
while ((!feof($s))) {
$buf .= fgets($s, 256);
if (strpos($buf,"END\r\n")!==false){ // stat says end
break;
}
if (strpos($buf,"DELETED\r\n")!==false || strpos($buf,"NOT_FOUND\r\n")!==false){ // delete says these
break;
}
if (strpos($buf,"OK\r\n")!==false){
break;
}
if (strpos($buf,"ERROR\r\n")!==false){
break;
}
}
fclose($s);
return $buf;
}
}
24 changes: 24 additions & 0 deletions DebugPlugin/openapi/cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ paths:
tags:
- Cache
summary: Invalidate all items in the cache
/cache/extendedstats:
get:
parameters:
- description: The type of statistics to fetch
in: query
name: type
schema:
type: string
- description: Slab ID
in: query
name: slabid
schema:
type: int
- description: Limit
in: query
name: limit
schema:
type: int
responses:
'200':
description: Memcached stats
tags:
- Cache
summary: Get Memcached stats
components:
schemas:
Records:
Expand Down
2 changes: 2 additions & 0 deletions ReplyTo/design/replyto.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
}
.MessageList .ItemDiscussion .Item-Body .Controls a.ReplyViewOptionLink, .ReplyViewOptions .ReplyViewOptionLink {
color: #137d60;
font-weight:500;
}

.MessageList .ItemDiscussion .Item-Body .Controls a.ReplyViewOptionLink.Active, .ReplyViewOptions .ReplyViewOptionLink.Active {
color: #555555;
font-weight:500;
}
/*
Indent comments according to their depth.
Expand Down
91 changes: 85 additions & 6 deletions Topcoder/class.topcoder.plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class TopcoderPlugin extends Gdn_Plugin {
const CACHE_TOPCODER_KEY_TOPCODER_ROLE_RESOURCES = 'topcoder.roleresources';
const CACHE_TOPCODER_KEY_TOPCODER_CHALLENGE_RESOURCES = 'topcoder.challenge.{ChallengeID}.resources';

const CACHE_DEFAULT_EXPIRY_TIME = 60*60*3; //The default expiration time in Memcached is in seconds, 10800 = 3 hours
const CACHE_TOPCODER_PROFILE_EXPIRY_TIME = 60*60*24*7; // 1 week
const CACHE_ONE_DAY_EXPIRY_TIME = 60*60*24; // 1 day

const ROLE_TYPE_TOPCODER = 'topcoder';
const ROLE_TOPCODER_CONNECT_ADMIN = 'Connect Admin';
const ROLE_TOPCODER_ADMINISTRATOR = 'administrator';
Expand Down Expand Up @@ -679,6 +683,21 @@ public function base_afterSignIn_handler($sender, $args) {
}
self::log('base_afterSignIn_handler', ['Session Permissions' => Gdn::session()->getPermissionsArray()]);

if(Gdn_Cache::activeEnabled()) {
$currentUser = Gdn::session()->User;
$lastVisit = val('DateLastActive', $currentUser, false);
if ($lastVisit) {
$seconds = now() - Gdn_Format::toTimestamp($lastVisit);
if ($seconds > self::CACHE_ONE_DAY_EXPIRY_TIME) { // Update the current User once a day
// remove from Topcoder cache by UserID and Topcoder handle
self::removeTopcoderUserFromCache($currentUser->UserID);
self::removeUserFromTopcoderCache($currentUser->Name);
// update cache
self::getTopcoderUserFromTopcoderCache($currentUser->Name);
self::getTopcoderUserFromCache($currentUser->UserID);
}
}
}
}

/**
Expand Down Expand Up @@ -1362,7 +1381,7 @@ public function getRoleResources() {
private static function topcoderRoleResourcesCache($roleResources) {
return Gdn::cache()->store(self::CACHE_TOPCODER_KEY_TOPCODER_ROLE_RESOURCES,
$roleResources, [
Gdn_Cache::FEATURE_EXPIRY => 3600
Gdn_Cache::FEATURE_EXPIRY => self::CACHE_ONE_DAY_EXPIRY_TIME
]);
}

Expand Down Expand Up @@ -1426,9 +1445,20 @@ public function getChallengeResources($challengeId) {
return $challengeResources;
}

$expirationTime = self::CACHE_DEFAULT_EXPIRY_TIME;
$challenge = self::loadChallenge($challengeId);
if($challenge && count($challenge) > 0) {
// Set expiration time for Challenge roles
$endDate = strtotime($challenge[0]->endDate);
$startDate = strtotime($challenge[0]->startDate);
// $duration = $endDate > -1 && $startDate > -1 ? $endDate - $startDate: 0;
// archived
$isEnded = $endDate > -1 && now() - $endDate > 0;
$expirationTime = $isEnded ? self::CACHE_DEFAULT_EXPIRY_TIME: self::CACHE_ONE_DAY_EXPIRY_TIME;
}
$challengeResources = self::loadChallengeResources($challengeId);
if(Gdn_Cache::activeEnabled() && $challengeResources) {
self::topcoderChallengeResourcesCache( $challengeId, $challengeResources);
self::topcoderChallengeResourcesCache( $challengeId, $challengeResources, $expirationTime);
}
return $challengeResources;
}
Expand All @@ -1454,10 +1484,10 @@ private static function getChallengeResourcesFromCache($challengeID) {
return $challengeResources;
}

private static function topcoderChallengeResourcesCache($challengeID, $challengeResources) {
private static function topcoderChallengeResourcesCache($challengeID, $challengeResources, $expirationTime = self::CACHE_DEFAULT_EXPIRY_TIME) {
$challengeKey = formatString(self::CACHE_TOPCODER_KEY_TOPCODER_CHALLENGE_RESOURCES, ['ChallengeID' => $challengeID]);
return Gdn::cache()->store($challengeKey , $challengeResources, [
Gdn_Cache::FEATURE_EXPIRY => 3600
Gdn_Cache::FEATURE_EXPIRY => $expirationTime
]);
}

Expand Down Expand Up @@ -1490,6 +1520,35 @@ private static function loadChallengeResources($challengeId) {
return null;
}

/**
* Load Topcoder Challenge by Challenge ID
* @param $challengeId
* @return mixed|null
*/
private static function loadChallenge($challengeId) {
$token = TopcoderPlugin::getM2MToken();
if ($token) {
$challengeURI = c('Plugins.Topcoder.ChallengeApiURI', '/v5/challenges/');
$topcoderChallengeApiUrl = c('Plugins.Topcoder.BaseApiURL') . $challengeURI;
$options = array('http' => array(
'method' => 'GET',
'header' => 'Authorization: Bearer ' .$token
));
$context = stream_context_create($options);
$data = file_get_contents($topcoderChallengeApiUrl . '?challengeId=' . $challengeId, false, $context);
if ($data === false) {
// Handle errors (e.g. 404 and others)
self::log('Couldn\'t get challenge: no token', ['headers'=> json_encode($http_response_header)]);
logMessage(__FILE__, __LINE__, 'TopcoderPlugin', 'loadChallenge', "Couldn't load Topcoder challenge".json_encode($http_response_header));
return null;
}

return json_decode($data);
}
self::log('Couldn\'t load challenge: no token', []);
return null;
}

/**
* Get a Topcoder Roles
*
Expand Down Expand Up @@ -1795,7 +1854,7 @@ private static function topcoderUserCache($userFields) {
$userID = val('UserID', $userFields);
$userKey = formatString(self::CACHE_KEY_TOPCODER_PROFILE, ['UserID' => $userID]);
$cached = $cached & Gdn::cache()->store($userKey, $userFields, [
Gdn_Cache::FEATURE_EXPIRY => 3600
Gdn_Cache::FEATURE_EXPIRY => self::CACHE_TOPCODER_PROFILE_EXPIRY_TIME
]);
return $cached;
}
Expand Down Expand Up @@ -1826,7 +1885,26 @@ public static function getTopcoderUserByHandle($topcoderHandle) {
return $topcoderUser;
}

private static function removeTopcoderUserFromCache($userID) {
if(!Gdn_Cache::activeEnabled()) {
return false;
}

$handleKey = formatString(self::CACHE_KEY_TOPCODER_PROFILE, ['UserID' => $userID]);
return Gdn::cache()->remove($handleKey);
}

// This cache includes Topcoder which might not exist in Vanilla
private static function removeUserFromTopcoderCache($topcoderHandle) {
if (!Gdn_Cache::activeEnabled()) {
return false;
}

$handleKey = formatString(self::CACHE_TOPCODER_KEY_TOPCODER_PROFILE, ['Handle' => $topcoderHandle]);
return Gdn::cache()->remove($handleKey);
}

// This cache includes Topcoder which might not exist in Vanilla
private static function getTopcoderUserFromTopcoderCache($topcoderHandle) {
if(!Gdn_Cache::activeEnabled()) {
return false;
Expand Down Expand Up @@ -1887,7 +1965,7 @@ private static function topcoderUserTopcoderCache($userFields) {
$handle = val('Handle', $userFields);
$userKey = formatString(self::CACHE_TOPCODER_KEY_TOPCODER_PROFILE, ['Handle' => $handle]);
$cached = $cached & Gdn::cache()->store($userKey, $userFields, [
Gdn_Cache::FEATURE_EXPIRY => 3600
Gdn_Cache::FEATURE_EXPIRY => self::CACHE_TOPCODER_PROFILE_EXPIRY_TIME
]);
return $cached;
}
Expand Down Expand Up @@ -2163,6 +2241,7 @@ function userAnchor($user, $cssClass = null, $options = null) {

Gdn::controller()->EventArguments['User'] = $user;
Gdn::controller()->EventArguments['IsTopcoderAdmin'] =$isTopcoderAdmin;
Gdn::controller()->EventArguments['HideRoles'] = val('HideRoles', $options, false);
Gdn::controller()->EventArguments['Text'] =& $text;
Gdn::controller()->EventArguments['Attributes'] =& $attributes;
Gdn::controller()->fireEvent('UserAnchor');
Expand Down
22 changes: 22 additions & 0 deletions Voting/addon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"key": "Voting",
"name": "Voting",
"description": "This plugin allows users to vote for comments and discussions.",
"version": "1.0.0",
"documentationUrl": "http://discussions.topcoder.com",
"type": "addon",
"priority": "100",
"icon": "topcoder-logo.jpeg",
"mobileFriendly": true,
"hasLocale": false,
"authors": [
{
"name": "Topcoder",
"email": "support@topcoder.com",
"homepage": "https://topcoder.com"
}
],
"require": {
"vanilla": ">=3.0"
}
}
Loading