Skip to content

Commit

Permalink
add channel_image_url to anarchy input event
Browse files Browse the repository at this point in the history
  • Loading branch information
Felk committed Feb 12, 2024
1 parent 5e81e40 commit 06f278d
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 14 deletions.
2 changes: 1 addition & 1 deletion TPP.Core/Chat/TwitchChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public async Task<JoinResult> Join(string userLogin)
return JoinResult.StreamOffline;
}

await _coStreamChannelsRepo.Add(userLogin);
await _coStreamChannelsRepo.Add(userLogin, user.ProfileImageUrl);
await _twitchClient.JoinChannelAsync(userLogin);
await _twitchClient.SendMessageAsync(userLogin, "Joined channel, hello!");

Expand Down
4 changes: 2 additions & 2 deletions TPP.Core/InputFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public AnarchyInputFeed(
_fps = fps;
}

public async Task Enqueue(InputSet inputSet, User user, string? channel)
public async Task Enqueue(InputSet inputSet, User user, string? channel, string? channelImageUrl)
{
QueuedInput queuedInput = new(_inputIdSeq++, inputSet);
bool enqueued = _inputBufferQueue.Enqueue(queuedInput);
if (enqueued)
await _overlayConnection.Send(new NewAnarchyInput(queuedInput.InputId, queuedInput.InputSet, user, channel),
await _overlayConnection.Send(new NewAnarchyInput(queuedInput.InputId, queuedInput.InputSet, user, channel, channelImageUrl),
CancellationToken.None);
}

Expand Down
11 changes: 9 additions & 2 deletions TPP.Core/Modes/Runmode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public sealed class Runmode : IWithLifecycle
private readonly IRunCounterRepo _runCounterRepo;
private readonly IInputLogRepo _inputLogRepo;
private readonly IInputSidePicksRepo _inputSidePicksRepo;
private readonly ICoStreamChannelsRepo _coStreamChannelsRepo;
private IInputParser _inputParser;
private readonly InputServer _inputServer;
private readonly WebsocketBroadcastServer _broadcastServer;
Expand All @@ -48,6 +49,7 @@ public Runmode(ILoggerFactory loggerFactory, BaseConfig baseConfig,
_runCounterRepo = repos.RunCounterRepo;
_inputLogRepo = repos.InputLogRepo;
_inputSidePicksRepo = repos.InputSidePicksRepo;
_coStreamChannelsRepo = repos.CoStreamChannelsRepo;
(_broadcastServer, _overlayConnection) = Setups.SetUpOverlayServer(loggerFactory,
baseConfig.OverlayWebsocketHost, baseConfig.OverlayWebsocketPort);
_modeBase = new ModeBase(
Expand Down Expand Up @@ -180,9 +182,14 @@ private async Task<bool> ProcessMessage(IChat chat, Message message)
await ProcessPotentialSidedInputs(chat, message, input);
foreach (InputSet inputSet in input.InputSets)
if (message.MessageSource is MessageSource.SecondaryChat secondaryChat)
await _anarchyInputFeed.Enqueue(inputSet, message.User, secondaryChat.ChannelName);
{
string? channelImageUrl = await _coStreamChannelsRepo.GetChannelImageUrl(secondaryChat.ChannelName);
await _anarchyInputFeed.Enqueue(inputSet, message.User, secondaryChat.ChannelName, channelImageUrl);
}
else
await _anarchyInputFeed.Enqueue(inputSet, message.User, null);
{
await _anarchyInputFeed.Enqueue(inputSet, message.User, null, null);
}
if (!_muteInputsToken.Muted)
await CollectRunStatistics(message.User, input, rawInput: potentialInput);
return true;
Expand Down
4 changes: 3 additions & 1 deletion TPP.Core/Overlay/Events/RunInputEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ public sealed class NewAnarchyInput : IOverlayEvent
[DataMember(Name = "side", EmitDefaultValue = false)] public string? Side { get; set; }
[DataMember(Name = "direct", EmitDefaultValue = false)] public bool? Direct { get; set; }
[DataMember(Name = "channel")] public string? Channel { get; set; }
[DataMember(Name = "channel_image_url")] public string? ChannelImageUrl { get; set; }

public NewAnarchyInput(int inputId, InputSet inputSet, User user, string? channel)
public NewAnarchyInput(int inputId, InputSet inputSet, User user, string? channel, string? channelImageUrl)
{
IEnumerable<Input> InputsForOverlay() => inputSet.Inputs.Where(i => i is not SideInput);
ButtonSet = InputsForOverlay().Select(i => i.ButtonName).ToImmutableList();
Expand All @@ -36,6 +37,7 @@ public NewAnarchyInput(int inputId, InputSet inputSet, User user, string? channe
Side = (sideInput?.Side)?.GetSideString();
Direct = sideInput?.Direct;
Channel = channel;
ChannelImageUrl = channelImageUrl;
}
}

Expand Down
25 changes: 18 additions & 7 deletions TPP.Persistence.MongoDB/Repos/CoStreamChannelsRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,40 @@ public CoStreamChannelsRepo(IMongoDatabase database)
}

public Task<bool> IsJoined(string channelName) =>
Collection.Find(doc => doc["_id"] == channelName).AnyAsync();
Collection.Find(doc => doc["_id"] == channelName.ToLower()).AnyAsync();

public async Task<string?> GetChannelImageUrl(string channelName)
{
BsonDocument? doc = await Collection.Find(doc => doc["_id"] == channelName.ToLower()).FirstOrDefaultAsync();
return doc?["profile_image_url"].AsString;
}

public async Task<IImmutableSet<string>> GetJoinedChannels() =>
(await Collection.Find(FilterDefinition<BsonDocument>.Empty).ToListAsync())
.Select(doc => doc["_id"].AsString).ToImmutableHashSet();

public async Task Add(string channelName)
public async Task Add(string channelName, string? profileImageUrl)
{
await Collection.InsertOneAsync(new BsonDocument { ["_id"] = channelName });
await LogJoin(channelName);
await Collection.InsertOneAsync(new BsonDocument
{
["_id"] = channelName.ToLower(),
["profile_image_url"] = profileImageUrl
});
await LogJoin(channelName.ToLower(), profileImageUrl);
}

public async Task Remove(string channelName)
{
await Collection.DeleteOneAsync(doc => doc["_id"] == channelName);
await LogLeave(channelName);
await Collection.DeleteOneAsync(doc => doc["_id"] == channelName.ToLower());
await LogLeave(channelName.ToLower());
}

private Task LogJoin(string channelName) =>
private Task LogJoin(string channelName, string? profileImageUrl) =>
LogCollection.InsertOneAsync(new BsonDocument
{
["_id"] = new ObjectId(),
["channel"] = channelName,
["profile_image_url"] = profileImageUrl,
["type"] = "join",
["timestamp"] = SystemClock.Instance.GetCurrentInstant().ToDateTimeUtc()
});
Expand Down
3 changes: 2 additions & 1 deletion TPP.Persistence/ICoStreamChannelsRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ namespace TPP.Persistence;
public interface ICoStreamChannelsRepo
{
public Task<bool> IsJoined(string channelName);
public Task<string?> GetChannelImageUrl(string channelName);
public Task<IImmutableSet<string>> GetJoinedChannels();
public Task Add(string channelName);
public Task Add(string channelName, string? profileImageUrl);
public Task Remove(string channelName);
}

0 comments on commit 06f278d

Please sign in to comment.