Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 37 additions & 0 deletions lib/stream-chat/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,43 @@ def query_team_usage_stats(**options)
post('stats/team_usage', data: options)
end

# Gives teams full history (the paid tier of the history window).
#
# Teams without full history keep the app's history window. This endpoint is
# server-side only and only for apps with the history window enabled; other
# apps get a StreamAPIException with code 17 (HTTP 403).
#
# @param teams [Array<String>] 1 to 10 teams, each listed once
# @return [StreamChat::StreamResponse] Response with the marked teams
sig { params(teams: T::Array[String]).returns(StreamChat::StreamResponse) }
def set_history_window_teams(teams)
post('history_window/teams', data: { teams: teams })
end

# Puts teams back on the app's default history window.
#
# A team without full history is ignored. This endpoint is server-side only
# and only for apps with the history window enabled; other apps get a
# StreamAPIException with code 17 (HTTP 403).
#
# @param teams [Array<String>] 1 to 10 teams, each listed once
# @return [StreamChat::StreamResponse] Response with the teams that had full history
sig { params(teams: T::Array[String]).returns(StreamChat::StreamResponse) }
def delete_history_window_teams(teams)
post('history_window/teams/delete', data: { teams: teams })
end

# Returns the app's default history window and the teams with full history.
#
# This endpoint is server-side only and only for apps with the history window
# enabled; other apps get a StreamAPIException with code 17 (HTTP 403).
#
# @return [StreamChat::StreamResponse] Response with default_window_hours and teams
sig { returns(StreamChat::StreamResponse) }
def get_history_window_teams
get('history_window/teams')
end

private

sig { returns(T::Hash[String, String]) }
Expand Down
53 changes: 53 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1666,4 +1666,57 @@ def whole_minute_from_now(seconds)
expect(team['users_daily']).to include 'total'
end
end

describe 'history window teams' do
# Only apps with the history window enabled can call these endpoints;
# every other app gets 403 with code 17. The edge of a region whose control
# plane predates the endpoints answers 404 with code 16 instead, which says
# nothing about the SDK, so both cases skip there.
def history_window_state
@client.get_history_window_teams
:enabled
rescue StreamChat::StreamAPIException => e
return :disabled if e.error_code == 17
return :unrouted if e.error_code == 16

raise
end

def skip_unless_history_window(expected)
state = history_window_state
skip "history window endpoints are not routed in this app's region yet" if state == :unrouted
skip "the app's history window state is #{state}" unless state == expected
end

it 'refuses every call with code 17 on an app without the history window' do
skip_unless_history_window(:disabled)

team = SecureRandom.uuid
[
-> { @client.set_history_window_teams([team]) },
-> { @client.delete_history_window_teams([team]) },
-> { @client.get_history_window_teams }
].each do |call|
expect(&call).to raise_error(StreamChat::StreamAPIException) do |error|
expect(error.error_code).to eq 17
expect(error.error_message).to include 'history window teams'
end
end
end

it 'marks, lists and unmarks teams on an app with the history window' do
skip_unless_history_window(:enabled)

team = SecureRandom.uuid
marked = @client.set_history_window_teams([team])
expect(marked['teams'].map { |t| t['team'] }).to eq [team]

listed = @client.get_history_window_teams
expect(listed).to include 'default_window_hours'
expect(listed['teams'].map { |t| t['team'] }).to include team

unmarked = @client.delete_history_window_teams([team])
expect(unmarked['teams']).to eq [team]
end
end
end
Loading