Changeset View
Changeset View
Standalone View
Standalone View
lib/types/thread-types.js
// @flow | // @flow | ||||
import invariant from 'invariant'; | import invariant from 'invariant'; | ||||
import t, { type TInterface } from 'tcomb'; | |||||
import type { | import { | ||||
AvatarDBContent, | type AvatarDBContent, | ||||
ClientAvatar, | type ClientAvatar, | ||||
UpdateUserAvatarRequest, | type UpdateUserAvatarRequest, | ||||
clientAvatarValidator, | |||||
} from './avatar-types.js'; | } from './avatar-types.js'; | ||||
import type { Shape } from './core.js'; | import type { Shape } from './core.js'; | ||||
import type { CalendarQuery, RawEntryInfo } from './entry-types.js'; | import type { CalendarQuery, RawEntryInfo } from './entry-types.js'; | ||||
import type { Media } from './media-types.js'; | import type { Media } from './media-types.js'; | ||||
import type { | import type { | ||||
RawMessageInfo, | RawMessageInfo, | ||||
MessageTruncationStatuses, | MessageTruncationStatuses, | ||||
} from './message-types.js'; | } from './message-types.js'; | ||||
import type { ThreadSubscription } from './subscription-types.js'; | import { | ||||
type ThreadSubscription, | |||||
threadSubscriptionValidator, | |||||
} from './subscription-types.js'; | |||||
import type { ServerUpdateInfo, ClientUpdateInfo } from './update-types.js'; | import type { ServerUpdateInfo, ClientUpdateInfo } from './update-types.js'; | ||||
import type { UserInfo, UserInfos } from './user-types.js'; | import type { UserInfo, UserInfos } from './user-types.js'; | ||||
import type { ThreadEntity } from '../utils/entity-text.js'; | import type { ThreadEntity } from '../utils/entity-text.js'; | ||||
import { values } from '../utils/objects.js'; | |||||
import { tNumEnum, tBool, tID, tShape } from '../utils/validation-utils.js'; | |||||
export const threadTypes = Object.freeze({ | export const threadTypes = Object.freeze({ | ||||
//OPEN: 0, (DEPRECATED) | //OPEN: 0, (DEPRECATED) | ||||
//CLOSED: 1, (DEPRECATED) | //CLOSED: 1, (DEPRECATED) | ||||
//SECRET: 2, (DEPRECATED) | //SECRET: 2, (DEPRECATED) | ||||
// has parent, not top-level (appears under parent in inbox), and visible to | // has parent, not top-level (appears under parent in inbox), and visible to | ||||
// all members of parent | // all members of parent | ||||
SIDEBAR: 5, | SIDEBAR: 5, | ||||
Show All 34 Lines | threadType === 3 || | ||||
threadType === 9 || | threadType === 9 || | ||||
threadType === 10 || | threadType === 10 || | ||||
threadType === 11 || | threadType === 11 || | ||||
threadType === 12, | threadType === 12, | ||||
'number is not ThreadType enum', | 'number is not ThreadType enum', | ||||
); | ); | ||||
return threadType; | return threadType; | ||||
} | } | ||||
const threadTypeValidator = tNumEnum(values(threadTypes)); | |||||
export const communityThreadTypes: $ReadOnlyArray<number> = Object.freeze([ | export const communityThreadTypes: $ReadOnlyArray<number> = Object.freeze([ | ||||
threadTypes.COMMUNITY_ROOT, | threadTypes.COMMUNITY_ROOT, | ||||
threadTypes.COMMUNITY_ANNOUNCEMENT_ROOT, | threadTypes.COMMUNITY_ANNOUNCEMENT_ROOT, | ||||
threadTypes.GENESIS, | threadTypes.GENESIS, | ||||
]); | ]); | ||||
export const communitySubthreads: $ReadOnlyArray<number> = Object.freeze([ | export const communitySubthreads: $ReadOnlyArray<number> = Object.freeze([ | ||||
threadTypes.COMMUNITY_OPEN_SUBTHREAD, | threadTypes.COMMUNITY_OPEN_SUBTHREAD, | ||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | ourThreadPermissions === 'know_of' || | ||||
ourThreadPermissions === 'react_to_message' || | ourThreadPermissions === 'react_to_message' || | ||||
ourThreadPermissions === 'edit_message' || | ourThreadPermissions === 'edit_message' || | ||||
ourThreadPermissions === 'edit_thread_avatar' || | ourThreadPermissions === 'edit_thread_avatar' || | ||||
ourThreadPermissions === 'manage_pins', | ourThreadPermissions === 'manage_pins', | ||||
'string is not threadPermissions enum', | 'string is not threadPermissions enum', | ||||
); | ); | ||||
return ourThreadPermissions; | return ourThreadPermissions; | ||||
} | } | ||||
const threadPermissionValidator = t.enums.of(values(threadPermissions)); | |||||
export const threadPermissionPropagationPrefixes = Object.freeze({ | export const threadPermissionPropagationPrefixes = Object.freeze({ | ||||
DESCENDANT: 'descendant_', | DESCENDANT: 'descendant_', | ||||
CHILD: 'child_', | CHILD: 'child_', | ||||
}); | }); | ||||
export type ThreadPermissionPropagationPrefix = $Values< | export type ThreadPermissionPropagationPrefix = $Values< | ||||
typeof threadPermissionPropagationPrefixes, | typeof threadPermissionPropagationPrefixes, | ||||
>; | >; | ||||
Show All 11 Lines | |||||
export type ThreadPermissionFilterPrefix = $Values< | export type ThreadPermissionFilterPrefix = $Values< | ||||
typeof threadPermissionFilterPrefixes, | typeof threadPermissionFilterPrefixes, | ||||
>; | >; | ||||
export type ThreadPermissionInfo = | export type ThreadPermissionInfo = | ||||
| { +value: true, +source: string } | | { +value: true, +source: string } | ||||
| { +value: false, +source: null }; | | { +value: false, +source: null }; | ||||
const threadPermissionInfoValidator = t.union([ | |||||
tShape({ value: tBool(true), source: t.String }), | |||||
tShape({ value: tBool(false), source: t.Nil }), | |||||
]); | |||||
export type ThreadPermissionsBlob = { | export type ThreadPermissionsBlob = { | ||||
+[permission: string]: ThreadPermissionInfo, | +[permission: string]: ThreadPermissionInfo, | ||||
}; | }; | ||||
export type ThreadRolePermissionsBlob = { +[permission: string]: boolean }; | export type ThreadRolePermissionsBlob = { +[permission: string]: boolean }; | ||||
const threadRolePermissionsBlobValidator = t.dict(t.String, t.Boolean); | |||||
export type ThreadPermissionsInfo = { | export type ThreadPermissionsInfo = { | ||||
+[permission: ThreadPermission]: ThreadPermissionInfo, | +[permission: ThreadPermission]: ThreadPermissionInfo, | ||||
}; | }; | ||||
const threadPermissionsInfoValidator = t.dict( | |||||
threadPermissionValidator, | |||||
threadPermissionInfoValidator, | |||||
); | |||||
export type MemberInfo = { | export type MemberInfo = { | ||||
+id: string, | +id: string, | ||||
+role: ?string, | +role: ?string, | ||||
+permissions: ThreadPermissionsInfo, | +permissions: ThreadPermissionsInfo, | ||||
+isSender: boolean, | +isSender: boolean, | ||||
}; | }; | ||||
const memberInfoValidator = tShape<MemberInfo>({ | |||||
id: t.String, | |||||
role: t.maybe(tID), | |||||
permissions: threadPermissionsInfoValidator, | |||||
isSender: t.Boolean, | |||||
}); | |||||
export type RelativeMemberInfo = { | export type RelativeMemberInfo = { | ||||
...MemberInfo, | ...MemberInfo, | ||||
+username: ?string, | +username: ?string, | ||||
+isViewer: boolean, | +isViewer: boolean, | ||||
}; | }; | ||||
export type RoleInfo = { | export type RoleInfo = { | ||||
+id: string, | +id: string, | ||||
+name: string, | +name: string, | ||||
+permissions: ThreadRolePermissionsBlob, | +permissions: ThreadRolePermissionsBlob, | ||||
+isDefault: boolean, | +isDefault: boolean, | ||||
}; | }; | ||||
const roleInfoValidator = tShape<RoleInfo>({ | |||||
id: tID, | |||||
name: t.String, | |||||
permissions: threadRolePermissionsBlobValidator, | |||||
isDefault: t.Boolean, | |||||
}); | |||||
export type ThreadCurrentUserInfo = { | export type ThreadCurrentUserInfo = { | ||||
+role: ?string, | +role: ?string, | ||||
+permissions: ThreadPermissionsInfo, | +permissions: ThreadPermissionsInfo, | ||||
+subscription: ThreadSubscription, | +subscription: ThreadSubscription, | ||||
+unread: ?boolean, | +unread: ?boolean, | ||||
}; | }; | ||||
const threadCurrentUserInfoValidator = tShape<ThreadCurrentUserInfo>({ | |||||
role: t.maybe(tID), | |||||
permissions: threadPermissionsInfoValidator, | |||||
subscription: threadSubscriptionValidator, | |||||
unread: t.maybe(t.Boolean), | |||||
}); | |||||
export type RawThreadInfo = { | export type RawThreadInfo = { | ||||
+id: string, | +id: string, | ||||
+type: ThreadType, | +type: ThreadType, | ||||
+name: ?string, | +name: ?string, | ||||
+avatar?: ?ClientAvatar, | +avatar?: ?ClientAvatar, | ||||
+description: ?string, | +description: ?string, | ||||
+color: string, // hex, without "#" or "0x" | +color: string, // hex, without "#" or "0x" | ||||
+creationTime: number, // millisecond timestamp | +creationTime: number, // millisecond timestamp | ||||
+parentThreadID: ?string, | +parentThreadID: ?string, | ||||
+containingThreadID: ?string, | +containingThreadID: ?string, | ||||
+community: ?string, | +community: ?string, | ||||
+members: $ReadOnlyArray<MemberInfo>, | +members: $ReadOnlyArray<MemberInfo>, | ||||
+roles: { +[id: string]: RoleInfo }, | +roles: { +[id: string]: RoleInfo }, | ||||
+currentUser: ThreadCurrentUserInfo, | +currentUser: ThreadCurrentUserInfo, | ||||
+sourceMessageID?: string, | +sourceMessageID?: string, | ||||
+repliesCount: number, | +repliesCount: number, | ||||
+pinnedCount?: number, | +pinnedCount?: number, | ||||
}; | }; | ||||
export const rawThreadInfoValidator: TInterface<RawThreadInfo> = | |||||
tShape<RawThreadInfo>({ | |||||
id: tID, | |||||
type: threadTypeValidator, | |||||
name: t.maybe(t.String), | |||||
avatar: t.maybe(clientAvatarValidator), | |||||
description: t.maybe(t.String), | |||||
color: t.String, | |||||
creationTime: t.Number, | |||||
parentThreadID: t.maybe(tID), | |||||
containingThreadID: t.maybe(tID), | |||||
community: t.maybe(tID), | |||||
members: t.list(memberInfoValidator), | |||||
roles: t.dict(tID, roleInfoValidator), | |||||
currentUser: threadCurrentUserInfoValidator, | |||||
sourceMessageID: t.maybe(tID), | |||||
repliesCount: t.Number, | |||||
pinnedCount: t.maybe(t.Number), | |||||
}); | |||||
export type ThreadInfo = { | export type ThreadInfo = { | ||||
+id: string, | +id: string, | ||||
+type: ThreadType, | +type: ThreadType, | ||||
+name: ?string, | +name: ?string, | ||||
+uiName: string | ThreadEntity, | +uiName: string | ThreadEntity, | ||||
+avatar?: ?ClientAvatar, | +avatar?: ?ClientAvatar, | ||||
+description: ?string, | +description: ?string, | ||||
▲ Show 20 Lines • Show All 287 Lines • Show Last 20 Lines |