Adds support for @mentions, by implementing this interface users can direct a readers attention to content in the community.
Why should I make my content mentionable?
Being able to mention content can provide users with easy access to your content. Keep in mind, you should be able to provide unique titles or it may be difficult for the user to find your content.
Creating an IMentionableContentType plugin
Adding support for @mentions is done by implementing the IMentionableContentType interface, it is defined in the Telligent.Evolution.Extensibility.Content.Version1
namespace of Telligent.Evolution.Core.dll
.
It is important to note that one or more Core Services can be implemented in the same IContentType class.
Mentions are made up of three parts, first begin by returning a list of items that match a search term.
public IEnumerable<IContent> GetMentionables(int userId, string queryText, IMentionableFilter filter) { var links = new List<IContent>(); var results = LinksData.ListLinks() .Where(r => r.Name.ToLowerInvariant().Contains(queryText.ToLowerInvariant())) .ToList<IContent>(); foreach (var result in results) { var content = LinksData.GetLink(result.ContentId); if (content != null) { links.Add(content); } } return links; }
Next add a preview for your item. This sample returns literal strings but if you have implemented ITranslatablePlugin use the ITranslatablePluginController to make the results translatable.
public string GetMentionPreviewHtml(int userId, Guid contentId) { try { var content = LinksData.GetLink(contentId); if (content != null) { return String.Format("{0} (Link)", content.HtmlName("Web")); } return "(deleted link)"; } catch (Exception) { return "(inaccessible link)"; } }
Finally you must create the full HTML view for the mention. Here a link is created to redirect the user to your content, make sure to add these classes internal-link
and view-application
in your anchor tag.
public string GetMentionViewHtml(int userId, Guid contentId) { try { var content = LinksData.GetLink(contentId); if (content != null) { if (!string.IsNullOrEmpty(content.Url)) { return string.Concat("<a href=\"", PublicApi.Html.Encode(content.Url), "\" class=\"internal-link view-application links\" data-linkid=\"", content.ContentId, "\">", content.Name, "</a>"); } return content.Name; } return "(deleted link)"; } catch (Exception) { return "(inaccessible link)"; } }
Here is the full sample.
using System; using System.Collections.Generic; using System.Linq; using Telligent.Evolution.Extensibility; using Telligent.Evolution.Extensibility.Api.Version1; using Telligent.Evolution.Extensibility.Content.Version1; using IContent = Telligent.Evolution.Extensibility.Content.Version1.IContent; namespace Samples.Links { public class LinkItemContentType : IContentType, IMentionableContentType { IContentStateChanges _contentState = null; #region IPlugin Members public string Description { get { return "Items in a Links collection"; } } public void Initialize() { } public string Name { get { return "Link Items"; } } #endregion #region IContentType Members public Guid[] ApplicationTypes { get { return new Guid[] { ContentTypes.LinksApplicationId }; } } public void AttachChangeEvents(IContentStateChanges stateChanges) { _contentState = stateChanges; } public Guid ContentTypeId { get { return ContentTypes.LinksItemId; } } public string ContentTypeName { get { return "Links Item"; } } public IContent Get(Guid contentId) { return LinksData.GetLink(contentId); } #endregion #region IMentionableContentType public IEnumerable<IContent> GetMentionables(int userId, string queryText, IMentionableFilter filter) { var links = new List<IContent>(); var results = LinksData.ListLinks() .Where(r => r.Name.ToLowerInvariant().Contains(queryText.ToLowerInvariant())) .ToList<IContent>(); foreach (var result in results) { var content = LinksData.GetLink(result.ContentId); if (content != null) { links.Add(content); } } return links; } public string GetMentionPreviewHtml(int userId, Guid contentId) { try { var content = LinksData.GetLink(contentId); if (content != null) { return String.Format("{0} (Link)", content.HtmlName("Web")); } return "(deleted link)"; } catch (Exception) { return "(inaccessible link)"; } } public string GetMentionViewHtml(int userId, Guid contentId) { try { var content = LinksData.GetLink(contentId); if (content != null) { if (!string.IsNullOrEmpty(content.Url)) { return string.Concat("<a href=\"", PublicApi.Html.Encode(content.Url), "\" class=\"internal-link view-application links\" data-linkid=\"", content.ContentId, "\">", content.Name, "</a>"); } return content.Name; } return "(deleted link)"; } catch (Exception) { return "(inaccessible link)"; } } #endregion } }
Here is a sample of the @mention UI.