Browse Source

removed a lot of unnecessary duplication

master
Lennart Weller 6 years ago
parent
commit
caec9461cd
2 changed files with 21 additions and 86 deletions
  1. +5
    -18
      controllers/class.shoutboxcontroller.php
  2. +16
    -68
      models/class.shoutmodel.php

+ 5
- 18
controllers/class.shoutboxcontroller.php View File

@@ -57,6 +57,7 @@ class ShoutboxController extends Gdn_Controller {
}

$start = time();
$init = true;
printf("retry: %s\n", $RECONNECT);

while(true) {
@@ -65,27 +66,13 @@ class ShoutboxController extends Gdn_Controller {
printf(": %s\n\n", sha1(mt_rand()));

if(!$this->ShoutModel->IsLatest($LastEventID)) {
//get create events
foreach($this->ShoutModel->CreateEventsByLastID($LastEventID) as $msg) {
foreach($this->ShoutModel->GetRecentByID($LastEventID, $init) as $msg) {
printf("id: %d\n", $msg["EventID"]);
printf("event: create\n");
printf("data: %s\n\n", $this->ShoutModel->GetJSONItem($msg));
if($LastEventID < $msg["EventID"]) $LastEventID = $msg["EventID"];
}
//get delete events
foreach($this->ShoutModel->DeleteEventsByLastID($LastEventID) as $msg) {
printf("id: %d\n", $msg["EventID"]);
printf("event: delete\n");
printf("data: %s\n\n", $this->ShoutModel->GetJSONItem($msg));
if($LastEventID < $msg["EventID"]) $LastEventID = $msg["EventID"];
}
//get edit events
foreach($this->ShoutModel->EditEventsByLastID($LastEventID) as $msg) {
printf("id: %d\n", $msg["EventID"]);
printf("event: edit\n");
printf("data: %s\n\n", $this->ShoutModel->GetJSONItem($msg));
printf("event: %s\n", strtolower($msg["EventType"]));
printf("data: %s\n\n", $this->ShoutModel->GetJSONMessage($msg));
if($LastEventID < $msg["EventID"]) $LastEventID = $msg["EventID"];
}
$init = false;
}

@ob_flush();


+ 16
- 68
models/class.shoutmodel.php View File

@@ -2,87 +2,35 @@

class ShoutModel extends Gdn_Model {

public function GetRecent($Limit = 50) {
$Session = GDN::Session();

$shouts = $this->SQL
->Select('*')
->From('Shoutbox')
->BeginWhereGroup()
->Where('MessageTo', 0)
->OrWhere('MessageTo', $Session->UserID)
->EndWhereGroup()
->OrderBy('EventID', 'desc')
->Limit($Limit)
->Get()
->ResultArray();
return array_reverse($shouts);
}

public function CreateEventsByLastID($EventID, $Limit = 50) {
public function GetRecentByID($EventID, $Init = false, $Limit = 50) {
if(!is_numeric($EventID)) return false;
$Session = GDN::Session();

$shouts = $this->SQL
->Select('*')
->From('Shoutbox')
->Where('EventID >', $EventID)
->Where('EventType', 'CREATE')
->BeginWhereGroup()
->Where('MessageTo', 0)
->OrWhere('MessageTo', $Session->UserID)
->EndWhereGroup()
->OrderBy('EventID', 'desc')
->Limit($Limit)
->Get()
->ResultArray();

return array_reverse($shouts);
}

public function DeleteEventsByLastID($EventID) {
if(!is_numeric($EventID)) return false;
$Session = GDN::Session();

$shouts = $this->SQL
$sql = $this->SQL
->Select('*')
->From('Shoutbox')
->Where('EventID >', $EventID)
->Where('EventType', 'DELETE')
->BeginWhereGroup()
->BeginWhereGroup()
->Where('MessageTo', 0)
->OrWhere('MessageTo', $Session->UserID)
->EndWhereGroup()
->OrderBy('EventID', 'desc')
->Get()
->ResultArray();

return array_reverse($shouts);
}
->From('Shoutbox');

public function EditEventsByLastID($EventID) {
if(!is_numeric($EventID)) return false;
$Session = GDN::Session();
if($Init)
$sql->Where('EventType', 'CREATE');
else
$sql->Where('OriginalID >=', $EventID);

$shouts = $this->SQL
->Select('*')
->From('Shoutbox')
$shouts = $sql
->Where('EventID >', $EventID)
->Where('EventType', 'EDIT')
->BeginWhereGroup()
->BeginWhereGroup()
->Where('MessageTo', 0)
->OrWhere('MessageTo', $Session->UserID)
->EndWhereGroup()
->OrderBy('EventID', 'desc')
->Limit($Limit)
->Get()
->ResultArray();

return array_reverse($shouts);
}

public function GetShout($EventID) {
public function GetID($EventID) {
if(!is_numeric($EventID)) return false;
$Session = GDN::Session();

@@ -119,7 +67,7 @@ class ShoutModel extends Gdn_Model {
return $lastShout->EventID <= $EventID;
}

public function AddShout($Content, $MessageTo = 0) {
public function Add($Content, $MessageTo = 0) {
$Session = GDN::Session();

$this->SQL->Insert('Shoutbox', array(
@@ -137,10 +85,10 @@ class ShoutModel extends Gdn_Model {
return true;
}

public function DeleteShout($EventID) {
public function Delete($EventID) {
if (!is_numeric($EventID)) return false;

$shout = GetShout($EventID);
$shout = GetID($EventID);
if(!$shout) return false;

$this->SQL->Delete('Shoutbox', array('EventID' => $EventID));
@@ -155,10 +103,10 @@ class ShoutModel extends Gdn_Model {
return true;
}

public function EditShout($EventID, $Content) {
public function Edit($EventID, $Content) {
if (!is_numeric($EventID)) return false;

$shout = GetShout($EventID);
$shout = GetID($EventID);
if(!$shout) return false;

$this->SQL->Update('Shoutbox')
@@ -201,7 +149,7 @@ class ShoutModel extends Gdn_Model {
return $ColorArray[$sum % count($ColorArray)];
}

public function GetJSONItem($msg) {
public function GetJSONMessage($msg) {
$UserModel = new UserModel();
if($msg['OriginalID'] == 0) {
$msg['UserName'] = $UserModel->GetID($msg['UserID'])->Name;


Loading…
Cancel
Save