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.

131 lines
4.7 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2024 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Talk\Model;
  25. use OCA\Talk\ResponseDefinitions;
  26. use OCP\AppFramework\Db\Entity;
  27. /**
  28. * @method void setLocalToken(string $localToken)
  29. * @method string getLocalToken()
  30. * @method void setRemoteServerUrl(string $remoteServerUrl)
  31. * @method string getRemoteServerUrl()
  32. * @method void setRemoteToken(string $remoteToken)
  33. * @method string getRemoteToken()
  34. * @method void setRemoteMessageId(int $remoteMessageId)
  35. * @method int getRemoteMessageId()
  36. * @method void setActorType(string $actorType)
  37. * @method string getActorType()
  38. * @method void setActorId(string $actorId)
  39. * @method string getActorId()
  40. * @method void setActorDisplayName(?string $actorDisplayName)
  41. * @method string|null getActorDisplayName()
  42. * @method void setMessageType(string $messageType)
  43. * @method string getMessageType()
  44. * @method void setSystemMessage(?string $systemMessage)
  45. * @method string|null getSystemMessage()
  46. * @method void setExpirationDatetime(?\DateTime $expirationDatetime)
  47. * @method \DateTime|null getExpirationDatetime()
  48. * @method void setMessage(?string $message)
  49. * @method string|null getMessage()
  50. * @method void setMessageParameters(?string $messageParameters)
  51. * @method string|null getMessageParameters()
  52. * @method void setCreationDatetime(?\DateTime $creationDatetime)
  53. * @method \DateTime|null getCreationDatetime()
  54. * @method void setMetaData(?string $metaData)
  55. * @method string|null getMetaData()
  56. *
  57. * @psalm-import-type TalkChatProxyMessage from ResponseDefinitions
  58. */
  59. class ProxyCacheMessage extends Entity implements \JsonSerializable {
  60. public const METADATA_REPLY_TO_ACTOR_TYPE = 'replyToActorType';
  61. public const METADATA_REPLY_TO_ACTOR_ID = 'replyToActorId';
  62. public const METADATA_REPLY_TO_MESSAGE_ID = 'replyToMessageId';
  63. protected string $localToken = '';
  64. protected string $remoteServerUrl = '';
  65. protected string $remoteToken = '';
  66. protected int $remoteMessageId = 0;
  67. protected string $actorType = '';
  68. protected string $actorId = '';
  69. protected ?string $actorDisplayName = null;
  70. protected ?string $messageType = null;
  71. protected ?string $systemMessage = null;
  72. protected ?\DateTime $expirationDatetime = null;
  73. protected ?string $message = null;
  74. protected ?string $messageParameters = null;
  75. protected ?\DateTime $creationDatetime = null;
  76. protected ?string $metaData = null;
  77. public function __construct() {
  78. $this->addType('localToken', 'string');
  79. $this->addType('remoteServerUrl', 'string');
  80. $this->addType('remoteToken', 'string');
  81. $this->addType('remoteMessageId', 'int');
  82. $this->addType('actorType', 'string');
  83. $this->addType('actorId', 'string');
  84. $this->addType('actorDisplayName', 'string');
  85. $this->addType('messageType', 'string');
  86. $this->addType('systemMessage', 'string');
  87. $this->addType('expirationDatetime', 'datetime');
  88. $this->addType('message', 'string');
  89. $this->addType('messageParameters', 'string');
  90. $this->addType('creationDatetime', 'datetime');
  91. $this->addType('metaData', 'string');
  92. }
  93. public function getParsedMessageParameters(): array {
  94. return json_decode($this->getMessageParameters() ?? '[]', true);
  95. }
  96. public function getParsedMetaData(): array {
  97. return json_decode($this->getMetaData() ?? '[]', true);
  98. }
  99. /**
  100. * @return TalkChatProxyMessage
  101. */
  102. public function jsonSerialize(): array {
  103. $expirationTimestamp = 0;
  104. if ($this->getExpirationDatetime()) {
  105. $expirationTimestamp = $this->getExpirationDatetime()->getTimestamp();
  106. }
  107. return [
  108. 'actorType' => $this->getActorType(),
  109. 'actorId' => $this->getActorId(),
  110. 'actorDisplayName' => $this->getActorDisplayName() ?? '',
  111. 'timestamp' => $this->getCreationDatetime()->getTimestamp(),
  112. 'expirationTimestamp' => $expirationTimestamp,
  113. 'messageType' => $this->getMessageType(),
  114. 'systemMessage' => $this->getSystemMessage() ?? '',
  115. 'message' => $this->getMessage() ?? '',
  116. 'messageParameters' => $this->getParsedMessageParameters(),
  117. ];
  118. }
  119. }