diff options
Diffstat (limited to 'OsuFederatedBeatmapApi/Services')
-rw-r--r-- | OsuFederatedBeatmapApi/Services/BeatmapFetcherService.cs | 34 | ||||
-rw-r--r-- | OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBot.cs | 23 |
2 files changed, 56 insertions, 1 deletions
diff --git a/OsuFederatedBeatmapApi/Services/BeatmapFetcherService.cs b/OsuFederatedBeatmapApi/Services/BeatmapFetcherService.cs new file mode 100644 index 0000000..ae108fb --- /dev/null +++ b/OsuFederatedBeatmapApi/Services/BeatmapFetcherService.cs @@ -0,0 +1,34 @@ +using System.Net.Http.Headers; +using OsuFederatedBeatmapApi.Events.State; +using OsuFederatedBeatmapApi.UtilityClasses; + +namespace OsuFederatedBeatmapApi.Services; + +public class BeatmapFetcherService(FederatedBeatmapApiBotAccountDataService ads) { + + public async Task<BeatmapSetInfo> FetchBeatmapSetInfo(int id) { + var hc = new HttpClient(); + hc.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("OsuFederatedBeatmapApi", "1.0")); + var res = await hc.GetFromJsonAsync<OsuDirectApiResponses.BeatmapSet>($"https://osu.direct/api/s/{id}"); + var info = new BeatmapSetInfo { + Title = res.Title, + Artist = res.Artist, + Creator = res.Creator, + Beatmaps = res.ChildrenBeatmaps.ToDictionary(b => b.BeatmapID, b => new BeatmapSetInfo.BeatmapInfo { + Difficulty = b.DiffName, + Mode = b.Mode, + DifficultyRating = b.DifficultyRating, + TotalLength = b.TotalLength, + BPM = b.BPM, + MaxCombo = b.MaxCombo + }) + }; + + await ads.LoadAccountDataAsync(); + + await ads.OwnBeatmapRepositoryRoom.SendStateEventAsync(BeatmapSetInfo.EventName, id.ToString(), info); + + return info; + } + +} diff --git a/OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBot.cs b/OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBot.cs index b8cc635..2aaf45c 100644 --- a/OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBot.cs +++ b/OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBot.cs @@ -40,7 +40,28 @@ public class FederatedBeatmapApiBot(AuthenticatedHomeserverGeneric hs, var controlRoomMembers = accountDataService.ControlRoom.GetMembersAsync(); await foreach (var member in controlRoomMembers) { if ((member.TypedContent as RoomMemberEventContent)? - .Membership == "join") admins.Add(member.UserId); + .Membership == "join") + admins.Add(member.StateKey); + } + + var pls = await accountDataService.OwnBeatmapRepositoryRoom.GetPowerLevelsAsync(); + var ownPl = pls.GetUserPowerLevel(hs.UserId); + + if (!pls.UserHasPermission(hs.UserId, RoomPowerLevelEventContent.EventId)) { + accountDataService.LogRoom.SendMessageEventAsync( + MessageFormatter.FormatError( + $"I don't have permission to send power level updates in " + + $"{MessageFormatter.HtmlFormatMention(accountDataService.OwnBeatmapRepositoryRoom.RoomId, "my own beatmap repository")}!")); + } + else { + foreach (var admin in admins) { + if (pls.GetUserPowerLevel(admin) < ownPl - 1) { + logger.LogInformation("Raising powerlevel of {} in {} to {}", admin, accountDataService.OwnBeatmapRepositoryRoom.RoomId, ownPl - 1); + pls.SetUserPowerLevel(admin, ownPl - 1); + await accountDataService.OwnBeatmapRepositoryRoom.SendStateEventAsync( + RoomPowerLevelEventContent.EventId, pls); + } + } } await Task.Delay(TimeSpan.FromSeconds(30), cancellationToken); |