Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Telligent.Evolution.Api.Services;
  5. using Telligent.Evolution.Extensibility;
  6. using Telligent.Evolution.Extensibility.Api.Version1;
  7. using Telligent.Evolution.Extensibility.Content.Version1;
  8.  
  9. namespace MyActivityStory
  10. {
  11. public class MyActivityStory : IActivityStoryType
  12. {
  13. public string Name
  14. {
  15. get { return "My Activity Story Plugin"; }
  16. }
  17.  
  18. public string Description
  19. {
  20. get { return "This plugin will demo how the IActivityStoryType works"; }
  21. }
  22.  
  23. public void Initialize()
  24. {
  25. Apis.Get<IComments>().Events.AfterCreate += EventsOnAfterCreate;
  26. }
  27.  
  28. private void EventsOnAfterCreate(CommentAfterCreateEventArgs e)
  29. {
  30. // Check to make sure the comment is for content via the REST API and not a comment on content.
  31. if (e.Content.ContentTypeId == new Guid("68C65AF2-AA15-4E4C-9C81-155C6A3159F6"))
  32. {
  33. CommentCreateStory(e.CommentId);
  34. }
  35. }
  36.  
  37. public Guid StoryTypeId
  38. {
  39. get { return new Guid("c0bd2266-09ba-4304-aa60-eb58e4157d72"); }
  40. }
  41.  
  42. public string StoryTypeName
  43. {
  44. get { return "My Activity Stories"; }
  45. }
  46.  
  47. public string StoryTypeDescription
  48. {
  49. get { return "Controls activity stories for My Activity Stories."; }
  50. }
  51.  
  52. public bool CanDeleteStory(Guid storyId, int userId)
  53. {
  54. // unregistered users cannot delete stories
  55. var user = Apis.Get<IUsers>().Get(new UsersGetOptions { Id = userId });
  56. var userIsRegistered = user != null && !user.IsSystemAccount.GetValueOrDefault(false);
  57. if (!userIsRegistered)
  58. {
  59. return false;
  60. }
  61.  
  62. // actors can delete
  63. var story = Apis.Get<ActivityStories>().Get(storyId);
  64. return story != null && story.Actors != null && story.Actors.ToList().Exists(actor => actor.UserId == userId);
  65. }
  66.  
  67. public Guid[] ContentTypeIds
  68. {
  69. get { return new[] { Apis.Get<IComments>().ContentTypeId }; }
  70. }
  71.  
  72. public string GetPreviewHtml(IActivityStory story, Target target)
  73. {
  74. var content = Apis.Get<Contents>().Get(story.ContentId.GetValueOrDefault(), story.ContentTypeId.GetValueOrDefault());
  75. if (content == null) return null;
  76.  
  77. return string.Format("<h3>{0}</h3>", content.HtmlName("Web"));
  78. }
  79.  
  80. public string GetViewHtml(IActivityStory story, Target target)
  81. {
  82. var content = Apis.Get<Contents>().Get(story.ContentId.GetValueOrDefault(), story.ContentTypeId.GetValueOrDefault());
  83. if (content == null) return null;
  84.  
  85. return $"<h3>A comment was created</h3><p>{content.HtmlName("Web")}</p>";
  86. }
  87.  
  88. public int? GetPrimaryUser(IActivityStory story)
  89. {
  90. if (story == null || story.Actors == null || story.Actors.Count == 0)
  91. {
  92. return null;
  93. }
  94.  
  95. return story.Actors.Last().UserId;
  96. }
  97.  
  98. private IActivityStoryController _storyController;
  99.  
  100. public void SetController(IActivityStoryController controller)
  101. {
  102. _storyController = controller;
  103. }
  104.  
  105. public bool IsCacheable
  106. {
  107. get { return true; }
  108. }
  109.  
  110. public bool VaryCacheByUser
  111. {
  112. get { return true; }
  113. }
  114.  
  115. private void CommentCreateStory(Guid commentId)
  116. {
  117. var comment = Apis.Get<IComments>().Get(commentId);
  118.  
  119. _storyController.Create(new ActivityStoryCreateOptions
  120. {
  121. ContentId = comment.ContentId,
  122. ContentTypeId = comment.ContentTypeId,
  123. LastUpdate = DateTime.UtcNow,
  124. Actors = new List<ActivityStoryActor>
  125. {
  126. new ActivityStoryActor
  127. {
  128. UserId = comment.UserId,
  129. Verb = "Add",
  130. Date = DateTime.UtcNow
  131. }
  132. }
  133. });
  134. }
  135.  
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement