Beispiel #1
0
        public async Task HandleContext(StatementBase statement, IStatementBaseEntity newStatement, CancellationToken cancellationToken)
        {
            if (statement.Context != null)
            {
                newStatement.Context = _mapper.Map <ContextEntity>(statement.Context);
                ContextEntity context = newStatement.Context;
                if (context.Instructor != null)
                {
                    var instructor = (AgentEntity)await _mediator.Send(UpsertActorCommand.Create(statement.Context.Instructor), cancellationToken);

                    context.InstructorId = instructor.AgentId;
                    context.Instructor   = null;
                }
                if (context.Team != null)
                {
                    var team = (AgentEntity)await _mediator.Send(UpsertActorCommand.Create(statement.Context.Team), cancellationToken);

                    context.TeamId = team.AgentId;
                    context.Team   = null;
                }

                if (context.ContextActivities != null)
                {
                    foreach (var contextActivity in context.ContextActivities)
                    {
                        Iri id       = new Iri(contextActivity.Activity.Id);
                        var activity = await _mediator.Send(UpsertActivityCommand.Create(id), cancellationToken);

                        contextActivity.Activity   = null;
                        contextActivity.ActivityId = activity.ActivityId;
                    }
                }
            }
        }
Beispiel #2
0
        public async Task HandleActor(StatementBase statement, IStatementBaseEntity newStatement, CancellationToken cancellationToken)
        {
            var actor = (AgentEntity)await _mediator.Send(UpsertActorCommand.Create(statement.Actor), cancellationToken);

            newStatement.ActorId = actor.AgentId;
            newStatement.Actor   = null;
        }
Beispiel #3
0
        public async Task HandleObject(StatementBase statement, IStatementBaseEntity newStatement, CancellationToken cancellationToken)
        {
            var objType = statement.Object.ObjectType;

            if (objType == ObjectType.Activity)
            {
                var activity = await _mediator.Send(UpsertActivityCommand.Create((Activity)statement.Object), cancellationToken);

                newStatement.ObjectType = EntityObjectType.Activity;
                newStatement.ObjectId   = activity.ActivityId;
            }
            else if (objType == ObjectType.Agent || objType == ObjectType.Group)
            {
                AgentEntity agent = await _mediator.Send(UpsertActorCommand.Create((Agent)statement.Object), cancellationToken);;
                newStatement.ObjectType = EntityObjectType.Agent;
                newStatement.ObjectId   = agent.AgentId;
            }
            else if (objType == ObjectType.SubStatement)
            {
                SubStatementEntity subStatement = await _mediator.Send(CreateSubStatementCommand.Create((SubStatement)statement.Object), cancellationToken);

                newStatement.ObjectType = EntityObjectType.SubStatement;
                newStatement.ObjectId   = subStatement.SubStatementId;
            }
            else if (objType == ObjectType.StatementRef)
            {
                StatementRef statementRef = (StatementRef)statement.Object;
                newStatement.ObjectType = EntityObjectType.StatementRef;
                newStatement.ObjectId   = statementRef.Id;
            }
        }
Beispiel #4
0
        public async Task <IDocument> CreateAgentProfile(Agent agent, string profileId, byte[] content, string contentType, CancellationToken cancellationToken = default)
        {
            var actor = await mediator.Send(UpsertActorCommand.Create(agent), cancellationToken);

            var profile = await mediator.Send(CreateAgentProfileCommand.Create(actor.AgentId, profileId, content, contentType), cancellationToken);

            return(mapper.Map <IDocument>(profile));
        }
        public async Task <IActionResult> PostSingleState(
            [BindRequired, FromQuery] string stateId,
            [BindRequired, FromQuery] Iri activityId,
            [BindRequired, FromQuery] Agent agent,
            [BindRequired, FromBody] byte[] body,
            [BindRequired, FromHeader(Name = "Content-Type")] string contentType,
            [FromQuery] Guid?registration       = null,
            CancellationToken cancellationToken = default)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            AgentEntity storedAgent = await _mediator.Send(UpsertActorCommand.Create(agent));

            ActivityEntity activity = (ActivityEntity)await _mediator.Send(UpsertActivityCommand.Create(activityId), cancellationToken);

            ActivityStateDocument stateDocument = await _mediator.Send(new GetActivityStateQuery()
            {
                StateId      = stateId,
                ActivityId   = activityId,
                AgentId      = storedAgent.AgentId,
                Registration = registration
            }, cancellationToken);

            if (stateDocument != null)
            {
                stateDocument = await _mediator.Send(new UpdateStateDocumentCommand()
                {
                    StateId      = stateId,
                    ActivityId   = activityId,
                    AgentId      = storedAgent.AgentId,
                    Content      = body,
                    ContentType  = contentType,
                    Registration = registration
                }, cancellationToken);
            }
            else
            {
                stateDocument = await _mediator.Send(new CreateStateDocumentCommand()
                {
                    StateId      = stateId,
                    Activity     = activity,
                    Agent        = storedAgent,
                    Content      = body,
                    ContentType  = contentType,
                    Registration = registration
                }, cancellationToken);
            }

            Response.Headers.Add(HeaderNames.ETag, $"\"{stateDocument.Tag}\"");
            Response.Headers.Add(HeaderNames.LastModified, stateDocument.LastModified?.ToString("o"));

            return(NoContent());
        }
        public async Task <IActionResult> GetSingleState(
            [BindRequired, FromQuery] Iri activityId,
            [BindRequired, FromQuery] Agent agent,
            [FromQuery] string stateId          = null,
            [FromQuery] DateTime?since          = null,
            [FromQuery] Guid?registration       = null,
            CancellationToken cancellationToken = default)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            AgentEntity agentEntity = await _mediator.Send(UpsertActorCommand.Create(agent));

            if (string.IsNullOrEmpty(stateId))
            {
                return(await GetMutipleStates(
                           activityId,
                           agentEntity.AgentId,
                           registration,
                           since,
                           cancellationToken
                           ));
            }

            ActivityStateDocument stateDocument = await _mediator.Send(new GetActivityStateQuery()
            {
                StateId      = stateId,
                ActivityId   = activityId,
                AgentId      = agentEntity.AgentId,
                Registration = registration
            }, cancellationToken);

            if (stateDocument == null)
            {
                return(NotFound());
            }

            if (Request.TryConcurrencyCheck(stateDocument.Tag, stateDocument.LastModified, out int statusCode))
            {
                return(StatusCode(statusCode));
            }

            var content = new FileContentResult(stateDocument.Content, stateDocument.ContentType.ToString())
            {
                LastModified = stateDocument.LastModified,
                EntityTag    = new EntityTagHeaderValue($"\"{stateDocument.Tag}\"")
            };

            return(content);
        }
        public async Task <IActionResult> DeleteSingleState(
            [BindRequired, FromQuery] Iri activityId,
            [BindRequired, FromQuery] Agent agent,
            [FromQuery] string stateId          = null,
            [FromQuery] Guid?registration       = null,
            CancellationToken cancellationToken = default)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var agentEntity = (await _mediator.Send(UpsertActorCommand.Create(agent)));

            if (string.IsNullOrEmpty(stateId))
            {
                await _mediator.Send(new DeleteActivityStatesCommand()
                {
                    ActivityId   = activityId,
                    AgentId      = agentEntity.AgentId,
                    Registration = registration
                }, cancellationToken);

                return(NoContent());
            }
            else
            {
                ActivityStateDocument stateDocument = await _mediator.Send(new GetActivityStateQuery()
                {
                    StateId      = stateId,
                    ActivityId   = activityId,
                    AgentId      = agentEntity.AgentId,
                    Registration = registration
                }, cancellationToken);

                if (stateDocument == null)
                {
                    return(NotFound());
                }

                await _mediator.Send(new DeleteActivityStateCommand()
                {
                    StateId      = stateId,
                    ActivityId   = activityId,
                    AgentId      = agentEntity.AgentId,
                    Registration = registration
                }, cancellationToken);
            }

            return(NoContent());
        }
Beispiel #8
0
        public async Task <AgentProfileEntity> Handle(CreateAgentProfileCommand request, CancellationToken cancellationToken)
        {
            var agent = await _mediator.Send(UpsertActorCommand.Create(request.Agent), cancellationToken);

            var profile = new AgentProfileEntity(request.Content, request.ContentType)
            {
                ProfileId = request.ProfileId,
                AgentId   = agent.AgentId
            };

            _context.AgentProfiles.Add(profile);
            await _context.SaveChangesAsync(cancellationToken);

            return(profile);
        }
Beispiel #9
0
        public async Task <IDocument> PostSingleState(string stateId, Iri activityId, Agent agent, byte[] body, string contentType, Guid?registration = null, CancellationToken cancellationToken = default)
        {
            var activity = await mediator.Send(UpsertActivityCommand.Create(activityId));

            AgentEntity savedAgent = await mediator.Send(UpsertActorCommand.Create(agent), cancellationToken);

            var document = await mediator.Send(new CreateStateDocumentCommand()
            {
                StateId      = stateId,
                ActivityId   = activity.ActivityId,
                AgentId      = savedAgent.AgentId,
                Content      = body,
                ContentType  = contentType,
                Registration = registration,
            }, cancellationToken);

            return(mapper.Map <IDocument>(document));
        }
Beispiel #10
0
        public async Task <SubStatementEntity> Handle(CreateSubStatementCommand request, CancellationToken cancellationToken)
        {
            var subStatement = _mapper.Map <SubStatementEntity>(request.SubStatement);

            subStatement.Timestamp = subStatement.Timestamp ?? DateTimeOffset.UtcNow;

            subStatement.Verb = (VerbEntity)await _mediator.Send(UpsertVerbCommand.Create(request.SubStatement.Verb));

            subStatement.Actor = (AgentEntity)await _mediator.Send(UpsertActorCommand.Create(request.SubStatement.Actor));

            if (subStatement.Context != null)
            {
                var context = subStatement.Context;
                if (context.Instructor != null)
                {
                    context.Instructor = (AgentEntity)await _mediator.Send(UpsertActorCommand.Create(request.SubStatement.Context.Instructor), cancellationToken);
                }

                if (context.Team != null)
                {
                    context.Team = (AgentEntity)await _mediator.Send(UpsertActorCommand.Create(request.SubStatement.Context.Team), cancellationToken);
                }
            }

            var objType = subStatement.Object.ObjectType;

            if (objType == EntityObjectType.Activity)
            {
                subStatement.Object.Activity = (ActivityEntity)await _mediator.Send(UpsertActivityCommand.Create((Activity)request.SubStatement.Object));
            }
            else if (objType == EntityObjectType.Agent || objType == EntityObjectType.Group)
            {
                subStatement.Object.Agent = await _mediator.Send(UpsertActorCommand.Create((Agent)request.SubStatement.Object));
            }
            else if (objType == EntityObjectType.StatementRef)
            {
                // It's already mapped from automapper
                // TODO: Additional logic should be performed here
            }
            _context.SubStatements.Add(subStatement);

            return(subStatement);
        }
        public async Task AnonymousGroup()
        {
            // Arrange
            var mediatorMock = new Mock <IMediator>();
            var mapperMock   = new Mock <IMapper>();
            var actor        = new Group()
            {
                Member = new[] {
                    new Agent()
                    {
                        Account = new Doctrina.ExperienceApi.Data.Account()
                        {
                            Name     = "test-user",
                            HomePage = new Uri("https://bitflipping-net"),
                        }
                    },
                    new Agent()
                    {
                        Name = "Test Agent",
                        Mbox = new Mbox("mailto:[email protected]")
                    }
                }
            };
            var handler   = new UpsertActorCommand.Handler(_context, mediatorMock.Object, mapperMock.Object);
            var validator = new UpsertActorCommandValidator();
            var cmd       = UpsertActorCommand.Create(actor);

            // Act
            var validationResult = validator.Validate(cmd);

            validationResult.IsValid.ShouldBeTrue();

            var result = await handler.Handle(cmd, CancellationToken.None);

            // Assert
            result.AgentId.ShouldNotBe(Guid.Empty);
            result.Hash.ShouldNotBeNullOrWhiteSpace();
            result.ShouldBeOfType <GroupEntity>();
        }
        /// <summary>
        /// Creates statement without saving to database
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns>Guid of the created statement</returns>
        public async Task <Guid> Handle(CreateStatementCommand request, CancellationToken cancellationToken)
        {
            // Prepare statement for mapping
            if (request.Statement.Id.HasValue)
            {
                bool any = await _context.Statements.AnyAsync(x => x.StatementId == request.Statement.Id, cancellationToken).ConfigureAwait(false);

                if (any)
                {
                    return(request.Statement.Id.Value);
                }
            }

            request.Statement.Stamp();

            // Ensure statement version and stored date
            request.Statement.Version = request.Statement.Version ?? ApiVersion.GetLatest().ToString();
            request.Statement.Stored  = request.Statement.Stored ?? DateTimeOffset.UtcNow;

            if (request.Statement.Authority == null)
            {
                // TODO: Map group?
                request.Statement.Authority = _mapper.Map <Agent>(_authorityContext.Authority);
            }
            else
            {
                // TODO: Validate authority
            }

            // Start mapping statement
            StatementEntity newStatement = new StatementEntity();

            newStatement.StatementId = request.Statement.Id.GetValueOrDefault();
            newStatement.Verb        = (VerbEntity)await _mediator.Send(UpsertVerbCommand.Create(request.Statement.Verb), cancellationToken).ConfigureAwait(false);

            newStatement.Actor = (AgentEntity)await _mediator.Send(UpsertActorCommand.Create(request.Statement.Actor), cancellationToken).ConfigureAwait(false);

            newStatement.Authority = (AgentEntity)await _mediator.Send(UpsertActorCommand.Create(request.Statement.Authority), cancellationToken).ConfigureAwait(false);

            if (request.Statement.Context != null)
            {
                newStatement.Context = _mapper.Map <ContextEntity>(request.Statement.Context);
                ContextEntity context = newStatement.Context;
                if (context.Instructor != null)
                {
                    context.Instructor = (AgentEntity)await _mediator.Send(UpsertActorCommand.Create(request.Statement.Context.Instructor), cancellationToken).ConfigureAwait(false);
                }
                if (context.Team != null)
                {
                    context.Team = (AgentEntity)await _mediator.Send(UpsertActorCommand.Create(request.Statement.Context.Team), cancellationToken).ConfigureAwait(false);
                }
            }

            var objType = request.Statement.Object.ObjectType;

            newStatement.Object = new StatementObjectEntity();
            if (objType == ObjectType.Activity)
            {
                newStatement.Object.Activity = (ActivityEntity)await _mediator.Send(UpsertActivityCommand.Create((Activity)request.Statement.Object), cancellationToken).ConfigureAwait(false);
            }
            else if (objType == ObjectType.Agent || objType == ObjectType.Group)
            {
                newStatement.Object.Agent = (AgentEntity)await _mediator.Send(UpsertActorCommand.Create((Agent)request.Statement.Object), cancellationToken).ConfigureAwait(false);
            }
            else if (objType == ObjectType.SubStatement)
            {
                newStatement.Object.SubStatement = (SubStatementEntity)await _mediator.Send(CreateSubStatementCommand.Create((SubStatement)request.Statement.Object), cancellationToken).ConfigureAwait(false);
            }
            else if (objType == ObjectType.StatementRef)
            {
                newStatement.Object.StatementRef = _mapper.Map <StatementRefEntity>((StatementRef)request.Statement.Object);
            }

            if (request.Statement.Result != null)
            {
                newStatement.Result = _mapper.Map <ResultEntity>(request.Statement.Result);
            }
            newStatement.Stored        = request.Statement.Stored;
            newStatement.Timestamp     = request.Statement.Timestamp;
            newStatement.Version       = request.Statement.Version.ToString();
            newStatement.FullStatement = request.Statement.ToJson();

            _context.Statements.Add(newStatement);

            // await _context.SaveChangesAsync(cancellationToken);
            await _mediator.Publish(StatementAdded.Create(newStatement), cancellationToken).ConfigureAwait(false);

            if (request.Persist)
            {
                await _context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
            }

            return(newStatement.StatementId);
        }