Changeset View
Changeset View
Standalone View
Standalone View
keyserver/src/responders/message-responders.js
Show All 31 Lines | |||||
import { threadPermissions } from 'lib/types/thread-types.js'; | import { threadPermissions } from 'lib/types/thread-types.js'; | ||||
import { userInfosValidator } from 'lib/types/user-types.js'; | import { userInfosValidator } from 'lib/types/user-types.js'; | ||||
import { ServerError } from 'lib/utils/errors.js'; | import { ServerError } from 'lib/utils/errors.js'; | ||||
import { values } from 'lib/utils/objects.js'; | import { values } from 'lib/utils/objects.js'; | ||||
import { | import { | ||||
tRegex, | tRegex, | ||||
tShape, | tShape, | ||||
tMediaMessageMedia, | tMediaMessageMedia, | ||||
tID, | |||||
} from 'lib/utils/validation-utils.js'; | } from 'lib/utils/validation-utils.js'; | ||||
import createMessages from '../creators/message-creator.js'; | import createMessages from '../creators/message-creator.js'; | ||||
import { SQL } from '../database/database.js'; | import { SQL } from '../database/database.js'; | ||||
import { | import { | ||||
fetchMessageInfos, | fetchMessageInfos, | ||||
fetchMessageInfoForLocalID, | fetchMessageInfoForLocalID, | ||||
fetchMessageInfoByID, | fetchMessageInfoByID, | ||||
Show All 10 Lines | |||||
import type { Viewer } from '../session/viewer.js'; | import type { Viewer } from '../session/viewer.js'; | ||||
import { | import { | ||||
assignImages, | assignImages, | ||||
assignMessageContainerToMedia, | assignMessageContainerToMedia, | ||||
} from '../updaters/upload-updaters.js'; | } from '../updaters/upload-updaters.js'; | ||||
import { validateInput, validateOutput } from '../utils/validation-utils.js'; | import { validateInput, validateOutput } from '../utils/validation-utils.js'; | ||||
const sendTextMessageRequestInputValidator = tShape({ | const sendTextMessageRequestInputValidator = tShape({ | ||||
threadID: t.String, | threadID: tID, | ||||
localID: t.maybe(t.String), | localID: t.maybe(t.String), | ||||
text: t.String, | text: t.String, | ||||
sidebarCreation: t.maybe(t.Boolean), | sidebarCreation: t.maybe(t.Boolean), | ||||
}); | }); | ||||
export const sendMessageResponseValidator: TInterface<SendMessageResponse> = | export const sendMessageResponseValidator: TInterface<SendMessageResponse> = | ||||
tShape<SendMessageResponse>({ newMessageInfo: rawMessageInfoValidator }); | tShape<SendMessageResponse>({ newMessageInfo: rawMessageInfoValidator }); | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | ): Promise<SendMessageResponse> { | ||||
} | } | ||||
const rawMessageInfos = await createMessages(viewer, [messageData]); | const rawMessageInfos = await createMessages(viewer, [messageData]); | ||||
const response = { newMessageInfo: rawMessageInfos[0] }; | const response = { newMessageInfo: rawMessageInfos[0] }; | ||||
return validateOutput(viewer, sendMessageResponseValidator, response); | return validateOutput(viewer, sendMessageResponseValidator, response); | ||||
} | } | ||||
const fetchMessageInfosRequestInputValidator = tShape({ | const fetchMessageInfosRequestInputValidator = tShape({ | ||||
cursors: t.dict(t.String, t.maybe(t.String)), | cursors: t.dict(tID, t.maybe(tID)), | ||||
numberPerThread: t.maybe(t.Number), | numberPerThread: t.maybe(t.Number), | ||||
}); | }); | ||||
export const fetchMessageInfosResponseValidator: TInterface<FetchMessageInfosResponse> = | export const fetchMessageInfosResponseValidator: TInterface<FetchMessageInfosResponse> = | ||||
tShape<FetchMessageInfosResponse>({ | tShape<FetchMessageInfosResponse>({ | ||||
rawMessageInfos: t.list(rawMessageInfoValidator), | rawMessageInfos: t.list(rawMessageInfoValidator), | ||||
truncationStatuses: messageTruncationStatusesValidator, | truncationStatuses: messageTruncationStatusesValidator, | ||||
userInfos: userInfosValidator, | userInfos: userInfosValidator, | ||||
Show All 14 Lines | return validateOutput(viewer, fetchMessageInfosResponseValidator, { | ||||
...response, | ...response, | ||||
userInfos: {}, | userInfos: {}, | ||||
}); | }); | ||||
} | } | ||||
const sendMultimediaMessageRequestInputValidator = t.union([ | const sendMultimediaMessageRequestInputValidator = t.union([ | ||||
// This option is only used for messageTypes.IMAGES | // This option is only used for messageTypes.IMAGES | ||||
tShape({ | tShape({ | ||||
threadID: t.String, | threadID: tID, | ||||
localID: t.String, | localID: t.String, | ||||
sidebarCreation: t.maybe(t.Boolean), | sidebarCreation: t.maybe(t.Boolean), | ||||
mediaIDs: t.list(t.String), | mediaIDs: t.list(tID), | ||||
}), | }), | ||||
tShape({ | tShape({ | ||||
threadID: t.String, | threadID: tID, | ||||
localID: t.String, | localID: t.String, | ||||
sidebarCreation: t.maybe(t.Boolean), | sidebarCreation: t.maybe(t.Boolean), | ||||
mediaMessageContents: t.list(tMediaMessageMedia), | mediaMessageContents: t.list(tMediaMessageMedia), | ||||
}), | }), | ||||
]); | ]); | ||||
async function multimediaMessageCreationResponder( | async function multimediaMessageCreationResponder( | ||||
viewer: Viewer, | viewer: Viewer, | ||||
input: any, | input: any, | ||||
▲ Show 20 Lines • Show All 71 Lines • ▼ Show 20 Lines | if (request.mediaIDs) { | ||||
); | ); | ||||
} | } | ||||
const response = { newMessageInfo }; | const response = { newMessageInfo }; | ||||
return validateOutput(viewer, sendMessageResponseValidator, response); | return validateOutput(viewer, sendMessageResponseValidator, response); | ||||
} | } | ||||
const sendReactionMessageRequestInputValidator = tShape({ | const sendReactionMessageRequestInputValidator = tShape({ | ||||
threadID: t.String, | threadID: tID, | ||||
localID: t.maybe(t.String), | localID: t.maybe(t.String), | ||||
targetMessageID: t.String, | targetMessageID: tID, | ||||
reaction: tRegex(onlyOneEmojiRegex), | reaction: tRegex(onlyOneEmojiRegex), | ||||
action: t.enums.of(['add_reaction', 'remove_reaction']), | action: t.enums.of(['add_reaction', 'remove_reaction']), | ||||
}); | }); | ||||
async function reactionMessageCreationResponder( | async function reactionMessageCreationResponder( | ||||
viewer: Viewer, | viewer: Viewer, | ||||
input: any, | input: any, | ||||
): Promise<SendMessageResponse> { | ): Promise<SendMessageResponse> { | ||||
const request: SendReactionMessageRequest = input; | const request: SendReactionMessageRequest = input; | ||||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | ): Promise<SendMessageResponse> { | ||||
const rawMessageInfos = await createMessages(viewer, [messageData]); | const rawMessageInfos = await createMessages(viewer, [messageData]); | ||||
const response = { newMessageInfo: rawMessageInfos[0] }; | const response = { newMessageInfo: rawMessageInfos[0] }; | ||||
return validateOutput(viewer, sendMessageResponseValidator, response); | return validateOutput(viewer, sendMessageResponseValidator, response); | ||||
} | } | ||||
const editMessageRequestInputValidator = tShape({ | const editMessageRequestInputValidator = tShape({ | ||||
targetMessageID: t.String, | targetMessageID: tID, | ||||
text: t.String, | text: t.String, | ||||
}); | }); | ||||
export const sendEditMessageResponseValidator: TInterface<SendEditMessageResponse> = | export const sendEditMessageResponseValidator: TInterface<SendEditMessageResponse> = | ||||
tShape<SendEditMessageResponse>({ | tShape<SendEditMessageResponse>({ | ||||
newMessageInfos: t.list(rawMessageInfoValidator), | newMessageInfos: t.list(rawMessageInfoValidator), | ||||
}); | }); | ||||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | ): Promise<SendEditMessageResponse> { | ||||
const newMessageInfos = await createMessages(viewer, messagesData); | const newMessageInfos = await createMessages(viewer, messagesData); | ||||
const response = { newMessageInfos }; | const response = { newMessageInfos }; | ||||
return validateOutput(viewer, sendEditMessageResponseValidator, response); | return validateOutput(viewer, sendEditMessageResponseValidator, response); | ||||
} | } | ||||
const fetchPinnedMessagesResponderInputValidator = tShape({ | const fetchPinnedMessagesResponderInputValidator = tShape({ | ||||
threadID: t.String, | threadID: tID, | ||||
}); | }); | ||||
export const fetchPinnedMessagesResultValidator: TInterface<FetchPinnedMessagesResult> = | export const fetchPinnedMessagesResultValidator: TInterface<FetchPinnedMessagesResult> = | ||||
tShape<FetchPinnedMessagesResult>({ | tShape<FetchPinnedMessagesResult>({ | ||||
pinnedMessages: t.list(rawMessageInfoValidator), | pinnedMessages: t.list(rawMessageInfoValidator), | ||||
}); | }); | ||||
async function fetchPinnedMessagesResponder( | async function fetchPinnedMessagesResponder( | ||||
Show All 21 Lines |