public override async Task <Response> Handle(Request message, CancellationToken cancellationToken)
        {
            var key          = ConversationKey.Parse(message.Key);
            var securityRule = this.conversationManagerCollection.GetInstance(key.EntityType);
            var conversation = this.context.EnsureConversation(message.Key);
            var participants = await securityRule.GetParticipants(key.EntityId);

            var model = securityRule.CanViewConversation(key.EntityId)
                                ? new Conversation
            {
                Key            = conversation.Key,
                Id             = conversation.Id,
                CreatedOn      = conversation.CreatedOn,
                Comments       = conversation.Comments.Select(t => GetComments(t, this.applicationContext, this.userContext)),
                CanAddComments = securityRule.CanAddNewComments(key.EntityId),
                Participants   = participants?.Participants,
                UserName       = this.userContext.User.UserName
            }
                                : null;

            return(new Response
            {
                Conversation = model
            });
        }
Exemple #2
0
        public override string ToString()
        {
            StringBuilder __sb    = new StringBuilder("Conversation(");
            bool          __first = true;

            if (ConversationKey != null && __isset.ConversationKey)
            {
                if (!__first)
                {
                    __sb.Append(", ");
                }
                __first = false;
                __sb.Append("ConversationKey: ");
                __sb.Append(ConversationKey == null ? "<null>" : ConversationKey.ToString());
            }
            if (__isset.ConversationId)
            {
                if (!__first)
                {
                    __sb.Append(", ");
                }
                __first = false;
                __sb.Append("ConversationId: ");
                __sb.Append(ConversationId);
            }
            if (__isset.ParentId)
            {
                if (!__first)
                {
                    __sb.Append(", ");
                }
                __first = false;
                __sb.Append("ParentId: ");
                __sb.Append(ParentId);
            }
            if (__isset.FirstSeen)
            {
                if (!__first)
                {
                    __sb.Append(", ");
                }
                __first = false;
                __sb.Append("FirstSeen: ");
                __sb.Append(FirstSeen);
            }
            if (__isset.LastSeen)
            {
                if (!__first)
                {
                    __sb.Append(", ");
                }
                __first = false;
                __sb.Append("LastSeen: ");
                __sb.Append(LastSeen);
            }
            __sb.Append(")");
            return(__sb.ToString());
        }
Exemple #3
0
        public static InlineForm Create <T>(object id) where T : IConversationManager
        {
            var key = ConversationKey.Get <T>(id);

            return(new InlineForm
            {
                Form = typeof(Commands.GetConversation).GetFormId(),
                InputFieldValues = new Dictionary <string, object>
                {
                    { nameof(Commands.GetConversation.Request.Key), key }
                }
            });
        }
        public override async Task <Response> Handle(Request message, CancellationToken cancellationToken)
        {
            var comment = await this.context.Comments.FindOrExceptionAsync(message.Id);

            var conversation = await this.context.Conversations.FindOrExceptionAsync(comment.ConversationId);

            var key     = ConversationKey.Parse(conversation.Key);
            var manager = this.conversationManagerCollection.GetInstance(key.EntityType);

            if (comment.AuthorId.ToString() != this.userContext.User.UserId)
            {
                throw new PermissionException("Delete conversation comment", this.userContext);
            }

            this.context.Comments.Remove(comment);
            await this.context.SaveChangesAsync();

            manager.PostAddComment(key.EntityId);
            return(new Response());
        }
Exemple #5
0
        public override async Task <Response> Handle(Request message, CancellationToken cancellationToken)
        {
            var key          = ConversationKey.Parse(message.Key);
            var securityRule = this.conversationManagerCollection.GetInstance(key.EntityType);

            if (!securityRule.CanAddNewComments(key.EntityId))
            {
                throw new PermissionException($"Comment on {key.EntityType} {key.EntityId}", this.userContext);
            }

            var conversation = this.context.EnsureConversation(message.Key);
            var comment      = new Comment <int>(conversation.Id, Convert.ToInt32(this.userContext.User.UserId), message.Text);

            this.context.Comments.Add(comment);
            await this.context.SaveChangesAsync(cancellationToken);

            await this.PublishNotificationAndEmails(securityRule, key, message.Text);

            return(new Response
            {
                Comment = GetConversation.GetComments(comment, this.applicationDbContext, this.userContext)
            });
        }
Exemple #6
0
        private async Task PublishNotificationAndEmails(IConversationManager securityRule, ConversationKey key, string comment)
        {
            var participants = await securityRule.GetParticipants(key.EntityId);

            var users            = participants.Participants.SelectMany(t => t.Users);
            var userParticipants = users.DistinctBy(a => a.Id).ToList();

            var link = securityRule.Link(key.EntityId).GetUrl();

            foreach (var user in userParticipants)
            {
                if (user.Id.ToString() != this.userContext.User.UserId)
                {
                    var model = new CommentAddedEmail.Model(this.appConfig.Value, comment, user.Username, user.Email, link);
                    await this.emailSender.SendEmail(user.Email, model);

                    this.notificationsDbContext.Add(
                        new Notification(
                            new EntityReference(NotificationRecipientType.UserId.Value, user.Id.ToString()),
                            new EntityReference(key.EntityType, key.EntityId.ToString()),
                            $"New comment posted for {key.EntityType.Split('.').Last()} #{key.EntityId}",
                            null));
                }
            }

            await this.notificationsDbContext.SaveChangesAsync();
        }
Exemple #7
0
 public void Write(TProtocol oprot)
 {
     oprot.IncrementRecursionDepth();
     try
     {
         TStruct struc = new TStruct("Conversation");
         oprot.WriteStructBegin(struc);
         TField field = new TField();
         if (ConversationKey != null && __isset.ConversationKey)
         {
             field.Name = "ConversationKey";
             field.Type = TType.Struct;
             field.ID   = 1;
             oprot.WriteFieldBegin(field);
             ConversationKey.Write(oprot);
             oprot.WriteFieldEnd();
         }
         if (__isset.ConversationId)
         {
             field.Name = "ConversationId";
             field.Type = TType.I32;
             field.ID   = 2;
             oprot.WriteFieldBegin(field);
             oprot.WriteI32(ConversationId);
             oprot.WriteFieldEnd();
         }
         if (__isset.ParentId)
         {
             field.Name = "ParentId";
             field.Type = TType.I32;
             field.ID   = 3;
             oprot.WriteFieldBegin(field);
             oprot.WriteI32(ParentId);
             oprot.WriteFieldEnd();
         }
         if (__isset.FirstSeen)
         {
             field.Name = "FirstSeen";
             field.Type = TType.I64;
             field.ID   = 4;
             oprot.WriteFieldBegin(field);
             oprot.WriteI64(FirstSeen);
             oprot.WriteFieldEnd();
         }
         if (__isset.LastSeen)
         {
             field.Name = "LastSeen";
             field.Type = TType.I64;
             field.ID   = 5;
             oprot.WriteFieldBegin(field);
             oprot.WriteI64(LastSeen);
             oprot.WriteFieldEnd();
         }
         oprot.WriteFieldStop();
         oprot.WriteStructEnd();
     }
     finally
     {
         oprot.DecrementRecursionDepth();
     }
 }