summary refs log tree commit diff
path: root/ModAS.Server/Controllers/HomeController.cs
blob: 48432d53a48f997e5e557d9b94f33da25e6086e9 (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
using System.Diagnostics;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using ModAS.Server.Controllers.AppService;
using MxApiExtensions.Services;

namespace ModAS.Server.Controllers;

/// <summary>
///    Manages the visual homepage.
/// </summary>
[ApiController]
public class HomeController(AppServiceRegistration asr, ModASConfiguration config) : Controller {
    private const string ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    /// <inheritdoc cref="HomeController"/>
    [HttpGet("/_matrix/_modas")]
    [ApiExplorerSettings(IgnoreApi = true)] //hide from swagger
    public IActionResult Index() {
        return LocalRedirect("/index.html");
    }

    [HttpGet("/_matrix/_modas/version")]
    public IActionResult Version() {
        return Ok(new {
            Version = Modas.Server.Version.VersionString
        });
    }

    [HttpGet("/_matrix/_modas/ping")]
    public async Task<IActionResult> Ping() {
        var txn = new PingController.TransactionIdContainer() {
            TransactionId = RandomNumberGenerator.GetString(ValidChars, 32)
        };
        var url = $"{config.HomeserverUrl}/_matrix/client/v1/appservice/{HttpUtility.UrlEncode(asr.Id)}/ping";
        var hrm = new HttpRequestMessage(HttpMethod.Post, url) {
            Content = new StringContent(JsonSerializer.Serialize(txn), Encoding.UTF8, "application/json"),
            Headers = {
                Authorization = new AuthenticationHeaderValue("Bearer", asr.AppServiceToken)
            }
        };
        var req = await new HttpClient().SendAsync(hrm);
        var resp = await req.Content.ReadFromJsonAsync<JsonObject>();
        resp!["tnxId"] = txn.TransactionId;
        return Ok(resp);
    }
}