@@ -7,8 +7,8 @@ import { User } from 'models/User'
7
7
import { accountSelectors } from 'store/account'
8
8
import { cacheUsersSelectors } from 'store/cache'
9
9
import { CommonState } from 'store/reducers'
10
- import { decodeHashId } from 'utils/hashIds'
11
- import { Maybe } from 'utils/typeUtils'
10
+ import { decodeHashId , encodeHashId } from 'utils/hashIds'
11
+ import { Maybe , removeNullable } from 'utils/typeUtils'
12
12
13
13
import { chatMessagesAdapter , chatsAdapter } from './slice'
14
14
import { ChatPermissionAction } from './types'
@@ -168,6 +168,30 @@ export const getSingleOtherChatUser = (
168
168
return getOtherChatUsers ( state , chatId ) [ 0 ]
169
169
}
170
170
171
+ /**
172
+ * Gets a list of the users the current user has chats with.
173
+ * Note that this only takes the first user of each chat that doesn't match the current one,
174
+ * so this will need to be adjusted when we do group chats.
175
+ */
176
+ export const getUserList = createSelector (
177
+ [ getUserId , getChats , getHasMoreChats , getChatsStatus ] ,
178
+ ( currentUserId , chats , hasMore , chatsStatus ) => {
179
+ const chatUserListIds = chats
180
+ . map (
181
+ ( c ) =>
182
+ c . chat_members
183
+ . filter ( ( u ) => decodeHashId ( u . user_id ) !== currentUserId )
184
+ . map ( ( u ) => decodeHashId ( u . user_id ) ) [ 0 ]
185
+ )
186
+ . filter ( removeNullable )
187
+ return {
188
+ userIds : chatUserListIds ,
189
+ hasMore,
190
+ loading : chatsStatus === Status . LOADING
191
+ }
192
+ }
193
+ )
194
+
171
195
export const getChatMessageByIndex = (
172
196
state : CommonState ,
173
197
chatId : string ,
@@ -228,6 +252,7 @@ export const getCanCreateChat = createSelector(
228
252
getBlockees ,
229
253
getBlockers ,
230
254
getChatPermissions ,
255
+ getChats ,
231
256
( state : CommonState , { userId } : { userId : Maybe < ID > } ) => {
232
257
if ( ! userId ) return null
233
258
const usersMap = getUsers ( state , { ids : [ userId ] } )
@@ -239,6 +264,7 @@ export const getCanCreateChat = createSelector(
239
264
blockees ,
240
265
blockers ,
241
266
chatPermissions ,
267
+ chats ,
242
268
user
243
269
) : { canCreateChat : boolean ; callToAction : ChatPermissionAction } => {
244
270
if ( ! currentUserId ) {
@@ -254,13 +280,24 @@ export const getCanCreateChat = createSelector(
254
280
}
255
281
}
256
282
283
+ // Check for existing chat, since unblocked users with existing chats
284
+ // don't need permission to continue chatting.
285
+ // Use a callback fn to prevent iteration until necessary to improve perf
286
+ // Note: this only works if the respective chat has been fetched already, like in chatsUserList
287
+ const encodedUserId = encodeHashId ( user . user_id )
288
+ const hasExistingChat = ( ) =>
289
+ ! ! chats . find ( ( c ) =>
290
+ c . chat_members . find ( ( u ) => u . user_id === encodedUserId )
291
+ )
292
+
257
293
const userPermissions = chatPermissions [ user . user_id ]
258
294
const isBlockee = blockees . includes ( user . user_id )
259
295
const isBlocker = blockers . includes ( user . user_id )
260
296
const canCreateChat =
261
297
! isBlockee &&
262
298
! isBlocker &&
263
- ( userPermissions ?. current_user_has_permission ?? true )
299
+ ( ( userPermissions ?. current_user_has_permission ?? true ) ||
300
+ hasExistingChat ( ) )
264
301
265
302
let action = ChatPermissionAction . NOT_APPLICABLE
266
303
if ( ! canCreateChat ) {
0 commit comments