Skip to content

Commit

Permalink
Optimize sql query to get files from the stream
Browse files Browse the repository at this point in the history
  • Loading branch information
yurabakhtin committed Sep 20, 2024
1 parent 447ac43 commit 1c02421
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Changelog
- Enh #224: Unifying positions of button on modals for consistency and better UX
- Enh #227: Use PHP CS Fixer
- Fix: Add autofocus on file or folder edit (for HumHub 1.17 - see https://github.com/humhub/humhub/issues/7136)
- Fix #230: Optimize sql query to get files from the stream

0.16.6 - March 14, 2024
-------------------------
Expand Down
51 changes: 29 additions & 22 deletions models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use humhub\modules\file\libs\FileHelper;
use humhub\modules\file\models\File as BaseFile;
use humhub\modules\file\models\FileUpload;
use humhub\modules\post\models\Post;
use humhub\modules\search\events\SearchAddEvent;
use humhub\modules\topic\models\Topic;
use humhub\modules\user\models\User;
Expand Down Expand Up @@ -323,7 +324,7 @@ public function getEditUrl()
/**
* Get the post related to the given file file.
*/
public static function getBasePost(\humhub\modules\file\models\File $file = null)
public static function getBasePost(BaseFile $file = null)
{
if ($file === null) {
return null;
Expand Down Expand Up @@ -386,31 +387,37 @@ public function getFullPath($separator = '/')
*/
public static function getPostedFiles($contentContainer, $filesOrder = ['file.updated_at' => SORT_ASC, 'file.title' => SORT_ASC])
{
// Get Posted Files
$query = \humhub\modules\file\models\File::find();
// join comments to the file if available
$query->join('LEFT JOIN', 'comment', '(file.object_id=comment.id AND file.object_model=' . Yii::$app->db->quoteValue(Comment::className()) . ')');
// join parent post of comment or file
$query->join('LEFT JOIN', 'content', '(comment.object_model=content.object_model AND comment.object_id=content.object_id) OR (file.object_model=content.object_model AND file.object_id=content.object_id)');
// only accept Posts as the base content, so stuff from sumbmodules like files itsself or gallery will be excluded

$query->andWhere(['content.contentcontainer_id' => $contentContainer->contentContainerRecord->id]);
// Initialise sub queries to get files from Posts and Comments
$subQueries = [
Post::class => Content::find()
->select('content.object_id')
->where(['content.object_model' => Post::class]),
Comment::class => Content::find()
->select('comment.id')
->innerJoin('comment', 'comment.object_model = content.object_model AND comment.object_id = content.object_id')
->where(['comment.object_model' => Post::class]),
];

if(!$contentContainer->canAccessPrivateContent()) {
// Note this will cut comment images, but including the visibility of comments is pretty complex...
$query->andWhere(['content.visibility' => Content::VISIBILITY_PUBLIC]);
}
$query = BaseFile::find();

$query->andWhere(['content.state' => Content::STATE_PUBLISHED]);
foreach ($subQueries as $objectClass => $subQuery) {
// Filter Content records by container and visibility states
$subQuery->andWhere(['content.contentcontainer_id' => $contentContainer->contentContainerRecord->id])
->andWhere(['content.state' => Content::STATE_PUBLISHED]);
if (!$contentContainer->canAccessPrivateContent()) {
// Note this will cut comment images, but including the visibility of comments is pretty complex...
$subQuery->andWhere(['content.visibility' => Content::VISIBILITY_PUBLIC]);
}

$query->orWhere([
'AND',
['file.object_model' => $objectClass],
['IN', 'file.object_id', $subQuery],
]);
}

// only accept Posts as the base content, so stuff from sumbmodules like files itsself or gallery will be excluded
$query->andWhere(
['or',
['=', 'comment.object_model', \humhub\modules\post\models\Post::className()],
['=', 'file.object_model', \humhub\modules\post\models\Post::className()],
],
);

// Get Files from comments
return $query->orderBy($filesOrder);
}

Expand Down

0 comments on commit 1c02421

Please sign in to comment.