[backend/masto-client] Cleanup MarkerController
This commit is contained in:
parent
8a917fee71
commit
5705e3f3f1
2 changed files with 19 additions and 70 deletions
|
@ -24,86 +24,49 @@ public class MarkerController(DatabaseContext db) : ControllerBase
|
||||||
{
|
{
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Authorize("read:statuses")]
|
[Authorize("read:statuses")]
|
||||||
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(MarkerSchemas.MarkerResponse))]
|
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(Dictionary<string, MarkerEntity>))]
|
||||||
public async Task<IActionResult> GetMarkers([FromQuery(Name = "timeline")] List<string> types)
|
public async Task<IActionResult> GetMarkers([FromQuery(Name = "timeline")] List<string> types)
|
||||||
{
|
{
|
||||||
var user = HttpContext.GetUserOrFail();
|
var user = HttpContext.GetUserOrFail();
|
||||||
var markers = await db.Markers.Where(p => p.User == user && types.Select(DecodeType).Contains(p.Type))
|
var markers = await db.Markers.Where(p => p.User == user && types.Select(DecodeType).Contains(p.Type))
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
var res = new MarkerSchemas.MarkerResponse();
|
var res = markers.ToDictionary(p => EncodeType(p.Type),
|
||||||
var home = markers.FirstOrDefault(p => p.Type == Marker.MarkerType.Home);
|
p => new MarkerEntity
|
||||||
var notifications = markers.FirstOrDefault(p => p.Type == Marker.MarkerType.Notifications);
|
{
|
||||||
if (home != null)
|
Position = p.Position,
|
||||||
{
|
Version = p.Version,
|
||||||
res.Home = new MarkerEntity
|
UpdatedAt = p.LastUpdatedAt.ToStringIso8601Like()
|
||||||
{
|
});
|
||||||
Position = home.Position,
|
|
||||||
Version = home.Version,
|
|
||||||
UpdatedAt = home.LastUpdatedAt.ToStringIso8601Like()
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (notifications != null)
|
|
||||||
{
|
|
||||||
res.Notifications = new MarkerEntity
|
|
||||||
{
|
|
||||||
Position = notifications.Position,
|
|
||||||
Version = notifications.Version,
|
|
||||||
UpdatedAt = notifications.LastUpdatedAt.ToStringIso8601Like()
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return Ok(res);
|
return Ok(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[Authorize("write:statuses")]
|
[Authorize("write:statuses")]
|
||||||
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(MarkerSchemas.MarkerResponse))]
|
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(Dictionary<string, MarkerEntity>))]
|
||||||
public async Task<IActionResult> SetMarkers([FromHybrid] MarkerSchemas.MarkerRequest request)
|
public async Task<IActionResult> SetMarkers([FromHybrid] Dictionary<string, MarkerSchemas.MarkerPosition> request)
|
||||||
{
|
{
|
||||||
var user = HttpContext.GetUserOrFail();
|
var user = HttpContext.GetUserOrFail();
|
||||||
var types = new List<Marker.MarkerType>();
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (request.Home != null)
|
foreach (var item in request)
|
||||||
{
|
{
|
||||||
types.Add(Marker.MarkerType.Home);
|
var type = DecodeType(item.Key);
|
||||||
var marker = await db.Markers.FirstOrDefaultAsync(p => p.User == user &&
|
|
||||||
p.Type == Marker.MarkerType.Home);
|
var marker = await db.Markers.FirstOrDefaultAsync(p => p.User == user && p.Type == type);
|
||||||
if (marker == null)
|
if (marker == null)
|
||||||
{
|
{
|
||||||
marker = new Marker { User = user, Type = Marker.MarkerType.Home };
|
marker = new Marker { User = user, Type = type };
|
||||||
await db.AddAsync(marker);
|
await db.AddAsync(marker);
|
||||||
}
|
}
|
||||||
else if (marker.Position != request.Home.LastReadId)
|
else if (marker.Position != item.Value.LastReadId)
|
||||||
{
|
{
|
||||||
marker.Version++;
|
marker.Version++;
|
||||||
}
|
}
|
||||||
|
|
||||||
marker.LastUpdatedAt = DateTime.UtcNow;
|
marker.LastUpdatedAt = DateTime.UtcNow;
|
||||||
marker.Position = request.Home.LastReadId;
|
marker.Position = item.Value.LastReadId;
|
||||||
}
|
|
||||||
|
|
||||||
if (request.Notifications != null)
|
|
||||||
{
|
|
||||||
types.Add(Marker.MarkerType.Notifications);
|
|
||||||
|
|
||||||
var marker = await db.Markers.FirstOrDefaultAsync(p => p.User == user &&
|
|
||||||
p.Type == Marker.MarkerType.Notifications);
|
|
||||||
if (marker == null)
|
|
||||||
{
|
|
||||||
marker = new Marker { User = user, Type = Marker.MarkerType.Notifications };
|
|
||||||
await db.AddAsync(marker);
|
|
||||||
}
|
|
||||||
else if (marker.Position != request.Notifications.LastReadId)
|
|
||||||
{
|
|
||||||
marker.Version++;
|
|
||||||
}
|
|
||||||
|
|
||||||
marker.LastUpdatedAt = DateTime.UtcNow;
|
|
||||||
marker.Position = request.Notifications.LastReadId;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await db.SaveChangesAsync();
|
await db.SaveChangesAsync();
|
||||||
|
@ -113,7 +76,7 @@ public class MarkerController(DatabaseContext db) : ControllerBase
|
||||||
throw new GracefulException(HttpStatusCode.Conflict, "Conflict during update, please try again");
|
throw new GracefulException(HttpStatusCode.Conflict, "Conflict during update, please try again");
|
||||||
}
|
}
|
||||||
|
|
||||||
return await GetMarkers(types.Select(EncodeType).ToList());
|
return await GetMarkers(request.Keys.ToList());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Marker.MarkerType DecodeType(string type) =>
|
public Marker.MarkerType DecodeType(string type) =>
|
||||||
|
|
|
@ -1,24 +1,10 @@
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
using Iceshrimp.Backend.Controllers.Mastodon.Schemas.Entities;
|
|
||||||
using J = System.Text.Json.Serialization.JsonPropertyNameAttribute;
|
using J = System.Text.Json.Serialization.JsonPropertyNameAttribute;
|
||||||
using JI = System.Text.Json.Serialization.JsonIgnoreAttribute;
|
|
||||||
using B = Microsoft.AspNetCore.Mvc.BindPropertyAttribute;
|
using B = Microsoft.AspNetCore.Mvc.BindPropertyAttribute;
|
||||||
|
|
||||||
namespace Iceshrimp.Backend.Controllers.Mastodon.Schemas;
|
namespace Iceshrimp.Backend.Controllers.Mastodon.Schemas;
|
||||||
|
|
||||||
public class MarkerSchemas
|
public class MarkerSchemas
|
||||||
{
|
{
|
||||||
public class MarkerResponse
|
|
||||||
{
|
|
||||||
[J("home")]
|
|
||||||
[JI(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
|
||||||
public MarkerEntity? Home { get; set; }
|
|
||||||
|
|
||||||
[J("notifications")]
|
|
||||||
[JI(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
|
||||||
public MarkerEntity? Notifications { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class MarkerRequest
|
public class MarkerRequest
|
||||||
{
|
{
|
||||||
[J("home")]
|
[J("home")]
|
||||||
|
|
Loading…
Add table
Reference in a new issue