diff --git a/keyserver/src/creators/message-report-creator.js b/keyserver/src/creators/message-report-creator.js new file mode 100644 --- /dev/null +++ b/keyserver/src/creators/message-report-creator.js @@ -0,0 +1,60 @@ +// @flow + +import bots from 'lib/facts/bots'; +import { type MessageReportCreationRequest } from 'lib/types/message-report-types'; +import { messageTypes } from 'lib/types/message-types'; + +import { fetchUsername } from '../fetchers/user-fetchers'; +import { handleAsyncPromise } from '../responders/handlers'; +import { createBotViewer } from '../session/bots'; +import type { Viewer } from '../session/viewer'; +import createMessages from './message-creator'; + +const { commbot } = bots; + +async function createMessageReport( + viewer: Viewer, + request: MessageReportCreationRequest, +) { + handleAsyncPromise(sendCommbotMessage(viewer, request)); +} + +async function sendCommbotMessage( + viewer: Viewer, + request: MessageReportCreationRequest, +) { + const username = await fetchUsername(viewer.id); + if (!username) { + return; + } + const message = getCommbotMessage(request, username); + + const time = Date.now(); + await createMessages(createBotViewer(commbot.userID), [ + { + type: messageTypes.TEXT, + threadID: commbot.staffThreadID, + creatorID: commbot.userID, + time, + text: message, + }, + ]); +} + +function getCommbotMessage( + request: MessageReportCreationRequest, + username: string, +): string { + const messageAuthor = request.messageAuthorInfo.username + ? `${request.messageAuthorInfo.username}’s` + : 'this'; + const thread = request.thread?.name + ? `chat titled "${request.thread.name}"` + : 'chat'; + return ( + `user ${username} reported ${messageAuthor} message in ${thread}\n` + + `> ${request.message}` + ); +} + +export default createMessageReport; diff --git a/lib/types/message-report-types.js b/lib/types/message-report-types.js new file mode 100644 --- /dev/null +++ b/lib/types/message-report-types.js @@ -0,0 +1,10 @@ +// @flow + +import { type ThreadInfo } from './thread-types'; +import type { UserInfo } from './user-types'; + +export type MessageReportCreationRequest = { + +thread: ThreadInfo, + +messageAuthorInfo: UserInfo, + +message: string, +};