[backend/api] Add user settings endpoints (ISH-299)
This commit is contained in:
parent
d109f00d55
commit
922a759fb1
4 changed files with 85 additions and 0 deletions
65
Iceshrimp.Backend/Controllers/SettingsController.cs
Normal file
65
Iceshrimp.Backend/Controllers/SettingsController.cs
Normal file
|
@ -0,0 +1,65 @@
|
|||
using System.Net.Mime;
|
||||
using Iceshrimp.Backend.Core.Database;
|
||||
using Iceshrimp.Backend.Core.Database.Tables;
|
||||
using Iceshrimp.Backend.Core.Middleware;
|
||||
using Iceshrimp.Shared.Schemas;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.RateLimiting;
|
||||
|
||||
namespace Iceshrimp.Backend.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Authenticate]
|
||||
[Authorize]
|
||||
[EnableRateLimiting("sliding")]
|
||||
[Route("/api/iceshrimp/settings")]
|
||||
[Produces(MediaTypeNames.Application.Json)]
|
||||
public class SettingsController(DatabaseContext db) : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserSettingsEntity))]
|
||||
public async Task<IActionResult> GetSettings()
|
||||
{
|
||||
var settings = await GetOrInitUserSettings();
|
||||
|
||||
var res = new UserSettingsEntity
|
||||
{
|
||||
FilterInaccessible = settings.FilterInaccessible,
|
||||
PrivateMode = settings.PrivateMode,
|
||||
DefaultNoteVisibility = (NoteVisibility)settings.DefaultNoteVisibility,
|
||||
DefaultRenoteVisibility = (NoteVisibility)settings.DefaultNoteVisibility
|
||||
};
|
||||
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
[Consumes(MediaTypeNames.Application.Json)]
|
||||
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(object))]
|
||||
public async Task<IActionResult> UpdateSettings(UserSettingsEntity newSettings)
|
||||
{
|
||||
var settings = await GetOrInitUserSettings();
|
||||
|
||||
settings.FilterInaccessible = newSettings.FilterInaccessible;
|
||||
settings.PrivateMode = newSettings.PrivateMode;
|
||||
settings.DefaultNoteVisibility = (Note.NoteVisibility)newSettings.DefaultNoteVisibility;
|
||||
settings.DefaultRenoteVisibility = (Note.NoteVisibility)newSettings.DefaultRenoteVisibility;
|
||||
|
||||
return Ok(new object());
|
||||
}
|
||||
|
||||
private async Task<UserSettings> GetOrInitUserSettings()
|
||||
{
|
||||
var user = HttpContext.GetUserOrFail();
|
||||
var settings = user.UserSettings;
|
||||
if (settings == null)
|
||||
{
|
||||
settings = new UserSettings { User = user };
|
||||
db.Add(settings);
|
||||
await db.SaveChangesAsync();
|
||||
await db.ReloadEntityAsync(settings);
|
||||
}
|
||||
|
||||
return settings;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
using Iceshrimp.Frontend.Core.Services;
|
||||
using Iceshrimp.Shared.Schemas;
|
||||
|
||||
namespace Iceshrimp.Frontend.Core.ControllerModels;
|
||||
|
||||
internal class SettingsControllerModel(ApiClient api)
|
||||
{
|
||||
public Task<UserSettingsEntity> GetSettings() => api.Call<UserSettingsEntity>(HttpMethod.Get, "/settings");
|
||||
public Task UpdateSettings(UserSettingsEntity settings) => api.Call(HttpMethod.Put, "/settings", data: settings);
|
||||
}
|
|
@ -9,6 +9,7 @@ internal class ApiService(ApiClient client)
|
|||
public readonly AuthControllerModel Auth = new(client);
|
||||
public readonly DriveControllerModel Drive = new(client);
|
||||
public readonly AdminControllerModel Admin = new(client);
|
||||
public readonly SettingsControllerModel Settings = new(client);
|
||||
public readonly TimelineControllerModel Timelines = new(client);
|
||||
public readonly NotificationControllerModel Notifications = new(client);
|
||||
|
||||
|
|
9
Iceshrimp.Shared/Schemas/UserSettingsEntity.cs
Normal file
9
Iceshrimp.Shared/Schemas/UserSettingsEntity.cs
Normal file
|
@ -0,0 +1,9 @@
|
|||
namespace Iceshrimp.Shared.Schemas;
|
||||
|
||||
public class UserSettingsEntity
|
||||
{
|
||||
public NoteVisibility DefaultNoteVisibility { get; set; }
|
||||
public NoteVisibility DefaultRenoteVisibility { get; set; }
|
||||
public bool PrivateMode { get; set; }
|
||||
public bool FilterInaccessible { get; set; }
|
||||
}
|
Loading…
Add table
Reference in a new issue