Browse Source

edit/delete events etc

master
Lennart Weller 7 years ago
parent
commit
9c9195381a
6 changed files with 117 additions and 19 deletions
  1. +1
    -0
      .gitignore
  2. +19
    -11
      controllers/class.shoutboxcontroller.php
  3. +90
    -4
      models/class.shoutmodel.php
  4. +2
    -2
      settings/about.php
  5. +3
    -2
      settings/configuration.php
  6. +2
    -0
      settings/structure.php

+ 1
- 0
.gitignore View File

@ -0,0 +1 @@
*.swp

+ 19
- 11
controllers/class.shoutboxcontroller.php View File

@ -52,10 +52,8 @@ class ShoutboxController extends Gdn_Controller {
$KEEP_ALIVE_TIME = C('Shoutbox.SSE.KeepAliveTime', 30); //seconds
//Got a an event id from the browser.
$init = true;
if(isset($_SERVER["HTTP_LAST_EVENT_ID"]) && $_SERVER["HTTP_LAST_EVENT_ID"] > $LastEventID) {
$LastEventID = $_SERVER["HTTP_LAST_EVENT_ID"];
$init = false;
}
$start = time();
@ -67,16 +65,26 @@ class ShoutboxController extends Gdn_Controller {
printf(": %s\n\n", sha1(mt_rand()));
if(!$this->ShoutModel->IsLatest($LastEventID)) {
foreach($this->ShoutModel->GetRecentByLastEventID($LastEventID) as $msg) {
//get create events
foreach($this->ShoutModel->CreateEventsByLastID($LastEventID) as $msg) {
printf("id: %d\n", $msg["EventID"]);
printf("event: %s\n", $init? "init": "update");
$msg["UserName"] = $this->UserModel->GetID($msg["UserID"])->Name;
$msg["NameColor"] = $this->ShoutModel->GetColor($msg["UserName"]);
if ($msg["MessageTo"] != 0)
$msg["MessageToName"] = $this->UserModel->GetID($msg["MessageTo"])->Name;
$msg["Content"] = $this->ShoutModel->PrepareText($msg["Content"]);
printf("data: %s\n\n", json_encode($msg));
$LastEventID = $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));
if($LastEventID < $msg["EventID"]) $LastEventID = $msg["EventID"];
}
}


+ 90
- 4
models/class.shoutmodel.php View File

@ -1,6 +1,7 @@
<?php if(!defined('APPLICATION')) exit();
class ShoutModel extends Gdn_Model {
public function GetRecent($Limit = 50) {
$Session = GDN::Session();
@ -18,7 +19,7 @@ class ShoutModel extends Gdn_Model {
return array_reverse($shouts);
}
public function GetRecentByLastEventID($EventID, $Limit = 50) {
public function CreateEventsByLastID($EventID, $Limit = 50) {
if(!is_numeric($EventID)) return false;
$Session = GDN::Session();
@ -26,7 +27,7 @@ class ShoutModel extends Gdn_Model {
->Select('*')
->From('Shoutbox')
->Where('EventID >', $EventID)
->AndOp()
->Where('EventType', 'CREATE')
->BeginWhereGroup()
->Where('MessageTo', 0)
->OrWhere('MessageTo', $Session->UserID)
@ -39,6 +40,48 @@ class ShoutModel extends Gdn_Model {
return array_reverse($shouts);
}
public function DeleteEventsByLastID($EventID) {
if(!is_numeric($EventID)) return false;
$Session = GDN::Session();
$shouts = $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);
}
public function EditEventsByLastID($EventID) {
if(!is_numeric($EventID)) return false;
$Session = GDN::Session();
$shouts = $this->SQL
->Select('*')
->From('Shoutbox')
->Where('EventID >', $EventID)
->Where('EventType', 'EDIT')
->BeginWhereGroup()
->BeginWhereGroup()
->Where('MessageTo', 0)
->OrWhere('MessageTo', $Session->UserID)
->EndWhereGroup()
->OrderBy('EventID', 'desc')
->Get()
->ResultArray();
return array_reverse($shouts);
}
public function GetShout($EventID) {
if(!is_numeric($EventID)) return false;
$Session = GDN::Session();
@ -83,6 +126,7 @@ class ShoutModel extends Gdn_Model {
'UserID' => $Session->UserID,
'MessageTo' => $MessageTo,
'Content' => $Content,
'EventType' => 'CREATE',
'Timestamp' => time()
));
return true;
@ -95,18 +139,41 @@ class ShoutModel extends Gdn_Model {
public function DeleteShout($EventID) {
if (!is_numeric($EventID)) return false;
$shout = GetShout($EventID);
if(!$shout) return false;
$this->SQL->Delete('Shoutbox', array('EventID' => $EventID));
$this->SQL->Insert('Shoutbox', array(
'UserID' => $shout->UserID,
'MessageTo' => $shout->MessageTo,
'Content' => $shout->Content,
'EventType' => 'DELETE',
'OriginalID' => $EventID,
'Timestamp' => time()
));
return true;
}
public function EditShout($EventID, $Content) {
if (!is_numeric($EventID)) return false;
$this->SQL
->Update('Shoutbox')
$shout = GetShout($EventID);
if(!$shout) return false;
$this->SQL->Update('Shoutbox')
->Set('Content', $Content)
->Where('EventID', $EventID)
->Put();
$this->Insert('Shoutbox', array(
'UserID' => $shout->UserID,
'MessageTo' => $shout->MessageTo,
'Content' => $Content,
'EventType' => 'UPDATE',
'OriginalID' => $EventID,
'Timestamp' => time()
));
return true;
}
@ -133,4 +200,23 @@ class ShoutModel extends Gdn_Model {
}
return $ColorArray[$sum % count($ColorArray)];
}
public function GetJSONItem($msg) {
$UserModel = new UserModel();
if($msg['OriginalID'] == 0) {
$msg['UserName'] = $UserModel->GetID($msg['UserID'])->Name;
$msg['NameColor'] = $this->GetColor($msg['UserName']);
$msg['Content'] = $this->PrepareText($msg['Content']);
if($msg['MessageTo'] != 0)
$msg['MessageToName'] = $UserModel->GetID($msg['MessageTo'])->Name;
else unset($msg['MessageTo']);
unset($msg['OriginalID']);
}
else {
unset($msg['UserID']);
unset($msg['MessageTo']);
if($msg['EventType'] == 'DELETE') unset($msg['Content']);
}
return json_encode($msg);
}
}

+ 2
- 2
settings/about.php View File

@ -5,8 +5,8 @@
$ApplicationInfo['Shoutbox'] = array(
'Description' => "A simple and elegant shoutbox for vanilla forums",
'Version' => '0.1',
'SettingsPermission' => array('Shoutbox.View', 'Shoutbox.Post', 'Shoutbox.Delete'),
'RegisterPermissions' => array('Shoutbox.View', 'Shoutbox.Post', 'Shoutbox.Delete'),
'SettingsPermission' => array('Shoutbox.View', 'Shoutbox.Post', 'Shoutbox.Delete'),
'RegisterPermissions' => array('Shoutbox.View', 'Shoutbox.Post', 'Shoutbox.Delete'),
'SetupController' => 'setup',
'Author' => "Lennart Weller",
'AuthorEmail' => 'lhw@ring0.de',


+ 3
- 2
settings/configuration.php View File

@ -16,7 +16,8 @@ $Configuration['Shoutbox']['Client']['Colors'] = array(
'#8ae234', '#4e9a06', #Chameleon
'#3465a4', '#204a87', #Sky Blue
'#ad7fa8', '#5c3566', #Plum
'#ef2929', '#cc0000', '#a40000',#Scarlet Red
'#2e3436' #Aluminium
'#ef2929', '#cc0000', '#a40000', #Scarlet Red
'#2e3436' #Aluminium
);
$Configuration['Shoutbox']['Client']['MessageLimit'] = 50;
$Configuration['Shoutbox']['Client']['MessageLength'] = 256;

+ 2
- 0
settings/structure.php View File

@ -50,6 +50,8 @@ if($Construct->TableExists()) {
//Create basis table for the shoutbox application
$Construct->PrimaryKey('EventID')
->Column('UserID', 'int', FALSE)
->Column('EventType', array('CREATE', 'EDIT', 'DELETE'), FALSE)
->Column('OriginalID', 'int', TRUE)
->Column('MessageTo', 'int', TRUE)
->Column('Timestamp', 'int(11)')
->Column('Content', 'text')


Loading…
Cancel
Save