summary refs log tree commit diff
path: root/extra/admin-api/Spacebar.Offload/Controllers/ChannelStatusController.cs
blob: ba242680b9da290f4b948ce0fd968e5c91358314 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Spacebar.Interop.Authentication.AspNetCore;
using Spacebar.Interop.Replication.Abstractions;
using Spacebar.Models.Db.Contexts;
using Spacebar.Models.Gateway;

namespace Spacebar.GatewayOffload.Controllers;

[ApiController]
[Route("/_spacebar/offload/gateway")]
public class ChannelStatusController(ILogger<ChannelStatusController> logger, SpacebarAspNetAuthenticationService authService, SpacebarDbContext db, IServiceProvider sp)
    : ControllerBase {
    [HttpPost("ChannelStatuses")]
    public async IAsyncEnumerable<ReplicationMessage<ChannelStatusesResponse>> GetChannelStatuses([FromBody] ChannelStatusesRequest req) {
        await foreach (var res in GetChannelInfos(new() {
                           Fields = ["status"],
                           GuildIdRawValue = req.GuildIdRawValue,
                       })) {
            yield return new() {
                Payload = new() {
                    GuildId = res.Payload.GuildId,
                    Channels = res.Payload.Channels.Select(c => new ChannelStatus {
                        ChannelId = c.ChannelId,
                        Status = c.Status!,
                    }).ToList(),
                }
            };
        }
    }

    [HttpPost("ChannelInfo")]
    public async IAsyncEnumerable<ReplicationMessage<ChannelInfoResponse>> GetChannelInfos([FromBody] ChannelInfoRequest req) {
        var user = await authService.GetCurrentUserAsync(Request);
        string[] statusOptions = [
            "Vibing ✨",
            "Hanging out: 12%...",
            "Communicating...",
            // idk, i cant come up with more stuff, maybe suggestions welcome, or actually storing some data?
        ];

        foreach (var guildId in req.GuildIds ?? [req.GuildId!.Value]) {
            var channels = (await db.Channels.Include(x => x.VoiceStates).Where(x => x.Type == 2 && x.GuildId == guildId && x.VoiceStates.Count > 0)
                    .Select(x => x.Id)
                    .ToListAsync())
                .Select(x => new {
                    id = x,
                    status = statusOptions[new Random().Next(statusOptions.Length)], // TODO: We don't currently store channel statuses, so make some stuff up
                    voiceStartTime = DateTime.Now.Subtract(TimeSpan.FromMinutes(new Random().Next(1, 120))), // TODO: We also don't store voice start times, so make some stuff up
                }).ToList();

            yield return new() {
                Payload = new() {
                    GuildId = guildId,
                    Channels = channels.Select(c => new ChannelInfo {
                        ChannelId = c.id,
                        Status = req.Fields.Contains("status") ? c.status : null,
                        VoiceStartTime = req.Fields.Contains("voice_start_time") ? c.voiceStartTime : null,
                    }).ToList(),
                },
            };
        }
    }
}