@@ -48,1622 +48,1622 @@ |
||
| 48 | 48 | |
| 49 | 49 | class Manager implements ICommentsManager { |
| 50 | 50 | |
| 51 | - /** @var IDBConnection */ |
|
| 52 | - protected $dbConn; |
|
| 53 | - |
|
| 54 | - /** @var LoggerInterface */ |
|
| 55 | - protected $logger; |
|
| 56 | - |
|
| 57 | - /** @var IConfig */ |
|
| 58 | - protected $config; |
|
| 59 | - |
|
| 60 | - /** @var ITimeFactory */ |
|
| 61 | - protected $timeFactory; |
|
| 62 | - |
|
| 63 | - /** @var IEmojiHelper */ |
|
| 64 | - protected $emojiHelper; |
|
| 65 | - |
|
| 66 | - /** @var IInitialStateService */ |
|
| 67 | - protected $initialStateService; |
|
| 68 | - |
|
| 69 | - /** @var IComment[] */ |
|
| 70 | - protected $commentsCache = []; |
|
| 71 | - |
|
| 72 | - /** @var \Closure[] */ |
|
| 73 | - protected $eventHandlerClosures = []; |
|
| 74 | - |
|
| 75 | - /** @var ICommentsEventHandler[] */ |
|
| 76 | - protected $eventHandlers = []; |
|
| 77 | - |
|
| 78 | - /** @var \Closure[] */ |
|
| 79 | - protected $displayNameResolvers = []; |
|
| 80 | - |
|
| 81 | - public function __construct(IDBConnection $dbConn, |
|
| 82 | - LoggerInterface $logger, |
|
| 83 | - IConfig $config, |
|
| 84 | - ITimeFactory $timeFactory, |
|
| 85 | - IEmojiHelper $emojiHelper, |
|
| 86 | - IInitialStateService $initialStateService) { |
|
| 87 | - $this->dbConn = $dbConn; |
|
| 88 | - $this->logger = $logger; |
|
| 89 | - $this->config = $config; |
|
| 90 | - $this->timeFactory = $timeFactory; |
|
| 91 | - $this->emojiHelper = $emojiHelper; |
|
| 92 | - $this->initialStateService = $initialStateService; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * converts data base data into PHP native, proper types as defined by |
|
| 97 | - * IComment interface. |
|
| 98 | - * |
|
| 99 | - * @param array $data |
|
| 100 | - * @return array |
|
| 101 | - */ |
|
| 102 | - protected function normalizeDatabaseData(array $data) { |
|
| 103 | - $data['id'] = (string)$data['id']; |
|
| 104 | - $data['parent_id'] = (string)$data['parent_id']; |
|
| 105 | - $data['topmost_parent_id'] = (string)$data['topmost_parent_id']; |
|
| 106 | - $data['creation_timestamp'] = new \DateTime($data['creation_timestamp']); |
|
| 107 | - if (!is_null($data['latest_child_timestamp'])) { |
|
| 108 | - $data['latest_child_timestamp'] = new \DateTime($data['latest_child_timestamp']); |
|
| 109 | - } |
|
| 110 | - if (!is_null($data['expire_date'])) { |
|
| 111 | - $data['expire_date'] = new \DateTime($data['expire_date']); |
|
| 112 | - } |
|
| 113 | - $data['children_count'] = (int)$data['children_count']; |
|
| 114 | - $data['reference_id'] = $data['reference_id'] ?? null; |
|
| 115 | - if ($this->supportReactions()) { |
|
| 116 | - if ($data['reactions'] !== null) { |
|
| 117 | - $list = json_decode($data['reactions'], true); |
|
| 118 | - // Ordering does not work on the database with group concat and Oracle, |
|
| 119 | - // So we simply sort on the output. |
|
| 120 | - if (is_array($list)) { |
|
| 121 | - uasort($list, static function ($a, $b) { |
|
| 122 | - if ($a === $b) { |
|
| 123 | - return 0; |
|
| 124 | - } |
|
| 125 | - return ($a > $b) ? -1 : 1; |
|
| 126 | - }); |
|
| 127 | - $data['reactions'] = $list; |
|
| 128 | - } else { |
|
| 129 | - $data['reactions'] = []; |
|
| 130 | - } |
|
| 131 | - } else { |
|
| 132 | - $data['reactions'] = []; |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - return $data; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * @param array $data |
|
| 141 | - * @return IComment |
|
| 142 | - */ |
|
| 143 | - public function getCommentFromData(array $data): IComment { |
|
| 144 | - return new Comment($this->normalizeDatabaseData($data)); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * prepares a comment for an insert or update operation after making sure |
|
| 149 | - * all necessary fields have a value assigned. |
|
| 150 | - * |
|
| 151 | - * @param IComment $comment |
|
| 152 | - * @return IComment returns the same updated IComment instance as provided |
|
| 153 | - * by parameter for convenience |
|
| 154 | - * @throws \UnexpectedValueException |
|
| 155 | - */ |
|
| 156 | - protected function prepareCommentForDatabaseWrite(IComment $comment) { |
|
| 157 | - if (!$comment->getActorType() |
|
| 158 | - || $comment->getActorId() === '' |
|
| 159 | - || !$comment->getObjectType() |
|
| 160 | - || $comment->getObjectId() === '' |
|
| 161 | - || !$comment->getVerb() |
|
| 162 | - ) { |
|
| 163 | - throw new \UnexpectedValueException('Actor, Object and Verb information must be provided for saving'); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - if ($comment->getVerb() === 'reaction' && !$this->emojiHelper->isValidSingleEmoji($comment->getMessage())) { |
|
| 167 | - // 4 characters: laptop + person + gender + skin color => " |
|