diff options
author | Morris Jobke <hey@morrisjobke.de> | 2016-10-26 14:02:49 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-26 14:02:49 +0200 |
commit | cde7f535bd9fd95325545a68e4c0e8926b726a2e (patch) | |
tree | 94a1525dd139de54e5e6db6534ed0aefe5435766 /lib/private | |
parent | b358b4eebc5079416c9cb437ff54705686cb215c (diff) | |
parent | b12b52b73bb225c29f4009f9b9095cacc093ea7e (diff) | |
download | nextcloud-server-cde7f535bd9fd95325545a68e4c0e8926b726a2e.tar.gz nextcloud-server-cde7f535bd9fd95325545a68e4c0e8926b726a2e.zip |
Merge pull request #1738 from nextcloud/comments-provide-displaynames-with-mentions
comment mentions: show displayname not uid
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/Comments/Comment.php | 37 | ||||
-rw-r--r-- | lib/private/Comments/Manager.php | 47 |
2 files changed, 84 insertions, 0 deletions
diff --git a/lib/private/Comments/Comment.php b/lib/private/Comments/Comment.php index f6f0801c683..b5f063be323 100644 --- a/lib/private/Comments/Comment.php +++ b/lib/private/Comments/Comment.php @@ -204,6 +204,43 @@ class Comment implements IComment { } /** + * returns an array containing mentions that are included in the comment + * + * @return array each mention provides a 'type' and an 'id', see example below + * @since 9.2.0 + * + * The return array looks like: + * [ + * [ + * 'type' => 'user', + * 'id' => 'citizen4' + * ], + * [ + * 'type' => 'group', + * 'id' => 'media' + * ], + * … + * ] + * + */ + public function getMentions() { + $ok = preg_match_all('/\B@[a-z0-9_\-@\.\']+/i', $this->getMessage(), $mentions); + if(!$ok || !isset($mentions[0]) || !is_array($mentions[0])) { + return []; + } + $uids = array_unique($mentions[0]); + $result = []; + foreach ($uids as $uid) { + // exclude author, no self-mentioning + if($uid === '@' . $this->getActorId()) { + continue; + } + $result[] = ['type' => 'user', 'id' => substr($uid, 1)]; + } + return $result; + } + + /** * returns the verb of the comment * * @return string diff --git a/lib/private/Comments/Manager.php b/lib/private/Comments/Manager.php index b3ecab731e1..001f4f9441c 100644 --- a/lib/private/Comments/Manager.php +++ b/lib/private/Comments/Manager.php @@ -55,6 +55,9 @@ class Manager implements ICommentsManager { /** @var ICommentsEventHandler[] */ protected $eventHandlers = []; + /** @var \Closure[] */ + protected $displayNameResolvers = []; + /** * Manager constructor. * @@ -760,6 +763,50 @@ class Manager implements ICommentsManager { } /** + * registers a method that resolves an ID to a display name for a given type + * + * @param string $type + * @param \Closure $closure + * @throws \OutOfBoundsException + * @since 9.2.0 + * + * Only one resolver shall be registered per type. Otherwise a + * \OutOfBoundsException has to thrown. + */ + public function registerDisplayNameResolver($type, \Closure $closure) { + if(!is_string($type)) { + throw new \InvalidArgumentException('String expected.'); + } + if(isset($this->displayNameResolvers[$type])) { + throw new \OutOfBoundsException('Displayname resolver for this type already registered'); + } + $this->displayNameResolvers[$type] = $closure; + } + + /** + * resolves a given ID of a given Type to a display name. + * + * @param string $type + * @param string $id + * @return string + * @throws \OutOfBoundsException + * @since 9.2.0 + * + * If a provided type was not registered, an \OutOfBoundsException shall + * be thrown. It is upon the resolver discretion what to return of the + * provided ID is unknown. It must be ensured that a string is returned. + */ + public function resolveDisplayName($type, $id) { + if(!is_string($type)) { + throw new \InvalidArgumentException('String expected.'); + } + if(!isset($this->displayNameResolvers[$type])) { + throw new \OutOfBoundsException('No Displayname resolver for this type registered'); + } + return (string)$this->displayNameResolvers[$type]($id); + } + + /** * returns valid, registered entities * * @return \OCP\Comments\ICommentsEventHandler[] |