diff --git a/CHANGELOG.md b/CHANGELOG.md index b48d253b2..3e7ce67a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ v0.18.1 (trunk) * Chats/Rooms redesign, common button to start/create a conversation * Improve the PresenceBuffer SQL requests * Automatically close SQL connections after a few seconds + * Conferences and Subscriptions multiple inserts SQL optimisations v0.18 --------------------------- diff --git a/app/Conference.php b/app/Conference.php index c4c804d43..28136f6b8 100644 --- a/app/Conference.php +++ b/app/Conference.php @@ -22,6 +22,11 @@ class Conference extends Model 'session_id' => SESSION_ID ]; + public static function saveMany(array $conferences) + { + return Conference::insert($conferences); + } + public function session() { return $this->hasOne('App\Session'); @@ -185,4 +190,21 @@ class Conference extends Model return false; } + + public function toArray() + { + $now = \Carbon\Carbon::now(); + return [ + 'session_id' => $this->attributes['session_id'] ?? null, + 'conference' => $this->attributes['conference'] ?? null, + 'name' => $this->attributes['name'] ?? null, + 'nick' => $this->attributes['nick'] ?? null, + 'autojoin' => $this->attributes['autojoin'] ?? null, + 'created_at' => $this->attributes['created_at'] ?? $now, + 'updated_at' => $this->attributes['updated_at'] ?? $now, + 'extensions' => $this->attributes['extensions'] ?? null, + 'bookmarkversion' => $this->attributes['bookmarkversion'] ?? 0, + 'notify' => $this->attributes['notify'] ?? 1, + ]; + } } diff --git a/app/Subscription.php b/app/Subscription.php index 1279d56b4..b585b5be9 100644 --- a/app/Subscription.php +++ b/app/Subscription.php @@ -10,6 +10,11 @@ class Subscription extends Model protected $primaryKey = ['jid', 'server', 'node']; protected $guarded = []; + public static function saveMany(array $conferences) + { + return Subscription::insert($conferences); + } + public function info() { return $this->hasOne('App\Info', 'server', 'server') @@ -20,4 +25,19 @@ class Subscription extends Model { return $this->hasOne('App\Contact', 'id', 'jid'); } + + public function toArray() + { + $now = \Carbon\Carbon::now(); + return [ + 'jid' => $this->attributes['jid'] ?? null, + 'server' => $this->attributes['server'] ?? null, + 'node' => $this->attributes['node'] ?? null, + 'subid' => $this->attributes['subid'] ?? null, + 'title' => $this->attributes['title'] ?? null, + 'public' => $this->attributes['public'] ?? false, + 'created_at' => $this->attributes['created_at'] ?? $now, + 'updated_at' => $this->attributes['updated_at'] ?? $now, + ]; + } } diff --git a/app/widgets/Chat/Chat.php b/app/widgets/Chat/Chat.php index fc3b94dc4..0e040a34e 100644 --- a/app/widgets/Chat/Chat.php +++ b/app/widgets/Chat/Chat.php @@ -651,6 +651,7 @@ class Chat extends \Movim\Widget\Base : $messages->whereIn('type', $this->_messageTypes); $messages = $messages->orderBy('published', 'desc') + ->withCount('reactions') ->take($this->_pagination) ->get(); @@ -800,7 +801,7 @@ class Chat extends \Movim\Widget\Base $messagesRequest = clone $messagesQuery; $messagesCount = clone $messagesQuery; - $messages = $messagesRequest->orderBy('published', 'desc')->take($this->_pagination)->get(); + $messages = $messagesRequest->withCount('reactions')->orderBy('published', 'desc')->take($this->_pagination)->get(); $unreadsCount = $messagesCount->where('seen', false)->count(); if ($unreadsCount > 0) { @@ -957,7 +958,7 @@ class Chat extends \Movim\Widget\Base } // Reactions - if ($message->reactions()->count()) { + if ($message->reactions_count) { $message->reactionsHtml = $this->prepareReactions($message); } diff --git a/lib/moxl/src/Xec/Action/Bookmark2/Get.php b/lib/moxl/src/Xec/Action/Bookmark2/Get.php index c1f38c00f..5d00691ab 100644 --- a/lib/moxl/src/Xec/Action/Bookmark2/Get.php +++ b/lib/moxl/src/Xec/Action/Bookmark2/Get.php @@ -25,12 +25,16 @@ class Get extends Action ->where('bookmarkversion', (int)$this->_version) ->delete(); + $conferences = []; + foreach ($stanza->pubsub->items->item as $c) { $conference = new Conference; $conference->set($c); - $conference->save(); + array_push($conferences, $conference->toArray()); } + Conference::saveMany($conferences); + $this->pack($this->_version); $this->deliver(); } diff --git a/lib/moxl/src/Xec/Action/PubsubSubscription/Get.php b/lib/moxl/src/Xec/Action/PubsubSubscription/Get.php index 63fe31ffe..d3fb18f33 100644 --- a/lib/moxl/src/Xec/Action/PubsubSubscription/Get.php +++ b/lib/moxl/src/Xec/Action/PubsubSubscription/Get.php @@ -2,6 +2,7 @@ namespace Moxl\Xec\Action\PubsubSubscription; +use App\Subscription; use Moxl\Xec\Action\Pubsub\Errors; use Moxl\Stanza\Pubsub; @@ -22,6 +23,8 @@ class Get extends Errors ->where('public', ($this->_pepnode == 'urn:xmpp:pubsub:subscription')) ->delete(); + $subscriptions = []; + foreach ($stanza->pubsub->items->children() as $i) { $subscription = \App\Subscription::firstOrNew([ 'jid' => $this->_to, @@ -32,9 +35,14 @@ class Get extends Errors if ($this->_pepnode == 'urn:xmpp:pubsub:subscription') { $subscription->public = true; } - $subscription->save(); + + if (!$subscription->exists) { + array_push($subscriptions, $subscription->toArray()); + } } + Subscription::saveMany($subscriptions); + $this->pack($this->_to); $this->deliver(); }