Skip to content

Commit

Permalink
Replace retired *History() methods with GetConversationsHistory() (#22)
Browse files Browse the repository at this point in the history
* Add SlackAPI as submodule
* Use locally built SlackAPI instead of the NuGet package to get the latest improvements/bugfixes
* Use GetConversationHistory method to replace retired methods. Use Parallel.Foreach to speed up initial synchronization
* Update appveyor.yml to initialize submodules
  • Loading branch information
gpailler authored Jun 7, 2021
1 parent c742357 commit a1a24d7
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "vendor/SlackAPI"]
path = vendor/SlackAPI
url = https://github.com/Inumedia/SlackAPI.git
6 changes: 6 additions & 0 deletions Luxa4Slack.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Luxa4Slack.OAuth.AzureFunct
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "_build", "build\_build.csproj", "{C8AD61D9-1BAA-4B0E-A980-AC7CBF60C29C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SlackAPI", "vendor\SlackAPI\SlackAPI\SlackAPI.csproj", "{72DA7C19-43C1-4829-97BA-34D85E83290E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -43,6 +45,10 @@ Global
{97C3955F-215E-4E71-942F-7BE2A9749A59}.Debug|Any CPU.Build.0 = Debug|Any CPU
{97C3955F-215E-4E71-942F-7BE2A9749A59}.Release|Any CPU.ActiveCfg = Release|Any CPU
{97C3955F-215E-4E71-942F-7BE2A9749A59}.Release|Any CPU.Build.0 = Release|Any CPU
{72DA7C19-43C1-4829-97BA-34D85E83290E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{72DA7C19-43C1-4829-97BA-34D85E83290E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{72DA7C19-43C1-4829-97BA-34D85E83290E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{72DA7C19-43C1-4829-97BA-34D85E83290E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
9 changes: 8 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
image:
- Visual Studio 2019

clone_script:
- cmd: >-
git clone -q --branch=%APPVEYOR_REPO_BRANCH% https://github.com/%APPVEYOR_REPO_NAME%.git %APPVEYOR_BUILD_FOLDER%
&& cd %APPVEYOR_BUILD_FOLDER%
&& git checkout -qf %APPVEYOR_REPO_COMMIT%
&& git submodule update --init --recursive
build_script:
- ps: .\build.ps1 Pack BuildInstaller

Expand All @@ -9,4 +16,4 @@ artifacts:
name: Artifact

- path: artifacts/Luxa4Slack.Installer-*.exe
name: Installer
name: Installer
10 changes: 6 additions & 4 deletions src/Luxa4Slack/Luxa4Slack.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@
<HintPath>..\..\packages\NLog.4.6.8\lib\net45\NLog.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="SlackAPI, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\packages\SlackAPI.1.2.0-pullrequest0016\lib\net45\SlackAPI.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
Expand Down Expand Up @@ -111,6 +107,12 @@
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\vendor\SlackAPI\SlackAPI\SlackAPI.csproj">
<Project>{72da7c19-43c1-4829-97ba-34d85e83290e}</Project>
<Name>SlackAPI</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
6 changes: 2 additions & 4 deletions src/Luxa4Slack/MessageHandlers/ChannelHandlerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ protected ChannelHandlerBase(SlackSocketClient client, HandlerContext context, I
this.Client.BindCallback<TMessage>(this.OnChannelMarked);

this.Logger.Debug("Fetch initial messages");
foreach (var channel in this.GetChannels())
{
this.UpdateChannelInfo(channel);
}

this.RunParallel(this.GetChannels(), this.UpdateChannelInfo);
}

public override void Dispose()
Expand Down
2 changes: 1 addition & 1 deletion src/Luxa4Slack/MessageHandlers/Channelhandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected override Channel FindChannel(ChannelMarked message)
return this.Client.ChannelLookup[message.channel];
}

protected override GetHistoryHandler HistoryMethod => this.Client.GetChannelHistory;
protected override GetHistoryHandler HistoryMethod => this.Client.GetConversationsHistory;

protected override bool ShouldMonitor(string id)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Luxa4Slack/MessageHandlers/GroupHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected override Channel FindChannel(GroupMarked message)
return this.Client.GroupLookup[message.channel];
}

protected override GetHistoryHandler HistoryMethod => this.Client.GetGroupHistory;
protected override GetHistoryHandler HistoryMethod => this.Client.GetConversationsHistory;

protected override bool ShouldMonitor(string id)
{
Expand Down
51 changes: 38 additions & 13 deletions src/Luxa4Slack/MessageHandlers/ImHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace CG.Luxa4Slack.MessageHandlers
{
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using NLog;
Expand All @@ -13,11 +14,35 @@ public ImHandler(SlackSocketClient client, HandlerContext context)
this.Client.BindCallback<ImMarked>(this.OnImMarked);

this.Logger.Debug("Fetch initial messages");
foreach (var channel in this.Client.DirectMessages.Where(x => this.ShouldMonitor(x.id)))

var allChannels = new List<Channel>();
string cursor = null;
do
{
this.UpdateChannelInfo(channel);
}
using (ManualResetEventSlim waiter = new ManualResetEventSlim())
{
this.Client.GetConversationsList(response =>
{
foreach (var channel in response.channels)
{
if (channel.is_im && this.Client.UserLookup.ContainsKey(channel.user))
{
allChannels.Add(channel);
}
}
cursor = response.response_metadata.next_cursor;
waiter.Set();
},
cursor,
limit: 500,
types: new[] { "mpim", "im" });

waiter.Wait();
}
} while (!string.IsNullOrEmpty(cursor));

this.RunParallel(allChannels, this.UpdateChannelInfo);
}

public override void Dispose()
Expand All @@ -33,10 +58,10 @@ private void OnImMarked(ImMarked message)

if (this.ShouldMonitor(message.channel))
{
var directMessageConversation = this.Client.DirectMessageLookup[message.channel];
var directMessageConversation = new Channel() { id = message.channel };
var channelNotification = this.Context.ChannelsInfo[directMessageConversation.id];

this.Client.GetDirectMessageHistory(
this.Client.GetConversationsHistory(
x =>
{
var messages = x.messages.Where(y => this.FilterMessageByDate(y, message.ts));
Expand All @@ -51,35 +76,35 @@ private void OnImMarked(ImMarked message)
}
}

private void UpdateChannelInfo(DirectMessageConversation im)
private void UpdateChannelInfo(Channel channel)
{
this.Logger.Debug($"Init IM {this.Context.GetNameFromId(im.id)}");
this.Logger.Debug($"Init IM {this.Context.GetNameFromId(channel.id)}");

int unreadCount = 0;
using (ManualResetEventSlim waiter = new ManualResetEventSlim())
{
this.Client.GetDirectMessageHistory(
this.Client.GetConversationsHistory(
x =>
{
unreadCount = x.unread_count_display;
waiter.Set();
},
im, null, null, 1, true);
channel, null, null, 1, true);
waiter.Wait(SlackNotificationAgent.Timeout);
}

if (unreadCount > 0)
{
using (ManualResetEventSlim waiter = new ManualResetEventSlim())
{
this.Client.GetDirectMessageHistory(
this.Client.GetConversationsHistory(
x =>
{
var hasMessage = x.messages.Any(y => this.FilterMessageByDate(y, im.last_read) && this.IsRegularMessage(y));
this.Context.ChannelsInfo[im.id].Update(hasMessage, hasMessage);
var hasMessage = x.messages.Any(y => this.FilterMessageByDate(y, channel.last_read) && this.IsRegularMessage(y));
this.Context.ChannelsInfo[channel.id].Update(hasMessage, hasMessage);
waiter.Set();
},
im, null, null, unreadCount);
channel, null, null, unreadCount);
waiter.Wait(SlackNotificationAgent.Timeout);
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/Luxa4Slack/MessageHandlers/MessageHandlerBase.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace CG.Luxa4Slack.MessageHandlers
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NLog;
using SlackAPI;
using SlackAPI.WebSocketMessages;
Expand All @@ -10,6 +12,8 @@ internal abstract class MessageHandlerBase : IDisposable
{
protected const int HistoryItemsToFetch = 50;

private const int MaxDegreeOfParallelism = 8;

protected readonly SlackSocketClient Client;

protected readonly HandlerContext Context;
Expand Down Expand Up @@ -65,6 +69,15 @@ protected bool FilterMessageByDate(Message message, DateTime minDate)

protected abstract bool ShouldMonitor(string id);

protected void RunParallel<T>(IEnumerable<T> source, Action<T> callback)
{
Parallel.ForEach(
source,
new ParallelOptions { MaxDegreeOfParallelism = MaxDegreeOfParallelism },
callback
);
}

private void OnMessageReceived(NewMessage message)
{
if (message.type == "message")
Expand Down
1 change: 0 additions & 1 deletion src/Luxa4Slack/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<package id="LuxaforSharp" version="2.0.0.0" targetFramework="net452" />
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net452" />
<package id="NLog" version="4.6.8" targetFramework="net452" />
<package id="SlackAPI" version="1.2.0-pullrequest0016" targetFramework="net452" />
<package id="System.ComponentModel.Annotations" version="4.0.0" targetFramework="net452" />
<package id="System.Net.Http" version="4.3.4" targetFramework="net452" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.0" targetFramework="net452" />
Expand Down
1 change: 1 addition & 0 deletions vendor/SlackAPI
Submodule SlackAPI added at 1c5121

0 comments on commit a1a24d7

Please sign in to comment.