This repository has been archived by the owner on Jun 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 438
[NEW] Send messages via HTTP + WebSockets fallback #1053
Merged
+168
−27
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
90985a0
Send messages via HTTP + WebSockets fallback
cardoso c596d24
Fix code and style mistakes
cardoso e2053ba
test sendMessage
cardoso c25b7b2
Add id parameter to sendMessage
cardoso 9c18656
TODO: Remove SendMessage Fallback + old methods after Rocket.Chat 1.0
cardoso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// MessagesClient.swift | ||
// Rocket.Chat | ||
// | ||
// Created by Matheus Cardoso on 12/7/17. | ||
// Copyright © 2017 Rocket.Chat. All rights reserved. | ||
// | ||
|
||
import RealmSwift | ||
import SwiftyJSON | ||
|
||
struct MessagesClient: APIClient { | ||
let api: AnyAPIFetcher | ||
|
||
func sendMessage(text: String, subscription: Subscription, id: String = String.random(18), user: User? = AuthManager.currentUser(), realm: Realm? = Realm.shared) { | ||
let message = Message() | ||
message.internalType = "" | ||
message.createdAt = Date.serverDate | ||
message.text = text | ||
message.subscription = subscription | ||
message.user = user | ||
message.identifier = id | ||
message.temporary = true | ||
|
||
try? realm?.write { | ||
realm?.add(message) | ||
} | ||
|
||
func updateMessage(json: JSON) { | ||
DispatchQueue.main.async { | ||
try? realm?.write { | ||
message.temporary = false | ||
message.map(json, realm: realm) | ||
realm?.add(message, update: true) | ||
} | ||
|
||
MessageTextCacheManager.shared.update(for: message, completion: nil) | ||
} | ||
} | ||
|
||
api.fetch(SendMessageRequest(id: id, roomId: subscription.rid, text: text), succeeded: { result in | ||
guard let message = result.raw?["message"] else { return } | ||
updateMessage(json: message) | ||
}, errored: { error in | ||
switch error { | ||
case .version: | ||
// old server version: fallback to web sockets | ||
SubscriptionManager.sendTextMessage(message, completion: { response in | ||
updateMessage(json: response.result["result"]) | ||
}) | ||
default: | ||
break | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// SendMessageRequest.swift | ||
// Rocket.Chat | ||
// | ||
// Created by Matheus Cardoso on 12/7/17. | ||
// Copyright © 2017 Rocket.Chat. All rights reserved. | ||
// | ||
|
||
import SwiftyJSON | ||
|
||
typealias SendMessageResult = APIResult<SendMessageRequest> | ||
|
||
class SendMessageRequest: APIRequest { | ||
let method = "POST" | ||
let path = "/api/v1/chat.sendMessage" | ||
let requiredVersion = Version(0, 60, 0) | ||
|
||
let id: String | ||
let roomId: String | ||
let text: String | ||
|
||
init(id: String, roomId: String, text: String) { | ||
self.id = id | ||
self.roomId = roomId | ||
self.text = text | ||
} | ||
|
||
func body() -> Data? { | ||
let body = JSON([ | ||
"message": [ | ||
"_id": id, | ||
"rid": roomId, | ||
"msg": text | ||
] | ||
]) | ||
|
||
return body.rawString()?.data(using: .utf8) | ||
} | ||
} | ||
|
||
extension APIResult where T == SendMessageRequest { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// MessagesClientSpec.swift | ||
// Rocket.ChatTests | ||
// | ||
// Created by Matheus Cardoso on 12/7/17. | ||
// Copyright © 2017 Rocket.Chat. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import SwiftyJSON | ||
|
||
@testable import Rocket_Chat | ||
|
||
class MessagesClientSpec: XCTestCase, RealmTestCase { | ||
func testSendMessage() { | ||
let api = MockAPI() | ||
let realm = testRealm() | ||
let client = MessagesClient(api: api) | ||
|
||
api.nextResult = JSON([ | ||
"success": true, | ||
"message": [ | ||
"mentions": [], | ||
"_id": "a43SYFpMdjEAdM0mrH", | ||
"_updatedAt": "2017-12-07T12:30:38.384Z", | ||
"channels": [], | ||
"rid": "GENERAL", | ||
"u": [ | ||
"name": "Matheus Cardoso", | ||
"username": "matheus.cardoso", | ||
"_id": "ERoZg2xpgcDnXbCJu" | ||
], | ||
"ts": "2017-12-07T12:30:38.382Z", | ||
"msg": "Test" | ||
] | ||
]) | ||
|
||
let user = User.testInstance() | ||
let subscription = Subscription.testInstance() | ||
|
||
client.sendMessage(text: "test", subscription: subscription, id: "a43SYFpMdjEAdM0mrH", user: user, realm: realm) | ||
|
||
let messages = realm.objects(Message.self) | ||
XCTAssertEqual(messages.count, 1) | ||
XCTAssertEqual(realm.objects(Message.self).first?.temporary, true) | ||
|
||
let expectation = XCTestExpectation(description: "message updated in realm") | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: { | ||
if realm.objects(Message.self).first?.temporary == false { | ||
expectation.fulfill() | ||
} | ||
}) | ||
wait(for: [expectation], timeout: 1.1) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add the TODO here?👌