Files
discord-decomp/modules/polls/PollsActionCreators.tsx
T
2026-11-01 12:12:55 +02:00

750 lines
23 KiB
TypeScript

// Metro module 9957 (f9958).
import LurkingStore from "../lurker_mode/LurkingStore";
import ReferencedMessageStore from "../replies/ReferencedMessageStore";
import AuthenticationStore from "../../stores/AuthenticationStore";
import ChannelStore from "../../stores/ChannelStore";
import { DraftType } from "../../stores/DraftStore";
import GuildVerificationStore from "../../stores/GuildVerificationStore";
import MessageStore from "../../stores/MessageStore";
import UploadAttachmentStore from "../../stores/UploadAttachmentStore";
import { updatePollState, getPollState } from "./PollsInteractionStore";
import { AnalyticEvents } from "../../Constants";
import Module38 from "../../_bundle/Module38";
import AlertActionCreators from "../../actions/AlertActionCreators";
import { intl, t } from "../../intl/index.native";
import { showVotesForAnswer } from "./PollInteractionUtils.native";
import { computeBasicPollChatData } from "./chat/formatPollMessageChatData";
import AppAnalyticsUtils from "../app_analytics/AppAnalyticsUtils";
import Module12 from "../../_bundle/Module12";
import Module589 from "../../_bundle/Module589";
import Dispatcher from "../../Dispatcher";
import { ReactionTypes } from "../messages/MessageReactionsTypes";
import * as _PollsHttpApi from "./PollsHttpApi";
import { submitPollVote } from "./PollsHttpApi";
import { AccessibilityAnnouncer } from "../../design/shared";
import { PollLayoutTypes } from "../../_bundle/Module8002";
import MessageActionCreators from "../../actions/MessageActionCreators";
import { handleUploadMessageAttachmentsErrors } from "../media_uploads/handleUploadAttachmentErrors.native";
import { APIError } from "../../errors/index";
import { HermesBuiltin } from "../../_hermes/prelude";
function $mk83611($env) {
return () => {
const r5 = $env.$parent;
(0, r5[1])(r5[3][14]).joinGuild($env[0], {
source: r5[16].POLL_ALERT
});
};
}
function $mk115581($env) {
return () => {
let r22;
const r5 = $env;
let r0 = void 0;
let r2 = r5[7][Symbol.iterator]();
const next = r2.next;
while (true) {
const r25 = r2 === void 0 ? void 0 : (r22 = next.call(r2)).done ? r2 = void 0 : r22.value;
if (r2 === void 0) {
return r0;
}
try {
const id = r25.id;
r0 = Dispatcher.dispatch({
type: r25.type,
channelId: r5[0],
messageId: r5[1],
emoji: {
id,
name: id
},
userId: r5[6],
optimistic: true,
reactionType: ReactionTypes.VOTE
});
} catch (r39) {
if (r2 !== void 0 && typeof r2.return === "function") {
try {
r2.return();
} catch {}
}
throw r39;
}
}
};
}
function $mk115582($env) {
return (a0) => {
Module38(a0 != null, "Must not be able to vote without existing state!");
return {
...a0,
submitting: true,
editing: false
};
};
}
function $mk115585($env) {
return (a0) => {
const r2 = {
channelId: $env[0],
selectedAnswerIds: new Set(),
submitting: false,
editing: false,
showResults: null
};
let showResults = a0?.showResults;
if (showResults == null) {
showResults = false;
}
r2.showResults = showResults;
return r2;
};
}
function $mk115586($env) {
return function handleClearPollVote() {
return $env[26].apply(this, arguments);
};
}
function $mk115587($env) {
return function handleShowVotes(a0) {
let env31516 = {
$parent: $env
};
const channelId3 = a0.channelId;
env31516[0] = channelId3;
const messageId2 = a0.messageId;
env31516[1] = messageId2;
(0, updatePollState)(channelId3, messageId2, (a0) => {
const show_results = a0 == null || !a0.showResults;
const r16 = env31516.$parent;
const r9 = r16[11];
const channel_id = env31516[0];
const message_id = env31516[1];
const message2 = r9.getMessage(channel_id, message_id);
const votes_count = message2 != null ? message2.reactions.reduce((acc, reaction) => {
let r4 = reaction.count_details?.vote;
if (r4 == null) {
r4 = 0;
}
return acc + r4;
}, 0) : 0;
(0, r16[1])(r16[3][17]).trackWithMetadata(r16[15].POLL_SHOW_RESULTS_CLICKED, {
channel_id,
message_id,
show_results,
votes_count
});
const r31 = {
channelId: channel_id,
selectedAnswerIds: new Set(),
submitting: null,
editing: null,
showResults: null
};
let submitting = a0?.submitting;
if (submitting == null) {
submitting = false;
}
r31.submitting = submitting;
let editing = a0?.submitting;
if (editing == null) {
editing = false;
}
r31.editing = editing;
r31.showResults = show_results;
return r31;
});
};
}
function $mk115588($env) {
return (a0) => {
const r6 = $env;
const r8 = r6[7];
const r9 = r8 == null ? void 0 : r8.findIndex((item) => item.id === a0.localCreationAnswerId);
const poll_media = {
attachment_ids: r9 !== -1 ? [`${r9}`] : void 0
};
const r19 = r6[5];
if (r19 === PollLayoutTypes.DEFAULT) {
const text = a0.text;
poll_media.text = text == null ? void 0 : text.trim();
}
const emoji = a0.image?.emoji;
if (emoji != null) {
if (emoji.id != null) {
poll_media.emoji = {
id: emoji.id,
name: ""
};
} else if (emoji.optionallyDiverseSequence != null) {
poll_media.emoji = {
name: emoji.optionallyDiverseSequence
};
}
}
return {
poll_media
};
};
}
function $mk115589($env) {
return (a0, a1, a2) => {
handleUploadMessageAttachmentsErrors({
file: a0,
guildId: $env[0].getGuildId(),
analyticsLocations: [],
code: a1,
reason: a2
});
};
}
let showLurkingAlert, handleShowVotesForAnswer, handleUpdateVoteEditingState, getCurrentAnswerIds, optimisticallySetAnswers, handlePollSubmitVote;
let env6619 = {};
const getPollVoteEventProperties = function getPollVoteEventProperties(c17577_0, a1) {
const analyticsSelectedAnswerIds = [...a1];
let selectedTextAnswersCount = 0;
let selectedEmojiAnswersCount = 0;
const _loop = function _loop(c26574_0) {
const r9 = c17577_0 == null ? void 0 : c17577_0.find((item) => item.answer_id === parseInt(c26574_0))?.poll_media;
if (r9?.text != null) {
selectedTextAnswersCount = selectedTextAnswersCount + 1;
}
if (r9?.emoji != null) {
selectedEmojiAnswersCount = selectedEmojiAnswersCount + 1;
}
};
for (const item of analyticsSelectedAnswerIds) {
_loop(item);
}
return {
analyticsSelectedAnswerIds,
selectedTextAnswersCount,
selectedEmojiAnswersCount
};
};
showLurkingAlert = function showLurkingAlert(a0) {
let env17578;
const r4 = env17578 = {
$parent: env6619
};
env17578[0] = a0.guildId;
AlertActionCreators.show({
title: a0.title,
body: a0.body,
confirmText: intl.string(t["9VLmlZ"]),
cancelText: intl.string(t["2m+Sqk"]),
onConfirm: $mk83611(r4)
});
};
handleShowVotesForAnswer = function handleShowVotesForAnswer(a0) {
const { channelId: channelId3, messageId: messageId2 } = a0;
let answerId2 = a0.answerId;
const channel = ChannelStore.getChannel(channelId3);
if (channel != null) {
if (LurkingStore.isLurking(channel.guild_id)) {
let env17579;
const guild_id = channel.guild_id;
const title = intl.string(t["7LpysO"]);
const body = intl.string(t["5sHHoy"]);
const r59 = env17579 = {
$parent: env6619
};
env17579[0] = guild_id;
AlertActionCreators.show({
title,
body,
confirmText: intl.string(t["9VLmlZ"]),
cancelText: intl.string(t["2m+Sqk"]),
onConfirm: $mk83611(r59)
});
} else {
const message2 = MessageStore.getMessage(channelId3, messageId2);
if (message2 != null && message2.poll != null && message2.poll.answers.length !== 0) {
if (answerId2 == null) {
answerId2 = String(message2.poll.answers[0].answer_id);
}
showVotesForAnswer({
message: message2,
initialAnswerId: answerId2
});
}
}
}
};
handleUpdateVoteEditingState = function handleUpdateVoteEditingState({ channelId, messageId: messageId2, isEditing }) {
(0, updatePollState)(channelId, messageId2, (a0) => {
const r2 = {
channelId,
selectedAnswerIds: new Set(),
submitting: false,
editing: isEditing,
showResults: null
};
let showResults = a0?.showResults;
if (showResults == null) {
showResults = false;
}
r2.showResults = showResults;
return r2;
});
};
getCurrentAnswerIds = function getCurrentAnswerIds(a0) {
const message2 = MessageStore.getMessage(a0.channelId, a0.messageId);
return message2 == null ? [] : message2.reactions.flatMap((reaction) => reaction.me_vote === true ? reaction.emoji.name : []);
};
optimisticallySetAnswers = async function optimisticallySetAnswers(c26575_0) {
let c26575_1, c26575_2, c26575_3;
L13: {
let env30165;
const r31 = env6619;
c26575_3 = r31;
const r10 = env30165 = {
$parent: r31
};
c26575_2 = r10;
env30165[0] = void 0;
env30165[1] = void 0;
env30165[2] = void 0;
env30165[0] = c26575_0.channelId;
env30165[1] = c26575_0.messageId;
env30165[2] = c26575_0.answerIds;
env30165[3] = void 0;
env30165[4] = void 0;
env30165[5] = void 0;
env30165[6] = void 0;
env30165[7] = void 0;
env30165[8] = void 0;
await void 0;
const r48 = c26575_2;
env30165[3] = (0, getCurrentAnswerIds)({
channelId: env30165[0],
messageId: env30165[1]
});
env30165[4] = Module12.difference(env30165[3], env30165[2]);
env30165[5] = Module12.difference(env30165[2], env30165[3]);
env30165[6] = AuthenticationStore.getId();
c26575_1 = 0;
const r20 = env30165[4].map((item) => ({
type: "MESSAGE_REACTION_REMOVE",
id: item
}));
const r71 = [];
c26575_1 = HermesBuiltin.arraySpread(r71, r20, c26575_1);
c26575_1 = HermesBuiltin.arraySpread(r71, env30165[5].map((item) => ({
type: "MESSAGE_REACTION_ADD",
id: item
})), c26575_1);
env30165[7] = r71;
env30165[8] = Module589.Emitter.batched($mk115581(r48));
if (env30165[8] != null) {
await env30165[8];
break L13;
}
return void 0;
}
};
handlePollSubmitVote = async function handlePollSubmitVote(c26576_0) {
let anyErrorMessage, anyErrorMessage2, c26576_3, c26576_4, c26576_5, c26576_6;
let c26576_7 = 0;
const r15 = env6619;
L22: {
try {
let env30166;
c26576_5 = r15;
const r43 = env30166 = {
$parent: r15
};
c26576_4 = r43;
env30166[0] = void 0;
env30166[1] = void 0;
env30166[0] = c26576_0.channelId;
env30166[1] = c26576_0.messageId;
env30166[2] = void 0;
env30166[3] = void 0;
env30166[4] = void 0;
env30166[5] = void 0;
await void 0;
env30166[2] = ChannelStore.getChannel(env30166[0]);
if (env30166[2] != null) {
if (LurkingStore.isLurking(env30166[2].guild_id)) {
(0, showLurkingAlert)({
guildId: env30166[2].guild_id,
title: intl.string(t.Qic1FD),
body: intl.string(t["5sHHoy"])
});
} else if (GuildVerificationStore.canChatInGuild(env30166[2].guild_id)) {
env30166[3] = (0, getPollState)(env30166[0], env30166[1]);
Module38(env30166[3] != null, "Must not be able to vote without existing state!");
env30166[4] = (0, getCurrentAnswerIds)({
channelId: env30166[0],
messageId: env30166[1]
});
c26576_7 = 1;
c26576_3 = 0;
const r28 = env30166[3].selectedAnswerIds.values();
const r136 = [];
c26576_3 = HermesBuiltin.arraySpread(r136, r28, c26576_3);
env30166[5] = r136;
updatePollState(env30166[0], env30166[1], $mk115582(env6619));
await (0, optimisticallySetAnswers)({
channelId: env30166[0],
messageId: env30166[1],
answerIds: env30166[5]
});
await submitPollVote({
channelId: env30166[0],
messageId: env30166[1],
answerIds: env30166[5]
});
(0, updatePollState)(env30166[0], env30166[1], () => {});
AccessibilityAnnouncer.announce(env30166[5].length === 0 ? intl.string(t["xcvy+3"]) : intl.string(t.o20GSo));
c26576_7 = 0;
} else {
AlertActionCreators.show({
title: intl.string(t.p245wu),
body: intl.string(t["U/uodt"])
});
}
}
} catch ($e) {
const r41 = $e;
let r8 = r41;
c26576_6 = r41;
if (c26576_7 !== 0) {
while (true) {
try {
const r281 = c26576_6;
c26576_7 = 0;
env30166[6] = r281;
const r301 = {
title: intl.string(t.iufib1),
body: null
};
const r302 = env30166[6];
const getAnyErrorMessage = r302.getAnyErrorMessage;
const r305 = getAnyErrorMessage == null ? void 0 : getAnyErrorMessage.call(r302);
anyErrorMessage2 = r305;
if (r305 == null) {
anyErrorMessage2 = env30166[6].message;
}
anyErrorMessage = anyErrorMessage2;
if (anyErrorMessage == null) {
anyErrorMessage = intl.string(t.eAn6z2);
}
r301.body = anyErrorMessage;
AlertActionCreators.show(r301);
await (0, optimisticallySetAnswers)({
channelId: env30166[0],
messageId: env30166[1],
answerIds: env30166[4]
});
(0, updatePollState)(env30166[0], env30166[1], (a0) => {
if (a0 != null) {
return {
...a0,
submitting: false,
editing: false
};
}
});
break L22;
} catch (r277) {
r8 = r277;
c26576_6 = r277;
if (c26576_7 === 0) {
break;
}
}
}
}
throw r8;
}
}
};
env6619[26] = function _handleClearPollVote() {
const r8 = async (c26577_0) => {
let c26577_1, c26577_2;
L15: {
let env30167;
c26577_2 = env6619;
const r29 = env30167 = {};
c26577_1 = r29;
env30167[0] = void 0;
env30167[1] = void 0;
env30167[0] = c26577_0.channelId;
env30167[1] = c26577_0.messageId;
env30167[2] = void 0;
await void 0;
env30167[2] = ChannelStore.getChannel(env30167[0]);
if (env30167[2] == null) {
break L15;
}
const isLurking = LurkingStore.isLurking(env30167[2].guild_id);
const r60 = c26577_1;
if (isLurking) {
showLurkingAlert({
guildId: env30167[2].guild_id,
title: intl.string(t.B9QnBp),
body: intl.string(t.BVZCTn)
});
break L15;
}
(0, updatePollState)(env30167[0], env30167[1], $mk115585(r60));
return await (0, handlePollSubmitVote)({
channelId: env30167[0],
messageId: env30167[1]
});
}
};
env6619[26] = r8;
return r8.apply(this, arguments);
};
const r54 = {
handlePollAnswerTapped: function handlePollAnswerTapped({ answerId, ...rest }) {
let r33;
let allow_multiselect;
allow_multiselect = void 0;
const { channelId: channelId3, messageId: messageId2 } = rest;
const message2 = MessageStore.getMessage(channelId3, messageId2);
if (message2 != null) {
r33 = {
message: message2,
channelId: channelId3,
messageId: messageId2
};
} else {
const message3 = ReferencedMessageStore.getMessage(channelId3, messageId2);
if (message3.message == null) {
Module38(message2 != null, "Tapped on a non-existent poll message");
throw new Error();
}
r33 = {
channelId: channelId3,
messageId: messageId2,
message: message3.message
};
}
const { channelId: channelId2, messageId, message } = r33;
let r43 = computeBasicPollChatData(message);
if (r43 == null) {
r43 = {};
}
if (r43.tapShouldOpenVotersModal !== true) {
allow_multiselect = message.poll?.allow_multiselect;
(0, updatePollState)(channelId2, messageId, (a0) => {
if (a0 == null) {
const set2 = new Set([answerId]);
const answers = message.poll?.answers;
const selectedAnswerIds = set2;
const r82 = getPollVoteEventProperties(answers, selectedAnswerIds);
AppAnalyticsUtils.trackWithMetadata(AnalyticEvents.POLL_VOTE_SELECTED, {
channel_id: channelId2,
message_id: messageId,
selected_answer_ids: r82.analyticsSelectedAnswerIds,
selected_text_answers_count: r82.selectedTextAnswersCount,
selected_emoji_answers_count: r82.selectedEmojiAnswersCount
});
return {
channelId: channelId2,
selectedAnswerIds,
submitting: false,
editing: false,
showResults: false
};
}
const r0 = {
...a0
};
const set = new Set(r0.selectedAnswerIds);
r0.selectedAnswerIds = set;
if (set.has(answerId)) {
set.delete(answerId);
} else {
if (!allow_multiselect) {
for (const item of set) {
set.delete(item);
}
}
set.add(answerId);
}
const pollVoteEventProperties = getPollVoteEventProperties(message.poll?.answers, set);
AppAnalyticsUtils.trackWithMetadata(AnalyticEvents.POLL_VOTE_SELECTED, {
channel_id: channelId2,
message_id: messageId,
selected_answer_ids: pollVoteEventProperties.analyticsSelectedAnswerIds,
selected_text_answers_count: pollVoteEventProperties.selectedTextAnswersCount,
selected_emoji_answers_count: pollVoteEventProperties.selectedEmojiAnswersCount
});
return r0;
});
} else {
handleShowVotesForAnswer({
channelId: channelId2,
messageId,
answerId
});
}
},
handlePollSubmitVote,
handleUpdateVoteEditingState,
handlePollActionTapped: async function handlePollActionTapped(c26578_0) {
let c26578_1, c26578_2;
L24: {
L22: {
let env30168;
const r29 = env6619;
c26578_2 = r29;
const r10 = env30168 = {
$parent: r29
};
c26578_1 = r10;
env30168[0] = void 0;
env30168[1] = void 0;
env30168[2] = void 0;
env30168[0] = c26578_0.channelId;
env30168[1] = c26578_0.messageId;
env30168[2] = c26578_0.type;
await void 0;
const r48 = env30168[2];
if (r48 === "submit") {
await (0, handlePollSubmitVote)({
channelId: env30168[0],
messageId: env30168[1]
});
break L24;
}
if (r48 === "remove") {
const r60 = {
channelId: env30168[0],
messageId: env30168[1]
};
await $mk115586(env6619)(r60);
break L22;
}
if (r48 === "cancel") {
(0, handleUpdateVoteEditingState)({
channelId: env30168[0],
messageId: env30168[1],
isEditing: false
});
} else if (r48 === "showVotes") {
const r77 = {
channelId: env30168[0],
messageId: env30168[1]
};
$mk115587(env6619)(r77);
} else if (r48 === "showVoterDetails") {
(0, handleShowVotesForAnswer)({
channelId: env30168[0],
messageId: env30168[1]
});
} else {
Module38(false, `Unknown poll action type: ${env30168[2]}`);
}
return void 0;
}
return void 0;
}
},
createPoll: async function createPoll(c26579_0) {
let c26579_1, c26579_2, c26579_3;
let c26579_4 = 0;
const r14 = env6619;
try {
let env30169;
c26579_2 = r14;
const r36 = env30169 = {
$parent: r14
};
c26579_1 = r36;
env30169[0] = void 0;
env30169[1] = void 0;
env30169[2] = void 0;
env30169[3] = void 0;
env30169[4] = void 0;
env30169[5] = void 0;
env30169[6] = void 0;
env30169[0] = c26579_0.channel;
env30169[1] = c26579_0.question;
env30169[2] = c26579_0.answers;
env30169[3] = c26579_0.allowMultiSelect;
env30169[4] = c26579_0.duration;
env30169[5] = c26579_0.layout;
env30169[6] = c26579_0.onClose;
env30169[7] = void 0;
env30169[8] = void 0;
env30169[9] = void 0;
await void 0;
const r61 = c26579_1;
env30169[7] = UploadAttachmentStore.getUploads(env30169[0].id, DraftType.Poll);
env30169[8] = env30169[2].map($mk115588(r61));
env30169[9] = {
question: {
text: env30169[1].trim()
},
answers: env30169[8],
allow_multiselect: env30169[3],
duration: env30169[4],
layout_type: env30169[5]
};
const r21 = c26579_1;
c26579_4 = 1;
await MessageActionCreators.sendPollMessage(env30169[0].id, env30169[9], {
attachmentsToUpload: env30169[7],
onAttachmentUploadError: $mk115589(r21)
});
const r112 = env30169[6];
if (r112 != null) {
r112();
}
c26579_4 = 0;
} catch ($e) {
const r34 = $e;
let r8 = r34;
c26579_3 = r34;
if (c26579_4 !== 0) {
while (true) {
try {
const r122 = c26579_3;
c26579_4 = 0;
env30169[10] = r122;
throw (env30169[10] instanceof APIError ? env30169[10] : new APIError(env30169[10])).getAnyErrorMessage() === "poll" && env30169[10].text != null ? {
...env30169[10],
body: JSON.parse(env30169[10].text)
} : env30169[10];
} catch (r118) {
r8 = r118;
c26579_3 = r118;
if (c26579_4 === 0) {
break;
}
}
}
}
throw r8;
}
},
endPollEarly: async function endPollEarly(c26580_0) {
let c30170_0, c30170_1;
L15: {
c30170_0 = void 0;
c30170_1 = void 0;
c30170_0 = c26580_0.channelId;
c30170_1 = c26580_0.messageId;
await void 0;
if (await AlertActionCreators.confirm({
title: intl.string(t["+rfkTK"]),
body: intl.string(t.H2I1gL)
})) {
await _PollsHttpApi.endPollEarly({
channelId: c30170_0,
messageId: c30170_1
});
break L15;
}
return void 0;
}
}
};
export default r54;
export { handleShowVotesForAnswer };