An IActivityStoryType allows the user to push story items to the activity stream in a group or main page.
[toc]
Why should I create Activity Stories?
When performing actions upon a community providing a stream of events will allow the user's to view the activity at a glance. A good example is when a user adds an event to the calendar an activity story can provide users the information they need. Telligent Community provides support for posting stories in the activity stream via the IActivityStoryType Plugin Type.
Creating an ActivityStoryType Plugin
To add support for creating stories, implement the IActivityStoryType plugin (which requires a reference to Telligent.Evolution.Api.dll, Telligent.Evolution.Components.dll and Telligent.Evolution.Core.dll). The IActivityStoryType interface extends IPlugin to add support for activity stories.
To begin you'll need to generate a Guid and return it as the StoryTypeId.
public Guid StoryTypeId { get { return new Guid("247A9821-75F0-4E57-81E2-15B9D0578491"); } }
After setting up the Id the story type also requires a name and description. They are used in the administration panel under the Activity Stream > Options tab.
public string StoryTypeName { get { return "My Activity Stories"; } } public string StoryTypeDescription { get { return "Controls activity stories for My Activity Stories."; } }
Activity stories can allow the user to delete an individual story. The CanDeleteStory method accepts a storyId and a userId. These id's can be used to find a particular story as well as a user. Then depending on your requirements return a boolean value that will allow a user to remove an item. In this sample we verify the user is logged in and not an anonymous user then we determine if the user is one of the actors of the story. An can be considered as a user or users of a particular activity.
public bool CanDeleteStory(Guid storyId, int userId) { Apis.Get<EventLog>().Write("", new EventLogEntryWriteOptions()); // unregistered users cannot delete stories var user = Apis.Get<Users>().Get(new UsersGetOptions { Id = userId }); var userIsRegistered = user != null && !user.IsSystemAccount.GetValueOrDefault(false); if (!userIsRegistered) { return false; } // actors can delete var story = Apis.Get<ActivityStories>().Get(storyId); return story != null && story.Actors != null && story.Actors.ToList().Exists(actor => actor.UserId == userId); }
The ContentTypeIds property returns a list of id's related to the content that the stories belong to. Typically it is related to one type but can be set to multiple types.
public Guid[] ContentTypeIds { get { return new[] { Apis.Get<Users>().ContentTypeId }; } }
Here's the complete source of our sample activity story type plugin:
using System; using System.Collections.Generic; using System.Linq; using Telligent.Evolution.Api.Services; using Telligent.Evolution.Extensibility; using Telligent.Evolution.Extensibility.Api.Entities.Version1; using Telligent.Evolution.Extensibility.Api.Version1; using Telligent.Evolution.Extensibility.Content.Version1; using ActivityStoryActor = Telligent.Evolution.Extensibility.Content.Version1.ActivityStoryActor; namespace MyActivityStory { public class MyActivityStory : IActivityStoryType { public string Name { get { return "My Activity Story Plugin"; } } public string Description { get { return "This plugin will demo how the IActivityStoryType works"; } } public void Initialize() { PublishStory(); } public Guid StoryTypeId { get { return new Guid("247A9821-75F0-4E57-81E2-15B9D0578491"); } } public string StoryTypeName { get { return "My Activity Stories"; } } public bool CanDeleteStory(Guid storyId, int userId) { // unregistered users cannot delete stories var user = Apis.Get<Users>().Get(new UsersGetOptions { Id = userId }); var userIsRegistered = user != null && !user.IsSystemAccount.GetValueOrDefault(false); if (!userIsRegistered) { return false; } // actors can delete var story = Apis.Get<ActivityStories>().Get(storyId); return story != null && story.Actors != null && story.Actors.ToList().Exists(actor => actor.UserId == userId); } public Guid[] ContentTypeIds { get { return new[] { Apis.Get<Users>().ContentTypeId }; } } public string GetPreviewHtml(IActivityStory story, Target target) { var content = Apis.Get<Contents>().Get(story.ContentId.GetValueOrDefault(), story.ContentTypeId.GetValueOrDefault()); if (content == null) return null; return string.Format("<h3>{0}</h3>", content.HtmlName("Web")); } public int? GetPrimaryUser(IActivityStory story) { if (story == null || story.Actors == null || story.Actors.Count == 0) { return null; } return story.Actors.Last().UserId; } public string GetViewHtml(IActivityStory story, Target target) { var content = Apis.Get<Contents>().Get(story.ContentId.GetValueOrDefault(), story.ContentTypeId.GetValueOrDefault()); if (content == null) return null; return string.Format("<h3>{0}</h3><p>{1}</p>", content.HtmlName("Web"), content.HtmlDescription("Web")); } private IActivityStoryController _storyController; public void SetController(IActivityStoryController controller) { _storyController = controller; } public string StoryTypeDescription { get { return "Controls activity stories for My Activity Stories."; } } public bool IsCacheable { get { return true; } } public bool VaryCacheByUser { get { return true; } } private void PublishStory() { var user = Apis.Get<Users>().AccessingUser; _storyController.Create(new ActivityStoryCreateOptions { ContentId = user.ContentId, ContentTypeId = Apis.Get<Users>().ContentTypeId, LastUpdate = DateTime.UtcNow, Actors = new List<ActivityStoryActor> { new ActivityStoryActor { UserId = user.Id.GetValueOrDefault(), Verb = "Add", Date = DateTime.UtcNow } } }); } } }