2017-10-12 10:00:28 +08:00
|
|
|
import Presentations from '/imports/api/presentations';
|
2019-08-01 03:10:41 +08:00
|
|
|
import { Slides, SlidePositions } from '/imports/api/slides';
|
2021-05-27 01:52:55 +08:00
|
|
|
import PollService from '/imports/ui/components/poll/service';
|
2022-10-18 21:23:38 +08:00
|
|
|
import { safeMatch } from '/imports/utils/string-utils';
|
2016-11-12 03:02:46 +08:00
|
|
|
|
2021-06-18 22:02:09 +08:00
|
|
|
const POLL_SETTINGS = Meteor.settings.public.poll;
|
|
|
|
const MAX_CUSTOM_FIELDS = POLL_SETTINGS.maxCustom;
|
2023-07-13 20:58:05 +08:00
|
|
|
const MAX_CHAR_LIMIT = POLL_SETTINGS.maxTypedAnswerLength;
|
2021-06-18 22:02:09 +08:00
|
|
|
|
2021-11-19 05:52:20 +08:00
|
|
|
const getCurrentPresentation = (podId) => Presentations.findOne({
|
2018-04-10 13:24:04 +08:00
|
|
|
podId,
|
2017-07-25 02:46:53 +08:00
|
|
|
current: true,
|
2016-11-12 03:02:46 +08:00
|
|
|
});
|
|
|
|
|
2019-02-21 01:17:17 +08:00
|
|
|
const isPresentationDownloadable = (podId) => {
|
|
|
|
const currentPresentation = getCurrentPresentation(podId);
|
|
|
|
if (!currentPresentation) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return currentPresentation.downloadable;
|
|
|
|
};
|
|
|
|
|
2018-04-10 13:24:04 +08:00
|
|
|
const getCurrentSlide = (podId) => {
|
|
|
|
const currentPresentation = getCurrentPresentation(podId);
|
2016-11-12 03:02:46 +08:00
|
|
|
|
|
|
|
if (!currentPresentation) {
|
|
|
|
return null;
|
2016-05-31 06:07:02 +08:00
|
|
|
}
|
|
|
|
|
2019-03-12 00:21:12 +08:00
|
|
|
return Slides.findOne({
|
|
|
|
podId,
|
|
|
|
presentationId: currentPresentation.id,
|
|
|
|
current: true,
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
meetingId: 0,
|
|
|
|
thumbUri: 0,
|
|
|
|
txtUri: 0,
|
2017-09-19 08:18:20 +08:00
|
|
|
},
|
2019-03-12 00:21:12 +08:00
|
|
|
});
|
2016-11-12 03:02:46 +08:00
|
|
|
};
|
2016-07-16 04:45:54 +08:00
|
|
|
|
2023-04-14 04:03:06 +08:00
|
|
|
const getSlidesLength = (podId) => getCurrentPresentation(podId)?.pages?.length || 0;
|
2023-04-05 23:37:56 +08:00
|
|
|
|
2019-08-01 03:10:41 +08:00
|
|
|
const getSlidePosition = (podId, presentationId, slideId) => SlidePositions.findOne({
|
|
|
|
podId,
|
|
|
|
presentationId,
|
|
|
|
id: slideId,
|
|
|
|
});
|
|
|
|
|
2019-02-22 05:01:39 +08:00
|
|
|
const currentSlidHasContent = () => {
|
|
|
|
const currentSlide = getCurrentSlide('DEFAULT_PRESENTATION_POD');
|
|
|
|
if (!currentSlide) return false;
|
|
|
|
|
2019-03-12 00:21:12 +08:00
|
|
|
const {
|
|
|
|
content,
|
|
|
|
} = currentSlide;
|
2019-02-22 05:01:39 +08:00
|
|
|
|
|
|
|
return !!content.length;
|
|
|
|
};
|
|
|
|
|
2023-05-16 00:01:53 +08:00
|
|
|
// Utility function to escape special characters for regex
|
|
|
|
const escapeRegExp = (string) => string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
|
|
|
|
|
|
// Function to create a regex pattern
|
|
|
|
const createPattern = (values) => new RegExp(`.*(${escapeRegExp(values[0])}\\/${escapeRegExp(values[1])}|${escapeRegExp(values[1])}\\/${escapeRegExp(values[0])}).*`, 'gmi');
|
|
|
|
|
2021-01-24 09:22:40 +08:00
|
|
|
const parseCurrentSlideContent = (yesValue, noValue, abstentionValue, trueValue, falseValue) => {
|
2021-11-19 05:52:20 +08:00
|
|
|
const { pollTypes } = PollService;
|
2019-02-22 05:01:39 +08:00
|
|
|
const currentSlide = getCurrentSlide('DEFAULT_PRESENTATION_POD');
|
|
|
|
const quickPollOptions = [];
|
|
|
|
if (!currentSlide) return quickPollOptions;
|
|
|
|
|
2021-03-05 02:00:21 +08:00
|
|
|
let {
|
2019-03-12 00:21:12 +08:00
|
|
|
content,
|
|
|
|
} = currentSlide;
|
2019-02-22 05:01:39 +08:00
|
|
|
|
2023-07-24 09:09:45 +08:00
|
|
|
let lines = content.split('\n');
|
|
|
|
let questions = [];
|
|
|
|
let questionLines = [];
|
|
|
|
|
|
|
|
for (let line of lines) {
|
|
|
|
let startsWithCapital = /^[A-Z]/.test(line);
|
|
|
|
let isEndOfQuestion = /\?$/.test(line);
|
|
|
|
|
|
|
|
if (startsWithCapital) {
|
|
|
|
if (questionLines.length > 0) {
|
|
|
|
questions.push(questionLines.join(' '));
|
|
|
|
}
|
|
|
|
questionLines = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
questionLines.push(line.trim());
|
|
|
|
|
|
|
|
if (isEndOfQuestion) {
|
|
|
|
questions.push(questionLines.join(' '));
|
|
|
|
questionLines = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (questionLines.length > 0) {
|
|
|
|
questions.push(questionLines.join(' '));
|
|
|
|
}
|
|
|
|
|
|
|
|
const question = questions.filter(q => /^[A-Z].*\?$/.test(q?.trim()));
|
2022-10-12 03:11:04 +08:00
|
|
|
|
2023-03-21 00:18:42 +08:00
|
|
|
if (question?.length > 0) {
|
2023-05-16 00:35:32 +08:00
|
|
|
question[0] = question[0]?.replace(/\n/g, ' ');
|
2023-03-21 00:18:42 +08:00
|
|
|
const urlRegex = /\bhttps?:\/\/\S+\b/g;
|
|
|
|
const hasUrl = safeMatch(urlRegex, question[0], '');
|
|
|
|
if (hasUrl.length > 0) question.pop();
|
|
|
|
}
|
|
|
|
|
2022-10-12 03:11:04 +08:00
|
|
|
const doubleQuestionRegex = /\?{2}/gm;
|
2022-10-19 04:13:33 +08:00
|
|
|
const doubleQuestion = safeMatch(doubleQuestionRegex, content, false);
|
2022-10-12 03:11:04 +08:00
|
|
|
|
2023-05-16 00:01:53 +08:00
|
|
|
const yesNoPatt = createPattern([yesValue, noValue]);
|
2022-10-31 00:12:37 +08:00
|
|
|
const hasYN = safeMatch(yesNoPatt, content, false);
|
|
|
|
|
2023-05-16 00:01:53 +08:00
|
|
|
const trueFalsePatt = createPattern([trueValue, falseValue]);
|
2023-03-15 21:36:40 +08:00
|
|
|
const hasTF = safeMatch(trueFalsePatt, content, false);
|
|
|
|
|
2023-02-06 22:14:38 +08:00
|
|
|
const pollRegex = /\b[1-9A-Ia-i][.)] .*/g;
|
2022-10-18 21:23:38 +08:00
|
|
|
let optionsPoll = safeMatch(pollRegex, content, []);
|
2022-10-18 22:45:47 +08:00
|
|
|
const optionsWithLabels = [];
|
2023-02-01 00:34:23 +08:00
|
|
|
|
|
|
|
if (hasYN) {
|
2023-02-01 02:31:59 +08:00
|
|
|
optionsPoll = ['yes', 'no'];
|
2023-02-01 00:34:23 +08:00
|
|
|
}
|
|
|
|
|
2022-10-19 01:13:09 +08:00
|
|
|
if (optionsPoll) {
|
|
|
|
optionsPoll = optionsPoll.map((opt) => {
|
2022-10-18 07:43:04 +08:00
|
|
|
const formattedOpt = opt.substring(0, MAX_CHAR_LIMIT);
|
|
|
|
optionsWithLabels.push(formattedOpt);
|
2022-10-18 22:45:47 +08:00
|
|
|
return `\r${opt[0]}.`;
|
2022-10-19 01:13:09 +08:00
|
|
|
});
|
|
|
|
}
|
2019-10-25 02:46:19 +08:00
|
|
|
|
2019-02-22 05:01:39 +08:00
|
|
|
optionsPoll.reduce((acc, currentValue) => {
|
|
|
|
const lastElement = acc[acc.length - 1];
|
|
|
|
|
|
|
|
if (!lastElement) {
|
|
|
|
acc.push({
|
|
|
|
options: [currentValue],
|
|
|
|
});
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
2019-03-12 00:21:12 +08:00
|
|
|
const {
|
|
|
|
options,
|
|
|
|
} = lastElement;
|
2019-02-22 05:01:39 +08:00
|
|
|
|
|
|
|
const lastOption = options[options.length - 1];
|
|
|
|
|
|
|
|
const isLastOptionInteger = !!parseInt(lastOption.charAt(1), 10);
|
|
|
|
const isCurrentValueInteger = !!parseInt(currentValue.charAt(1), 10);
|
|
|
|
|
|
|
|
if (isLastOptionInteger === isCurrentValueInteger) {
|
|
|
|
if (currentValue.toLowerCase().charCodeAt(1) > lastOption.toLowerCase().charCodeAt(1)) {
|
|
|
|
options.push(currentValue);
|
|
|
|
} else {
|
|
|
|
acc.push({
|
|
|
|
options: [currentValue],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
acc.push({
|
|
|
|
options: [currentValue],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return acc;
|
2019-03-12 00:21:12 +08:00
|
|
|
}, []).filter(({
|
|
|
|
options,
|
2022-10-18 22:45:47 +08:00
|
|
|
}) => options.length > 1 && options.length < 10).forEach((p) => {
|
|
|
|
const poll = p;
|
2022-10-13 01:34:40 +08:00
|
|
|
if (doubleQuestion) poll.multiResp = true;
|
2021-06-18 22:02:09 +08:00
|
|
|
if (poll.options.length <= 5 || MAX_CUSTOM_FIELDS <= 5) {
|
2021-11-19 05:52:20 +08:00
|
|
|
const maxAnswer = poll.options.length > MAX_CUSTOM_FIELDS
|
2021-06-18 22:02:09 +08:00
|
|
|
? MAX_CUSTOM_FIELDS
|
2021-11-19 05:52:20 +08:00
|
|
|
: poll.options.length;
|
2021-06-18 22:02:09 +08:00
|
|
|
quickPollOptions.push({
|
|
|
|
type: `${pollTypes.Letter}${maxAnswer}`,
|
|
|
|
poll,
|
2021-11-19 05:52:20 +08:00
|
|
|
});
|
2021-06-18 22:02:09 +08:00
|
|
|
} else {
|
|
|
|
quickPollOptions.push({
|
|
|
|
type: pollTypes.Custom,
|
|
|
|
poll,
|
2021-11-19 05:52:20 +08:00
|
|
|
});
|
2021-06-18 22:02:09 +08:00
|
|
|
}
|
|
|
|
});
|
2019-03-12 00:21:12 +08:00
|
|
|
|
2023-03-15 21:36:40 +08:00
|
|
|
if (question.length > 0 && optionsPoll.length === 0 && !doubleQuestion && !hasYN && !hasTF) {
|
2022-10-12 03:11:04 +08:00
|
|
|
quickPollOptions.push({
|
2022-10-18 22:45:47 +08:00
|
|
|
type: 'R-',
|
2022-10-12 03:11:04 +08:00
|
|
|
poll: {
|
2022-10-18 22:45:47 +08:00
|
|
|
question: question[0],
|
|
|
|
},
|
2022-10-12 03:11:04 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-05 02:00:21 +08:00
|
|
|
if (quickPollOptions.length > 0) {
|
|
|
|
content = content.replace(new RegExp(pollRegex), '');
|
|
|
|
}
|
|
|
|
|
2021-05-27 01:52:55 +08:00
|
|
|
const ynPoll = PollService.matchYesNoPoll(yesValue, noValue, content);
|
|
|
|
const ynaPoll = PollService.matchYesNoAbstentionPoll(yesValue, noValue, abstentionValue, content);
|
|
|
|
const tfPoll = PollService.matchTrueFalsePoll(trueValue, falseValue, content);
|
2021-03-05 02:00:21 +08:00
|
|
|
|
2021-11-19 05:52:20 +08:00
|
|
|
ynPoll.forEach((poll) => quickPollOptions.push({
|
2021-05-27 01:52:55 +08:00
|
|
|
type: pollTypes.YesNo,
|
2019-03-12 00:21:12 +08:00
|
|
|
poll,
|
|
|
|
}));
|
|
|
|
|
2021-11-19 05:52:20 +08:00
|
|
|
ynaPoll.forEach((poll) => quickPollOptions.push({
|
2021-05-27 01:52:55 +08:00
|
|
|
type: pollTypes.YesNoAbstention,
|
2021-01-24 09:22:40 +08:00
|
|
|
poll,
|
|
|
|
}));
|
|
|
|
|
2021-11-19 05:52:20 +08:00
|
|
|
tfPoll.forEach((poll) => quickPollOptions.push({
|
2021-05-27 01:52:55 +08:00
|
|
|
type: pollTypes.TrueFalse,
|
2019-03-12 00:21:12 +08:00
|
|
|
poll,
|
|
|
|
}));
|
|
|
|
|
2022-10-18 22:45:47 +08:00
|
|
|
const pollQuestion = (question?.length > 0 && question[0]?.replace(/ *\([^)]*\) */g, '')) || '';
|
2022-10-19 01:13:09 +08:00
|
|
|
|
2019-03-12 00:21:12 +08:00
|
|
|
return {
|
|
|
|
slideId: currentSlide.id,
|
|
|
|
quickPollOptions,
|
2022-10-19 01:13:09 +08:00
|
|
|
optionsWithLabels,
|
|
|
|
pollQuestion,
|
2019-03-12 00:21:12 +08:00
|
|
|
};
|
2019-02-22 05:01:39 +08:00
|
|
|
};
|
|
|
|
|
2016-05-31 06:07:02 +08:00
|
|
|
export default {
|
2016-11-12 03:02:46 +08:00
|
|
|
getCurrentSlide,
|
2019-08-01 03:10:41 +08:00
|
|
|
getSlidePosition,
|
2019-02-21 01:17:17 +08:00
|
|
|
isPresentationDownloadable,
|
2019-02-22 05:01:39 +08:00
|
|
|
currentSlidHasContent,
|
|
|
|
parseCurrentSlideContent,
|
2019-05-10 01:44:54 +08:00
|
|
|
getCurrentPresentation,
|
2023-04-05 23:37:56 +08:00
|
|
|
getSlidesLength,
|
2016-05-31 06:07:02 +08:00
|
|
|
};
|