Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,23 @@
"image-push": "docker push gcr.io/contenta-ai/slack-poll:latest"
},
"dependencies": {
"@slack/bolt": "^3.8.1",
"dotenv": "^10.0.0",
"moment": "^2.29.1",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moment is being deprecated, please use the successor, date-fns.

"moment-timezone": "^0.5.34",
"reflect-metadata": "^0.1.13",
"sqlite3": "^5.0.2",
"typeorm": "^0.2.41",
"reflect-metadata": "^0.1.13",
"yargs": "^17.2.1",
"@slack/bolt": "^3.8.1",
"uuid": "^8.3.2",
"dotenv": "^10.0.0"
"yargs": "^17.2.1"
},
"devDependencies": {
"typescript": "^4.5.2",
"@types/node": "^16.11.10",
"@types/sqlite3": "^3.1.7",
"ts-node": "^10.4.0",
"nodemon": "^2.0.15",
"@types/uuid": "^8.3.3",
"@types/yargs": "^17.0.7",
"@types/uuid": "^8.3.3"
"nodemon": "^2.0.15",
"ts-node": "^10.4.0",
"typescript": "^4.5.2"
}
}
}
13 changes: 11 additions & 2 deletions src/bot/create.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { SectionBlock } from "@slack/types"
import moment from "moment"
import { Moment } from "moment"
import { v4 } from "uuid"
import { OptionEntity } from "../entities/option"
import { PollEntity } from "../entities/poll"
Expand All @@ -10,10 +12,12 @@ export async function createPoll(
channelId,
optionTexts,
userId,
deadlineUTC,
}: {
channelId: string,
optionTexts: string[],
userId,
userId: string,
deadlineUTC: Moment,
}
) {
const app = getSlackApp()
Expand All @@ -39,8 +43,13 @@ export async function createPoll(
userId,
options,
voteRights,
deadline: deadlineUTC.toDate(),
})
await poll.save()

const expirationDateUTCStr = deadlineUTC.format("ddd, MMM DD YYYY, hh:mm A [GMT]")
const expirationTimeToNow = moment.utc().to(deadlineUTC)
const expirationDateUTCStrWithToNow = `${expirationDateUTCStr} (${expirationTimeToNow})`

await app.client.chat.postMessage({
channel: channelId,
Expand All @@ -50,7 +59,7 @@ export async function createPoll(
type: "section",
text: {
type: "mrkdwn",
text: `<@${userId}> has started a new poll!`
text: `<@${userId}> has started a new poll!\nEnds on ${expirationDateUTCStrWithToNow}`,
}
},
...options.map(
Expand Down
15 changes: 15 additions & 0 deletions src/bot/form.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getSlackApp } from "../slack"
import { createPoll } from "./create"
import moment from "moment-timezone"

const app = getSlackApp()

Expand All @@ -15,15 +16,29 @@ app.view(/.*/, async ({ view, ack, body: { user } }) => {
.filter(line => line.length > 0)

const channel = view.state.values.channel.channel.selected_conversation

const userInfo = await app.client.users.info({user: user.id})
if(!userInfo.ok) {
return
}

const deadlineDate = view.state.values.deadlineDate.deadlineDate.selected_date
const deadlineTime = view.state.values.deadlineTime.deadlineTime.selected_time
const deadlineUTC = moment.tz(`${deadlineDate} ${deadlineTime}`, userInfo?.user.tz).utc()

if (optionTexts.length < 2) {
return
}

if(moment.utc() > deadlineUTC) {
return
}

await createPoll({
channelId: channel,
optionTexts,
userId: user.id,
deadlineUTC,
})

await ack()
Expand Down
48 changes: 47 additions & 1 deletion src/bot/global-shortcut.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { getSlackApp } from "../slack"
import moment from "moment-timezone"

const app = getSlackApp()

app.shortcut("poll/create-poll-form", async ({ shortcut, ack }) => {
app.shortcut("poll/create-poll-form", async ({ shortcut, ack, body: { user } }) => {
if (shortcut.type !== 'shortcut') {
return
}

const userInfo = await app.client.users.info({user: user.id})
if(!userInfo.ok) {
return
}

const momentTomorrowNextHourUTC = moment.tz(userInfo?.user.tz).add(1, "day").add(1, "hour").startOf('hour')
const initialDateStr = momentTomorrowNextHourUTC.format("YYYY-MM-DD")
const initialTimeStr = momentTomorrowNextHourUTC.format("HH:mm")

await app.client.views.open({
trigger_id: shortcut.trigger_id,
view: {
Expand Down Expand Up @@ -51,6 +61,42 @@ app.shortcut("poll/create-poll-form", async ({ shortcut, ack }) => {
initial_value: "Option 1\nOption 2",
},
},
{
block_id: 'deadlineDate',
type: "input",
element: {
type: "datepicker",
initial_date: initialDateStr,
placeholder: {
type: "plain_text",
text: "Select a date",
emoji: true,
},
action_id: "deadlineDate",
},
label: {
type: "plain_text",
text: "Deadline date",
},
},
{
block_id: 'deadlineTime',
type: "input",
element: {
type: "timepicker",
initial_time: initialTimeStr,
placeholder: {
type: "plain_text",
text: "Select time",
emoji: true,
},
action_id: "deadlineTime",
},
label: {
type: "plain_text",
text: "Deadline time",
},
}
],
},
})
Expand Down
4 changes: 3 additions & 1 deletion src/bot/message-shortcut.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { getSlackApp } from "../slack"
import { createPoll } from "./create"
import moment from "moment"

const app = getSlackApp()

app.shortcut("poll/create-poll", async ({ shortcut, ack }) => {
if (shortcut.type !== 'message_action') {
return
}

const message = shortcut.message
if (message.type !== 'message') {
return
Expand Down Expand Up @@ -44,6 +45,7 @@ app.shortcut("poll/create-poll", async ({ shortcut, ack }) => {
channelId: shortcut.channel.id,
optionTexts,
userId: shortcut.user.id,
deadlineUTC: moment.utc(),
})

await ack()
Expand Down
27 changes: 26 additions & 1 deletion src/bot/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ app.action(/^poll\/.*\/send$/, async ({ action, ack, body }) => {
if (action.type !== 'button' || !('message' in body)) {
return
}

const optionId = action.value
const userId = body.user.id

Expand All @@ -20,6 +20,31 @@ app.action(/^poll\/.*\/send$/, async ({ action, ack, body }) => {

const poll = option.poll

if(new Date() > poll.deadline) {
body.message.blocks = [
body.message.blocks[0],
{
"type": "context",
"elements": [
{
"type": "plain_text",
"text": "The poll has expired.",
"emoji": true,
},
],
},
body.message.blocks[body.message.blocks.length - 1],
]

await app.client.chat.update({
channel: body.channel.id,
...body.message,
})

await ack()
return
}

const voteRight = await VoteRightEntity.findOne({
where: {
poll,
Expand Down
6 changes: 6 additions & 0 deletions src/entities/poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export class PollEntity extends BaseEntity {
})
userId: string

@Column('datetime', {
name: 'deadline',
nullable: false,
})
deadline: Date

@OneToMany(() => OptionEntity, option => option.poll, { cascade: true })
options: OptionEntity[]

Expand Down
52 changes: 52 additions & 0 deletions src/migrations/1639772969440-AddDeadlineColumnInPollTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {MigrationInterface, QueryRunner} from "typeorm";

export class AddDeadlineColumnInPollTable1639772969440 implements MigrationInterface {
name = 'AddDeadlineColumnInPollTable1639772969440'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE "temporary_vote" ("option_id" varchar NOT NULL, "vote_right_id" varchar PRIMARY KEY NOT NULL, CONSTRAINT "FK_ce69ed4c96964be74d3d57e89cb" FOREIGN KEY ("vote_right_id") REFERENCES "vote_right" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT, CONSTRAINT "FK_d17980c91005358383b7ad59ab0" FOREIGN KEY ("option_id") REFERENCES "option" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT)`);
await queryRunner.query(`INSERT INTO "temporary_vote"("option_id", "vote_right_id") SELECT "option_id", "vote_right_id" FROM "vote"`);
await queryRunner.query(`DROP TABLE "vote"`);
await queryRunner.query(`ALTER TABLE "temporary_vote" RENAME TO "vote"`);
await queryRunner.query(`CREATE TABLE "temporary_poll" ("id" varchar PRIMARY KEY NOT NULL, "user_id" varchar NOT NULL, "deadline" datetime NOT NULL)`);
await queryRunner.query(`INSERT INTO "temporary_poll"("id", "user_id") SELECT "id", "user_id" FROM "poll"`);
await queryRunner.query(`DROP TABLE "poll"`);
await queryRunner.query(`ALTER TABLE "temporary_poll" RENAME TO "poll"`);
await queryRunner.query(`CREATE TABLE "temporary_vote" ("option_id" varchar NOT NULL, "vote_right_id" varchar PRIMARY KEY NOT NULL)`);
await queryRunner.query(`INSERT INTO "temporary_vote"("option_id", "vote_right_id") SELECT "option_id", "vote_right_id" FROM "vote"`);
await queryRunner.query(`DROP TABLE "vote"`);
await queryRunner.query(`ALTER TABLE "temporary_vote" RENAME TO "vote"`);
await queryRunner.query(`CREATE TABLE "temporary_vote" ("option_id" varchar NOT NULL, "vote_right_id" varchar PRIMARY KEY NOT NULL, CONSTRAINT "UQ_1f4ba94e11f0f6afec9cb013d4e" UNIQUE ("vote_right_id"))`);
await queryRunner.query(`INSERT INTO "temporary_vote"("option_id", "vote_right_id") SELECT "option_id", "vote_right_id" FROM "vote"`);
await queryRunner.query(`DROP TABLE "vote"`);
await queryRunner.query(`ALTER TABLE "temporary_vote" RENAME TO "vote"`);
await queryRunner.query(`CREATE TABLE "temporary_vote" ("option_id" varchar NOT NULL, "vote_right_id" varchar PRIMARY KEY NOT NULL, CONSTRAINT "UQ_1f4ba94e11f0f6afec9cb013d4e" UNIQUE ("vote_right_id"), CONSTRAINT "FK_d17980c91005358383b7ad59ab0" FOREIGN KEY ("option_id") REFERENCES "option" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT, CONSTRAINT "FK_ce69ed4c96964be74d3d57e89cb" FOREIGN KEY ("vote_right_id") REFERENCES "vote_right" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT)`);
await queryRunner.query(`INSERT INTO "temporary_vote"("option_id", "vote_right_id") SELECT "option_id", "vote_right_id" FROM "vote"`);
await queryRunner.query(`DROP TABLE "vote"`);
await queryRunner.query(`ALTER TABLE "temporary_vote" RENAME TO "vote"`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "vote" RENAME TO "temporary_vote"`);
await queryRunner.query(`CREATE TABLE "vote" ("option_id" varchar NOT NULL, "vote_right_id" varchar PRIMARY KEY NOT NULL, CONSTRAINT "UQ_1f4ba94e11f0f6afec9cb013d4e" UNIQUE ("vote_right_id"))`);
await queryRunner.query(`INSERT INTO "vote"("option_id", "vote_right_id") SELECT "option_id", "vote_right_id" FROM "temporary_vote"`);
await queryRunner.query(`DROP TABLE "temporary_vote"`);
await queryRunner.query(`ALTER TABLE "vote" RENAME TO "temporary_vote"`);
await queryRunner.query(`CREATE TABLE "vote" ("option_id" varchar NOT NULL, "vote_right_id" varchar PRIMARY KEY NOT NULL)`);
await queryRunner.query(`INSERT INTO "vote"("option_id", "vote_right_id") SELECT "option_id", "vote_right_id" FROM "temporary_vote"`);
await queryRunner.query(`DROP TABLE "temporary_vote"`);
await queryRunner.query(`ALTER TABLE "vote" RENAME TO "temporary_vote"`);
await queryRunner.query(`CREATE TABLE "vote" ("option_id" varchar NOT NULL, "vote_right_id" varchar PRIMARY KEY NOT NULL, CONSTRAINT "FK_ce69ed4c96964be74d3d57e89cb" FOREIGN KEY ("vote_right_id") REFERENCES "vote_right" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT)`);
await queryRunner.query(`INSERT INTO "vote"("option_id", "vote_right_id") SELECT "option_id", "vote_right_id" FROM "temporary_vote"`);
await queryRunner.query(`DROP TABLE "temporary_vote"`);
await queryRunner.query(`ALTER TABLE "poll" RENAME TO "temporary_poll"`);
await queryRunner.query(`CREATE TABLE "poll" ("id" varchar PRIMARY KEY NOT NULL, "user_id" varchar NOT NULL)`);
await queryRunner.query(`INSERT INTO "poll"("id", "user_id") SELECT "id", "user_id" FROM "temporary_poll"`);
await queryRunner.query(`DROP TABLE "temporary_poll"`);
await queryRunner.query(`ALTER TABLE "vote" RENAME TO "temporary_vote"`);
await queryRunner.query(`CREATE TABLE "vote" ("option_id" varchar NOT NULL, "vote_right_id" varchar PRIMARY KEY NOT NULL, CONSTRAINT "FK_ce69ed4c96964be74d3d57e89cb" FOREIGN KEY ("vote_right_id") REFERENCES "vote_right" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT, CONSTRAINT "FK_d17980c91005358383b7ad59ab0" FOREIGN KEY ("option_id") REFERENCES "option" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT)`);
await queryRunner.query(`INSERT INTO "vote"("option_id", "vote_right_id") SELECT "option_id", "vote_right_id" FROM "temporary_vote"`);
await queryRunner.query(`DROP TABLE "temporary_vote"`);
}

}
2 changes: 2 additions & 0 deletions src/migrations/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Initial1638093041896 } from "./1638093041896-Initial";
import { AddUserIdColumnInPollTable1638098141565 } from "./1638098141565-AddUserIdColumnInPollTable";
import { AddDeadlineColumnInPollTable1639772969440 } from "./1639772969440-AddDeadlineColumnInPollTable";

export const migrations = [
Initial1638093041896,
AddUserIdColumnInPollTable1638098141565,
AddDeadlineColumnInPollTable1639772969440,
]
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1808,6 +1808,18 @@ mkdirp@^1.0.4:
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==

moment-timezone@^0.5.34:
version "0.5.34"
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.34.tgz#a75938f7476b88f155d3504a9343f7519d9a405c"
integrity sha512-3zAEHh2hKUs3EXLESx/wsgw6IQdusOT8Bxm3D9UrHPQR7zlMmzwybC8zHEM1tQ4LJwP7fcxrWr8tuBg05fFCbg==
dependencies:
moment ">= 2.9.0"

"moment@>= 2.9.0", moment@^2.29.1:
version "2.29.1"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.1.tgz#b2be769fa31940be9eeea6469c075e35006fa3d3"
integrity sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==

ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
Expand Down