Skip to content

Commit ccc2f94

Browse files
authored
feat: add hide_history_before option for adding members (#182)
* feat: add hide_history_before option for adding members * remove trailing whitespace * Add helper for normalizing timestamps * Use hide_history_before in test case * Fix error * Don't use short argument name * Add signature
1 parent f37c2d4 commit ccc2f94

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

lib/stream-chat/channel.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ def update_member_partial(user_id, set: nil, unset: nil)
234234
# Adds members to the channel.
235235
sig { params(user_ids: T::Array[String], options: T.untyped).returns(StreamChat::StreamResponse) }
236236
def add_members(user_ids, **options)
237-
payload = options.merge({ add_members: user_ids })
237+
payload = options.dup
238+
payload[:hide_history_before] = StreamChat.normalize_timestamp(payload[:hide_history_before]) if payload[:hide_history_before]
239+
payload = payload.merge({ add_members: user_ids })
238240
update(nil, nil, **payload)
239241
end
240242

lib/stream-chat/client.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ def delete_channels(cids, hard_delete: false)
738738
# Revoke tokens for an application issued since the given date.
739739
sig { params(before: T.any(DateTime, String)).returns(StreamChat::StreamResponse) }
740740
def revoke_tokens(before)
741-
before = before.rfc3339 if before.instance_of?(DateTime)
741+
before = StreamChat.normalize_timestamp(before)
742742
update_app_settings({ 'revoke_tokens_issued_before' => before })
743743
end
744744

@@ -751,7 +751,7 @@ def revoke_user_token(user_id, before)
751751
# Revoke tokens for users issued since.
752752
sig { params(user_ids: T::Array[String], before: T.any(DateTime, String)).returns(StreamChat::StreamResponse) }
753753
def revoke_users_token(user_ids, before)
754-
before = before.rfc3339 if before.instance_of?(DateTime)
754+
before = StreamChat.normalize_timestamp(before)
755755

756756
updates = []
757757
user_ids.map do |user_id|
@@ -1024,7 +1024,7 @@ def query_threads(filter, sort: nil, **options)
10241024
sig { params(message_id: String, user_id: String, remind_at: T.nilable(DateTime)).returns(StreamChat::StreamResponse) }
10251025
def create_reminder(message_id, user_id, remind_at = nil)
10261026
data = { user_id: user_id }
1027-
data[:remind_at] = remind_at.rfc3339 if remind_at.instance_of?(DateTime)
1027+
data[:remind_at] = StreamChat.normalize_timestamp(remind_at) if remind_at
10281028
post("messages/#{message_id}/reminders", data: data)
10291029
end
10301030

@@ -1036,7 +1036,7 @@ def create_reminder(message_id, user_id, remind_at = nil)
10361036
sig { params(message_id: String, user_id: String, remind_at: T.nilable(DateTime)).returns(StreamChat::StreamResponse) }
10371037
def update_reminder(message_id, user_id, remind_at = nil)
10381038
data = { user_id: user_id }
1039-
data[:remind_at] = remind_at.rfc3339 if remind_at
1039+
data[:remind_at] = StreamChat.normalize_timestamp(remind_at) if remind_at
10401040
patch("messages/#{message_id}/reminders", data: data)
10411041
end
10421042

lib/stream-chat/util.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,14 @@ def self.get_sort_fields(sort)
1414
end
1515
sort_fields
1616
end
17+
18+
# Normalizes a timestamp to RFC 3339 / ISO 8601 string format.
19+
sig { params(timestamp: T.any(DateTime, Time, String)).returns(String) }
20+
def self.normalize_timestamp(timestamp)
21+
case timestamp
22+
when DateTime then timestamp.rfc3339
23+
when Time then timestamp.iso8601
24+
else timestamp
25+
end
26+
end
1727
end

spec/channel_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ def loop_times(times)
145145
response = @channel.remove_members([@random_users[0][:id], @random_users[1][:id]])
146146
expect(response['members'].length).to eq 0
147147

148-
@channel.add_members([@random_users[0][:id]])
148+
history_cutoff = DateTime.now - 1
149+
@channel.add_members([@random_users[0][:id]], hide_history_before: history_cutoff)
149150
response = @channel.add_members([@random_users[1][:id]], hide_history: true)
150151
expect(response['members'].length).to eq 2
151152
response['members']&.each do |m|

0 commit comments

Comments
 (0)