Skip to content

Commit

Permalink
resolve issues with non-global post polls & switch back to old layout…
Browse files Browse the repository at this point in the history
… for them
  • Loading branch information
dsevillamartin committed May 20, 2024
1 parent 5acd5c7 commit 054cdb2
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 38 deletions.
7 changes: 7 additions & 0 deletions js/src/@types/shims.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type Poll from '../forum/models/Poll';

declare module 'flarum/common/models/Post' {
export default interface Post {
polls: () => Poll[];
}
}
12 changes: 4 additions & 8 deletions js/src/forum/addPollsToPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ import CommentPost from 'flarum/forum/components/CommentPost';
import PollView from './components/PollView';
import DiscussionPage from 'flarum/forum/components/DiscussionPage';
import Poll from './models/Poll';
import Post from 'flarum/common/models/Post';
import PollOption from './models/PollOption';

interface PollPost extends Post {
polls: () => Poll[];
}
import PostPoll from './components/PostPoll';

interface PusherPollDto {
pollId: string;
Expand All @@ -20,20 +16,20 @@ interface PusherPollDto {

export default () => {
extend(CommentPost.prototype, 'content', function (content) {
const post = this.attrs.post as PollPost;
const post = this.attrs.post;

if ((!post.isHidden() || this.revealContent) && post.polls()) {
for (const poll of post.polls()) {
if (poll) {
content.push(<PollView poll={poll} />);
content.push(<PostPoll post={post} poll={poll} />);
}
}
}
});

extend(CommentPost.prototype, 'oninit', function () {
this.subtree.check(() => {
const polls = (this.attrs.post as PollPost).polls();
const polls = this.attrs.post.polls();

const checks = polls?.map?.(
(poll) =>
Expand Down
60 changes: 34 additions & 26 deletions js/src/forum/components/ComposePollPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,51 @@ export default class ComposePollPage extends Page {
oninit(vnode: Mithril.Vnode) {
super.oninit(vnode);

if (!app.forum.attribute<boolean>('globalPollsEnabled')) {
// prevent users from accessing the page if they can't start global polls or if they are disabled altogether
if (!app.forum.attribute<boolean>('globalPollsEnabled') || !app.forum.attribute<boolean>('canStartGlobalPolls')) {
m.route.set('/');
return;
}

this.bodyClass = 'App--compose-poll';

// Get the `edit` parameter from the URL
const editId = m.route.param('id');
if (editId) {
this.poll = app.store.getById<Poll>('poll', editId);

if (!this.poll) {
this.loading = true;

app.store.find<Poll>('fof/polls', editId).then((item) => {
this.poll = item;
this.loading = false;
app.setTitle(app.translator.trans(`fof-polls.forum.compose.${!!this.poll?.id() ? 'edit' : 'add'}_title`) as string);
m.redraw();
});

// either load the poll we're editing or create a new one
const pollPromise = editId ? this.loadEditingPoll(editId) : Promise.resolve(PollFormState.createNewPoll());

pollPromise.then((poll: Poll | null | undefined) => {
this.poll = poll;

if (poll && !poll.canEdit()) {
m.route.set('/');
return;
}
} else {
this.poll = PollFormState.createNewPoll();
}

app.history.push('compose-poll', app.translator.trans(`fof-polls.forum.compose.${!!this.poll?.id() ? 'edit' : 'add'}_title`) as string);
this.bodyClass = 'App--compose-poll';
app.setTitle(app.translator.trans(`fof-polls.forum.compose.${!!this.poll?.id() ? 'edit' : 'add'}_title`) as string);
app.history.push('compose-poll', app.translator.trans(`fof-polls.forum.compose.${!!this.poll?.id() ? 'edit' : 'add'}_title`) as string);
app.setTitle(app.translator.trans(`fof-polls.forum.compose.${!!this.poll?.id() ? 'edit' : 'add'}_title`) as string);

m.redraw();
});
}

view(): Mithril.Children {
// prevent users from accessing the page if they can't start global polls
if (!app.forum.attribute<boolean>('canStartGlobalPolls')) {
m.route.set('/');
return;
}
async loadEditingPoll(editId: string) {
const alreadyLoaded = app.store.getById<Poll>('poll', editId);

if (alreadyLoaded) return alreadyLoaded;

this.loading = true;

const poll = await app.store.find<Poll>('fof/polls', editId);

if (this.loading) {
this.loading = false;

return poll;
}

view(): Mithril.Children {
if (this.loading || !this.poll) {
return <LoadingIndicator />;
}

Expand Down
5 changes: 4 additions & 1 deletion js/src/forum/components/PostPoll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type Mithril from 'mithril';
import PollOption from '../models/PollOption';
import Post from 'flarum/common/models/Post';
import extractText from 'flarum/common/utils/extractText';
import PollImage from './Poll/PollImage';

export interface PostPollAttrs extends ComponentAttrs {
poll: Poll;
Expand Down Expand Up @@ -82,6 +83,8 @@ export default class PostPoll extends Component<PostPollAttrs> {
</div>
</div>

{!!poll.imageUrl() && <PollImage poll={poll} />}

<div>
<div className="PollOptions">{options.map(this.viewOption.bind(this))}</div>

Expand Down Expand Up @@ -168,7 +171,7 @@ export default class PostPoll extends Component<PostPollAttrs> {
const bar = (
<div className="PollBar" data-selected={!!voted} style={`--poll-option-width: ${width}%`}>
{showCheckmark && (
<label className="PollAnswer-checkbox checkbox">
<label className="PollAnswer-checkbox">
<input onchange={this.changeVote.bind(this, opt)} type="checkbox" checked={voted} disabled={isDisabled} />
<span className="checkmark" />
</label>
Expand Down
9 changes: 7 additions & 2 deletions resources/less/forum.less
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
row-gap: 15px;
margin-left: 15px;
align-items: start;
margin-top: 15px;

.PollAnswer when (@fof-polls-options-color-blend = true) {
&-checkbox,
Expand Down Expand Up @@ -174,7 +175,12 @@
.PollHeading {
display: flex;
align-items: baseline;
gap: 10px;

&-actions {
display: flex;
gap: 10px;
align-items: baseline;
}

&-title-container {
flex-grow: 1;
Expand Down Expand Up @@ -494,7 +500,6 @@

.PollOption {
position: relative;
padding: 10px;
background: var(--body-bg);
border-radius: 4px;

Expand Down
6 changes: 5 additions & 1 deletion src/Access/PollPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,18 @@ public function edit(User $actor, Poll $poll)
return $this->allow();
}

if (!$poll->hasEnded() && $actor->can('edit', $poll->post)) {
if (!$poll->isGlobal() && !$poll->hasEnded() && $actor->can('edit', $poll->post)) {
// User either created poll & can edit own poll or can edit all polls in post
if (($actor->id === $poll->user_id && $actor->hasPermission('polls.selfEdit'))
|| ($actor->id == $poll->post->user_id && $actor->hasPermission('polls.selfPostEdit'))
) {
return $this->allow();
}
}

if ($poll->isGlobal()) {
return $actor->id === $poll->user_id && $actor->hasPermission('polls.selfEdit');
}
}

public function delete(User $actor, Poll $poll)
Expand Down

0 comments on commit 054cdb2

Please sign in to comment.