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.

UserStatusMapper.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Georg Ehrke
  5. *
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\UserStatus\Db;
  24. use OCP\AppFramework\Db\QBMapper;
  25. use OCP\DB\QueryBuilder\IQueryBuilder;
  26. use OCP\IDBConnection;
  27. /**
  28. * Class UserStatusMapper
  29. *
  30. * @package OCA\UserStatus\Db
  31. *
  32. * @method UserStatus insert(UserStatus $entity)
  33. * @method UserStatus update(UserStatus $entity)
  34. * @method UserStatus insertOrUpdate(UserStatus $entity)
  35. * @method UserStatus delete(UserStatus $entity)
  36. */
  37. class UserStatusMapper extends QBMapper {
  38. /**
  39. * @param IDBConnection $db
  40. */
  41. public function __construct(IDBConnection $db) {
  42. parent::__construct($db, 'user_status');
  43. }
  44. /**
  45. * @param int|null $limit
  46. * @param int|null $offset
  47. * @return UserStatus[]
  48. */
  49. public function findAll(?int $limit = null, ?int $offset = null):array {
  50. $qb = $this->db->getQueryBuilder();
  51. $qb
  52. ->select('*')
  53. ->from($this->tableName);
  54. if ($limit !== null) {
  55. $qb->setMaxResults($limit);
  56. }
  57. if ($offset !== null) {
  58. $qb->setFirstResult($offset);
  59. }
  60. return $this->findEntities($qb);
  61. }
  62. /**
  63. * @param string $userId
  64. * @return UserStatus
  65. * @throws \OCP\AppFramework\Db\DoesNotExistException
  66. */
  67. public function findByUserId(string $userId):UserStatus {
  68. $qb = $this->db->getQueryBuilder();
  69. $qb
  70. ->select('*')
  71. ->from($this->tableName)
  72. ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)));
  73. return $this->findEntity($qb);
  74. }
  75. /**
  76. * Clear all statuses older than a given timestamp
  77. *
  78. * @param int $timestamp
  79. */
  80. public function clearOlderThan(int $timestamp): void {
  81. $qb = $this->db->getQueryBuilder();
  82. $qb->update($this->tableName)
  83. ->set('message_id', $qb->createNamedParameter(null))
  84. ->set('custom_icon', $qb->createNamedParameter(null))
  85. ->set('custom_message', $qb->createNamedParameter(null))
  86. ->set('clear_at', $qb->createNamedParameter(null))
  87. ->where($qb->expr()->isNotNull('clear_at'))
  88. ->andWhere($qb->expr()->lte('clear_at', $qb->createNamedParameter($timestamp, IQueryBuilder::PARAM_INT)));
  89. $qb->execute();
  90. }
  91. }