You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
4.4 KiB

  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OCA\Spreed\Signaling;
  22. use OCA\Spreed\Room;
  23. use OCP\AppFramework\Utility\ITimeFactory;
  24. use OCP\DB\QueryBuilder\IQueryBuilder;
  25. use OCP\IDBConnection;
  26. class Messages {
  27. /** @var IDBConnection */
  28. protected $db;
  29. /** @var ITimeFactory */
  30. protected $time;
  31. /**
  32. * @param IDBConnection $db
  33. * @param ITimeFactory $time
  34. */
  35. public function __construct(IDBConnection $db, ITimeFactory $time) {
  36. $this->db = $db;
  37. $this->time = $time;
  38. }
  39. /**
  40. * @param string[] $sessionIds
  41. */
  42. public function deleteMessages(array $sessionIds) {
  43. $query = $this->db->getQueryBuilder();
  44. $query->delete('videocalls_signaling')
  45. ->where($query->expr()->in('recipient', $query->createNamedParameter($sessionIds, IQueryBuilder::PARAM_STR_ARRAY)))
  46. ->orWhere($query->expr()->in('sender', $query->createNamedParameter($sessionIds, IQueryBuilder::PARAM_STR_ARRAY)));
  47. $query->execute();
  48. }
  49. /**
  50. * @param string $senderSessionId
  51. * @param string $recipientSessionId
  52. * @param string $message
  53. */
  54. public function addMessage($senderSessionId, $recipientSessionId, $message) {
  55. $query = $this->db->getQueryBuilder();
  56. $query->insert('videocalls_signaling')
  57. ->values(
  58. [
  59. 'sender' => $query->createNamedParameter($senderSessionId),
  60. 'recipient' => $query->createNamedParameter($recipientSessionId),
  61. 'timestamp' => $query->createNamedParameter($this->time->getTime()),
  62. 'message' => $query->createNamedParameter($message),
  63. ]
  64. );
  65. $query->execute();
  66. }
  67. /**
  68. * @param Room $room
  69. * @param string $message
  70. */
  71. public function addMessageForAllParticipants(Room $room, $message) {
  72. $query = $this->db->getQueryBuilder();
  73. $query->insert('videocalls_signaling')
  74. ->values(
  75. [
  76. 'sender' => $query->createParameter('sender'),
  77. 'recipient' => $query->createParameter('recipient'),
  78. 'timestamp' => $query->createNamedParameter($this->time->getTime()),
  79. 'message' => $query->createNamedParameter($message),
  80. ]
  81. );
  82. foreach ($room->getActiveSessions() as $sessionId) {
  83. $query->setParameter('sender', $sessionId)
  84. ->setParameter('recipient', $sessionId)
  85. ->execute();
  86. }
  87. }
  88. /**
  89. * Get messages and delete them afterwards
  90. *
  91. * To make sure we don't delete messages which we didn't return
  92. * we do it with 1 second difference. This means you don't receive messages
  93. * immediately, but the next polling is only 1 second later and will get the
  94. * "new" message.
  95. *
  96. * @param string $sessionId
  97. * @return array
  98. */
  99. public function getAndDeleteMessages($sessionId) {
  100. $messages = [];
  101. $time = $this->time->getTime() - 1;
  102. $query = $this->db->getQueryBuilder();
  103. $query->select('*')
  104. ->from('videocalls_signaling')
  105. ->where($query->expr()->eq('recipient', $query->createNamedParameter($sessionId)))
  106. ->andWhere($query->expr()->lte('timestamp', $query->createNamedParameter($time)));
  107. $result = $query->execute();
  108. while ($row = $result->fetch()) {
  109. $messages[] = ['type' => 'message', 'data' => $row['message']];
  110. }
  111. $result->closeCursor();
  112. $query = $this->db->getQueryBuilder();
  113. $query->delete('videocalls_signaling')
  114. ->where($query->expr()->eq('recipient', $query->createNamedParameter($sessionId)))
  115. ->andWhere($query->expr()->lte('timestamp', $query->createNamedParameter($time)));
  116. $query->execute();
  117. return $messages;
  118. }
  119. /**
  120. * Expires all signaling messages that are too old or invalid
  121. *
  122. * @param int $olderThan
  123. */
  124. public function expireOlderThan($olderThan) {
  125. $time = $this->time->getTime() - $olderThan;
  126. $query = $this->db->getQueryBuilder();
  127. $query->delete('videocalls_signaling')
  128. ->where($query->expr()->lt('timestamp', $query->createNamedParameter($time)));
  129. $query->execute();
  130. }
  131. }