about summary refs log tree commit diff
path: root/MatrixRoomUtils.Bot/MRUBot.cs
blob: 9b2a395b1db6cfa9847f48b38af509b38ceeb829 (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
using System.CodeDom.Compiler;
using System.Diagnostics.CodeAnalysis;
using MatrixRoomUtils.Core;
using MatrixRoomUtils.Core.Extensions;
using MatrixRoomUtils.Core.Helpers;
using MatrixRoomUtils.Core.Responses;
using MatrixRoomUtils.Core.Services;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

public class MRUBot : IHostedService {
    private readonly HomeserverProviderService _homeserverProviderService;
    private readonly ILogger<MRUBot> _logger;

    public MRUBot(HomeserverProviderService homeserverProviderService, ILogger<MRUBot> logger) {
        Console.WriteLine("MRUBot hosted service instantiated!");
        _homeserverProviderService = homeserverProviderService;
        _logger = logger;
    }

    /// <summary>Triggered when the application host is ready to start the service.</summary>
    /// <param name="cancellationToken">Indicates that the start process has been aborted.</param>
    [SuppressMessage("ReSharper", "FunctionNeverReturns")]
    public async Task StartAsync(CancellationToken cancellationToken) {
        var hs = await _homeserverProviderService.GetAuthenticatedWithToken("rory.gay", "syt_bXJ1Y29yZXRlc3Q_XKUmPswDGZBiLAmFfAut_1iO0KD");
        await (await hs.GetRoom("!DoHEdFablOLjddKWIp:rory.gay")).JoinAsync();
        // #pragma warning disable CS4014
        //         Task.Run(async Task? () => {
        // #pragma warning restore CS4014
        //             while (true) {
        //                 var rooms = await hs.GetJoinedRooms();
        //                 foreach (var room in rooms) {
        //                     var states = await room.GetStateAsync<List<StateEventResponse>>("");
        //                     foreach (var state in states) {
        //                         // Console.WriteLine(
        //                         //     $"{state.RoomId}: {state.Type}::{state.StateKey} = {ObjectExtensions.ToJson(state.Content, indent: false)}");
        //                     }
        //                 }
        //
        //                 await Task.Delay(1000, cancellationToken);
        //             }
        //         }, cancellationToken);
        #pragma warning disable CS4014
                Task.Run(async Task? () => {
        #pragma warning restore CS4014
                    SyncResult? sync = null;
                    while (true) {
                        sync = await hs.SyncHelper.Sync(sync?.NextBatch);
                        _logger.LogInformation($"Got sync, next batch: {sync?.NextBatch}!");
                    }
                }, cancellationToken);
        
    }

    /// <summary>Triggered when the application host is performing a graceful shutdown.</summary>
    /// <param name="cancellationToken">Indicates that the shutdown process should no longer be graceful.</param>
    public async Task StopAsync(CancellationToken cancellationToken) {
        Console.WriteLine("Shutting down bot!");
    }
}