public async Task <MultipleDocumentResult> GetActivityStates(Iri activityId, Agent agent, Guid?registration, DateTimeOffset?since = null, CancellationToken cancellationToken = default) { AgentEntity savedAgent = await mediator.Send(GetAgentQuery.Create(agent), cancellationToken); if (savedAgent == null) { return(MultipleDocumentResult.Empty()); } var states = await mediator.Send(new GetActivityStatesQuery() { ActivityId = activityId, AgentId = savedAgent.AgentId, Registration = registration, Since = since }, cancellationToken); if (!states.Any()) { return(MultipleDocumentResult.Empty()); } var keys = states.Select(x => x.Key).ToHashSet(); var lastModified = states.OrderByDescending(x => x.UpdatedAt) .Select(x => x.UpdatedAt) .FirstOrDefault(); return(MultipleDocumentResult.Success(keys, lastModified)); }
public async Task <IDocument> UpdateAgentProfile(Agent agent, string profileId, byte[] content, string contentType, CancellationToken cancellationToken = default) { var agentEntity = await mediator.Send(GetAgentQuery.Create(agent), cancellationToken); var document = await mediator.Send(UpdateAgentProfileCommand.Create(agentEntity.AgentId, profileId, content, contentType)); return(mapper.Map <IDocument>(document)); }
public async Task DeleteAgentProfile(Agent agent, string profileId, CancellationToken cancellationToken = default) { var agentEntity = await mediator.Send(GetAgentQuery.Create(agent), cancellationToken); if (agentEntity == null) { return; } await mediator.Send(DeleteAgentProfileCommand.Create(profileId, agentEntity.AgentId), cancellationToken); }
public async Task <IActionResult> GetAgent(GetAgentQuery query) { var agent = await queryExecutor.Execute <GetAgentQuery, Agent>(query); if (agent is null) { return(NotFound()); } return(Ok(agent)); }
public async Task <IDocument> GetAgentProfile(Agent agent, string profileId, CancellationToken cancellationToken = default) { var agentEntity = await mediator.Send(GetAgentQuery.Create(agent), cancellationToken); if (agentEntity == null) { return(null); } var profile = await mediator.Send(GetAgentProfileQuery.Create(agentEntity.AgentId, profileId)); return(mapper.Map <IDocument>(profile)); }
public async Task <Unit> Handle(DeleteActivityStatesCommand request, CancellationToken cancellationToken) { var agent = await mediator.Send(GetAgentQuery.Create(request.Agent)); string activityHash = request.ActivityId.ComputeHash(); var activities = _context.Documents .OfType <ActivityStateEntity>() .Where(x => x.Activity.Hash == activityHash) .Where(x => x.Agent.AgentId == agent.AgentId); _context.Documents.RemoveRange(activities); await _context.SaveChangesAsync(cancellationToken); return(await Unit.Task); }
public async Task <AgentProfileEntity> Handle(GetAgentProfileQuery request, CancellationToken cancellationToken) { var agent = await _mediator.Send(GetAgentQuery.Create(request.Agent)); if (agent == null) { return(null); } var profile = await _context.AgentProfiles .Include(x => x.Document) .AsNoTracking() .Where(x => x.AgentId == agent.AgentId) .SingleOrDefaultAsync(x => x.ProfileId == request.ProfileId, cancellationToken); return(profile); }
public async Task <AgentEntity> Handle(UpsertActorCommand request, CancellationToken cancellationToken) { // Try find in cache AgentEntity actor = await _mediator.Send(GetAgentQuery.Create(request.Actor), cancellationToken); if (actor == null) { actor = (request.Actor.ObjectType == ObjectType.Agent ? _mapper.Map <AgentEntity>(request.Actor) : _mapper.Map <GroupEntity>(request.Actor)); actor.AgentId = Guid.NewGuid(); _context.Agents.Add(actor); } if (request.Actor is Group group && actor is GroupEntity groupEntity) { if (_context.Entry(actor).State != EntityState.Added) { _context.Entry(actor).State = EntityState.Modified; } // Perform group update logic, add group member etc. foreach (var member in group.Member) { var savedGrpActor = await _mediator.Send(UpsertActorCommand.Create(member), cancellationToken); if (groupEntity.Members.Any(x => x.AgentId == savedGrpActor.AgentId)) { continue; } groupEntity.Members.Add(new GroupMemberEntity() { GroupMemberId = Guid.NewGuid(), AgentId = savedGrpActor.AgentId, GroupId = groupEntity.AgentId, }); } await _mediator.Publish(AgentUpdated.Create(actor)).ConfigureAwait(false); } return(actor); }
public async Task <Unit> Handle(DeleteActivityStateCommand request, CancellationToken cancellationToken) { var agent = await _mediator.Send(GetAgentQuery.Create(request.Agent)); string activityHash = request.ActivityId.ComputeHash(); var activity = await _context.Documents .OfType <ActivityStateEntity>() .Where(x => x.Key == request.StateId && x.Activity.Hash == activityHash && (!request.Registration.HasValue || x.RegistrationId == request.Registration)) .Where(x => x.Agent.AgentId == agent.AgentId) .FirstOrDefaultAsync(cancellationToken); if (activity != null) { _context.Documents.Remove(activity); await _context.SaveChangesAsync(cancellationToken); } return(await Unit.Task); }
public async Task <AgentProfileEntity> Handle(UpdateAgentProfileCommand request, CancellationToken cancellationToken) { var agent = await _mediator.Send(GetAgentQuery.Create(request.Agent)); var profile = await _context.AgentProfiles .Include(x => x.Document) .Where(x => x.AgentId == agent.AgentId) .SingleOrDefaultAsync(x => x.ProfileId == request.ProfileId, cancellationToken); if (profile == null) { throw new NotFoundException("AgentProfile", request.ProfileId); } profile.Document.UpdateDocument(request.Content, request.ContentType); _context.AgentProfiles.Update(profile); await _context.SaveChangesAsync(cancellationToken); return(profile); }
public async Task <MultipleDocumentResult> GetAgentProfiles(Agent agent, DateTimeOffset?since = null, CancellationToken cancellationToken = default) { var agentEntity = await mediator.Send(GetAgentQuery.Create(agent), cancellationToken); if (agentEntity == null) { return(MultipleDocumentResult.Empty()); } var profiles = await mediator.Send(GetAgentProfilesQuery.Create(agentEntity.AgentId, since), cancellationToken); if (!profiles.Any()) { return(MultipleDocumentResult.Empty()); } ICollection <string> ids = profiles.Select(x => x.Key).ToHashSet(); DateTimeOffset? lastModified = profiles.OrderByDescending(x => x.UpdatedAt).Select(x => x.UpdatedAt).FirstOrDefault(); return(MultipleDocumentResult.Success(ids, lastModified)); }
public async Task <Unit> Handle(DeleteAgentProfileCommand request, CancellationToken cancellationToken) { var agentEntity = await _mediator.Send(GetAgentQuery.Create(request.Agent)); if (agentEntity == null) { return(await Unit.Task); } AgentProfileEntity profile = await _context.AgentProfiles .AsNoTracking() .Where(x => x.AgentId == agentEntity.AgentId) .SingleOrDefaultAsync(x => x.ProfileId == request.ProfileId, cancellationToken); if (profile != null) { _context.AgentProfiles.Remove(profile); await _context.SaveChangesAsync(cancellationToken); } return(await Unit.Task); }
public async Task <ActivityStateEntity> Handle(GetActivityStateQuery request, CancellationToken cancellationToken) { var agent = await _mediator.Send(GetAgentQuery.Create(request.Agent)); string activityHash = request.ActivityId.ComputeHash(); var query = _context.Documents .AsNoTracking() .OfType <ActivityStateEntity>() .Where(x => x.Key == request.StateId) .Where(x => x.Activity.Hash == activityHash) .Where(x => x.Agent.AgentId == agent.AgentId); if (request.Registration.HasValue) { query.Where(x => x.RegistrationId == request.Registration); } ActivityStateEntity state = await query.SingleOrDefaultAsync(cancellationToken); return(state); }
public async Task <Person> GetPerson(Agent agent, CancellationToken cancellationToken = default) { var fallback = new Person(); fallback.Add(agent); var storedAgent = await mediator.Send(GetAgentQuery.Create(agent)); if (storedAgent == null) { return(fallback); } var person = await mediator.Send(GetPersonQuery.Create(storedAgent.ObjectType, storedAgent.AgentId), cancellationToken); if (person == null) { return(fallback); } return(mapper.Map <Person>(person)); }
public async Task <AgentEntity> Handle(UpsertActorCommand request, CancellationToken cancellationToken) { AgentEntity persona = await _mediator.Send(GetAgentQuery.Create(request.Actor), cancellationToken); bool isNew = false; if (persona == null) { persona = (request.Actor.ObjectType == ObjectType.Agent ? _mapper.Map <AgentEntity>(request.Actor) : _mapper.Map <GroupEntity>(request.Actor)); persona.AgentId = Guid.NewGuid(); if (persona.ObjectType == EntityObjectType.Agent && !string.IsNullOrEmpty(request.Actor.Name)) { persona.Person = new PersonEntity() { PersonId = Guid.NewGuid(), Name = request.Actor.Name }; } _context.Agents.Add(persona); await _context.SaveChangesAsync(cancellationToken); isNew = true; } if (!isNew) { if (request.Actor is Group group && !group.IsAnonymous() && persona is GroupEntity groupEntity) { var upserts = group.Member.Select(member => _mediator.Send(UpsertActorCommand.Create(member), cancellationToken)); var members = await Task.WhenAll(upserts); // Remove any members that does not exist in the request group foreach (var member in groupEntity.Members) { if (!members.Any(x => x.AgentId == member.AgentId)) { groupEntity.Members.Remove(member); } } // Add any member that does not exist in the stored group from the request group foreach (var member in members) { if (!groupEntity.Members.Any(x => x.AgentId == member.AgentId)) { groupEntity.Members.Add(new GroupMemberEntity() { GroupId = groupEntity.AgentId, AgentId = member.AgentId }); } } await _mediator.Publish(AgentUpdated.Create(persona)); } } await _context.SaveChangesAsync(cancellationToken); return(persona); }
public async Task <PagedStatementsResult> Handle(PagedStatementsQuery request, CancellationToken cancellationToken) { if (!string.IsNullOrWhiteSpace(request.Cursor)) { string token = request.Cursor; string jsonString = await _distributedCache.GetStringAsync(token, cancellationToken); request = PagedStatementsQuery.FromJson(jsonString); request.Cursor = null; } var query = _context.Statements.AsNoTracking(); if (request.VerbId != null) { string verbHash = request.VerbId.ComputeHash(); query = query.Where(x => x.Verb.Hash == verbHash); } if (request.Agent != null) { var currentAgent = await _mediator.Send(GetAgentQuery.Create(request.Agent)); if (currentAgent != null) { Guid agentId = currentAgent.AgentId; EntityObjectType agentType = currentAgent.ObjectType; if (request.RelatedAgents.GetValueOrDefault()) { query = ( from sta in query join sub in _context.SubStatements on new { ObjectType = sta.ObjectType, ObjectId = sta.ObjectId } equals new { ObjectType = sub.ObjectType, ObjectId = sub.SubStatementId } let ctx = sta.Context where sta.Actor.AgentId == agentId || (sta.ObjectType == agentType && sta.ObjectId == agentId) || (sta.Context.InstructorId == agentId || sta.Context.TeamId == agentId) // SubStatement || sub.Actor.AgentId == agentId || (sub.ObjectType == agentType && sub.ObjectId == agentId) || // Object (sub.Context.InstructorId == agentId || sub.Context.TeamId == agentId) select sta ); } else { query = query.Where(x => x.ActorId == currentAgent.AgentId); } } else { return(new PagedStatementsResult()); } } if (request.ActivityId != null) { var activity = await _mediator.Send(GetActivityQuery.Create(request.ActivityId), cancellationToken); if (activity == null) { return(new PagedStatementsResult()); } if (request.RelatedActivities.GetValueOrDefault()) { query = ( from statement in query join subStatement in _context.SubStatements on new { ObjectId = statement.ObjectId, ObjectType = statement.ObjectType } equals new { ObjectId = subStatement.ObjectId, ObjectType = EntityObjectType.SubStatement } where ( statement.ObjectType == EntityObjectType.Activity && statement.ObjectId == activity.ActivityId ) || ( subStatement.ObjectType == EntityObjectType.Activity && subStatement.ObjectId == activity.ActivityId ) || ( statement.Context != null && statement.Context.ContextActivities != null && ( statement.Context.ContextActivities.Any(x => x.ActivityId == activity.ActivityId) ) ) select statement ); } else { query = query.Where(x => x.ObjectType == EntityObjectType.Activity && x.ObjectId == activity.ActivityId); } } if (request.Registration.HasValue) { Guid registration = request.Registration.Value; query = ( from statement in query where statement.Context != null && statement.Context.Registration == registration select statement ); } if (request.Ascending.GetValueOrDefault()) { query = query.OrderBy(x => x.Stored); } else { query = query.OrderByDescending(x => x.Stored); } if (request.Since.HasValue) { query = query.Where(x => x.Stored > request.Since.Value); } if (request.Until.HasValue) { query = query.Where(x => x.Stored < request.Until.Value); } int pageSize = request.Limit ?? 1000; int skipRows = request.PageIndex * pageSize; IQueryable <StatementEntity> pagedQuery = null; // Include voiding statements query = query.Select(p => p.VoidingStatementId != null ? p.VoidingStatement : p); if (!request.Attachments.GetValueOrDefault()) { pagedQuery = query.Select(p => new StatementEntity { StatementId = p.StatementId, FullStatement = p.FullStatement }); } else { pagedQuery = query.Select(p => new StatementEntity { StatementId = p.StatementId, FullStatement = p.FullStatement, Attachments = p.Attachments, }); } var result = await pagedQuery.Skip(skipRows).Take(pageSize + 1) .ToListAsync(cancellationToken); if (result == null) { return(new PagedStatementsResult()); } List <StatementEntity> statements = result.Take(pageSize).ToList(); if (result.Count > pageSize) { request.Cursor = Guid.NewGuid().ToString(); request.PageIndex += 1; if (!request.Until.HasValue) { request.Until = DateTimeOffset.UtcNow; } var options = new DistributedCacheEntryOptions() .SetSlidingExpiration(TimeSpan.FromSeconds(60 * 10)); await _distributedCache.SetStringAsync(request.Cursor, request.ToJson(), options, cancellationToken); return(new PagedStatementsResult(statements, request.Cursor)); } return(new PagedStatementsResult(statements)); }