✨ Automated status
This commit is contained in:
		| @@ -191,7 +191,9 @@ public class AccountController( | |||||||
|         public StatusAttitude Attitude { get; set; } |         public StatusAttitude Attitude { get; set; } | ||||||
|         public bool IsInvisible { get; set; } |         public bool IsInvisible { get; set; } | ||||||
|         public bool IsNotDisturb { get; set; } |         public bool IsNotDisturb { get; set; } | ||||||
|  |         public bool IsAutomated { get; set; } = false; | ||||||
|         [MaxLength(1024)] public string? Label { get; set; } |         [MaxLength(1024)] public string? Label { get; set; } | ||||||
|  |         [MaxLength(4096)] public string? AppIdentifier { get; set; } | ||||||
|         public Instant? ClearedAt { get; set; } |         public Instant? ClearedAt { get; set; } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -197,6 +197,8 @@ public class AccountCurrentController( | |||||||
|     public async Task<ActionResult<Status>> UpdateStatus([FromBody] AccountController.StatusRequest request) |     public async Task<ActionResult<Status>> UpdateStatus([FromBody] AccountController.StatusRequest request) | ||||||
|     { |     { | ||||||
|         if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized(); |         if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized(); | ||||||
|  |         if (request is { IsAutomated: true, AppIdentifier: not null }) | ||||||
|  |             return BadRequest("Automated status cannot be updated."); | ||||||
|  |  | ||||||
|         var now = SystemClock.Instance.GetCurrentInstant(); |         var now = SystemClock.Instance.GetCurrentInstant(); | ||||||
|         var status = await db.AccountStatuses |         var status = await db.AccountStatuses | ||||||
| @@ -205,11 +207,15 @@ public class AccountCurrentController( | |||||||
|             .OrderByDescending(e => e.CreatedAt) |             .OrderByDescending(e => e.CreatedAt) | ||||||
|             .FirstOrDefaultAsync(); |             .FirstOrDefaultAsync(); | ||||||
|         if (status is null) return NotFound(ApiError.NotFound("status", traceId: HttpContext.TraceIdentifier)); |         if (status is null) return NotFound(ApiError.NotFound("status", traceId: HttpContext.TraceIdentifier)); | ||||||
|  |         if (status.IsAutomated && request.AppIdentifier is null) | ||||||
|  |             return BadRequest("Automated status cannot be updated."); | ||||||
|  |  | ||||||
|         status.Attitude = request.Attitude; |         status.Attitude = request.Attitude; | ||||||
|         status.IsInvisible = request.IsInvisible; |         status.IsInvisible = request.IsInvisible; | ||||||
|         status.IsNotDisturb = request.IsNotDisturb; |         status.IsNotDisturb = request.IsNotDisturb; | ||||||
|  |         status.IsAutomated = request.IsAutomated; | ||||||
|         status.Label = request.Label; |         status.Label = request.Label; | ||||||
|  |         status.AppIdentifier = request.AppIdentifier; | ||||||
|         status.ClearedAt = request.ClearedAt; |         status.ClearedAt = request.ClearedAt; | ||||||
|  |  | ||||||
|         db.Update(status); |         db.Update(status); | ||||||
| @@ -225,13 +231,42 @@ public class AccountCurrentController( | |||||||
|     { |     { | ||||||
|         if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized(); |         if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized(); | ||||||
|  |  | ||||||
|  |         if (request is { IsAutomated: true, AppIdentifier: not null }) | ||||||
|  |         { | ||||||
|  |             var now = SystemClock.Instance.GetCurrentInstant(); | ||||||
|  |             var existingStatus = await db.AccountStatuses | ||||||
|  |                 .Where(s => s.AccountId == currentUser.Id) | ||||||
|  |                 .Where(s => s.ClearedAt == null || s.ClearedAt > now) | ||||||
|  |                 .OrderByDescending(s => s.CreatedAt) | ||||||
|  |                 .FirstOrDefaultAsync(); | ||||||
|  |             if (existingStatus is not null) | ||||||
|  |                 if (existingStatus.IsAutomated && request.AppIdentifier == existingStatus.AppIdentifier) | ||||||
|  |                 { | ||||||
|  |                     existingStatus.Attitude = request.Attitude; | ||||||
|  |                     existingStatus.IsInvisible = request.IsInvisible; | ||||||
|  |                     existingStatus.IsNotDisturb = request.IsNotDisturb; | ||||||
|  |                     existingStatus.Label = request.Label; | ||||||
|  |                     db.Update(existingStatus); | ||||||
|  |                     await db.SaveChangesAsync(); | ||||||
|  |                     return Ok(existingStatus); | ||||||
|  |                 } | ||||||
|  |                 else | ||||||
|  |                 { | ||||||
|  |                     existingStatus.ClearedAt = now; | ||||||
|  |                     db.Update(existingStatus); | ||||||
|  |                     await db.SaveChangesAsync(); | ||||||
|  |                 } | ||||||
|  |         } | ||||||
|  |  | ||||||
|         var status = new Status |         var status = new Status | ||||||
|         { |         { | ||||||
|             AccountId = currentUser.Id, |             AccountId = currentUser.Id, | ||||||
|             Attitude = request.Attitude, |             Attitude = request.Attitude, | ||||||
|             IsInvisible = request.IsInvisible, |             IsInvisible = request.IsInvisible, | ||||||
|             IsNotDisturb = request.IsNotDisturb, |             IsNotDisturb = request.IsNotDisturb, | ||||||
|  |             IsAutomated = request.IsAutomated, | ||||||
|             Label = request.Label, |             Label = request.Label, | ||||||
|  |             AppIdentifier = request.AppIdentifier, | ||||||
|             ClearedAt = request.ClearedAt |             ClearedAt = request.ClearedAt | ||||||
|         }; |         }; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -23,6 +23,12 @@ public class Status : ModelBase | |||||||
|     public bool IsNotDisturb { get; set; } |     public bool IsNotDisturb { get; set; } | ||||||
|     [MaxLength(1024)] public string? Label { get; set; } |     [MaxLength(1024)] public string? Label { get; set; } | ||||||
|     public Instant? ClearedAt { get; set; } |     public Instant? ClearedAt { get; set; } | ||||||
|  |     [MaxLength(4096)] public string? AppIdentifier { get; set; } | ||||||
|  |      | ||||||
|  |     /// <summary> | ||||||
|  |     /// Indicates this status is created based on running process or rich presence | ||||||
|  |     /// </summary> | ||||||
|  |     public bool IsAutomated { get; set; } | ||||||
|  |  | ||||||
|     public Guid AccountId { get; set; } |     public Guid AccountId { get; set; } | ||||||
|     public Account Account { get; set; } = null!; |     public Account Account { get; set; } = null!; | ||||||
|   | |||||||
							
								
								
									
										2035
									
								
								DysonNetwork.Pass/Migrations/20250908151924_AddAutomatedStatus.Designer.cs
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										2035
									
								
								DysonNetwork.Pass/Migrations/20250908151924_AddAutomatedStatus.Designer.cs
									
									
									
										generated
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -0,0 +1,40 @@ | |||||||
|  | using Microsoft.EntityFrameworkCore.Migrations; | ||||||
|  |  | ||||||
|  | #nullable disable | ||||||
|  |  | ||||||
|  | namespace DysonNetwork.Pass.Migrations | ||||||
|  | { | ||||||
|  |     /// <inheritdoc /> | ||||||
|  |     public partial class AddAutomatedStatus : Migration | ||||||
|  |     { | ||||||
|  |         /// <inheritdoc /> | ||||||
|  |         protected override void Up(MigrationBuilder migrationBuilder) | ||||||
|  |         { | ||||||
|  |             migrationBuilder.AddColumn<string>( | ||||||
|  |                 name: "app_identifier", | ||||||
|  |                 table: "account_statuses", | ||||||
|  |                 type: "character varying(4096)", | ||||||
|  |                 maxLength: 4096, | ||||||
|  |                 nullable: true); | ||||||
|  |  | ||||||
|  |             migrationBuilder.AddColumn<bool>( | ||||||
|  |                 name: "is_automated", | ||||||
|  |                 table: "account_statuses", | ||||||
|  |                 type: "boolean", | ||||||
|  |                 nullable: false, | ||||||
|  |                 defaultValue: false); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         /// <inheritdoc /> | ||||||
|  |         protected override void Down(MigrationBuilder migrationBuilder) | ||||||
|  |         { | ||||||
|  |             migrationBuilder.DropColumn( | ||||||
|  |                 name: "app_identifier", | ||||||
|  |                 table: "account_statuses"); | ||||||
|  |  | ||||||
|  |             migrationBuilder.DropColumn( | ||||||
|  |                 name: "is_automated", | ||||||
|  |                 table: "account_statuses"); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -767,6 +767,11 @@ namespace DysonNetwork.Pass.Migrations | |||||||
|                         .HasColumnType("uuid") |                         .HasColumnType("uuid") | ||||||
|                         .HasColumnName("account_id"); |                         .HasColumnName("account_id"); | ||||||
|  |  | ||||||
|  |                     b.Property<string>("AppIdentifier") | ||||||
|  |                         .HasMaxLength(4096) | ||||||
|  |                         .HasColumnType("character varying(4096)") | ||||||
|  |                         .HasColumnName("app_identifier"); | ||||||
|  |  | ||||||
|                     b.Property<int>("Attitude") |                     b.Property<int>("Attitude") | ||||||
|                         .HasColumnType("integer") |                         .HasColumnType("integer") | ||||||
|                         .HasColumnName("attitude"); |                         .HasColumnName("attitude"); | ||||||
| @@ -783,6 +788,10 @@ namespace DysonNetwork.Pass.Migrations | |||||||
|                         .HasColumnType("timestamp with time zone") |                         .HasColumnType("timestamp with time zone") | ||||||
|                         .HasColumnName("deleted_at"); |                         .HasColumnName("deleted_at"); | ||||||
|  |  | ||||||
|  |                     b.Property<bool>("IsAutomated") | ||||||
|  |                         .HasColumnType("boolean") | ||||||
|  |                         .HasColumnName("is_automated"); | ||||||
|  |  | ||||||
|                     b.Property<bool>("IsInvisible") |                     b.Property<bool>("IsInvisible") | ||||||
|                         .HasColumnType("boolean") |                         .HasColumnType("boolean") | ||||||
|                         .HasColumnName("is_invisible"); |                         .HasColumnName("is_invisible"); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user