Browse Source

Conferences and Subscriptions multiple inserts SQL optimisations

pull/978/head
Timothée Jaussoin 5 years ago
parent
commit
f7b23b72f3
  1. 1
      CHANGELOG.md
  2. 22
      app/Conference.php
  3. 20
      app/Subscription.php
  4. 5
      app/widgets/Chat/Chat.php
  5. 6
      lib/moxl/src/Xec/Action/Bookmark2/Get.php
  6. 10
      lib/moxl/src/Xec/Action/PubsubSubscription/Get.php

1
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
---------------------------

22
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,
];
}
}

20
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,
];
}
}

5
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);
}

6
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();
}

10
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();
}

Loading…
Cancel
Save