Skip to content

Commit 056f893

Browse files
PatriceGsampaiodiego
authored andcommitted
Add setting to display voter name instead of username (#25)
1 parent f4b05b7 commit 056f893

File tree

4 files changed

+65
-52
lines changed

4 files changed

+65
-52
lines changed

PollApp.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { App } from '@rocket.chat/apps-engine/definition/App';
33
import { IAppInfo } from '@rocket.chat/apps-engine/definition/metadata';
44

55
// Commands
6+
import { SettingType } from '@rocket.chat/apps-engine/definition/settings';
67
import { PollCommand } from './src/PollCommand';
78
import { VoteCommand } from './src/VoteCommand';
89

@@ -15,5 +16,15 @@ export class PollApp extends App {
1516
public async initialize(configuration: IConfigurationExtend): Promise<void> {
1617
await configuration.slashCommands.provideSlashCommand(new PollCommand());
1718
await configuration.slashCommands.provideSlashCommand(new VoteCommand(this));
19+
await configuration.settings.provideSetting({
20+
id : 'use-user-name',
21+
i18nLabel: 'Use name attribute to display voters, instead of username',
22+
i18nDescription: 'When checked, display voters as full user names instead of @name',
23+
required: false,
24+
type: SettingType.BOOLEAN,
25+
public: true,
26+
packageValue: false,
27+
});
1828
}
29+
1930
}

package-lock.json

Lines changed: 48 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/VoteCommand.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ export class VoteCommand implements ISlashCommand {
4242

4343
const poll = polls[0] as IPoll;
4444

45-
const { username } = context.getSender();
45+
const useUserName = await read.getEnvironmentReader().getSettings().getById('use-user-name');
4646

47-
const hasVoted = poll.votes[voteIndex].voters.indexOf(username);
47+
const displayedName = useUserName.value ? context.getSender().name : '@' + context.getSender().username;
48+
49+
const hasVoted = poll.votes[voteIndex].voters.indexOf(displayedName);
4850

4951
if (hasVoted !== -1) {
5052
poll.totalVotes--;
@@ -53,7 +55,7 @@ export class VoteCommand implements ISlashCommand {
5355
} else {
5456
poll.totalVotes++;
5557
poll.votes[voteIndex].quantity++;
56-
poll.votes[voteIndex].voters.push(username);
58+
poll.votes[voteIndex].voters.push(displayedName);
5759
}
5860
await persis.updateByAssociation(association, poll);
5961

src/buildOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function addVoters(votes: IVoter, totalVotes: IPoll['totalVotes']) {
2323
let voters = ` \`${ votes.quantity }\``;
2424

2525
if (votes.voters.length > 0) {
26-
voters += `\n_@${ votes.voters.join('_, _@') }_`;
26+
voters += `\n_${ votes.voters.join('_, _') }_`;
2727
}
2828

2929
return voters;

0 commit comments

Comments
 (0)