ChatChannelController
ChatChannelController
is a controller class which allows mutating and observing changes of a specific chat channel.
public class ChatChannelController: DataController, DelegateCallable, DataStoreProvider
ChatChannelController
objects are lightweight, and they can be used for both, continuous data change observations (like
getting new messages in the channel), and for quick channel mutations (like adding a member to a channel).
Inheritance
DataController
, DelegateCallable
, DataStoreProvider
Properties
statePublisher
A publisher emitting a new value every time the state of the controller changes.
public var statePublisher: AnyPublisher<DataController.State, Never>
channelChangePublisher
A publisher emitting a new value every time the channel changes.
public var channelChangePublisher: AnyPublisher<EntityChange<ChatChannel>, Never>
messagesChangesPublisher
A publisher emitting a new value every time the list of the messages matching the query changes.
public var messagesChangesPublisher: AnyPublisher<[ListChange<ChatMessage>], Never>
memberEventPublisher
A publisher emitting a new value every time member event received.
public var memberEventPublisher: AnyPublisher<MemberEvent, Never>
typingUsersPublisher
A publisher emitting a new value every time typing users change.
public var typingUsersPublisher: AnyPublisher<Set<ChatUser>, Never>
observableObject
A wrapper object that exposes the controller variables in the form of ObservableObject
to be used in SwiftUI.
public var observableObject: ObservableObject
channelQuery
The ChannelQuery this controller observes.
@Atomic public private(set) var channelQuery: ChannelQuery
hasLoadedAllPreviousMessages
A Boolean value that returns whether the previous messages have all been loaded or not.
public private(set) var hasLoadedAllPreviousMessages: Bool = false
cid
The identifier of a channel this controller observes.
Will be nil
when we want to create direct message channel and id
is not yet generated by backend.
public var cid: ChannelId?
client
The ChatClient
instance this controller belongs to.
public let client: ChatClient
channel
The channel the controller represents.
public var channel: ChatChannel?
To observe changes of the channel, set your class as a delegate of this controller or use the provided
Combine
publishers.
messages
The messages of the channel the controller represents.
public var messages: LazyCachedMapCollection<ChatMessage>
To observe changes of the messages, set your class as a delegate of this controller or use the provided
Combine
publishers.
messageOrdering
Describes the ordering the messages are presented.
public let messageOrdering: MessageOrdering
areTypingEventsEnabled
true
if the channel has typing events enabled. Defaults to false
if the channel doesn't exist yet.
var areTypingEventsEnabled: Bool
areReactionsEnabled
true
if the channel has reactions enabled. Defaults to false
if the channel doesn't exist yet.
var areReactionsEnabled: Bool
areRepliesEnabled
true
if the channel has replies enabled. Defaults to false
if the channel doesn't exist yet.
var areRepliesEnabled: Bool
areReadEventsEnabled
true
if the channel has read events enabled. Defaults to false
if the channel doesn't exist yet.
var areReadEventsEnabled: Bool
areUploadsEnabled
true
if the channel supports uploading files/images. Defaults to false
if the channel doesn't exist yet.
var areUploadsEnabled: Bool
delegate
Set the delegate of ChannelController
to observe the changes in the system.
var delegate: ChatChannelControllerDelegate?
Methods
synchronize(_:)
override public func synchronize(_ completion: ((_ error: Error?) -> Void)? = nil)
setDelegate(_:)
Sets the provided object as a delegate of this controller.
public func setDelegate<Delegate: ChatChannelControllerDelegate>(_ delegate: Delegate)
Parameters
delegate
: The object used as a delegate. It's referenced weakly, so you need to keep the object alive if you want keep receiving updates.
updateChannel(name:imageURL:team:members:invites:extraData:completion:)
Updated channel with new data.
func updateChannel(
name: String?,
imageURL: URL?,
team: String?,
members: Set<UserId> = [],
invites: Set<UserId> = [],
extraData: [String: RawJSON] = [:],
completion: ((Error?) -> Void)? = nil
)
Parameters
team
: New team.members
: New members.invites
: New invites.extraData
: NewExtraData
.completion
: The completion. Will be called on acallbackQueue
when the network request is finished. If request fails, the completion will be called with an error.
muteChannel(completion:)
Mutes the channel this controller manages.
func muteChannel(completion: ((Error?) -> Void)? = nil)
Parameters
completion
: The completion. Will be called on acallbackQueue
when the network request is finished. If request fails, the completion will be called with an error.
unmuteChannel(completion:)
Un-mutes the channel this controller manages.
func unmuteChannel(completion: ((Error?) -> Void)? = nil)
Parameters
completion
: The completion. Will be called on acallbackQueue
when the network request is finished. If request fails, the completion will be called with an error.
deleteChannel(completion:)
Delete the channel this controller manages.
func deleteChannel(completion: ((Error?) -> Void)? = nil)
Parameters
completion
: The completion. Will be called on acallbackQueue
when the network request is finished. If request fails, the completion will be called with an error.
truncateChannel(completion:)
Truncates the channel this controller manages.
func truncateChannel(completion: ((Error?) -> Void)? = nil)
Removes all of the messages of the channel but doesn't affect the channel data or members.
Parameters
completion
: The completion. Will be called on acallbackQueue
when the network request is finished. If request fails, the completion will be called with an error.
hideChannel(clearHistory:completion:)
Hide the channel this controller manages from queryChannels
for the user until a message is added.
func hideChannel(clearHistory: Bool = false, completion: ((Error?) -> Void)? = nil)
Parameters
clearHistory
: Flag to remove channel history (false by default)completion
: The completion. Will be called on acallbackQueue
when the network request is finished. If request fails, the completion will be called with an error.
showChannel(completion:)
Removes hidden status for the channel this controller manages.
func showChannel(completion: ((Error?) -> Void)? = nil)
Parameters
completion
: The completion. Will be called on acallbackQueue
when the network request is finished. If request fails, the completion will be called with an error.
loadPreviousMessages(before:limit:completion:)
Loads previous messages from backend.
func loadPreviousMessages(
before messageId: MessageId? = nil,
limit: Int = 25,
completion: ((Error?) -> Void)? = nil
)
Parameters
messageId
: ID of the last fetched message. You will get messagesolder
than the provided ID.limit
: Limit for page size.completion
: The completion. Will be called on acallbackQueue
when the network request is finished. If request fails, the completion will be called with an error.
loadNextMessages(after:limit:completion:)
Loads next messages from backend.
func loadNextMessages(
after messageId: MessageId? = nil,
limit: Int = 25,
completion: ((Error?) -> Void)? = nil
)
Parameters
messageId
: ID of the current first message. You will get messagesnewer
than the provided ID.limit
: Limit for page size.completion
: The completion. Will be called on acallbackQueue
when the network request is finished. If request fails, the completion will be called with an error.
sendKeystrokeEvent(completion:)
Sends the start typing event and schedule a timer to send the stop typing event.
func sendKeystrokeEvent(completion: ((Error?) -> Void)? = nil)
This method is meant to be called every time the user presses a key. The method will manage requests and timer as needed.
Parameters
completion
: a completion block with an error if the request was failed.
sendStartTypingEvent(completion:)
Sends the start typing event.
func sendStartTypingEvent(completion: ((Error?) -> Void)? = nil)
For the majority of cases, you don't need to call sendStartTypingEvent
directly. Instead, use sendKeystrokeEvent
method and call it every time the user presses a key. The controller will manage
sendStartTypingEvent
/sendStopTypingEvent
calls automatically.
Parameters
completion
: a completion block with an error if the request was failed.
sendStopTypingEvent(completion:)
Sends the stop typing event.
func sendStopTypingEvent(completion: ((Error?) -> Void)? = nil)
For the majority of cases, you don't need to call sendStopTypingEvent
directly. Instead, use sendKeystrokeEvent
method and call it every time the user presses a key. The controller will manage
sendStartTypingEvent
/sendStopTypingEvent
calls automatically.
Parameters
completion
: a completion block with an error if the request was failed.
createNewMessage(text:pinning:isSilent:attachments:mentionedUserIds:quotedMessageId:extraData:completion:)
Creates a new message locally and schedules it for send.
func createNewMessage(
text: String,
pinning: MessagePinning? = nil,
// command: String? = nil,
// arguments: String? = nil,
isSilent: Bool = false,
attachments: [AnyAttachmentPayload] = [],
mentionedUserIds: [UserId] = [],
quotedMessageId: MessageId? = nil,
extraData: [String: RawJSON] = [:],
completion: ((Result<MessageId, Error>) -> Void)? = nil
)
Parameters
text
: Text of the message.pinning
: Pins the new message.nil
if should not be pinned.isSilent
: A flag indicating whether the message is a silent message. Silent messages are special messages that don't increase the unread messages count nor mark a channel as unread.attachments
: An array of the attachments for the message.Note
: can be built-in types, custom attachment types conforming toAttachmentEnvelope
protocol andChatMessageAttachmentSeed
s.quotedMessageId
: An id of the message new message quotes. (inline reply)extraData
: Additional extra data of the message object.completion
: Called when saving the message to the local DB finishes.
addMembers(userIds:completion:)
Add users to the channel as members.
func addMembers(userIds: Set<UserId>, completion: ((Error?) -> Void)? = nil)
Parameters
users
: Users Id to add to a channel.completion
: The completion. Will be called on acallbackQueue
when the network request is finished. If request fails, the completion will be called with an error.
removeMembers(userIds:completion:)
Remove users to the channel as members.
func removeMembers(userIds: Set<UserId>, completion: ((Error?) -> Void)? = nil)
Parameters
users
: Users Id to add to a channel.completion
: The completion. Will be called on acallbackQueue
when the network request is finished. If request fails, the completion will be called with an error.
inviteMembers(userIds:completion:)
Invite members to a channel. They can then accept or decline the invitation
func inviteMembers(userIds: Set<UserId>, completion: ((Error?) -> Void)? = nil)
Parameters
userIds
: Set of ids of users to be invited to the channelcompletion
: Called when the API call is finished. Called withError
if the remote update fails.
acceptInvite(message:completion:)
Accept Request
func acceptInvite(message: String? = nil, completion: ((Error?) -> Void)? = nil)
Parameters
cid
: The channel identifier.userId
:userId
message
: messagecompletion
: Called when the API call is finished. Called withError
if the remote update fails.
rejectInvite(completion:)
Reject Request
func rejectInvite(completion: ((Error?) -> Void)? = nil)
Parameters
cid
: The channel identifier.completion
: Called when the API call is finished. Called withError
if the remote update fails.
markRead(completion:)
Marks the channel as read.
func markRead(completion: ((Error?) -> Void)? = nil)
Parameters
completion
: The completion. Will be called on acallbackQueue
when the network request is finished. If request fails, the completion will be called with an error.
enableSlowMode(cooldownDuration:completion:)
Enables slow mode for the channel
func enableSlowMode(cooldownDuration: Int, completion: ((Error?) -> Void)? = nil)
When slow mode is enabled, users can only send a message every cooldownDuration
time interval.
cooldownDuration
is specified in seconds, and should be between 1-120.
For more information, please check documentation.
Parameters
cooldownDuration
: Duration of the time interval users have to wait between messages. Specified in seconds. Should be between 1-120.completion
: Called when the API call is finished. Called withError
if the remote update fails.
disableSlowMode(completion:)
Disables slow mode for the channel
func disableSlowMode(completion: ((Error?) -> Void)? = nil)
For more information, please check documentation.
Parameters
completion
: Called when the API call is finished. Called withError
if the remote update fails.
freezeChannel(completion:)
Freezes the channel.
func freezeChannel(completion: ((Error?) -> Void)? = nil)
Freezing a channel will disallow sending new messages and sending / deleting reactions. For more information, see https://getstream.io/chat/docs/ios-swift/freezing\_channels/?language=swift
Parameters
completion
: The completion. Will be called on acallbackQueue
when the network request is finished. If request fails, the completion will be called with an error.
unfreezeChannel(completion:)
Unfreezes the channel.
func unfreezeChannel(completion: ((Error?) -> Void)? = nil)
Freezing a channel will disallow sending new messages and sending / deleting reactions. For more information, see https://getstream.io/chat/docs/ios-swift/freezing\_channels/?language=swift
Parameters
completion
: The completion. Will be called on acallbackQueue
when the network request is finished. If request fails, the completion will be called with an error.
eventsController()
Creates a new ChannelEventsController
that can be used to listen to system events
and for sending custom events into a channel the current controller manages.
func eventsController() -> ChannelEventsController
Returns
A new instance of ChannelEventsController
.