Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
@@ -11,26 +11,47 @@
|
||||
namespace IPS\core\Stream;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use BadMethodCallException;
|
||||
use IPS\Content\Comment;
|
||||
use IPS\Content\Search\Query;
|
||||
use IPS\Content\Search\SearchContent;
|
||||
use IPS\core\Stream;
|
||||
use IPS\DateTime;
|
||||
use IPS\Db;
|
||||
use IPS\Email;
|
||||
use IPS\Member;
|
||||
use IPS\Patterns\ActiveRecord;
|
||||
use IPS\Patterns\ActiveRecordIterator;
|
||||
use IPS\Settings;
|
||||
use OutOfRangeException;
|
||||
use UnderflowException;
|
||||
use function array_slice;
|
||||
use function count;
|
||||
use function defined;
|
||||
use function get_called_class;
|
||||
use function in_array;
|
||||
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Content Discovery Stream Subscription
|
||||
*/
|
||||
class _Subscription extends \IPS\Patterns\ActiveRecord
|
||||
class Subscription extends ActiveRecord
|
||||
{
|
||||
/**
|
||||
* @brief [ActiveRecord] Multiton Store
|
||||
*/
|
||||
protected static $multitons;
|
||||
protected static array $multitons;
|
||||
|
||||
/**
|
||||
* @brief [ActiveRecord] Database Table
|
||||
*/
|
||||
public static $databaseTable = 'core_stream_subscriptions';
|
||||
public static ?string $databaseTable = 'core_stream_subscriptions';
|
||||
|
||||
|
||||
/*
|
||||
@@ -38,19 +59,24 @@ class _Subscription extends \IPS\Patterns\ActiveRecord
|
||||
*
|
||||
* return \IPS\core\Stream
|
||||
*/
|
||||
public function get_stream() : \IPS\core\Stream
|
||||
public function get_stream() : Stream
|
||||
{
|
||||
return \IPS\core\Stream::load( $this->stream_id) ;
|
||||
return Stream::load( $this->stream_id) ;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send Digest
|
||||
*
|
||||
* @param array $data
|
||||
* @param Stream $stream
|
||||
* @param Member $recipient
|
||||
* @param array $subscriptionRow
|
||||
* @param bool $showMoreLink
|
||||
* @return void
|
||||
*/
|
||||
public function send( array $data, $stream, \IPS\Member $recipient, $subscriptionRow, $showMoreLink = FALSE )
|
||||
public function send( array $data, Stream $stream, Member $recipient, array $subscriptionRow, bool $showMoreLink = FALSE ) : void
|
||||
{
|
||||
$email = \IPS\Email::buildFromTemplate( 'core', 'activity_stream_subscription', array( $stream, $data, $recipient, $subscriptionRow, $showMoreLink ), \IPS\Email::TYPE_LIST );
|
||||
$email = Email::buildFromTemplate( 'core', 'activity_stream_subscription', array( $stream, $data, $recipient, $subscriptionRow, $showMoreLink ), Email::TYPE_LIST );
|
||||
$email->setUnsubscribe( 'core', 'unsubscribeStream', array( $subscriptionRow['id'], $stream->_title, md5( $recipient->email . ';' . $recipient->ip_address . ';' . $recipient->joined->getTimestamp() ) ) );
|
||||
|
||||
$email->send($recipient);
|
||||
@@ -63,14 +89,13 @@ class _Subscription extends \IPS\Patterns\ActiveRecord
|
||||
* @param int $numberToSend The number of digests to send for this batch
|
||||
* @return bool
|
||||
*/
|
||||
public static function sendBatch( $frequency = 'daily', $numberToSend = 25 )
|
||||
public static function sendBatch( string $frequency = 'daily', int $numberToSend = 25 ) : bool
|
||||
{
|
||||
/* @note SELECT_FROM_WRITE_SERVER to prevent further cycles from having data on the write but not read servers */
|
||||
$subscriptions = iterator_to_array(
|
||||
\IPS\Db::i()->select( 'core_stream_subscriptions.*, core_members.last_visit', 'core_stream_subscriptions', array('frequency = ? AND sent < ? and last_visit > ?', $frequency, ( $frequency == 'daily' ) ? time() - 86400 : time() - 604800, time() - \IPS\Settings::i()->activity_stream_subscriptions_inactive_limit * 86400), 'sent ASC', array(0, $numberToSend), NULL, NULL, \IPS\Db::SELECT_DISTINCT | \IPS\Db::SELECT_FROM_WRITE_SERVER )
|
||||
Db::i()->select( 'core_stream_subscriptions.*, core_members.last_visit', 'core_stream_subscriptions', array('frequency = ? AND sent < ? and last_visit > ?', $frequency, ( $frequency == 'daily' ) ? time() - 86400 : time() - 604800, time() - Settings::i()->activity_stream_subscriptions_inactive_limit * 86400), 'sent ASC', array(0, $numberToSend), NULL, NULL, Db::SELECT_DISTINCT | Db::SELECT_FROM_WRITE_SERVER )
|
||||
->join( 'core_members', 'core_stream_subscriptions.member_id=core_members.member_id') );
|
||||
|
||||
if ( !\count( $subscriptions ) )
|
||||
if ( !count( $subscriptions ) )
|
||||
{
|
||||
/* Nothing to send */
|
||||
return FALSE;
|
||||
@@ -79,11 +104,11 @@ class _Subscription extends \IPS\Patterns\ActiveRecord
|
||||
$ids = [];
|
||||
foreach ( $subscriptions as $row )
|
||||
{
|
||||
$member = \IPS\Member::load( $row['member_id'] );
|
||||
$member = Member::load( $row['member_id'] );
|
||||
if ( !$member->email or $member->isBanned() )
|
||||
{
|
||||
/* Update sent time, so the batch doesn't get stuck in a loop */
|
||||
\IPS\Db::i()->update( 'core_stream_subscriptions', array( 'sent' => time() ), array( 'id=?', $row['id'] ) );
|
||||
Db::i()->update( 'core_stream_subscriptions', array( 'sent' => time() ), array( 'id=?', $row['id'] ) );
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -91,40 +116,40 @@ class _Subscription extends \IPS\Patterns\ActiveRecord
|
||||
$mail = new static;
|
||||
$data = $mail->getContentForStream( $row );
|
||||
$ids[] = $row['id'];
|
||||
if( $items = \count( $data ) )
|
||||
if( $items = count( $data ) )
|
||||
{
|
||||
$showMore = FALSE;
|
||||
if( $items > 10 )
|
||||
{
|
||||
$data = \array_slice($data, 0, 10);
|
||||
$data = array_slice($data, 0, 10);
|
||||
$showMore = TRUE;
|
||||
}
|
||||
|
||||
$mail->send( $data, \IPS\core\Stream::load( $row['stream_id'] ), \IPS\Member::load( $row['member_id'] ), $row, $showMore );
|
||||
$mail->send( $data, Stream::load( $row['stream_id'] ), Member::load( $row['member_id'] ), $row, $showMore );
|
||||
}
|
||||
}
|
||||
|
||||
if( \count( $ids ) )
|
||||
if( count( $ids ) )
|
||||
{
|
||||
\IPS\Db::i()->update( 'core_stream_subscriptions', array( 'sent' => time() ), \IPS\Db::i()->in( 'id', $ids ) );
|
||||
Db::i()->update( 'core_stream_subscriptions', array( 'sent' => time() ), Db::i()->in( 'id', $ids ) );
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \IPS\core\Stream $stream
|
||||
* @param \IPS\Member|null $member
|
||||
* @param Stream $stream
|
||||
* @param Member|null $member
|
||||
* @return Subscription|null
|
||||
*/
|
||||
public static function loadByStreamAndMember( \IPS\core\Stream $stream, \IPS\Member $member = NULL ) : ?\IPS\core\Stream\Subscription
|
||||
public static function loadByStreamAndMember( Stream $stream, Member $member = NULL ) : ?Subscription
|
||||
{
|
||||
$member = $member ?: \IPS\Member::loggedIn();
|
||||
$member = $member ?: Member::loggedIn();
|
||||
try
|
||||
{
|
||||
return static::constructFromData( \IPS\Db::i()->select('*', static::$databaseTable, ['stream_id=? AND member_id=?', $stream->id, $member->member_id ] )->first() );
|
||||
return static::constructFromData( Db::i()->select('*', static::$databaseTable, ['stream_id=? AND member_id=?', $stream->id, $member->member_id ] )->first() );
|
||||
}
|
||||
catch( \UnderflowException $e )
|
||||
catch( UnderflowException $e )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
@@ -139,18 +164,18 @@ class _Subscription extends \IPS\Patterns\ActiveRecord
|
||||
public function getContentForStream( array $subscriptionRow ) : array
|
||||
{
|
||||
$items = [];
|
||||
$stream = \IPS\core\Stream::load( $subscriptionRow['stream_id'] );
|
||||
$stream = Stream::load( $subscriptionRow['stream_id'] );
|
||||
|
||||
$query = $stream->query( \IPS\Member::load( $subscriptionRow['member_id']) );
|
||||
$query = $stream->query( Member::load( $subscriptionRow['member_id']) );
|
||||
|
||||
|
||||
/* Override the timeframe and set it to the last sent time */
|
||||
$query->filterByCreateDate( \IPS\DateTime::ts( $subscriptionRow['sent'] ) );
|
||||
$query->filterByCreateDate( DateTime::ts( $subscriptionRow['sent'] ) );
|
||||
/* We want only 10 items for the email, so we'll grab 11 to see if we need to show the "more link" */
|
||||
$query->setLimit(11);
|
||||
|
||||
/* Get the results */
|
||||
$results = $query->search( NULL, $stream->tags ? explode( ',', $stream->tags ) : NULL, ( $stream->include_comments ? \IPS\Content\Search\Query::TAGS_MATCH_ITEMS_ONLY + \IPS\Content\Search\Query::TERM_OR_TAGS : \IPS\Content\Search\Query::TERM_OR_TAGS ) );
|
||||
$results = $query->search( NULL, $stream->tags ? explode( ',', $stream->tags ) : NULL, ( $stream->include_comments ? Query::TAGS_MATCH_ITEMS_ONLY + Query::TERM_OR_TAGS : Query::TERM_OR_TAGS ) );
|
||||
|
||||
/* Load data we need like the authors, etc */
|
||||
$results->init();
|
||||
@@ -161,7 +186,7 @@ class _Subscription extends \IPS\Patterns\ActiveRecord
|
||||
$itemClass = $data['indexData']['index_class'];
|
||||
$object = $itemClass::load( $data['indexData']['index_object_id']);
|
||||
|
||||
if( \in_array( 'IPS\Content\Comment', class_parents( $itemClass ) ) )
|
||||
if( in_array( 'IPS\Content\Comment', class_parents( $itemClass ) ) )
|
||||
{
|
||||
$itemClass = $itemClass::$itemClass;
|
||||
}
|
||||
@@ -175,20 +200,29 @@ class _Subscription extends \IPS\Patterns\ActiveRecord
|
||||
$containerUrl = $containerClass::urlFromIndexData( $data['indexData'], $data['itemData'], $data['containerData'] );
|
||||
}
|
||||
|
||||
$summaryLanguage = null;
|
||||
$title = $object instanceof Comment ? $object->item()->mapped( 'title' ) : $object->mapped( 'title' );
|
||||
if( $extension = SearchContent::extension( $object ) )
|
||||
{
|
||||
$summaryLanguage = $extension::searchResultSummaryLanguage( $data['authorData'], $extension::articlesFromIndexData( $itemClass, $data['containerData'] ), $data['indexData'], $data['itemData'], false );
|
||||
$title = $extension->searchIndexTitle();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$items[] = array_merge($data, [
|
||||
'title' => $object instanceof \IPS\Content\Comment ? $object->item()->searchIndexTitle() : $object->searchIndexTitle(),
|
||||
'title' => $title,
|
||||
'url' => $object->url(),
|
||||
'content' => $object->content(),
|
||||
'object' => $object,
|
||||
'date' => \IPS\DateTime::ts( $object->mapped('date') ),
|
||||
'date' => DateTime::ts( $object->mapped('date') ),
|
||||
'itemClass' => $itemClass,
|
||||
'containerUrl' => $containerUrl,
|
||||
'containerTitle' => $containerTitle
|
||||
'containerTitle' => $containerTitle,
|
||||
'summaryLanguage' => $summaryLanguage
|
||||
]);
|
||||
}
|
||||
catch( \OutOfRangeException|\BadMethodCallException $e )
|
||||
catch( OutOfRangeException|BadMethodCallException $e )
|
||||
{
|
||||
/* If the item is no longer available, we'll just skip it */
|
||||
}
|
||||
@@ -200,25 +234,25 @@ class _Subscription extends \IPS\Patterns\ActiveRecord
|
||||
/**
|
||||
* Has the member any subscribed streams
|
||||
*
|
||||
* @param \IPS\Member|null $member
|
||||
* @param Member|null $member
|
||||
* @return bool
|
||||
*/
|
||||
public static function hasSubscribedStreams( \IPS\Member $member = NULL ) : bool
|
||||
public static function hasSubscribedStreams( Member $member = NULL ) : bool
|
||||
{
|
||||
$member = $member ?: \IPS\Member::loggedIn();
|
||||
return (bool) \IPS\Db::i()->select( 'COUNT(*)', 'core_stream_subscriptions', array( 'member_id=?', $member->member_id ) )->first();
|
||||
$member = $member ?: Member::loggedIn();
|
||||
return (bool) Db::i()->select( 'COUNT(*)', 'core_stream_subscriptions', array( 'member_id=?', $member->member_id ) )->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Has the member any subscribed streams
|
||||
*
|
||||
* @param \IPS\Member|null $member
|
||||
* @return \Iterator
|
||||
* @param Member|null $member
|
||||
* @return ActiveRecordIterator
|
||||
*/
|
||||
public static function getSubscribedStreams( \IPS\Member $member = NULL )
|
||||
public static function getSubscribedStreams( Member $member = NULL ) : ActiveRecordIterator
|
||||
{
|
||||
$member = $member ?: \IPS\Member::loggedIn();
|
||||
return new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'core_stream_subscriptions', array( 'member_id=?', $member->member_id ) ), \get_called_class() );
|
||||
$member = $member ?: Member::loggedIn();
|
||||
return new ActiveRecordIterator( Db::i()->select( '*', 'core_stream_subscriptions', array( 'member_id=?', $member->member_id ) ), get_called_class() );
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user