-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathsayable.ts
More file actions
115 lines (105 loc) · 3.85 KB
/
sayable.ts
File metadata and controls
115 lines (105 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* eslint-disable sort-keys */
import { createAction } from 'typesafe-actions'
import type { FileBoxInterface } from 'file-box'
import { MessageType } from './message.js'
import type { LocationPayload } from './location.js'
import type { UrlLinkPayload } from './url-link.js'
import type { MiniProgramPayload } from './mini-program.js'
import type { ChannelPayload } from './channel.js'
import type {
PostPayload,
SayablePayloadPost,
} from './post.js'
const payloadContact = (contactId: string) => ({ contactId })
const payloadFilebox = (filebox: string | FileBoxInterface) => ({ filebox })
const payloadText = (text: string, mentions: string[] = []) => ({ text, mentions })
/**
* expand/merge the payload altogether
*/
const payloadLocation = (locationPayload: LocationPayload) => ({ ...locationPayload })
const payloadMiniProgram = (miniProgramPayload: MiniProgramPayload) => ({ ...miniProgramPayload })
const payloadUrlLink = (urlLinkPayload: UrlLinkPayload) => ({ ...urlLinkPayload })
const payloadPost = (postPayload: PostPayload) => ({ ...postPayload })
const payloadChannel = (channelPayload: ChannelPayload) => ({ ...channelPayload })
/**
* using `types` as a static typed string name list for `createAction`
*
* Huan(202201): if we remove the `(() => ({}))()`, then the typing will fail.
* FIXME: remove the `(() => ({}))()` after we fix the issue.
*/
const sayableTypes = (() => ({
...Object.keys(MessageType)
.filter(k => isNaN(Number(k)))
.reduce((acc, cur) => ({
...acc,
[cur]: cur,
}), {}),
} as {
[k in keyof typeof MessageType]: k
}))()
/**
* Simple data
*/
const contact = createAction(sayableTypes.Contact, payloadContact)()
const text = createAction(sayableTypes.Text, payloadText)()
// (conversationId: string, text: string, mentionIdList?: string[]) => ({ conversationId, mentionIdList, text }
/**
* FileBoxs
*/
const attatchment = createAction(sayableTypes.Attachment, payloadFilebox)()
const audio = createAction(sayableTypes.Audio, payloadFilebox)()
const emoticon = createAction(sayableTypes.Emoticon, payloadFilebox)()
const image = createAction(sayableTypes.Image, payloadFilebox)()
const video = createAction(sayableTypes.Video, payloadFilebox)()
/**
* Payload data
*/
const location = createAction(sayableTypes.Location, payloadLocation)()
const miniProgram = createAction(sayableTypes.MiniProgram, payloadMiniProgram)()
const url = createAction(sayableTypes.Url, payloadUrlLink)()
const post = createAction(sayableTypes.Post, payloadPost)()
const channel = createAction(sayableTypes.Channel, payloadChannel)()
/**
* Huan(202201): Recursive type references
* @link https://github.com/microsoft/TypeScript/pull/33050#issuecomment-1002455128
*/
const sayablePayloadsNoPost = {
attatchment,
audio,
contact,
emoticon,
image,
location,
miniProgram,
text,
url,
video,
channel,
} as const
/**
*
* Huan(202201): Recursive type references
* @link https://github.com/microsoft/TypeScript/pull/33050#issuecomment-1002455128
* @link https://github.com/wechaty/puppet/issues/180
*/
const sayablePayloads = {
...sayablePayloadsNoPost,
post,
} as const
type SayablePayloadNoPost = ReturnType<typeof sayablePayloadsNoPost[keyof typeof sayablePayloadsNoPost]>
type SayablePayload = SayablePayloadNoPost | SayablePayloadPost
// TODO: add an unit test to confirm that all unsupported type are listed here
type SayablePayloadUnsupportedType =
| 'ChatHistory'
| 'GroupNote'
| 'Recalled'
| 'RedEnvelope'
| 'Transfer'
| 'Unknown'
export {
sayablePayloads,
sayableTypes,
type SayablePayloadNoPost,
type SayablePayload,
type SayablePayloadUnsupportedType,
}