Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #79 from noobot/file-to-files-fix
Browse files Browse the repository at this point in the history
Update to resolve File attachment issues since slack changes their API
  • Loading branch information
Workshop2 authored Jul 29, 2018
2 parents bef492c + b94d043 commit 6517f77
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ public ChatMessage()
public string User { get; set; }
public string Text { get; set; }
public string Team { get; set; }
public File File { get; set; }
public File[] Files { get; set; }

[JsonProperty("ts")]
public double Timestamp { get; set; }

}
}
14 changes: 13 additions & 1 deletion src/SlackConnector/Extensions/FileExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using SlackConnector.Connections.Sockets.Messages.Inbound;
using SlackConnector.Models;

namespace SlackConnector.Extensions
{
internal static class FileExtensions
{
public static SlackFile ToSlackFile(this File file)
public static IEnumerable<SlackFile> ToSlackFiles(this IEnumerable<File> file)
{
if (file == null)
{
return Enumerable.Empty<SlackFile>();
}

return file.Select(ToSlackFile);
}

private static SlackFile ToSlackFile(this File file)
{
if (file == null)
return null;
Expand Down
6 changes: 4 additions & 2 deletions src/SlackConnector/Models/SlackMessage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace SlackConnector.Models
using System.Collections.Generic;

namespace SlackConnector.Models
{
public class SlackMessage
{
Expand All @@ -9,6 +11,6 @@ public class SlackMessage
public SlackUser User { get; set; }
public double Timestamp { get; set; }
public SlackMessageSubType MessageSubType { get; set; }
public SlackFile File { get; set; }
public IEnumerable<SlackFile> Files { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/SlackConnector/SlackConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private Task HandleMessage(ChatMessage inboundMessage)
RawData = inboundMessage.RawData,
MentionsBot = _mentionDetector.WasBotMentioned(Self.Name, Self.Id, inboundMessage.Text),
MessageSubType = inboundMessage.MessageSubType.ToSlackMessageSubType(),
File = inboundMessage.File.ToSlackFile()
Files = inboundMessage.Files.ToSlackFiles()
};

return RaiseMessageReceived(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ private void should_return_message_with_file(MessageInterpreter interpreter)
string json = @"
{
'type': 'message',
'file':{
'files': [
{
'id':'some-id',
'created':12345,
'timestamp':54321,
Expand Down Expand Up @@ -86,7 +87,7 @@ private void should_return_message_with_file(MessageInterpreter interpreter)
'deanimate_gif':'https:\/\/deanimate_gif',
'permalink':'https:\/\/permalink',
'permalink_public':'https:\/\/permalink_public'
}
}]
}
";

Expand All @@ -97,41 +98,44 @@ private void should_return_message_with_file(MessageInterpreter interpreter)
var expected = new ChatMessage
{
MessageType = MessageType.Message,
File = new File
Files = new[]
{
Id = "some-id",
Created = 12345,
Timestamp = 54321,
Name = "name.gif",
Title = "title.gif",
Mimetype = "image/gif",
FileType = "gif",
PrettyType = "GIF",
User = "some-user",
Editable = true,
Size = 63689,
Mode = "hosted",
IsExternal = true,
ExternalType = "some-external-type",
IsPublic = true,
PublicUrlShared = true,
DisplayAsBot = true,
Username = "some-username",
UrlPrivate = "https://url_private",
UrlPrivateDownload = "https://url_private_download",
Thumb64 = "https://thumb_64",
Thumb80 = "https://thumb_80",
Thumb360 = "https://thumb_360",
Thumb360Width = 43,
Thumb360Height = 29,
Thumb160 = "https://thumb_160",
Thumb360Gif = "https://thumb_360_gif",
ImageExifRotation = 6,
OriginalWidth = 53,
OriginalHeight = 39,
DeanimateGif = "https://deanimate_gif",
Permalink = "https://permalink",
PermalinkPublic = "https://permalink_public"
new File
{
Id = "some-id",
Created = 12345,
Timestamp = 54321,
Name = "name.gif",
Title = "title.gif",
Mimetype = "image/gif",
FileType = "gif",
PrettyType = "GIF",
User = "some-user",
Editable = true,
Size = 63689,
Mode = "hosted",
IsExternal = true,
ExternalType = "some-external-type",
IsPublic = true,
PublicUrlShared = true,
DisplayAsBot = true,
Username = "some-username",
UrlPrivate = "https://url_private",
UrlPrivateDownload = "https://url_private_download",
Thumb64 = "https://thumb_64",
Thumb80 = "https://thumb_80",
Thumb360 = "https://thumb_360",
Thumb360Width = 43,
Thumb360Height = 29,
Thumb160 = "https://thumb_160",
Thumb360Gif = "https://thumb_360_gif",
ImageExifRotation = 6,
OriginalWidth = 53,
OriginalHeight = 39,
DeanimateGif = "https://deanimate_gif",
Permalink = "https://permalink",
PermalinkPublic = "https://permalink_public"
}
},
RawData = json
};
Expand Down
30 changes: 23 additions & 7 deletions tests/SlackConnector.Tests.Unit/Extensions/FileExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AutoFixture;
using System.Linq;
using AutoFixture;
using Shouldly;
using SlackConnector.Connections.Sockets.Messages.Inbound;
using SlackConnector.Extensions;
Expand All @@ -8,17 +9,30 @@ namespace SlackConnector.Tests.Unit.Extensions
{
public class FileExtensionsTests
{
[Theory, AutoMoqData]
private void should_return_null()
[Fact]
private void should_return_empty_enumeration()
{
// given

// when
var slackFiles = ((File[])null).ToSlackFiles();

// then
slackFiles.ShouldBeEmpty();
}

[Fact]
private void should_return_null_file_if_entry_is_null()
{
// given
File file = null;
var files = new File[] { null };

// when
var slackFile = file.ToSlackFile();
var slackFiles = files.ToSlackFiles();

// then
slackFile.ShouldBeNull();
slackFiles.ShouldHaveSingleItem();
slackFiles.First().ShouldBeNull();
}


Expand All @@ -39,8 +53,10 @@ private void should_return_slack_message(Fixture fixture)
.With(f => f.DeanimateGif, $"https://{fixture.Create<string>()}.com/{fixture.Create<string>()}")
.Create();

var files = new[] { file };

// when
var slackFile = file.ToSlackFile();
var slackFile = files.ToSlackFiles().FirstOrDefault();

// then
slackFile.ShouldNotBeNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ private async Task should_raise_event(
Text = "amazing-text",
User = new SlackUser { Id = "userABC", Name = "i-have-a-name" },
RawData = inboundMessage.RawData,
MessageSubType = SlackMessageSubType.ChannelLeave
});
MessageSubType = SlackMessageSubType.ChannelLeave,
Files = Enumerable.Empty<SlackFile>()
});
}

[Theory, AutoMoqData]
Expand Down Expand Up @@ -88,8 +89,9 @@ private async Task should_raise_event_given_user_information_is_missing_from_cac
// then
receivedMessage.ShouldLookLike(new SlackMessage
{
User = new SlackUser { Id = "userABC", Name = string.Empty }
});
User = new SlackUser { Id = "userABC", Name = string.Empty },
Files = Enumerable.Empty<SlackFile>()
});
}

[Theory, AutoMoqData]
Expand Down

0 comments on commit 6517f77

Please sign in to comment.