forked from zulip/zulip-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: Add getSingleMessage binding for GET messages/{message_id}
We'll use this for zulip#5306; see the plan in discussion: https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/.23M5306.20Follow.20.2Fnear.2F.20links.20through.20topic.20moves.2Frenames/near/1407930 In particular, we want the stream and topic for a stream message that might not be in our data structures. We'll use this endpoint to fetch that information. topic edit modal [nfc]: Add TopicModalProvider context component. Contains visibility context and handler callback context. Sets up context for modal handler to be called inside topic action sheets. topic edit modal [nfc]: Provide topic modal Context hook to children. The useTopicModalHandler is called normally in TopicItem and TitleStream. In order to deliver the callbacks to the action sheets in MessageList, the context hook is called in ChatScreen and a bit of prop-drilling is performed. topic edit modal: Add translation for action sheet button. topic edit modal: Add modal and functionality to perform topic name updates. Fixes zulip#5365 topid edit modal [nfc]: Revise Flow types for relevant components. topic edit modal: Modify webview unit tests to accommodate feature update.
- Loading branch information
1 parent
04d2c95
commit 5cc72d4
Showing
15 changed files
with
430 additions
and
26 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
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,55 @@ | ||
/* @flow strict-local */ | ||
|
||
import type { Auth, ApiResponseSuccess } from '../transportTypes'; | ||
import type { Message } from '../apiTypes'; | ||
import { transformFetchedMessage, type FetchedMessage } from '../rawModelTypes'; | ||
import { apiGet } from '../apiFetch'; | ||
import { identityOfAuth } from '../../account/accountMisc'; | ||
|
||
// The actual response from the server. We convert the message to a proper | ||
// Message before returning it to application code. | ||
type ServerApiResponseSingleMessage = {| | ||
...$Exact<ApiResponseSuccess>, | ||
-raw_content: string, // deprecated | ||
|
||
// Until we narrow FetchedMessage into its FL 120+ form, FetchedMessage | ||
// will be a bit less precise than we could be here. That's because we | ||
// only get this field from servers FL 120+. | ||
// TODO(server-5.0): Make this field required, and remove FL-120 comment. | ||
+message?: FetchedMessage, | ||
|}; | ||
|
||
/** | ||
* See https://zulip.com/api/get-message | ||
* | ||
* Gives undefined if the `message` field is missing, which it will be for | ||
* FL <120. | ||
*/ | ||
// TODO(server-5.0): Simplify FL-120 condition in jsdoc and implementation. | ||
export default async ( | ||
auth: Auth, | ||
args: {| | ||
+message_id: number, | ||
|}, | ||
|
||
// TODO(#4659): Don't get this from callers. | ||
zulipFeatureLevel: number, | ||
|
||
// TODO(#4659): Don't get this from callers? | ||
allowEditHistory: boolean, | ||
): Promise<Message | void> => { | ||
const { message_id } = args; | ||
const response: ServerApiResponseSingleMessage = await apiGet(auth, `messages/${message_id}`, { | ||
apply_markdown: true, | ||
}); | ||
|
||
return ( | ||
response.message | ||
&& transformFetchedMessage<Message>( | ||
response.message, | ||
identityOfAuth(auth), | ||
zulipFeatureLevel, | ||
allowEditHistory, | ||
) | ||
); | ||
}; |
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,92 @@ | ||
/* @flow strict-local */ | ||
import React, { createContext, useState, useMemo, useCallback, useContext } from 'react'; | ||
import type { Context, Node } from 'react'; | ||
import { useSelector } from '../react-redux'; | ||
import TopicEditModal from '../topics/TopicEditModal'; | ||
import type { Stream, GetText } from '../types'; | ||
import { fetchSomeMessageIdForConversation } from '../message/fetchActions'; | ||
import { getAuth, getZulipFeatureLevel } from '../selectors'; | ||
|
||
type Props = $ReadOnly<{| | ||
children: Node, | ||
|}>; | ||
|
||
type TopicModalContext = $ReadOnly<{| | ||
startEditTopic: ( | ||
streamId: number, | ||
topic: string, | ||
streamsById: Map<number, Stream>, | ||
_: GetText, | ||
) => Promise<void>, | ||
closeEditTopicModal: () => void, | ||
|}>; | ||
|
||
// $FlowIssue[incompatible-type] | ||
const TopicModal: Context<TopicModalContext> = createContext(undefined); | ||
|
||
export const useTopicModalHandler = (): TopicModalContext => useContext(TopicModal); | ||
|
||
export default function TopicModalProvider(props: Props): Node { | ||
const { children } = props; | ||
const auth = useSelector(getAuth); | ||
const zulipFeatureLevel = useSelector(getZulipFeatureLevel); | ||
const [topicModalState, setTopicModalState] = useState({ | ||
visible: false, | ||
topic: '', | ||
fetchArgs: { | ||
auth: null, | ||
messageId: null, | ||
zulipFeatureLevel: null, | ||
}, | ||
}); | ||
|
||
const startEditTopic = useCallback( | ||
async (streamId, topic, streamsById, _) => { | ||
const messageId = await fetchSomeMessageIdForConversation( | ||
auth, | ||
streamId, | ||
topic, | ||
streamsById, | ||
zulipFeatureLevel, | ||
); | ||
if (messageId == null) { | ||
throw new Error( | ||
_('No messages in topic: {streamAndTopic}', { | ||
streamAndTopic: `#${streamsById.get(streamId)?.name ?? 'unknown'} > ${topic}`, | ||
}), | ||
); | ||
} | ||
setTopicModalState({ | ||
visible: true, | ||
topic, | ||
fetchArgs: { auth, messageId, zulipFeatureLevel }, | ||
}); | ||
}, | ||
[auth, zulipFeatureLevel], | ||
); | ||
|
||
const closeEditTopicModal = useCallback(() => { | ||
setTopicModalState({ | ||
visible: false, | ||
topic: null, | ||
fetchArgs: { auth: null, messageId: null, zulipFeatureLevel: null }, | ||
}); | ||
}, []); | ||
|
||
const topicModalHandler = useMemo( | ||
() => ({ | ||
startEditTopic, | ||
closeEditTopicModal, | ||
}), | ||
[startEditTopic, closeEditTopicModal], | ||
); | ||
|
||
return ( | ||
<TopicModal.Provider value={topicModalHandler}> | ||
{topicModalState.visible && ( | ||
<TopicEditModal topicModalState={topicModalState} topicModalHandler={topicModalHandler} /> | ||
)} | ||
{children} | ||
</TopicModal.Provider> | ||
); | ||
} |
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,34 @@ | ||
/* @flow strict-local */ | ||
import React from 'react'; | ||
import type { Node } from 'react'; | ||
import MessageList from '../webview/MessageList'; | ||
import { useTopicModalHandler } from '../boot/TopicModalProvider'; | ||
import type { Message, Narrow } from '../types'; | ||
|
||
type Props = $ReadOnly<{| | ||
messages: $ReadOnlyArray<Message>, | ||
narrow: Narrow, | ||
|}>; | ||
|
||
/* We can't call Context hooks from SearchMessagesCard because it's a class component. This wrapper allows the startEditTopic callback to be passed to this particular MessageList child without breaking Rules of Hooks. */ | ||
|
||
export default function MessageListWrapper({ messages, narrow }: Props): Node { | ||
const { startEditTopic } = useTopicModalHandler(); | ||
|
||
return ( | ||
<MessageList | ||
initialScrollMessageId={ | ||
// This access is OK only because of the `.length === 0` check | ||
// above. | ||
messages[messages.length - 1].id | ||
} | ||
messages={messages} | ||
narrow={narrow} | ||
showMessagePlaceholders={false} | ||
// TODO: handle editing a message from the search results, | ||
// or make this prop optional | ||
startEditMessage={() => undefined} | ||
startEditTopic={startEditTopic} | ||
/> | ||
); | ||
} |
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
Oops, something went wrong.