Skip to content

Commit f3ac3de

Browse files
authored
Merge pull request #163 from tinode/next
Topic redirects
2 parents 21d6311 + dd28f09 commit f3ac3de

9 files changed

+71
-29
lines changed

TinodeSDK/ComTopic.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public class ComTopic<DP: Codable & Mergeable>: Topic<DP, PrivateType, DP, Priva
3333
}
3434

3535
public var isChannel: Bool {
36+
return ComTopic.isChannel(name: name)
37+
}
38+
39+
public static func isChannel(name: String) -> Bool {
3640
return name.starts(with: Tinode.kTopicChnPrefix)
3741
}
3842

TinodeSDK/Topic.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,10 @@ open class Topic<DP: Codable & Mergeable, DR: Codable & Mergeable, SP: Codable,
521521
}
522522
return tnd.subscribe(to: name, set: set, get: get).then(
523523
onSuccess: { [weak self] msg in
524+
if let code = msg?.ctrl?.code, code >= 300 {
525+
// 3XX: status unchanged.
526+
return nil
527+
}
524528
let isAttached = self?.attached ?? false
525529
if !isAttached {
526530
self?.attached = true

TinodeSDK/model/ClientMessages.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,17 @@ public class MsgGetMeta: CustomStringConvertible, Encodable {
194194

195195
public var description: String {
196196
//return
197-
let desc_str = desc != nil ? String(describing: desc!) : "null"
198-
let sub_str = sub != nil ? String(describing: sub!) : "null"
199-
let data_str = data != nil ? String(describing: data!) : "null"
200-
let del_str = del != nil ? String(describing: del!) : "null"
197+
let desc_str = desc != nil ? String(describing: desc!) : "nil"
198+
let sub_str = sub != nil ? String(describing: sub!) : "nil"
199+
let data_str = data != nil ? String(describing: data!) : "nil"
200+
let del_str = del != nil ? String(describing: del!) : "nil"
201201
return "[\(self.what)]" +
202202
" desc=[\(desc_str)]," +
203203
" sub=[\(sub_str)]," +
204204
" data=[\(data_str)]," +
205205
" del=[\(del_str)]" +
206-
" tags=[\((set & MsgGetMeta.kTagsSet) != 0 ? "set" : "null")]" +
207-
" cred=[\((set & MsgGetMeta.kCredSet) != 0 ? "set" : "null")]"
206+
" tags=[\((set & MsgGetMeta.kTagsSet) != 0 ? "set" : "nil")]" +
207+
" cred=[\((set & MsgGetMeta.kCredSet) != 0 ? "set" : "nil")]"
208208
}
209209

210210
init() {

TinodeSDK/model/ServerMessages.swift

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,17 @@ public class MsgServerInfo: Decodable {
181181
}
182182

183183
public class ServerMessage: Decodable {
184-
public static let kStatusOk = 200 // RFC 7231, 6.3.1
185-
public static let kStatusResetContent = 205 // RFC 7231, 6.3.6
186-
public static let kStatusMultipleChoices = 300 // RFC 7231, 6.4.1
187-
public static let kStatusBadRequest = 400 // RFC 7231, 6.5.1
188-
public static let kStatusUnauthorized = 401 // RFC 7235, 3.1
189-
public static let kStatusInternalServerError = 500 // RFC 7231, 6.6.1
190-
public static let kStatusServiceUnavailable = 503 // RFC 7231, 6.6.4
191-
public static let kStatusGatewayTimeout = 504 // RFC 7231, 6.6.5
184+
// RFC 7231 HTTP status messages
185+
// https://tools.ietf.org/html/rfc7231#section-6
186+
public static let kStatusOk = 200 // 6.3.1
187+
public static let kStatusResetContent = 205 // 6.3.6
188+
public static let kStatusMultipleChoices = 300 // 6.4.1
189+
public static let kStatusSeeOther = 303 // 6.4.4
190+
public static let kStatusBadRequest = 400 // 6.5.1
191+
public static let kStatusUnauthorized = 401 // 3.1
192+
public static let kStatusInternalServerError = 500 // 6.6.1
193+
public static let kStatusServiceUnavailable = 503 // 6.6.4
194+
public static let kStatusGatewayTimeout = 504 // 6.6.5
192195

193196
public var ctrl: MsgServerCtrl?
194197
public var meta: MsgServerMeta?

Tinodios/MessageInteractor.swift

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class MessageInteractor: DefaultComTopic.Listener, MessageBusinessLogic, Message
8989
self.pagesToLoad = 1
9090

9191
if let pub = self.topic?.pub {
92-
self.presenter?.updateTitleBar(icon: pub.photo?.image(), title: pub.fn, online: self.topic?.online ?? false)
92+
self.presenter?.updateTitleBar(icon: pub.photo?.image(), title: pub.fn, online: (topic?.isChannel ?? false) ? nil : self.topic?.online)
9393
}
9494
self.lastSeenRead = self.topic?.read
9595
self.lastSeenRecv = self.topic?.recv
@@ -133,7 +133,17 @@ class MessageInteractor: DefaultComTopic.Listener, MessageBusinessLogic, Message
133133
builder = builder.withTags()
134134
}
135135
topic.subscribe(set: nil, get: builder.build()).then(
136-
onSuccess: { [weak self] _ in
136+
onSuccess: { [weak self] msg in
137+
// Check for topic redirects.
138+
if let ctrl = msg?.ctrl, ctrl.code == ServerMessage.kStatusSeeOther {
139+
if let redirectTo = ctrl.getStringParam(for: "topic") {
140+
// Redirected to another topic
141+
self?.presenter?.switchTopic(topic: redirectTo)
142+
self?.setup(topicName: redirectTo, sendReadReceipts: interactively)
143+
_ = self?.attachToTopic(interactively: interactively)
144+
}
145+
return nil
146+
}
137147
self?.messageInteractorQueue.async {
138148
self?.topic?.syncAll().then(
139149
onSuccess: { [weak self] _ in
@@ -401,7 +411,9 @@ class MessageInteractor: DefaultComTopic.Listener, MessageBusinessLogic, Message
401411
self.presenter?.applyTopicPermissions(withError: nil)
402412
}
403413
override func onOnline(online: Bool) {
404-
self.presenter?.setOnline(online: online)
414+
if !(self.topic?.isChannel ?? false) {
415+
self.presenter?.setOnline(online: online)
416+
}
405417
}
406418
override func onInfo(info: MsgServerInfo) {
407419
switch info.what {
@@ -437,7 +449,8 @@ class MessageInteractor: DefaultComTopic.Listener, MessageBusinessLogic, Message
437449
override func onMetaDesc(desc: Description<VCard, PrivateType>) {
438450
self.presenter?.applyTopicPermissions(withError: nil)
439451
if let pub = topic?.pub {
440-
self.presenter?.updateTitleBar(icon: pub.photo?.image(), title: pub.fn, online: self.topic?.online ?? false)
452+
let online = (self.topic?.isChannel ?? false) ? nil : self.topic?.online
453+
self.presenter?.updateTitleBar(icon: pub.photo?.image(), title: pub.fn, online: online)
441454
}
442455
}
443456
}

Tinodios/MessagePresenter.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import UIKit
1111
import TinodiosDB
1212

1313
protocol MessagePresentationLogic {
14-
func updateTitleBar(icon: UIImage?, title: String?, online: Bool)
15-
func setOnline(online: Bool)
14+
func switchTopic(topic: String?)
15+
func updateTitleBar(icon: UIImage?, title: String?, online: Bool?)
16+
func setOnline(online: Bool?)
1617
func runTypingAnimation()
1718
func presentMessages(messages: [StoredMessage])
1819
func reloadMessages(fromSeqId loId: Int, toSeqId hiId: Int)
@@ -26,12 +27,18 @@ protocol MessagePresentationLogic {
2627
class MessagePresenter: MessagePresentationLogic {
2728
weak var viewController: MessageDisplayLogic?
2829

29-
func updateTitleBar(icon: UIImage?, title: String?, online: Bool) {
30+
func switchTopic(topic: String?) {
31+
DispatchQueue.main.async {
32+
self.viewController?.switchTopic(topic: topic)
33+
}
34+
}
35+
36+
func updateTitleBar(icon: UIImage?, title: String?, online: Bool?) {
3037
DispatchQueue.main.async {
3138
self.viewController?.updateTitleBar(icon: icon, title: title, online: online)
3239
}
3340
}
34-
func setOnline(online: Bool) {
41+
func setOnline(online: Bool?) {
3542
DispatchQueue.main.async {
3643
self.viewController?.setOnline(online: online)
3744
}

Tinodios/MessageViewController.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import TinodeSDK
1010
import TinodiosDB
1111

1212
protocol MessageDisplayLogic: class {
13-
func updateTitleBar(icon: UIImage?, title: String?, online: Bool)
14-
func setOnline(online: Bool)
13+
func switchTopic(topic: String?)
14+
func updateTitleBar(icon: UIImage?, title: String?, online: Bool?)
15+
func setOnline(online: Bool?)
1516
func runTypingAnimation()
1617
func displayChatMessages(messages: [StoredMessage])
1718
func reloadAllMessages()
@@ -455,7 +456,11 @@ extension MessageViewController: MessageDisplayLogic {
455456
self.present(alert, animated: true)
456457
}
457458

458-
func updateTitleBar(icon: UIImage?, title: String?, online: Bool) {
459+
func switchTopic(topic: String?) {
460+
topicName = topic
461+
}
462+
463+
func updateTitleBar(icon: UIImage?, title: String?, online: Bool?) {
459464
assert(Thread.isMainThread)
460465
self.navigationItem.title = title ?? NSLocalizedString("Undefined", comment: "Undefined chat name")
461466

@@ -470,7 +475,7 @@ extension MessageViewController: MessageDisplayLogic {
470475
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: navBarAvatarView)
471476
}
472477

473-
func setOnline(online: Bool) {
478+
func setOnline(online: Bool?) {
474479
assert(Thread.isMainThread)
475480
navBarAvatarView.setOnline(online: online)
476481
}

Tinodios/widgets/AvatarWithOnlineIndicator.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,17 @@ class AvatarWithOnlineIndicator: UIView {
2828
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
2929
}
3030

31-
public func setOnline(online: Bool) {
31+
/// Three states: true (show green dot), false (gray dot), nil (no dot).
32+
public func setOnline(online: Bool?) {
33+
guard let online = online else {
34+
self.online.isHidden = true
35+
return
36+
}
37+
self.online.isHidden = false
3238
self.online.backgroundColor = online ?
3339
UIColor.init(fromHexCode: 0xFF40C040) : UIColor.init(fromHexCode: 0xFFE0E0E0)
3440
}
35-
public func set(icon: UIImage?, title: String?, id: String?, online: Bool) {
41+
public func set(icon: UIImage?, title: String?, id: String?, online: Bool?) {
3642
self.avatar.set(icon: icon, title: title, id: id)
3743
self.setOnline(online: online)
3844
}

Tinodios/widgets/ChatListViewCell.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ class ChatListViewCell: UITableViewCell {
5151
iconMuted.isHidden = !topic.isMuted
5252

5353
// Avatar image
54-
icon.set(icon: topic.pub?.photo?.image(), title: topic.pub?.fn, id: topic.name, online: topic.online)
54+
icon.set(icon: topic.pub?.photo?.image(), title: topic.pub?.fn, id: topic.name, online: topic.isChannel ? nil : topic.online)
5555
}
5656
}

0 commit comments

Comments
 (0)