This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3394 from withspectrum/2.4.13
2.4.13
- Loading branch information
Showing
21 changed files
with
579 additions
and
473 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
api/queries/directMessageThread/rootDirectMessageThreadByUserId.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// @flow | ||
import type { GraphQLContext } from '../../'; | ||
import { checkForExistingDMThread } from '../../models/directMessageThread'; | ||
import { isAuthedResolver as requireAuth } from '../../utils/permissions'; | ||
|
||
type Args = { | ||
userId: string, | ||
}; | ||
|
||
export default requireAuth(async (_: any, args: Args, ctx: GraphQLContext) => { | ||
// signed out users will never be able to view a dm thread | ||
const { user: currentUser, loaders } = ctx; | ||
const { userId } = args; | ||
|
||
const allMemberIds = [userId, currentUser.id]; | ||
const existingThread = await checkForExistingDMThread(allMemberIds); | ||
|
||
if (!existingThread) return null; | ||
|
||
return loaders.directMessageThread.load(existingThread); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
// @flow | ||
import React from 'react'; | ||
import compose from 'recompose/compose'; | ||
import type { NavigationProps } from 'react-navigation'; | ||
import { withCurrentUser } from '../../components/WithCurrentUser'; | ||
import { throttle, debounce } from 'throttle-debounce'; | ||
import withSafeView from '../../components/SafeAreaView'; | ||
import ChatInput from '../../components/ChatInput'; | ||
import PeopleSearchView from '../Search/PeopleSearchView'; | ||
import getCurrentUserDMThreadConnection from '../../../shared/graphql/queries/directMessageThread/getCurrentUserDMThreadConnection'; | ||
import createDirectMessageThread, { | ||
type CreateDirectMessageThreadType, | ||
type CreateDirectMessageThreadProps, | ||
} from '../../../shared/graphql/mutations/directMessageThread/createDirectMessageThread'; | ||
import type { GetUserType } from '../../../shared/graphql/queries/user/getUser'; | ||
import { | ||
ComposerWrapper, | ||
SearchInputArea, | ||
SelectedUsers, | ||
UserSearchInput, | ||
} from './style'; | ||
import { events, track } from '../../utils/analytics'; | ||
import { FullscreenNullState } from '../../components/NullStates'; | ||
import SelectedUser from './SelectedUser'; | ||
|
||
type Props = { | ||
...$Exact<NavigationProps>, | ||
...$Exact<CreateDirectMessageThreadProps>, | ||
currentUser: GetUserType, | ||
presetUserId?: string, | ||
}; | ||
|
||
type State = { | ||
wipSearchString: ?string, | ||
searchString: ?string, | ||
selectedUsers: string[], | ||
}; | ||
|
||
class DirectMessageComposer extends React.Component<Props, State> { | ||
state = { | ||
wipSearchString: '', | ||
searchString: '', | ||
selectedUsers: [], | ||
}; | ||
|
||
searchInput: ?{ | ||
focus: () => void, | ||
}; | ||
|
||
constructor() { | ||
super(); | ||
this.searchDebounced = debounce(300, this.searchDebounced); | ||
this.searchThrottled = throttle(300, this.searchThrottled); | ||
} | ||
|
||
componentDidMount() { | ||
track(events.DIRECT_MESSAGE_THREAD_COMPOSER_VIEWED); | ||
|
||
const { presetUserId } = this.props; | ||
|
||
if (presetUserId) { | ||
this.setState({ | ||
selectedUsers: [presetUserId], | ||
}); | ||
} | ||
} | ||
|
||
onChangeText = (text: string) => { | ||
this.setState({ | ||
wipSearchString: text, | ||
}); | ||
}; | ||
|
||
onChangeText = (text: string) => { | ||
this.setState({ wipSearchString: text }, () => { | ||
const { wipSearchString } = this.state; | ||
if (wipSearchString && wipSearchString.length < 5) { | ||
this.searchThrottled(wipSearchString); | ||
} else { | ||
this.searchDebounced(wipSearchString); | ||
} | ||
}); | ||
}; | ||
|
||
onFinishTyping = (e: { nativeEvent: { text: string } }) => { | ||
this.search(e.nativeEvent.text); | ||
}; | ||
|
||
searchDebounced = (searchString: ?string) => { | ||
this.setState({ | ||
searchString, | ||
}); | ||
}; | ||
|
||
searchThrottled = (searchString: ?string) => { | ||
this.setState({ | ||
searchString, | ||
}); | ||
}; | ||
|
||
search = (searchString: ?string) => { | ||
this.setState({ | ||
searchString, | ||
}); | ||
}; | ||
|
||
selectUser = (userId: string) => { | ||
this.setState(prev => ({ | ||
selectedUsers: prev.selectedUsers.concat([userId]), | ||
wipSearchString: '', | ||
searchString: '', | ||
})); | ||
this.searchInput && this.searchInput.focus(); | ||
}; | ||
|
||
removeSelectedUser = (userId: string) => () => { | ||
this.setState(prev => ({ | ||
selectedUsers: prev.selectedUsers.filter( | ||
selectedId => selectedId !== userId | ||
), | ||
})); | ||
}; | ||
|
||
onSubmit = (text: string) => { | ||
if (this.state.selectedUsers.length === 0) return; | ||
this.props | ||
.createDirectMessageThread({ | ||
participants: this.state.selectedUsers, | ||
message: { | ||
messageType: 'text', | ||
threadType: 'directMessageThread', | ||
content: { | ||
body: text, | ||
}, | ||
}, | ||
}) | ||
.then((result: CreateDirectMessageThreadType) => { | ||
const { navigation } = this.props; | ||
|
||
return navigation.navigate('DirectMessageThread', { | ||
id: result.data.createDirectMessageThread.id, | ||
}); | ||
}) | ||
.catch(err => { | ||
console.error(err); | ||
}); | ||
}; | ||
|
||
filterResults = (results: Array<Object>) => { | ||
const { currentUser } = this.props; | ||
const { selectedUsers } = this.state; | ||
|
||
return results | ||
.filter(row => row.id !== currentUser.id) | ||
.filter(row => selectedUsers.indexOf(row.id) < 0); | ||
}; | ||
|
||
render() { | ||
return ( | ||
<ComposerWrapper> | ||
<SearchInputArea> | ||
<SelectedUsers | ||
horizontal | ||
empty={this.state.selectedUsers.length === 0} | ||
> | ||
{this.state.selectedUsers.map(userId => ( | ||
<SelectedUser | ||
key={userId} | ||
id={userId} | ||
keyboardShouldPersistTaps={'always'} | ||
onPressHandler={this.removeSelectedUser(userId)} | ||
/> | ||
))} | ||
</SelectedUsers> | ||
<UserSearchInput | ||
onChangeText={this.onChangeText} | ||
onEndEditing={this.onFinishTyping} | ||
onSubmitEditing={this.onFinishTyping} | ||
value={this.state.wipSearchString} | ||
returnKeyType="search" | ||
autoFocus | ||
autoCorrect={false} | ||
autoCapitalize={'none'} | ||
innerRef={elem => (this.searchInput = elem)} | ||
placeholder={'Search for people...'} | ||
/> | ||
</SearchInputArea> | ||
|
||
{!this.state.searchString && ( | ||
<FullscreenNullState title={''} subtitle={''} /> | ||
)} | ||
|
||
{this.state.searchString && ( | ||
<PeopleSearchView | ||
onPress={this.selectUser} | ||
queryString={this.state.searchString} | ||
keyboardShouldPersistTaps={'always'} | ||
filter={results => this.filterResults(results)} | ||
/> | ||
)} | ||
|
||
<ChatInput | ||
onSubmit={this.onSubmit} | ||
disableSubmit={this.state.selectedUsers.length === 0} | ||
/> | ||
</ComposerWrapper> | ||
); | ||
} | ||
} | ||
|
||
export default compose( | ||
withCurrentUser, | ||
getCurrentUserDMThreadConnection, | ||
createDirectMessageThread, | ||
withSafeView | ||
)(DirectMessageComposer); |
Oops, something went wrong.