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

[NEW] Send messages via HTTP + WebSockets fallback #1053

Merged
merged 5 commits into from
Dec 7, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Rocket.Chat.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@
77C2612E1F97453600724A1F /* TextFieldSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77C2612B1F97453600724A1F /* TextFieldSpec.swift */; };
77CCB6BE1F8D0597004BBF67 /* DictionaryExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77CCB6BD1F8D0597004BBF67 /* DictionaryExtensions.swift */; };
7DE1B07114BBD1D46E9CC71B /* Pods_Rocket_Chat.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0F5191CD6C53AEA006C82524 /* Pods_Rocket_Chat.framework */; };
80054CF11FD9505A00F5ECF9 /* SendMessageRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 80054CF01FD9505A00F5ECF9 /* SendMessageRequest.swift */; };
80054CF31FD951B100F5ECF9 /* MessagesClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 80054CF21FD951B100F5ECF9 /* MessagesClient.swift */; };
800E22841F8500A200DA84F1 /* MessagesListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 800E22831F8500A200DA84F1 /* MessagesListViewController.swift */; };
800E22861F8507E400DA84F1 /* SubscriptionMessagesRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 800E22851F8507E400DA84F1 /* SubscriptionMessagesRequest.swift */; };
800FCD3E1F72893E00D9A692 /* MembersListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 800FCD3D1F72893E00D9A692 /* MembersListViewController.swift */; };
Expand Down Expand Up @@ -512,6 +514,8 @@
77C2612A1F97453600724A1F /* SelectFieldSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectFieldSpec.swift; sourceTree = "<group>"; };
77C2612B1F97453600724A1F /* TextFieldSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TextFieldSpec.swift; sourceTree = "<group>"; };
77CCB6BD1F8D0597004BBF67 /* DictionaryExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DictionaryExtensions.swift; sourceTree = "<group>"; };
80054CF01FD9505A00F5ECF9 /* SendMessageRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SendMessageRequest.swift; sourceTree = "<group>"; };
80054CF21FD951B100F5ECF9 /* MessagesClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessagesClient.swift; sourceTree = "<group>"; };
800E22831F8500A200DA84F1 /* MessagesListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = MessagesListViewController.swift; path = Controllers/Chat/MessagesListViewController.swift; sourceTree = "<group>"; };
800E22851F8507E400DA84F1 /* SubscriptionMessagesRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubscriptionMessagesRequest.swift; sourceTree = "<group>"; };
800FCD3D1F72893E00D9A692 /* MembersListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = MembersListViewController.swift; path = Controllers/Chat/MembersListViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1307,6 +1311,7 @@
8013F86E1FD6B59D00EE1A4E /* Clients */ = {
isa = PBXGroup;
children = (
80054CF21FD951B100F5ECF9 /* MessagesClient.swift */,
8013F8701FD6B5B000EE1A4E /* CommandsClient.swift */,
8013F86F1FD6B5B000EE1A4E /* InfoClient.swift */,
);
Expand Down Expand Up @@ -1407,6 +1412,7 @@
isa = PBXGroup;
children = (
806C59A11FBB0BD600C32D0A /* PostMessageRequest.swift */,
80054CF01FD9505A00F5ECF9 /* SendMessageRequest.swift */,
);
path = Message;
sourceTree = "<group>";
Expand Down Expand Up @@ -2107,6 +2113,7 @@
80113DF81F98330C0048F2C2 /* OAuthViewController.swift in Sources */,
41BD37E11E290F2900CBC4C2 /* UserModelMapping.swift in Sources */,
412BCC871E55C6B800F7F4EE /* ChatMessageTextView.swift in Sources */,
80054CF31FD951B100F5ECF9 /* MessagesClient.swift in Sources */,
8013F86D1FD6B59A00EE1A4E /* APIClient.swift in Sources */,
414D99161EA0E7CB0020F7E9 /* SignupViewController.swift in Sources */,
800FCD4F1F728EC800D9A692 /* ChannelInfoUserCell.swift in Sources */,
Expand Down Expand Up @@ -2189,6 +2196,7 @@
D32E28251DFD86C300D6019C /* LauncherProtocol.swift in Sources */,
41DF76E31D2C50710028DBF8 /* AppDelegate.swift in Sources */,
D18675EC1F716A0D00406FB4 /* LoginRequest.swift in Sources */,
80054CF11FD9505A00F5ECF9 /* SendMessageRequest.swift in Sources */,
4174CB131D2D99960086DAC8 /* BaseViewController.swift in Sources */,
897083D31F8CF08100233561 /* CheckTableViewCell.swift in Sources */,
597ECBA21E3708A50041C5C5 /* DataExtension.swift in Sources */,
Expand Down
63 changes: 63 additions & 0 deletions Rocket.Chat/API/Clients/MessagesClient.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// 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, user: User? = AuthManager.currentUser(), realm: Realm? = Realm.shared) {
let id = String.random(18)
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"])
Copy link
Contributor

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?👌

})
default:
break
}
})
}

func runCommand(command: String, params: String, roomId: String,
succeeded: RunCommandSucceeded? = nil, errored: APIErrored? = nil) {
api.fetch(RunCommandRequest(command: command, params: params, roomId: roomId),
succeeded: succeeded, errored: errored)
}
}
44 changes: 44 additions & 0 deletions Rocket.Chat/API/Requests/Message/SendMessageRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// 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 {

}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Newline Violation: Files should have a single trailing newline. (trailing_newline)

29 changes: 2 additions & 27 deletions Rocket.Chat/Controllers/Chat/ChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -384,33 +384,8 @@ final class ChatViewController: SLKTextViewController, Alerter {
return
}

var message: Message?
Realm.executeOnMainThread({ (realm) in
message = Message()
message?.internalType = ""
message?.createdAt = Date.serverDate
message?.text = text
message?.subscription = subscription
message?.identifier = String.random(18)
message?.temporary = true
message?.user = AuthManager.currentUser()

if let message = message {
realm.add(message)
}
})

if let message = message {
SubscriptionManager.sendTextMessage(message) { response in
Realm.executeOnMainThread({ (realm) in
message.temporary = false
message.map(response.result["result"], realm: realm)
realm.add(message, update: true)

MessageTextCacheManager.shared.update(for: message, completion: nil)
})
}
}
guard let client = API.current()?.client(MessagesClient.self) else { return }
client.sendMessage(text: text, subscription: subscription)
}

fileprivate func updateCellForMessage(identifier: String) {
Expand Down