[backend/api] Add mastodon session management endpoints

This commit is contained in:
Laura Hausmann 2025-01-07 08:32:42 +01:00
parent 76a3f94fe8
commit 0723f39797
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 80 additions and 0 deletions

View file

@ -57,4 +57,65 @@ public class SessionController(DatabaseContext db) : ControllerBase
db.Remove(session);
await db.SaveChangesAsync();
}
[HttpGet("mastodon")]
[ProducesResults(HttpStatusCode.OK)]
public async Task<List<MastodonSessionResponse>> 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();
}
}

View file

@ -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<string> 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; }
}
}