Skip to content

Commit

Permalink
support usergroups & subscribing w/o office arg
Browse files Browse the repository at this point in the history
  • Loading branch information
VoxBorealis committed Aug 7, 2023
1 parent e9def27 commit f090d50
Show file tree
Hide file tree
Showing 7 changed files with 454 additions and 420 deletions.
5 changes: 0 additions & 5 deletions app/src/actionFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,4 @@ exports.enableActionFunctions = ({ app, userCache }) => {
home.updateDefaultSettingsView(client, body.user.id);
await ack();
});

app.action("register_from_message", async ({ body, ack, client }) => {
await ack();
console.log("register from message click");
});
};
5 changes: 1 addition & 4 deletions app/src/controllers/db.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,7 @@ exports.getAllDefaultOfficeRegistrationsForWeekday = async (weekday, officeId) =
},
},
});
return registrations.map((s) => ({
slackId: s.dataValues.slackId,
officeId: s.dataValues.defaultsignup[0].dataValues.OfficeId,
}));
return registrations.map((s) => s.dataValues.slackId);
} catch (error) {
console.log("Error while finding default registrations:", error);
return null;
Expand Down
8 changes: 2 additions & 6 deletions app/src/databaseService.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,9 @@ const getRegistrationsFor = async (date, officeId) => {
const registrations = await db.getAllRegistrationsForDate(date);
registrations.forEach((obj) => {
if (obj.status === true && (officeId ? obj.officeId === officeId : true)) {
result.add({ slackId: obj.slackId, officeId: obj.officeId });
result.add(obj.slackId);
} else {
result.forEach((entry) => {
if (entry.slackId === obj.slackId) {
result.delete(entry);
}
});
result.delete(obj.slackId);
}
});
return Array.from(result);
Expand Down
25 changes: 18 additions & 7 deletions app/src/responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,17 @@ const correctVerbForm = (date, peopleCnt) => {
return verb;
};

const scheduledMessageNotificationMsg = (office, nPeople) => {
const message = `Tänään toimistolla ${formatOffice(office)} on ${nPeople} ${
nPeople === 1 ? "henkilö" : "henkilöä"
}.`;
/**
* Formats a notification message to be sent with the scheduled message block.
* @param {Object} office Office object.
* @param {Number} nPeople Number of people at the office.
* @param {String} [usergroupMention] Optional name of the usergroup.
* @returns {String} A message containing the status of the number of people at the office.
*/
const scheduledMessageNotificationMsg = (office, nPeople, usergroupMention) => {
const message = `Tänään toimistolla ${formatOffice(office)} ${
usergroupMention ? `tiimistä ${usergroupMention}` : ""
} on ${nPeople} ${nPeople === 1 ? "henkilö" : "henkilöä"}.`;
return message;
};

Expand Down Expand Up @@ -135,7 +142,9 @@ const registrationListWithUsergroup = (
) => {
if (registrations.length === 0) return nobodyAtOfficeFromTeam(date, usergroupMention);
const verb = correctVerbForm(date, registrations.length);
let response = `${atDate(date)} tiimistä ${usergroupMention} ${verb} toimistolla:\n`;
let response = `${atDate(date)} tiimistä ${usergroupMention} ${verb} toimistolla: _(${
registrations.length
})_\n\n`;
registrations = formatUserIdList(registrations, userFormatter);
for (const user of registrations) {
response += `${user}\n`;
Expand All @@ -149,8 +158,10 @@ const registrationListWithUsergroup = (
* @param {Object} office
*/
const automatedMessageRescheduled = (time, office) =>
`Ajastettu viesti tilattu kanavalle kello ${time}${
office ? ` sisältäen toimiston '${office.officeName}' ilmoittautumiset.` : "."
`Ajastettu viesti tilattu kanavalle kello ${time} ${
office
? `sisältäen toimiston '${office.officeName}' ilmoittautumiset.`
: "sisältäen kaikkien toimistojen ilmoittautumiset."
}`;

/**
Expand Down
91 changes: 50 additions & 41 deletions app/src/scheduler/scheduledMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ const library = require("../responses");
const helper = require("../helperFunctions");
const service = require("../databaseService");
const { generatePlaintextString } = require("../userCache");
const { getRegistrationsForTheDayBlock } = require("../ui/customBlocks");
const {
getRegistrationsForTheDayBlock,
getRegistrationsForTheDayBlockWithUG,
} = require("../ui/customBlocks");

/**
* Sends the list of registered users to the given channel.
Expand All @@ -13,19 +16,7 @@ const { getRegistrationsForTheDayBlock } = require("../ui/customBlocks");
* @param {string} channelId - Slack channel id.
* @param {string} date - Date string in the ISO date format.
*/
const postRegistrations = async (app, registrations, channelId, date) => {
const messageWithoutMentions = library.registrationList(
DateTime.now(),
registrations,
generatePlaintextString,
);
const messageId = (await helper.postMessage(app, channelId, messageWithoutMentions)).ts;
if (messageId) {
service.addScheduledMessage(messageId, date, channelId);
}
};

const postRegistrationsAsBlock = async (app, registrations, channelId, date, officeId) => {
const postRegistrations = async (app, registrations, channelId, date, officeId) => {
const office = await service.getOffice(officeId);
const registrationsBlock = getRegistrationsForTheDayBlock(date, registrations, office);
const fallbackMessage = library.scheduledMessageNotificationMsg(office, registrations.length);
Expand Down Expand Up @@ -53,14 +44,24 @@ const postRegistrationsWithUsergroup = async (
usergroups,
usergroupId,
date,
officeId,
) => {
const messageWithoutMentions = library.registrationListWithUsergroup(
DateTime.now(),
const office = await service.getOffice(officeId);
const registrationsBlock = getRegistrationsForTheDayBlockWithUG(
date,
registrations,
office,
usergroups.generatePlaintextString(usergroupId),
generatePlaintextString,
);
const messageId = (await helper.postMessage(app, channelId, messageWithoutMentions)).ts;
const fallbackMessage = library.scheduledMessageNotificationMsg(
office,
registrations.length,
usergroups.generatePlaintextString(usergroupId),
);
const messageId = (
await helper.postBlockMessage(app, channelId, fallbackMessage, registrationsBlock)
).ts;
if (messageId) {
service.addScheduledMessage(messageId, date, channelId, usergroupId);
}
Expand All @@ -72,36 +73,44 @@ const postRegistrationsWithUsergroup = async (
* and posts separate messages for each.
* @param {*} app - Slack app instance.
* @param {string} channelId - ID of the channel where messages will be posted
* @param {string} [officeId] - Optional ID of a office, will only include registrations
* from this office, otherwise includes all.
* @param {*} usergroups - usergroups cache instance
* @param {*} userCache - userCache instance
* @returns
*/
const sendScheduledMessage = async (app, channelId, officeId, usergroups) => {
console.log("delivering scheduled posts");
const date = DateTime.now();
const registrations = await service.getRegistrationsFor(date.toISODate(), officeId);
const officeIds = [];
officeId
? officeIds.push(officeId)
: officeIds.push(...(await service.getAllOffices()).map((office) => office.id));
// Send a separate message for each office if no office argument was given.
for (officeId of officeIds) {
const registrations = await service.getRegistrationsFor(date.toISODate(), officeId);

const usergroupIds = usergroups.getUsergroupsForChannel(channelId);
// No Slack user groups are added to this channel.
// Send normal message containing everyone that is registered.
if (usergroupIds.length === 0) {
return postRegistrationsAsBlock(app, registrations, channelId, date, officeId);
} else {
// Send a separate list of registered users from each
// Slack user group in this channel
usergroupIds.forEach(async (usergroupId) => {
const filteredRegistrations = registrations.filter((userId) =>
usergroups.isUserInUsergroup(userId, usergroupId),
);
return postRegistrationsWithUsergroup(
app,
filteredRegistrations,
channelId,
usergroups,
usergroupId,
date,
);
});
const usergroupIds = usergroups.getUsergroupsForChannel(channelId);
// No Slack user groups are added to this channel.
// Send normal message containing everyone that is registered.
if (usergroupIds.length === 0) {
postRegistrations(app, registrations, channelId, date, officeId);
} else {
// Send a separate list of registered users from each
// Slack user group in this channel
usergroupIds.forEach(async (usergroupId) => {
const filteredRegistrations = registrations.filter((userId) =>
usergroups.isUserInUsergroup(userId, usergroupId),
);
postRegistrationsWithUsergroup(
app,
filteredRegistrations,
channelId,
usergroups,
usergroupId,
date,
officeId,
);
});
}
}
};

Expand Down
38 changes: 30 additions & 8 deletions app/src/ui/customBlocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,19 +358,40 @@ const getNoOfficesBlock = async (admin) => {
return noOfficesBlock;
};

/**
* Creates a Slack block message containing the office name as header
* and the names of the registered users for the day.
* @param {{}} date Luxon date object.
* @param {[]} registrations List of Slack user IDs.
* @param {{}} office Office object.
* @returns {{}} Scheduled message formated in Slack Block Kit.
*/
const getRegistrationsForTheDayBlock = (date, registrations, office) => {
const registrationsforTheDayBlock = [];
const registrationList = library.registrationList(
const registrationList = library.registrationList(date, registrations, generatePlaintextString);
registrationsforTheDayBlock.push(header(formatOffice(office)), mrkdwn(registrationList));

return registrationsforTheDayBlock;
};

/**
* Creates a Slack block message containing the office name as header
* and the names of the registered users for the day and usergroup.
* @param {{}} date Luxon date object.
* @param {[]} registrations List of Slack user IDs.
* @param {{}} office Office object.
* @param {String} usergroupMention Name of the usergroup.
* @returns {{}} Scheduled message formated in Slack Block Kit.
*/
const getRegistrationsForTheDayBlockWithUG = (date, registrations, office, usergroupMention) => {
const registrationsforTheDayBlock = [];
const registrationList = library.registrationListWithUsergroup(
date,
registrations.map((registration) => registration.slackId),
registrations,
usergroupMention,
generatePlaintextString,
);
registrationsforTheDayBlock.push(
header(formatOffice(office)),
mrkdwn(registrationList),
divider(),
actions([button("Ilmoittaudu", "register_from_message")]),
);
registrationsforTheDayBlock.push(header(formatOffice(office)), mrkdwn(registrationList));

return registrationsforTheDayBlock;
};
Expand All @@ -385,4 +406,5 @@ module.exports = {
getNoOfficesBlock,
stylizeRegisterButtons,
getRegistrationsForTheDayBlock,
getRegistrationsForTheDayBlockWithUG,
};
Loading

0 comments on commit f090d50

Please sign in to comment.