diff --git a/keyserver/src/endpoints.js b/keyserver/src/endpoints.js --- a/keyserver/src/endpoints.js +++ b/keyserver/src/endpoints.js @@ -18,6 +18,7 @@ } from './responders/entry-responders'; import type { JSONResponder } from './responders/handlers'; import { getSessionPublicKeysResponder } from './responders/keys-responders'; +import { messageReportCreationResponder } from './responders/message-report-responder'; import { textMessageCreationResponder, messageFetchResponder, @@ -58,6 +59,7 @@ create_account: accountCreationResponder, create_entry: entryCreationResponder, create_error_report: reportCreationResponder, + create_message_report: messageReportCreationResponder, create_multimedia_message: multimediaMessageCreationResponder, create_report: reportCreationResponder, create_reports: reportMultiCreationResponder, diff --git a/keyserver/src/responders/message-report-responder.js b/keyserver/src/responders/message-report-responder.js new file mode 100644 --- /dev/null +++ b/keyserver/src/responders/message-report-responder.js @@ -0,0 +1,48 @@ +// @flow + +import t from 'tcomb'; + +import { getKeyserverAdmin } from 'lib/shared/user-utils'; +import { type MessageReportCreationRequest } from 'lib/types/message-report-types'; +import { tShape } from 'lib/utils/validation-utils'; + +import createMessageReport from '../creators/message-report-creator'; +import { fetchMessageInfoByID } from '../fetchers/message-fetchers'; +import { serverThreadInfoFromMessageInfo } from '../fetchers/thread-fetchers'; +import { fetchAllUserIDs, fetchUserInfos } from '../fetchers/user-fetchers'; +import type { Viewer } from '../session/viewer'; +import { validateInput } from '../utils/validation-utils'; + +const messageReportCreationRequestInputValidator = tShape({ + messageID: t.String, +}); + +async function messageReportCreationResponder(viewer: Viewer, input: any) { + await validateInput( + viewer, + messageReportCreationRequestInputValidator, + input, + ); + const request: MessageReportCreationRequest = input; + + const userInfos = await fetchUserInfos(await fetchAllUserIDs()); + const reportedMessage = await fetchMessageInfoByID(viewer, request.messageID); + const threadInfo = await serverThreadInfoFromMessageInfo(reportedMessage); + + if (!threadInfo) { + return; + } + const keyserverAdmin = getKeyserverAdmin(threadInfo, userInfos); + if (!keyserverAdmin?.id) { + return; + } + + await createMessageReport( + viewer, + reportedMessage, + threadInfo, + keyserverAdmin.id, + ); +} + +export { messageReportCreationResponder }; diff --git a/lib/types/endpoints.js b/lib/types/endpoints.js --- a/lib/types/endpoints.js +++ b/lib/types/endpoints.js @@ -48,6 +48,7 @@ const socketPreferredEndpoints = Object.freeze({ CREATE_ENTRY: 'create_entry', CREATE_ERROR_REPORT: 'create_error_report', + CREATE_MESSAGE_REPORT: 'create_message_report', CREATE_MULTIMEDIA_MESSAGE: 'create_multimedia_message', CREATE_TEXT_MESSAGE: 'create_text_message', CREATE_THREAD: 'create_thread',