Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default stream sort by created_at instead of id #5315

Merged
merged 17 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

use yii\db\Migration;

/**
* Class m210928_162609_stream_sort_idx
*/
class m210928_162609_stream_sort_idx extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->createIndex('idx_stream_created', 'content', 'created_at', false);
$this->createIndex('idx_stream_updated', 'content', 'stream_sort_date', false);
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
echo "m210928_162609_stream_sort_idx cannot be reverted.\n";

return false;
}

/*
// Use up()/down() to run migration code without a transaction.
public function up()
{

}

public function down()
{
echo "m210928_162609_stream_sort_idx cannot be reverted.\n";

return false;
}
*/
}
4 changes: 2 additions & 2 deletions protected/humhub/modules/content/models/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public function beforeSave($insert)
$this->created_by ??= Yii::$app->user->id;
}

$this->stream_sort_date = date('Y-m-d G:i:s');
$this->stream_sort_date = date('Y-m-d H:i:s');

if ($this->created_by == "") {
throw new Exception("Could not save content without created_by!");
Expand Down Expand Up @@ -1033,7 +1033,7 @@ public function checkGuestAccess()
*/
public function updateStreamSortTime()
{
$this->updateAttributes(['stream_sort_date' => date('Y-m-d G:i:s')]);
$this->updateAttributes(['stream_sort_date' => date('Y-m-d H:i:s')]);
}

/**
Expand Down
24 changes: 21 additions & 3 deletions protected/humhub/modules/stream/models/StreamQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,9 @@ protected function setupCriteria()
return;
}

/**
* Setup Sorting
*/
/**
* Setup Sorting
*/
Expand All @@ -549,13 +552,28 @@ protected function setupCriteria()
], [':to' => $this->to]);
}
} else {
$this->_query->orderBy('content.id DESC');
$this->_query->orderBy('content.created_at DESC,content.id DESC');
if (!empty($this->from)) {
$this->_query->andWhere("content.id < :from", [':from' => $this->from]);
$this->_query->andWhere(
['or',
"content.created_at < (SELECT created_at FROM content wd WHERE wd.id=:from)",
['and',
"content.created_at = (SELECT created_at FROM content wd WHERE wd.id=:from)",
"content.id > :from"
],
], [':from' => $this->from]);
} elseif (!empty($this->to)) {
$this->_query->andWhere("content.id > :to", [':to' => $this->to]);
$this->_query->andWhere(
['or',
"content.created_at > (SELECT created_at FROM content wd WHERE wd.id=:to)",
['and',
"content.created_at = (SELECT created_at FROM content wd WHERE wd.id=:to)",
"content.id < :to"
],
], [':to' => $this->to]);
}
}

}

/**
Expand Down