Skip to content

Commit 2cd4cf7

Browse files
committed
api types: Define list of valid message flags
1 parent 2700b78 commit 2cd4cf7

File tree

7 files changed

+37
-11
lines changed

7 files changed

+37
-11
lines changed

src/actionTypes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import {
6060
DISMISS_SERVER_COMPAT_NOTICE,
6161
} from './actionConstants';
6262

63+
import type { UserMessageFlag } from './api/modelTypes';
6364
import type {
6465
CustomProfileFieldsEvent,
6566
MessageEvent,
@@ -413,7 +414,7 @@ type EventUpdateMessageFlagsAction = $ReadOnly<{|
413414
type: typeof EVENT_UPDATE_MESSAGE_FLAGS,
414415
all: boolean,
415416
allMessages: MessagesState,
416-
flag: string,
417+
flag: UserMessageFlag,
417418
messages: $ReadOnlyArray<number>,
418419
op: 'add' | 'remove',
419420
message_details?: Map<number, UpdateMessageFlagsMessageDetails>,

src/api/eventTypes.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type {
2020
PropagateMode,
2121
Stream,
2222
UserId,
23+
UserMessageFlag,
2324
UserPresence,
2425
UserStatusUpdate,
2526
} from './modelTypes';
@@ -106,7 +107,7 @@ export type MessageEvent = $ReadOnly<{|
106107
message: Message,
107108

108109
/** See the same-named property on `Message`. */
109-
flags?: $ReadOnlyArray<string>,
110+
flags?: $ReadOnlyArray<UserMessageFlag>,
110111

111112
/**
112113
* When the message was sent by this client (with `queue_id` this queue),
@@ -335,7 +336,7 @@ export type UpdateMessageEvent = $ReadOnly<{|
335336
// guarantee of that in the API nor, apparently, in the implementation.
336337
message_ids: $ReadOnlyArray<number>,
337338

338-
flags: $ReadOnlyArray<string>,
339+
flags: $ReadOnlyArray<UserMessageFlag>,
339340

340341
// TODO(server-5.0): Always present as of FL 114; make required.
341342
edit_timestamp?: number,

src/api/messages/messagesFlags.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* @flow strict-local */
22
import type { ApiResponseSuccess, Auth } from '../transportTypes';
3+
import type { UserMessageFlag } from '../modelTypes';
34
import { apiPost } from '../apiFetch';
45

56
export type ApiResponseMessagesFlags = {|
@@ -11,6 +12,6 @@ export default (
1112
auth: Auth,
1213
messages: $ReadOnlyArray<number>,
1314
op: string,
14-
flag: string,
15+
flag: UserMessageFlag,
1516
): Promise<ApiResponseMessagesFlags> =>
1617
apiPost(auth, 'messages/flags', { messages: JSON.stringify(messages), flag, op });

src/api/modelTypes.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,21 @@ export type Submessage = $ReadOnly<{|
749749
content: string,
750750
|}>;
751751

752+
/**
753+
* A flag that can be set on a message for a user.
754+
*
755+
* See:
756+
* https://zulip.com/api/update-message-flags#available-flags
757+
*/
758+
export type UserMessageFlag =
759+
| 'read'
760+
| 'starred'
761+
| 'collapsed'
762+
| 'mentioned'
763+
| 'wildcard_mentioned'
764+
| 'has_alert_word'
765+
| 'historical';
766+
752767
/**
753768
* Properties in common among the two different flavors of a
754769
* `Message`: `PmMessage` and `StreamMessage`.
@@ -845,7 +860,7 @@ type MessageBase = $ReadOnly<{|
845860
* * Absent in the Redux `state.messages`; we move the information to a
846861
* separate subtree `state.flags`.
847862
*/
848-
flags?: $ReadOnlyArray<string>,
863+
flags?: $ReadOnlyArray<UserMessageFlag>,
849864

850865
/** Our own flag; if true, really type `Outbox`. */
851866
isOutbox?: false,

src/chat/flagsReducer.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
ACCOUNT_SWITCH,
1414
} from '../actionConstants';
1515
import { deeperMerge } from '../utils/misc';
16+
import type { UserMessageFlag } from '../api/modelTypes';
1617

1718
type ReadWriteFlagsState = $Rest<ReadWrite<$ObjMap<FlagsState, <V>(V) => ReadWrite<V>>>, { ... }>;
1819
type ReadWritePerFlagState = $Values<ReadWriteFlagsState>;
@@ -35,7 +36,7 @@ const initialState = {
3536
const addFlagsForMessages = (
3637
state: FlagsState,
3738
messages: $ReadOnlyArray<number>,
38-
flags: $ReadOnlyArray<string>,
39+
flags: $ReadOnlyArray<UserMessageFlag>,
3940
): FlagsState => {
4041
if (messages.length === 0 || flags.length === 0) {
4142
return state;
@@ -117,7 +118,7 @@ const eventUpdateMessageFlags = (state, action) => {
117118
// know about. After all, we can't have any code intending to do
118119
// anything with them. Flow should be complaining here:
119120
// https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/Flow.20spread.20bug/near/1318081
120-
return { ...state, [action.flag]: {} };
121+
return { ...state, [(action.flag: string)]: {} };
121122
}
122123
}
123124

src/reduxTypes.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,23 @@ export type FetchingState = $ReadOnly<{|
129129
* `state.messages`.
130130
*/
131131
export type FlagsState = $ReadOnly<{|
132+
// Flags that are currently documented in the API:
133+
// https://zulip.com/api/update-message-flags#available-flags
134+
// i.e., those found in `UserMessageFlag`.
132135
read: {| +[messageId: number]: true |},
133136
starred: {| +[messageId: number]: true |},
134137
collapsed: {| +[messageId: number]: true |},
135138
mentioned: {| +[messageId: number]: true |},
136139
wildcard_mentioned: {| +[messageId: number]: true |},
140+
has_alert_word: {| +[messageId: number]: true |},
141+
historical: {| +[messageId: number]: true |},
142+
143+
// Flags not documented in the API.
144+
// TODO(server): Do these ever exist? Did they use to and get removed?
137145
summarize_in_home: {| +[messageId: number]: true |},
138146
summarize_in_stream: {| +[messageId: number]: true |},
139147
force_expand: {| +[messageId: number]: true |},
140148
force_collapse: {| +[messageId: number]: true |},
141-
has_alert_word: {| +[messageId: number]: true |},
142-
historical: {| +[messageId: number]: true |},
143149
is_me_message: {| +[messageId: number]: true |},
144150
|}>;
145151

src/utils/narrow.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
pmKeyRecipientsFor1to1,
1313
makePmKeyRecipients_UNSAFE,
1414
} from './recipient';
15+
import type { UserMessageFlag } from '../api/modelTypes';
1516

1617
/* eslint-disable no-use-before-define */
1718

@@ -461,7 +462,7 @@ export const apiNarrowOfNarrow = (
461462
*/
462463
export const isMessageInNarrow = (
463464
message: Message | Outbox,
464-
flags: $ReadOnlyArray<string>,
465+
flags: $ReadOnlyArray<UserMessageFlag>,
465466
narrow: Narrow,
466467
ownUserId: UserId,
467468
): boolean =>
@@ -524,7 +525,7 @@ export const showComposeBoxOnNarrow = (narrow: Narrow): boolean =>
524525
export const getNarrowsForMessage = (
525526
message: Message | Outbox,
526527
ownUserId: UserId,
527-
flags: $ReadOnlyArray<string>,
528+
flags: $ReadOnlyArray<UserMessageFlag>,
528529
): $ReadOnlyArray<Narrow> => {
529530
const result = [];
530531

0 commit comments

Comments
 (0)