| | | 1 | | using Elsa.Expressions.Models; |
| | | 2 | | using Elsa.Http; |
| | | 3 | | using Elsa.SasTokens.Contracts; |
| | | 4 | | using Elsa.Workflows; |
| | | 5 | | using Elsa.Workflows.Api; |
| | | 6 | | using Elsa.Workflows.Runtime; |
| | | 7 | | using Microsoft.Extensions.Options; |
| | | 8 | | |
| | | 9 | | // ReSharper disable once CheckNamespace |
| | | 10 | | namespace Elsa.Extensions; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Provides extension methods for working with <see cref="ExpressionExecutionContext"/> and generating bookmark trigger |
| | | 14 | | /// </summary> |
| | | 15 | | public static class BookmarkExecutionContextExtensions |
| | | 16 | | { |
| | 0 | 17 | | public static string GenerateBookmarkTriggerUrl(this ActivityExecutionContext context, string bookmarkId, TimeSpan l |
| | 0 | 18 | | public static string GenerateBookmarkTriggerUrl(this ActivityExecutionContext context, string bookmarkId, DateTimeOf |
| | 0 | 19 | | public static string GenerateBookmarkTriggerUrl(this ActivityExecutionContext context, string bookmarkId) => context |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Generates a URL that can be used to resume a bookmarked workflow. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="context">The expression execution context.</param> |
| | | 25 | | /// <param name="bookmarkId">The ID of the bookmark to resume.</param> |
| | | 26 | | /// <param name="lifetime">The lifetime of the bookmark trigger token.</param> |
| | | 27 | | /// <returns>A URL that can be used to resume a bookmarked workflow.</returns> |
| | | 28 | | public static string GenerateBookmarkTriggerUrl(this ExpressionExecutionContext context, string bookmarkId, TimeSpan |
| | | 29 | | { |
| | 0 | 30 | | var token = context.GenerateBookmarkTriggerTokenInternal(bookmarkId, lifetime); |
| | 0 | 31 | | return context.GenerateBookmarkTriggerUrlInternal(token); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Generates a URL that can be used to resume a bookmarked workflow. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="context">The expression execution context.</param> |
| | | 38 | | /// <param name="bookmarkId">The ID of the bookmark to resume.</param> |
| | | 39 | | /// <param name="expiresAt">The expiration date of the bookmark trigger token.</param> |
| | | 40 | | /// <returns>A URL that can be used to resume a bookmarked workflow.</returns> |
| | | 41 | | public static string GenerateBookmarkTriggerUrl(this ExpressionExecutionContext context, string bookmarkId, DateTime |
| | | 42 | | { |
| | 0 | 43 | | var token = context.GenerateBookmarkTriggerTokenInternal(bookmarkId, expiresAt: expiresAt); |
| | 0 | 44 | | return context.GenerateBookmarkTriggerUrlInternal(token); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Generates a URL that can be used to resume a bookmarked workflow. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="context">The expression execution context.</param> |
| | | 51 | | /// <param name="bookmarkId">The ID of the bookmark to resume.</param> |
| | | 52 | | /// <returns>A URL that can be used to trigger an event.</returns> |
| | | 53 | | public static string GenerateBookmarkTriggerUrl(this ExpressionExecutionContext context, string bookmarkId) |
| | | 54 | | { |
| | 0 | 55 | | var token = context.GenerateBookmarkTriggerTokenInternal(bookmarkId); |
| | 0 | 56 | | return context.GenerateBookmarkTriggerUrlInternal(token); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | private static string GenerateBookmarkTriggerUrlInternal(this ExpressionExecutionContext context, string token) |
| | | 60 | | { |
| | 0 | 61 | | var options = context.GetRequiredService<IOptions<ApiEndpointOptions>>().Value; |
| | 0 | 62 | | var url = $"{options.RoutePrefix}/bookmarks/resume?t={token}"; |
| | 0 | 63 | | var absoluteUrlProvider = context.GetRequiredService<IAbsoluteUrlProvider>(); |
| | 0 | 64 | | return absoluteUrlProvider.ToAbsoluteUrl(url).ToString(); |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | private static string GenerateBookmarkTriggerTokenInternal(this ExpressionExecutionContext context, string bookmarkI |
| | | 68 | | { |
| | 0 | 69 | | var workflowInstanceId = context.GetWorkflowExecutionContext().Id; |
| | 0 | 70 | | var payload = new BookmarkTokenPayload(bookmarkId, workflowInstanceId); |
| | 0 | 71 | | var tokenService = context.GetRequiredService<ITokenService>(); |
| | | 72 | | |
| | 0 | 73 | | return lifetime != null |
| | 0 | 74 | | ? tokenService.CreateToken(payload, lifetime.Value) |
| | 0 | 75 | | : expiresAt != null |
| | 0 | 76 | | ? tokenService.CreateToken(payload, expiresAt.Value) |
| | 0 | 77 | | : tokenService.CreateToken(payload); |
| | | 78 | | } |
| | | 79 | | } |