-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #805 from nextcloud/format-mentions-without-avatar…
…s-in-the-message-list Format mentions (without avatars) in the message list
- Loading branch information
Showing
20 changed files
with
669 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* global OC, OCA, Handlebars */ | ||
|
||
/** | ||
* @copyright (c) 2016 Joas Schilling <coding@schilljs.com> | ||
* | ||
* @author Joas Schilling <coding@schilljs.com> | ||
* | ||
* This file is licensed under the Affero General Public License version 3 or | ||
* later. See the COPYING file. | ||
*/ | ||
|
||
(function(OC, OCA, Handlebars) { | ||
|
||
OCA.SpreedMe.RichObjectStringParser = { | ||
|
||
_userLocalTemplate: '<span class="mention-user {{#if isCurrentUser}}current-user{{/if}}" data-user="{{id}}">@{{name}}</span>', | ||
|
||
_unknownTemplate: '<strong>{{name}}</strong>', | ||
_unknownLinkTemplate: '<a href="{{link}}">{{name}}</a>', | ||
|
||
/** | ||
* @param {string} subject | ||
* @param {Object} parameters | ||
* @returns {string} | ||
*/ | ||
parseMessage: function(subject, parameters) { | ||
var self = this, | ||
regex = /\{([a-z0-9-]+)\}/gi, | ||
matches = subject.match(regex); | ||
|
||
_.each(matches, function(parameter) { | ||
parameter = parameter.substring(1, parameter.length - 1); | ||
if (!parameters.hasOwnProperty(parameter) || !parameters[parameter]) { | ||
// Malformed translation? | ||
console.error('Potential malformed ROS string: parameter {' + parameter + '} was found in the string but is missing from the parameter list'); | ||
return; | ||
} | ||
|
||
var parsed = self.parseParameter(parameters[parameter]); | ||
subject = subject.replace('{' + parameter + '}', parsed); | ||
}); | ||
|
||
return subject; | ||
}, | ||
|
||
/** | ||
* @param {Object} parameter | ||
* @param {string} parameter.type | ||
* @param {string} parameter.id | ||
* @param {string} parameter.name | ||
* @param {string} parameter.link | ||
*/ | ||
parseParameter: function(parameter) { | ||
switch (parameter.type) { | ||
case 'user': | ||
if (!this.userLocalTemplate) { | ||
this.userLocalTemplate = Handlebars.compile(this._userLocalTemplate); | ||
} | ||
if (!parameter.name) { | ||
parameter.name = parameter.id; | ||
} | ||
if (OC.getCurrentUser().uid === parameter.id) { | ||
parameter.isCurrentUser = true; | ||
} | ||
return this.userLocalTemplate(parameter); | ||
|
||
default: | ||
if (!_.isUndefined(parameter.link)) { | ||
if (!this.unknownLinkTemplate) { | ||
this.unknownLinkTemplate = Handlebars.compile(this._unknownLinkTemplate); | ||
} | ||
return this.unknownLinkTemplate(parameter); | ||
} | ||
|
||
if (!this.unknownTemplate) { | ||
this.unknownTemplate = Handlebars.compile(this._unknownTemplate); | ||
} | ||
return this.unknownTemplate(parameter); | ||
} | ||
} | ||
|
||
}; | ||
|
||
})(OC, OCA, Handlebars); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
/** | ||
* | ||
* @copyright Copyright (c) 2017, Daniel Calviño Sánchez (danxuliu@gmail.com) | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OCA\Spreed\Chat; | ||
|
||
use OCP\Comments\IComment; | ||
use OCP\Comments\ICommentsManager; | ||
|
||
/** | ||
* Helper class to get a rich message from a plain text message. | ||
*/ | ||
class RichMessageHelper { | ||
|
||
/** @var ICommentsManager */ | ||
private $commentsManager; | ||
|
||
/** | ||
* @param ICommentsManager $commentsManager | ||
*/ | ||
public function __construct(ICommentsManager $commentsManager) { | ||
$this->commentsManager = $commentsManager; | ||
} | ||
|
||
/** | ||
* Returns the equivalent rich message to the given comment. | ||
* | ||
* The mentions in the comment are replaced by "{mention-$type$index}" in | ||
* the returned rich message; each "mention-$type$index" parameter contains | ||
* the following attributes: | ||
* -type: the type of the mention ("user") | ||
* -id: the ID of the user | ||
* -name: the display name of the user, or an empty string if it could | ||
* not be resolved. | ||
* | ||
* @param IComment $comment | ||
* @return Array first element, the rich message; second element, the | ||
* parameters of the rich message (or an empty array if there are no | ||
* parameters). | ||
*/ | ||
public function getRichMessage(IComment $comment) { | ||
$message = $comment->getMessage(); | ||
$messageParameters = []; | ||
|
||
$mentionTypeCount = []; | ||
|
||
$mentions = $comment->getMentions(); | ||
foreach ($mentions as $mention) { | ||
if (!array_key_exists($mention['type'], $mentionTypeCount)) { | ||
$mentionTypeCount[$mention['type']] = 0; | ||
} | ||
$mentionTypeCount[$mention['type']]++; | ||
|
||
// To keep a limited character set in parameter IDs ([a-zA-Z0-9-]) | ||
// the mention parameter ID does not include the mention ID (which | ||
// could contain characters like '@' for user IDs) but a one-based | ||
// index of the mentions of that type. | ||
$mentionParameterId = 'mention-' . $mention['type'] . $mentionTypeCount[$mention['type']]; | ||
|
||
$message = str_replace('@' . $mention['id'], '{' . $mentionParameterId . '}', $message); | ||
|
||
try { | ||
$displayName = $this->commentsManager->resolveDisplayName($mention['type'], $mention['id']); | ||
} catch (\OutOfBoundsException $e) { | ||
// There is no registered display name resolver for the mention | ||
// type, so the client decides what to display. | ||
$displayName = ''; | ||
} | ||
|
||
$messageParameters[$mentionParameterId] = [ | ||
'type' => $mention['type'], | ||
'id' => $mention['id'], | ||
'name' => $displayName | ||
]; | ||
} | ||
|
||
return [$message, $messageParameters]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.