Skip to content

Commit 15fbdd6

Browse files
committed
Fix channel capabilities logic when channel is empty (#2754)
* Simplify logic of checking if capability exists in ChatChannel * Fix capability logic when channel does not exist yet
1 parent 1b79e00 commit 15fbdd6

File tree

3 files changed

+61
-32
lines changed

3 files changed

+61
-32
lines changed

Sources/StreamChat/Models/Channel.swift

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -368,146 +368,146 @@ public struct ChannelCapability: RawRepresentable, ExpressibleByStringLiteral, H
368368
public extension ChatChannel {
369369
/// Can the current user ban members from this channel.
370370
var canBanChannelMembers: Bool {
371-
ownCapabilities.contains(.banChannelMembers) == true
371+
ownCapabilities.contains(.banChannelMembers)
372372
}
373373

374374
/// Can the current user receive connect events from this channel.
375375
var canReceiveConnectEvents: Bool {
376-
ownCapabilities.contains(.connectEvents) == true
376+
ownCapabilities.contains(.connectEvents)
377377
}
378378

379379
/// Can the current user delete any message from this channel.
380380
var canDeleteAnyMessage: Bool {
381-
ownCapabilities.contains(.deleteAnyMessage) == true
381+
ownCapabilities.contains(.deleteAnyMessage)
382382
}
383383

384384
/// Can the current user delete the channel.
385385
var canDeleteChannel: Bool {
386-
ownCapabilities.contains(.deleteChannel) == true
386+
ownCapabilities.contains(.deleteChannel)
387387
}
388388

389389
/// Can the current user delete own messages from the channel.
390390
var canDeleteOwnMessage: Bool {
391-
ownCapabilities.contains(.deleteOwnMessage) == true
391+
ownCapabilities.contains(.deleteOwnMessage)
392392
}
393393

394394
/// Can the current user flag a message in this channel.
395395
var canFlagMessage: Bool {
396-
ownCapabilities.contains(.flagMessage) == true
396+
ownCapabilities.contains(.flagMessage)
397397
}
398398

399399
/// Can the current user freeze or unfreeze the channel.
400400
var canFreezeChannel: Bool {
401-
ownCapabilities.contains(.freezeChannel) == true
401+
ownCapabilities.contains(.freezeChannel)
402402
}
403403

404404
/// Can the current user leave the channel (remove own membership).
405405
var canLeaveChannel: Bool {
406-
ownCapabilities.contains(.leaveChannel) == true
406+
ownCapabilities.contains(.leaveChannel)
407407
}
408408

409409
/// Can the current user join the channel (add own membership).
410410
var canJoinChannel: Bool {
411-
ownCapabilities.contains(.joinChannel) == true
411+
ownCapabilities.contains(.joinChannel)
412412
}
413413

414414
/// Can the current user mute the channel.
415415
var canMuteChannel: Bool {
416-
ownCapabilities.contains(.muteChannel) == true
416+
ownCapabilities.contains(.muteChannel)
417417
}
418418

419419
/// Can the current user pin a message in this channel.
420420
var canPinMessage: Bool {
421-
ownCapabilities.contains(.pinMessage) == true
421+
ownCapabilities.contains(.pinMessage)
422422
}
423423

424424
/// Can the current user quote a message in this channel.
425425
var canQuoteMessage: Bool {
426-
ownCapabilities.contains(.quoteMessage) == true
426+
ownCapabilities.contains(.quoteMessage)
427427
}
428428

429429
/// Can the current user receive read events from this channel.
430430
var canReceiveReadEvents: Bool {
431-
ownCapabilities.contains(.readEvents) == true
431+
ownCapabilities.contains(.readEvents)
432432
}
433433

434434
/// Can the current user use message search in this channel.
435435
var canSearchMessages: Bool {
436-
ownCapabilities.contains(.searchMessages) == true
436+
ownCapabilities.contains(.searchMessages)
437437
}
438438

439439
/// Can the current user send custom events in this channel.
440440
var canSendCustomEvents: Bool {
441-
ownCapabilities.contains(.sendCustomEvents) == true
441+
ownCapabilities.contains(.sendCustomEvents)
442442
}
443443

444444
/// Can the current user attach links to messages in this channel.
445445
var canSendLinks: Bool {
446-
ownCapabilities.contains(.sendLinks) == true
446+
ownCapabilities.contains(.sendLinks)
447447
}
448448

449449
/// Can the current user send a message in this channel.
450450
var canSendMessage: Bool {
451-
ownCapabilities.contains(.sendMessage) == true
451+
ownCapabilities.contains(.sendMessage)
452452
}
453453

454454
/// Can the current user send reactions in this channel.
455455
var canSendReaction: Bool {
456-
ownCapabilities.contains(.sendReaction) == true
456+
ownCapabilities.contains(.sendReaction)
457457
}
458458

459459
/// Can the current user thread reply to a message in this channel.
460460
var canSendReply: Bool {
461-
ownCapabilities.contains(.sendReply) == true
461+
ownCapabilities.contains(.sendReply)
462462
}
463463

464464
/// Can the current user enable or disable slow mode in this channel.
465465
var canSetChannelCooldown: Bool {
466-
ownCapabilities.contains(.setChannelCooldown) == true
466+
ownCapabilities.contains(.setChannelCooldown)
467467
}
468468

469469
/// Can the current user send and receive typing events in this channel.
470470
var canSendTypingEvents: Bool {
471-
ownCapabilities.contains(.sendTypingEvents) == true
471+
ownCapabilities.contains(.sendTypingEvents)
472472
}
473473

474474
/// Can the current user update any message in this channel.
475475
var canUpdateAnyMessage: Bool {
476-
ownCapabilities.contains(.updateAnyMessage) == true
476+
ownCapabilities.contains(.updateAnyMessage)
477477
}
478478

479479
/// Can the current user update channel data.
480480
var canUpdateChannel: Bool {
481-
ownCapabilities.contains(.updateChannel) == true
481+
ownCapabilities.contains(.updateChannel)
482482
}
483483

484484
/// Can the current user update channel members.
485485
var canUpdateChannelMembers: Bool {
486-
ownCapabilities.contains(.updateChannelMembers) == true
486+
ownCapabilities.contains(.updateChannelMembers)
487487
}
488488

489489
/// Can the current user update own messages in this channel.
490490
var canUpdateOwnMessage: Bool {
491-
ownCapabilities.contains(.updateOwnMessage) == true
491+
ownCapabilities.contains(.updateOwnMessage)
492492
}
493493

494494
/// Can the current user upload message attachments in this channel.
495495
var canUploadFile: Bool {
496-
ownCapabilities.contains(.uploadFile) == true
496+
ownCapabilities.contains(.uploadFile)
497497
}
498498

499499
/// Can the current user join a call in this channel.
500500
var canJoinCall: Bool {
501-
ownCapabilities.contains(.joinCall) == true
501+
ownCapabilities.contains(.joinCall)
502502
}
503503

504504
/// Can the current user create a call in this channel.
505505
var canCreateCall: Bool {
506-
ownCapabilities.contains(.createCall) == true
506+
ownCapabilities.contains(.createCall)
507507
}
508508

509509
/// Is slow mode active in this channel.
510510
var isSlowMode: Bool {
511-
ownCapabilities.contains(.slowMode) == true
511+
ownCapabilities.contains(.slowMode)
512512
}
513513
}

Sources/StreamChatUI/Composer/ComposerVC.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,17 @@ open class ComposerVC: _ViewController,
289289

290290
/// A Boolean value indicating whether the attachments are enabled.
291291
open var isAttachmentsEnabled: Bool {
292-
channelController?.channel?.canUploadFile == true
292+
channelController?.channel?.canUploadFile ?? true
293293
}
294294

295+
/// A Boolean value indicating whether sending message is enabled.
295296
open var isSendMessageEnabled: Bool {
296-
channelController?.channel?.canSendMessage == true
297+
channelController?.channel?.canSendMessage ?? true
298+
}
299+
300+
/// A Boolean value indicating whether if the message being sent can contain links.
301+
open var canSendLinks: Bool {
302+
channelController?.channel?.canSendLinks ?? true
297303
}
298304

299305
/// A Boolean value indicating whether the current input text contains links.
@@ -593,7 +599,7 @@ open class ComposerVC: _ViewController,
593599
// MARK: - Actions
594600

595601
@objc open func publishMessage(sender: UIButton) {
596-
if channelController?.channel?.canSendLinks == false && inputContainsLinks {
602+
if !canSendLinks && inputContainsLinks {
597603
presentAlert(title: L10n.Composer.LinksDisabled.title, message: L10n.Composer.LinksDisabled.subtitle)
598604
return
599605
}

Tests/StreamChatUITests/SnapshotTests/Composer/ComposerVC_Tests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,13 @@ final class ComposerVC_Tests: XCTestCase {
348348
XCTAssertEqual(composerVC.composerView.attachmentButton.isHidden, true)
349349
}
350350

351+
func test_isAttachmentsEnabled_whenChannelIsEmpty_thenReturnsTrue() {
352+
let mock = ChatChannelController_Mock.mock()
353+
mock.channel_mock = nil
354+
composerVC.channelController = mock
355+
XCTAssertEqual(composerVC.isAttachmentsEnabled, true)
356+
}
357+
351358
func test_canNotSendMessage() {
352359
composerVC.appearance = Appearance.default
353360
composerVC.content = .initial()
@@ -359,6 +366,13 @@ final class ComposerVC_Tests: XCTestCase {
359366
AssertSnapshot(composerVC)
360367
}
361368

369+
func test_isSendMessageEnabled_whenChannelIsEmpty_thenReturnsTrue() {
370+
let mock = ChatChannelController_Mock.mock()
371+
mock.channel_mock = nil
372+
composerVC.channelController = mock
373+
XCTAssertEqual(composerVC.isSendMessageEnabled, true)
374+
}
375+
362376
func test_canNotSendLinks() {
363377
composerVC.appearance = Appearance.default
364378
composerVC.content = .initial()
@@ -375,6 +389,7 @@ final class ComposerVC_Tests: XCTestCase {
375389
composerVC.publishMessage(sender: composerVC.composerView.sendButton)
376390

377391
XCTAssertEqual(mock.createNewMessageCallCount, 1)
392+
XCTAssertEqual(composerVC.canSendLinks, false)
378393
}
379394

380395
func test_canSendLinks() {
@@ -388,6 +403,14 @@ final class ComposerVC_Tests: XCTestCase {
388403
composerVC.publishMessage(sender: composerVC.composerView.sendButton)
389404

390405
XCTAssertEqual(mock.createNewMessageCallCount, 1)
406+
XCTAssertEqual(composerVC.canSendLinks, true)
407+
}
408+
409+
func test_canSendLinks_whenChannelIsEmpty() {
410+
let mock = ChatChannelController_Mock.mock()
411+
mock.channel_mock = nil
412+
composerVC.channelController = mock
413+
XCTAssertEqual(composerVC.canSendLinks, true)
391414
}
392415

393416
// MARK: - audioPlayer

0 commit comments

Comments
 (0)