Skip to content

Commit

Permalink
Make messageId a required parameter for SharpXmppHelper.message (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Sep 21, 2019
1 parent e7cdc78 commit 02808f5
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Emulsion.Tests/Xmpp/EmulsionXmppTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ type SendTests(outputHelper: ITestOutputHelper) =
let client =
XmppClientFactory.create(
addMessageHandler = (fun _ h -> messageHandlers.Add h),
send = fun m -> messageId.SetResult(SharpXmppHelper.getMessageId m)
send = fun m -> messageId.SetResult(Option.get <| SharpXmppHelper.getMessageId m)
)
let outgoingMessage = { author = "author"; text = "text" }

Expand Down
2 changes: 1 addition & 1 deletion Emulsion.Tests/Xmpp/SharpXmppHelperTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ open Emulsion.Xmpp

[<Fact>]
let ``Message body has a proper namespace``() =
let message = SharpXmppHelper.message None "cthulhu@test" "text"
let message = SharpXmppHelper.message "" "cthulhu@test" "text"
let body = Seq.exactlyOne(message.Descendants())
Assert.Equal(XNamespace.Get "jabber:client", body.Name.Namespace)

Expand Down
2 changes: 1 addition & 1 deletion Emulsion.Tests/Xmpp/XmppClientTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ let ``sendRoomMessage's result doesn't get resolved after receiving other messag
let deliveryTask = Async.StartAsTask deliveryInfo.Delivery
Assert.False deliveryTask.IsCompleted

let otherMessage = SharpXmppHelper.message (Some "xxx") "nickname@example.org" "foo bar"
let otherMessage = SharpXmppHelper.message "xxx" "nickname@example.org" "foo bar"
messageHandler otherMessage
Assert.False deliveryTask.IsCompleted
)
Expand Down
5 changes: 2 additions & 3 deletions Emulsion/Xmpp/SharpXmppHelper.fs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ let joinRoom (client: XmppClient) (roomJid: string) (nickname: string): unit =
let room = bookmark roomJid nickname
client.BookmarkManager.Join(room)

let message (id: string option) (toAddr: string) (text: string): XMPPMessage =
// TODO[F]: Make id a mandatory parameter?
let message (id: string) (toAddr: string) (text: string): XMPPMessage =
let m = XMPPMessage()
id |> Option.iter (fun id -> m.SetAttributeValue(Id, id))
m.SetAttributeValue(Id, id)
m.SetAttributeValue(Type, "groupchat")
m.SetAttributeValue(To, toAddr)
let body = XElement(Body)
Expand Down
7 changes: 5 additions & 2 deletions Emulsion/Xmpp/XmppClient.fs
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,14 @@ let private awaitMessageReceival (client: IXmppClient) (lifetime: Lifetime) mess
messageLifetimeDefinition.Dispose()
}

let private newMessageId(): string =
Guid.NewGuid().ToString()

/// Sends the message to the room. Returns an object that allows to track the message receival.
let sendRoomMessage (client: IXmppClient) (lifetime: Lifetime) (messageInfo: MessageInfo): Async<MessageDeliveryInfo> =
async {
let messageId = Guid.NewGuid().ToString() // TODO[F]: Move to a new function
let message = SharpXmppHelper.message (Some messageId) messageInfo.RecipientJid.FullJid messageInfo.Text
let messageId = newMessageId()
let message = SharpXmppHelper.message messageId messageInfo.RecipientJid.FullJid messageInfo.Text
let! delivery = Async.StartChild <| awaitMessageReceival client lifetime messageId
client.Send message
return {
Expand Down

0 comments on commit 02808f5

Please sign in to comment.