Skip to content

Commit b2129e3

Browse files
committed
Add helper for normalizing timestamps
1 parent ec8839c commit b2129e3

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

lib/stream-chat/channel.rb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -235,17 +235,7 @@ def update_member_partial(user_id, set: nil, unset: nil)
235235
sig { params(user_ids: T::Array[String], options: T.untyped).returns(StreamChat::StreamResponse) }
236236
def add_members(user_ids, **options)
237237
payload = options.dup
238-
239-
# Convert hide_history_before timestamp to RFC 3339 format if it's a DateTime or Time object
240-
if payload.key?(:hide_history_before)
241-
hide_history_before = payload[:hide_history_before]
242-
if hide_history_before.is_a?(DateTime)
243-
payload[:hide_history_before] = hide_history_before.rfc3339
244-
elsif hide_history_before.is_a?(Time)
245-
payload[:hide_history_before] = hide_history_before.iso8601
246-
end
247-
end
248-
238+
payload[:hide_history_before] = StreamChat.normalize_timestamp(payload[:hide_history_before]) if payload[:hide_history_before]
249239
payload = payload.merge({ add_members: user_ids })
250240
update(nil, nil, **payload)
251241
end

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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,13 @@ 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+
def self.normalize_timestamp(t)
20+
case t
21+
when DateTime then t.rfc3339
22+
when Time then t.iso8601
23+
else t
24+
end
25+
end
1726
end

0 commit comments

Comments
 (0)