Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
13 changes: 13 additions & 0 deletions src/Actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export class Actions {
case "lock":
this.onLockSelected(payload, poll);
break;
case "unlock":
this.onUnlockSelected(payload,poll);
break;
case "delete":
this.onDeleteSelected(payload, poll);
break;
Expand Down Expand Up @@ -115,6 +118,16 @@ export class Actions {
this.postEphemeralOnlyAuthor("lock", "poll", payload.channel.id, payload.user.id);
}
}

private onUnlockSelected(payload: any, poll: Poll): void {
payload.message.text = "Poll unlocked!";
if (Actions.isPollAuthor(payload,poll)) {
poll.unlockpoll();
payload.message.blocks = poll.getBlocks();
} else{
this.postEphemeralOnlyAuthor("unlock","poll",payload.channel.id, payload.user.id);
}
}

private onDeleteSelected(payload: any, poll: Poll): void {
if (Actions.isPollAuthor(payload, poll)) {
Expand Down
48 changes: 29 additions & 19 deletions src/Poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,8 @@ export class Poll {

const titleBlock = Static.buildSectionBlock(mrkdwnValue);
message.push(titleBlock, Static.buildContextBlock(`Asked by: ${author}`));
const actionBlocks: ActionsBlock[] = [{ type: "actions", elements: [] }];
let actionBlockCount = 0;
// Construct all the buttons
const start = titleBlock.text!.text === parameters[0] ? 1 : 2;
for (let i = start; i < parameters.length; i++) {
if (i % 5 === 0) {
const newActionBlock: ActionsBlock = { type: "actions", elements: [] };
actionBlocks.push(newActionBlock);
actionBlockCount++;
}
// Remove special characters, should be able to remove this once slack figures itself out
parameters[i] = parameters[i].replace("&amp;", "+").replace("&lt;", "greater than ")
.replace("&gt;", "less than ");
// We set value to empty string so that it is always defined
const button: Button = { type: "button", value: " ", text: Static.buildTextElem(parameters[i]) };
actionBlocks[actionBlockCount].elements.push(button);
}
const actionBlocks = Static.buildVoteOptions(parameters,start);
// The various poll options
const selection: StaticSelect = {
type: "static_select",
Expand Down Expand Up @@ -104,6 +89,20 @@ export class Poll {
return { votes, userIdIndex: votes.indexOf(userId) };
}

private getDynamicSelect(): ActionsBlock[] {
const selection: StaticSelect = {
type: "static_select",
placeholder: Static.buildTextElem("Poll Options"),
options: [
Static.buildSelectOption("Reset your vote", "reset"),
this.isLocked ? Static.buildSelectOption(":unlock: Unlock poll", "unlock") : Static.buildSelectOption(":lock: Lock poll", "lock"),
Static.buildSelectOption("Move to bottom", "bottom"),
Static.buildSelectOption("Delete poll", "delete")
]
};
return [{ type: "actions", elements: [selection] }];
}

public resetVote(userId: string): void {
this.processButtons(this.message.length, button => {
const { votes, userIdIndex } = this.getVotesAndUserIndex(button, userId);
Expand Down Expand Up @@ -131,13 +130,24 @@ export class Poll {
}

public lockPoll(): void {
if (this.isLocked) return;
this.isLocked = true;
this.generateVoteResults();
this.message = this.message.slice(0, 2).concat(this.message.slice(this.getDividerId() - 1));
this.message = this.message.slice(0, 2).concat(this.getDynamicSelect()).concat(this.message.slice(this.getDividerId()));
// ((this.message[2] as ActionsBlock).elements[0] as StaticSelect).options!.splice(0, 2);
}

public unlockpoll(): void {
const voteoptions = [];
const results = this.message.slice(this.getDividerId()+2);
for (let i = 0; i < results.length; i++) {
const option = ((results[i] as SectionBlock).text as MrkdwnElement).text;
voteoptions.push(option.substr(option.indexOf("* "), option.indexOf(" »")).slice(2,-2));
}
const actionBlocks = Static.buildVoteOptions(voteoptions,0);
this.isLocked = false;
this.message = this.message.slice(0,2).concat(actionBlocks).concat(this.getDynamicSelect()).concat({ type: "divider" }).concat(this.message.slice(this.getDividerId()+2));
}

// Creates the message that will be sent to the poll author with the final results
public collectResults(): KnownBlock[] {
const results = this.generateResults(true);
Expand All @@ -163,7 +173,7 @@ export class Poll {
const users: string[] = votes[key].split(",");
users.splice(0, 1);
// Don"t bother with empty votes
if (users.length === 0) return null;

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.

Does this mean we're now going to show an option if it has 0 votes?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Originally, yes. However since there appears to be a want for anonymous polling, we can re-add it.

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.

Well even with non anonymous polling we don't want to show votes with 0 people in the results block.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We need to though, otherwise how will we know what the vote options are?

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.

But it looks like you kept the buttons but disabled voting.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There were previous iterations (see previous commits) where the buttons were deleted. However I re-added the buttons since they store information in regards to who voted. Since we kept the buttons, this will be re-added.

if (users.length === 0) return Static.buildSectionBlock(`*0* ${key} »`);
Comment thread
zapdos26 marked this conversation as resolved.
Outdated
// When anonymous we don"t display the user"s names
const names = !this.anonymous || overrideAnon ? users.map((k: string) => `<@${k}>`).join(",") : "~HIDDEN~";
return Static.buildSectionBlock(`*${users.length}* ${key} » ${names}`);
Expand Down
29 changes: 24 additions & 5 deletions src/Static.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
SectionBlock, ContextBlock, PlainTextElement, Option
SectionBlock, ContextBlock, PlainTextElement, Option, ActionsBlock, Button
} from "@slack/types";

export class Static {
Expand All @@ -8,18 +8,37 @@ export class Static {
}

public static buildSectionBlock(mrkdwnValue: string): SectionBlock {
return { type: "section", text: { type: "mrkdwn", text: mrkdwnValue } };
return {type: "section", text: {type: "mrkdwn", text: mrkdwnValue}};
}

public static buildContextBlock(mrkdwnValue: string): ContextBlock {
return { type: "context", elements: [ { type: "mrkdwn", text: mrkdwnValue } ] };
return {type: "context", elements: [{type: "mrkdwn", text: mrkdwnValue}]};
Comment thread
zapdos26 marked this conversation as resolved.
}

public static buildSelectOption(text: string, value: string): Option {
return { text: this.buildTextElem(text), value: value };
return {text: this.buildTextElem(text), value: value};
}

public static buildTextElem(text: string): PlainTextElement {
return { type: "plain_text", text, emoji: true };
return {type: "plain_text", text, emoji: true};
}
public static buildVoteOptions(parameters: string [], start: number ): ActionsBlock[] {
const actionBlocks: ActionsBlock[] = [{ type: "actions", elements: [] }];
let actionBlockCount = 0;
// Construct all the buttons
for (let i = start; i < parameters.length; i++) {
if (i % 5 === 0 && i != 0) {
const newActionBlock: ActionsBlock = { type: "actions", elements: [] };
actionBlocks.push(newActionBlock);
Comment on lines +31 to +32

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.

We should just make this one line if we're not going to re-use the defined constant again.

actionBlockCount++;
}
// Remove special characters, should be able to remove this once slack figures itself out
parameters[i] = parameters[i].replace("&amp;", "+").replace("&lt;", "greater than ")
Comment thread
zapdos26 marked this conversation as resolved.
Outdated
.replace("&gt;", "less than ");
// We set value to empty string so that it is always defined
const button: Button = { type: "button", value: " ", text: Static.buildTextElem(parameters[i]) };
actionBlocks[actionBlockCount].elements.push(button);
Comment on lines +39 to +40

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.

same as above.

}
return actionBlocks;
}
}