Initial commit

This commit is contained in:
Joey Kimsey
2024-08-04 21:15:59 -04:00
parent c9d1fb983f
commit 0d469501ee
695 changed files with 70184 additions and 71 deletions

View File

@ -0,0 +1,145 @@
<?php
/**
* app/plugins/chat/models/chat.php
*
* This class is used for the manipulation of the chat database table.
*
* @todo make this send a confirmation email
*
* @package TP Chat
* @version 3.0
* @author Joey Kimsey <Joey@thetempusproject.com>
* @link https://TheTempusProject.com
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
*/
namespace TheTempusProject\Models;
use TheTempusProject\Bedrock\Classes\Config;
use TheTempusProject\Bedrock\Functions\Check;
use TheTempusProject\Canary\Canary as Debug;
use TheTempusProject\Classes\DatabaseModel;
use TheTempusProject\TheTempusProject as App;
use TheTempusProject\Houdini\Classes\Filters;
class Chat extends DatabaseModel {
public $tableName = 'chat';
public $databaseMatrix = [
[ 'submittedAt', 'int', '10' ],
[ 'submittedBy', 'int', '11' ],
[ 'chatMessage', 'text', '' ],
];
/**
* The model constructor.
*/
public function __construct() {
parent::__construct();
}
/**
* Saves a chat form to the db.
*
* @param string $message -contents of the chat form.
* @return bool
*/
public function create( $message ) {
$fields = [
'submittedBy' => App::$activeUser->ID,
'submittedAt' => time(),
'chatMessage' => $message,
];
if ( !self::$db->insert( $this->tableName, $fields ) ) {
Debug::info( 'Chat::create - failed to insert to db' );
return false;
}
return self::$db->lastId();
}
public function filter( $data, $params = [] ) {
foreach ( $data as $instance ) {
if ( !is_object( $instance ) ) {
$instance = $data;
$end = true;
}
$instance->chatMessage = Filters::applyOne( 'mentions.0', $instance->chatMessage, true );
$instance->chatMessage = Filters::applyOne( 'hashtags.0', $instance->chatMessage, true );
$user = self::$user->findById( $instance->submittedBy );
if ( ! empty( $user ) ) {
$instance->submittedByName = $user->username;
$instance->profileUrl = '/home/profile/' . $user->username;
} else {
$instance->submittedByName = 'Unknown';
$instance->profileUrl = '#';
}
$instance->avatar = self::$user->getAvatar( $instance->submittedBy );
$out[] = $instance;
if ( !empty( $end ) ) {
$out = $out[0];
break;
}
}
return $out;
}
/**
* Function to clear chat from the DB.
*
* @todo is there a way i could check for success here I'm pretty sure this is just a bad idea?
* @return bool
*/
public function clear() {
if ( empty( self::$log ) ) {
self::$log = new Log;
}
self::$db->delete( $this->tableName, ['ID', '>=', '0'] );
self::$log->admin( 'Cleared Chat' );
Debug::info( 'Chat Cleared' );
return true;
}
public function recent( $limit = null ) {
if ( empty( $limit ) ) {
$postData = self::$db->get( $this->tableName, '*' );
} else {
$postData = self::$db->get( $this->tableName, '*', 'ID', 'ASC', [0, $limit] );
}
if ( !$postData->count() ) {
Debug::info( 'No messages found.' );
return false;
}
return $this->filter( $postData->results() );
}
public function sinceMessage($id = 0) {
if (empty($id)) {
$postData = self::$db->get($this->tableName, '*', 'ID', 'DESC', [0, 20]);
} else {
$postData = self::$db->get($this->tableName, ['ID', '>', $id], 'ID', 'ASC', [0, 20]);
}
if ( ! $postData->count() ) {
Debug::debug( 'No messages found.' );
return false;
}
if (empty($id)) {
$results = array_reverse($postData->results()); // Reverse the order to get ascending IDs
} else {
$results = $postData->results();
}
return $this->filter($results);
}
public function sinceMessageSingle( $id = 0 ) {
if ( empty( $id ) ) {
$postData = self::$db->get( $this->tableName, '*', 'ID', 'ASC', [0, 1] );
} else {
$postData = self::$db->get( $this->tableName, [ 'ID', '>', $id ], 'ID', 'ASC', [0, 1] );
}
if ( ! $postData->count() ) {
Debug::debug( 'No messages found.' );
return false;
}
return $this->filter( $postData->results() );
}
}