Page MenuHomePhabricator

D4941.id16155.diff
No OneTemporary

D4941.id16155.diff

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,91 @@
+// @flow
+
+import bots from 'lib/facts/bots';
+import { createMessageReply } from 'lib/shared/message-utils';
+import { type MessageReportCreationRequest } from 'lib/types/message-report-types';
+import { messageTypes } from 'lib/types/message-types';
+
+import { createCommbotThread } from '../bots/commbot';
+import { SQL } from '../database/database';
+import { fetchMessageInfoByID } from '../fetchers/message-fetchers';
+import {
+ fetchPersonalThreadID,
+ fetchServerThreadInfos,
+} from '../fetchers/thread-fetchers';
+import { fetchUsername } from '../fetchers/user-fetchers';
+import { createBotViewer } from '../session/bots';
+import type { Viewer } from '../session/viewer';
+import createMessages from './message-creator';
+
+const { commbot } = bots;
+
+async function getCommbotThreadID(userID: string): Promise<string> {
+ let commbotThreadID = await fetchPersonalThreadID(userID, commbot.userID);
+ if (!commbotThreadID) {
+ commbotThreadID = createCommbotThread(userID);
+ }
+ return commbotThreadID;
+}
+
+async function createMessageReport(
+ viewer: Viewer,
+ request: MessageReportCreationRequest,
+ recipientID: string,
+) {
+ const threadUserInfoPromise = Promise.all([
+ fetchUsername(viewer.id),
+ getCommbotThreadID(recipientID),
+ ]);
+
+ const reportedMessage = await fetchMessageInfoByID(viewer, request.messageID);
+
+ const reportedThreadID = reportedMessage?.threadID ?? '[null]';
+ const reportedMessageAuthorID = reportedMessage?.creatorID;
+
+ const reportedPromise = Promise.all([
+ fetchServerThreadInfos(SQL`t.id = ${reportedThreadID}`),
+ fetchUsername(reportedMessageAuthorID ?? '[null]'),
+ ]);
+
+ const reportedMessageText =
+ reportedMessage?.type === 0 ? reportedMessage.text : '[null]';
+
+ const [viewerUsername, commbotThreadID] = await threadUserInfoPromise;
+ const [reportedThread, reportedMessageAuthor] = await reportedPromise;
+
+ const reportMessage = getCommbotMessage(
+ viewerUsername,
+ reportedMessageAuthor,
+ reportedThread.threadInfos[reportedThreadID]?.name,
+ reportedMessageText,
+ );
+ const time = Date.now();
+ await createMessages(createBotViewer(commbot.userID), [
+ {
+ type: messageTypes.TEXT,
+ threadID: commbotThreadID,
+ creatorID: commbot.userID,
+ time,
+ text: reportMessage,
+ },
+ ]);
+}
+
+function getCommbotMessage(
+ reporterUsername: ?string,
+ messageAuthorUsername: ?string,
+ threadName: ?string,
+ message: string,
+): string {
+ reporterUsername = reporterUsername ?? '[null]';
+ const messageAuthor = messageAuthorUsername
+ ? `${messageAuthorUsername}’s`
+ : 'this';
+ const thread = threadName ? `chat titled "${threadName}"` : 'chat';
+ return (
+ `user ${reporterUsername} reported ${messageAuthor} message in ${thread}\n` +
+ createMessageReply(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,5 @@
+// @flow
+
+export type MessageReportCreationRequest = {
+ +messageID: string,
+};

File Metadata

Mime Type
text/plain
Expires
Wed, Nov 27, 7:51 AM (20 h, 37 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2589464
Default Alt Text
D4941.id16155.diff (3 KB)

Event Timeline