diff --git a/Iceshrimp.Backend/Controllers/Web/SessionController.cs b/Iceshrimp.Backend/Controllers/Web/SessionController.cs index 4eec4a5e..9acf7a2b 100644 --- a/Iceshrimp.Backend/Controllers/Web/SessionController.cs +++ b/Iceshrimp.Backend/Controllers/Web/SessionController.cs @@ -57,4 +57,65 @@ public class SessionController(DatabaseContext db) : ControllerBase db.Remove(session); await db.SaveChangesAsync(); } + + [HttpGet("mastodon")] + [ProducesResults(HttpStatusCode.OK)] + public async Task> GetMastodonSessions(int page = 0) + { + const int pageSize = 20; + + return await db.OauthTokens + .Include(p => p.App) + .Where(p => p.User == HttpContext.GetUserOrFail()) + .OrderByDescending(p => p.LastActiveDate ?? p.CreatedAt) + .Skip(page * pageSize) + .Take(pageSize) + .Select(p => new MastodonSessionResponse + { + Id = p.Id, + Active = p.Active, + CreatedAt = p.CreatedAt, + LastActive = p.LastActiveDate, + App = p.App.Name, + Scopes = p.Scopes, + Flags = new MastodonSessionFlags + { + SupportsHtmlFormatting = p.SupportsHtmlFormatting, + AutoDetectQuotes = p.AutoDetectQuotes, + IsPleroma = p.IsPleroma, + SupportsInlineMedia = p.SupportsInlineMedia + } + }) + .ToListAsync(); + } + + [HttpPatch("mastodon/{id}")] + [ProducesResults(HttpStatusCode.OK)] + [ProducesErrors(HttpStatusCode.BadRequest, HttpStatusCode.NotFound)] + public async Task UpdateMastodonSession(string id, [FromBody] MastodonSessionFlags flags) + { + var user = HttpContext.GetUserOrFail(); + var token = await db.OauthTokens.FirstOrDefaultAsync(p => p.Id == id && p.User == user) + ?? throw GracefulException.NotFound("Session not found"); + + token.SupportsHtmlFormatting = flags.SupportsHtmlFormatting; + token.AutoDetectQuotes = flags.AutoDetectQuotes; + token.IsPleroma = flags.IsPleroma; + token.SupportsInlineMedia = flags.SupportsInlineMedia; + + await db.SaveChangesAsync(); + } + + [HttpDelete("mastodon/{id}")] + [ProducesResults(HttpStatusCode.OK)] + [ProducesErrors(HttpStatusCode.BadRequest, HttpStatusCode.NotFound)] + public async Task TerminateMastodonSession(string id) + { + var user = HttpContext.GetUserOrFail(); + var token = await db.OauthTokens.FirstOrDefaultAsync(p => p.Id == id && p.User == user) + ?? throw GracefulException.NotFound("Session not found"); + + db.Remove(token); + await db.SaveChangesAsync(); + } } diff --git a/Iceshrimp.Shared/Schemas/Web/SessionSchemas.cs b/Iceshrimp.Shared/Schemas/Web/SessionSchemas.cs index 9b7c7995..86e8d994 100644 --- a/Iceshrimp.Shared/Schemas/Web/SessionSchemas.cs +++ b/Iceshrimp.Shared/Schemas/Web/SessionSchemas.cs @@ -12,4 +12,23 @@ public class SessionSchemas public required DateTime CreatedAt { get; set; } public required DateTime? LastActive { get; set; } } + + public class MastodonSessionResponse : IIdentifiable + { + public required string Id { get; set; } + public required bool Active { get; set; } + public required DateTime CreatedAt { get; set; } + public required DateTime? LastActive { get; set; } + public required string App { get; set; } + public required List Scopes { get; set; } + public required MastodonSessionFlags Flags { get; set; } + } + + public class MastodonSessionFlags + { + public required bool SupportsHtmlFormatting { get; set; } + public required bool AutoDetectQuotes { get; set; } + public required bool IsPleroma { get; set; } + public required bool SupportsInlineMedia { get; set; } + } }