Skip to content

Commit

Permalink
Reduced queries
Browse files Browse the repository at this point in the history
  • Loading branch information
GodMod committed May 16, 2020
1 parent 6551655 commit badd2ea
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
2 changes: 1 addition & 1 deletion admin/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private function fields(){

$arrValues = $this->config->get_config('discord');
if($arrValues['bot_client_id'] != ""){
$arrFields['general']['bot_token']['after_txt'] = '<a href="https://discordapp.com/oauth2/authorize?&client_id='.$arrValues['bot_client_id'].'&scope=bot&permissions=522304" target="_blank" class="button" onclick="init_functions()">'.$this->user->lang('discord_autorize_bot').'</a>';
$arrFields['general']['bot_token']['after_txt'] = '<div><br /><a href="https://discordapp.com/oauth2/authorize?&client_id='.$arrValues['bot_client_id'].'&scope=bot&permissions=522304" target="_blank" class="button" onclick="init_functions()">'.$this->user->lang('discord_autorize_bot').'</a></div>';
}

if($arrValues['bot_client_id'] == "" && $arrValues['bot_token'] == ""){
Expand Down
2 changes: 1 addition & 1 deletion discord_plugin_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function __shortcuts()
return array_merge(parent::$shortcuts, $shortcuts);
}

public $version = '1.2.1';
public $version = '1.2.2';
public $build = '';
public $copyright = 'GodMod';
public $vstatus = 'Stable';
Expand Down
70 changes: 56 additions & 14 deletions portal/discordpostviewer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public function __construct($id, $blnWideContent=false){
include_once($this->root_path.'plugins/discord/includes/Parsedown.php');
}

public function getChannels(){
$arrOut = array();
public function getChannels($blnReturnRaw = false){
$arrOut = $arrJSON = array();

$arrDiscordConfig = register('config')->get_config('discord');
$guildid = $arrDiscordConfig['guild_id'];
Expand All @@ -49,14 +49,22 @@ public function getChannels(){
}
}

return $arrOut;
return ($blnReturnRaw) ? $arrJSON : $arrOut;
}

public function getPosts($arrPrivateforums, $black_or_white, $topicnumber, $showcontent){
$topicnumber = ($this->config('amount')) ? $this->config('amount') : 5;
$intCachetime = ($this->config('cachetime')) ? (60*intval($this->config('cachetime'))) : (3*60);

$arrForums = $this->getChannels();
$arrLastMessage = $arrForums = array();
$arrRawChannels = $this->getChannels(true);
foreach($arrRawChannels as $val){
if($val['type'] == '0'){
$arrForums[$val['id']] = $val['name'];
$arrLastMessage[$val['id']] = $val['last_message_id'];
}
}

$arrData = $arrTime = array();

$Parsedown = new Parsedown();
Expand All @@ -74,16 +82,18 @@ public function getPosts($arrPrivateforums, $black_or_white, $topicnumber, $show
//BL
if(is_array($arrPrivateforums) && !empty($arrPrivateforums) && in_array($forumID, $arrPrivateforums)) continue;
}


$result = register('urlfetcher')->fetch('https://discordapp.com/api/channels/'.$forumID, array('Authorization: Bot '.$token));
if($result){
$arrJSON = json_decode($result, true);

$strLastMessage = $arrJSON['last_message_id'];

$result = register('urlfetcher')->fetch('https://discordapp.com/api/channels/'.$forumID.'/messages?around='.$strLastMessage.'&limit='.($topicnumber*2), array('Authorization: Bot '.$token));
if($result){
$arrJSON = json_decode($result, true);
$strLastMessage = $arrLastMessage[$forumID];
if(!$strLastMessage) continue;

$strCachedLastMessage = $this->pdc->get('discord.lastmessageid.'.$forumID);

//Load from Cache
if($strCachedLastMessage && ($strLastMessage === $strCachedLastMessage)) {
$arrJSON = $this->pdc->get('discord.messages.'.$forumID);
if($arrJSON){
if(isset($arrJSON['error'])) continue;

foreach($arrJSON as $arrPost){

Expand All @@ -99,10 +109,42 @@ public function getPosts($arrPrivateforums, $black_or_white, $topicnumber, $show

$arrTime[] = strtotime($arrPost['timestamp']);
}

continue;
}
}
}

$this->pdc->put('discord.lastmessageid.'.$forumID, $strLastMessage, 3600*24*7);

$result = register('urlfetcher')->fetch('https://discordapp.com/api/channels/'.$forumID.'/messages?around='.$strLastMessage.'&limit='.($topicnumber*2), array('Authorization: Bot '.$token));
if($result){
$arrJSON = json_decode($result, true);
if($arrJSON){
$this->pdc->put('discord.messages.'.$forumID, $arrJSON, 3600*24*7);
}

foreach($arrJSON as $arrPost){
$arrData[] = array(
'username' => $arrPost['author']['username'],
'content' => nl2br($Parsedown->text($arrPost['content'])),
'topic_link' => 'https://discordapp.com/channels/'.$guildid.'/'.$forumID,
'topic_title' => '#'.$forumName,
'posttime' => $arrPost['timestamp'],
'topic_id' => $forumID,
'avatar' => ($arrPost['author']['avatar']) ? "https://cdn.discordapp.com/avatars/".$arrPost['author']['id']."/".$arrPost['author']['avatar'].".png" : "https://discordapp.com/assets/1cbd08c76f8af6dddce02c5138971129.png",
);

$arrTime[] = strtotime($arrPost['timestamp']);
}


} else {
$arrJSON = array('error' => true);
$this->pdc->put('discord.messages.'.$forumID, $arrJSON, 3600);
d($arrJSON);
}
}

//Now sort the date
array_multisort($arrTime, SORT_DESC, SORT_NUMERIC, $arrData);

Expand Down

0 comments on commit badd2ea

Please sign in to comment.