Skip to content
Merged
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ All notable changes to this project will be documented in this file.
- Delete polls without the need to archive them first
- Collapsible poll description
- Transfer polls to another owner by the current poll owner or the administration
- Added reference provider for link previews
- Added reference provider for link previews and smart picker

## [7.4.1] - 2024-03-07
### New
Expand Down
8 changes: 6 additions & 2 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,22 @@
use OCA\Polls\Listener\GroupDeletedListener;
use OCA\Polls\Listener\OptionListener;
use OCA\Polls\Listener\PollListener;
use OCA\Polls\Listener\PollsReferenceListener;
use OCA\Polls\Listener\ShareListener;
use OCA\Polls\Listener\UserDeletedListener;
use OCA\Polls\Listener\VoteListener;
use OCA\Polls\Middleware\RequestAttributesMiddleware;
use OCA\Polls\Model\Settings\AppSettings;
use OCA\Polls\Model\Settings\SystemSettings;
use OCA\Polls\Notification\Notifier;
use OCA\Polls\Provider\ReferenceProvider;
use OCA\Polls\Provider\SearchProvider;
use OCA\Polls\Reference\PollReferenceProvider;
use OCA\Polls\UserSession;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Collaboration\Reference\RenderReferenceEvent;
use OCP\Group\Events\GroupDeletedEvent;
use OCP\IAppConfig;
use OCP\IDBConnection;
Expand Down Expand Up @@ -89,6 +91,7 @@ public function register(IRegistrationContext $context): void {
include_once __DIR__ . '/../../vendor/autoload.php';
$this->registerServices($context);

$context->registerEventListener(RenderReferenceEvent::class, PollsReferenceListener::class);
$context->registerMiddleWare(RequestAttributesMiddleware::class);
$context->registerNotifierService(Notifier::class);

Expand Down Expand Up @@ -128,7 +131,8 @@ public function register(IRegistrationContext $context): void {

$context->registerSearchProvider(SearchProvider::class);
$context->registerDashboardWidget(PollWidget::class);
$context->registerReferenceProvider(PollReferenceProvider::class);
$context->registerReferenceProvider(ReferenceProvider::class);

}

/**
Expand Down
28 changes: 28 additions & 0 deletions lib/Listener/PollsReferenceListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Polls\Listener;

use OCA\Polls\AppInfo\Application;
use OCP\Collaboration\Reference\RenderReferenceEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Util;

/**
* @template-implements IEventListener<Event>
*/
class PollsReferenceListener implements IEventListener {
public function handle(Event $event): void {
if (!$event instanceof RenderReferenceEvent) {
return;
}

Util::addScript(Application::APP_ID, 'polls-reference');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Polls\Reference;
namespace OCA\Polls\Provider;

use Exception;
use OCA\Polls\AppInfo\Application;
use OCA\Polls\Exceptions\ForbiddenException;
use OCA\Polls\Exceptions\NotFoundException;
use OCA\Polls\Service\PollService;
use OCP\Collaboration\Reference\ADiscoverableReferenceProvider;
use OCP\Collaboration\Reference\IReference;
use OCP\Collaboration\Reference\IReferenceProvider;
use OCP\Collaboration\Reference\ISearchableReferenceProvider;
use OCP\Collaboration\Reference\Reference;
use OCP\IL10N;
use OCP\IURLGenerator;

class PollReferenceProvider implements IReferenceProvider {
class ReferenceProvider extends ADiscoverableReferenceProvider implements ISearchableReferenceProvider {

/** @psalm-suppress PossiblyUnusedMethod */
public function __construct(
Expand All @@ -38,7 +39,7 @@ public function matchReference(string $referenceText): bool {
return ($this->extractPollId($referenceText) !== 0);
}

private function extractPollId($referenceText): int {
public function extractPollId(string $referenceText): int {
$matchingUrls = [
$this->urlGenerator->getAbsoluteURL('/apps/' . Application::APP_ID . '/vote'), // poll url base without index.php
$this->urlGenerator->getAbsoluteURL('/index.php/apps/' . Application::APP_ID . '/vote'), // poll url base with index.php
Expand All @@ -60,6 +61,10 @@ private function extractPollId($referenceText): int {
public function resolveReference(string $referenceText): ?IReference {
if ($this->matchReference($referenceText)) {
$pollId = $this->extractPollId($referenceText);
$expired = false;
$expiry = 0;
$participated = false;


if ($pollId) {
try {
Expand All @@ -69,6 +74,9 @@ public function resolveReference(string $referenceText): ?IReference {
$ownerId = $poll->getUser()->getId();
$ownerDisplayName = $poll->getUser()->getDisplayName();
$url = $poll->getVoteUrl();
$expired = $poll->getExpired();
$expiry = $poll->getExpire();
$participated = $poll->getCurrentUserVotes() ? true : false;

} catch (NotFoundException $e) {
$pollId = 0;
Expand All @@ -91,15 +99,11 @@ public function resolveReference(string $referenceText): ?IReference {
return null;
}

$imageUrl = $this->urlGenerator->getAbsoluteURL(
$this->urlGenerator->imagePath(Application::APP_ID, 'polls.svg')
);

$reference = new Reference($referenceText);
$reference->setTitle($title);
$reference->setDescription($description ? $description : $this->l10n->t('No description available.'));
$reference->setImageUrl($imageUrl);
$reference->setRichObject(Application::APP_ID . '_poll_widget', [
$reference->setImageUrl($this->getIconUrl());
$reference->setRichObject(Application::APP_ID . '_reference_widget', [
'id' => $pollId,
'poll' => [
'id' => $pollId,
Expand All @@ -108,6 +112,9 @@ public function resolveReference(string $referenceText): ?IReference {
'ownerDisplayName' => $ownerDisplayName,
'ownerId' => $ownerId,
'url' => $url,
'expired' => $expired,
'expiry' => $expiry,
'participated' => $participated,
],
]);
return $reference;
Expand All @@ -129,4 +136,22 @@ public function getCachePrefix(string $referenceId): string {
public function getCacheKey(string $referenceId): ?string {
return $this->userId ?? '';
}
public function getId(): string {
return Application::APP_ID;
}
public function getTitle(): string {
return $this->l10n->t('Poll');
}

public function getIconUrl(): string {
return $this->urlGenerator->imagePath(Application::APP_ID, 'polls.svg');
}

public function getOrder(): int {
return 51;
}

public function getSupportedSearchProviderIds(): array {
return ['search-poll'];
}
}
2 changes: 1 addition & 1 deletion lib/Provider/SearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(
}

public function getId(): string {
return 'poll';
return 'search-poll';
}

public function getName(): string {
Expand Down
17 changes: 10 additions & 7 deletions src/components/PollList/PollItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<script setup lang="ts">
import { RouterLink } from 'vue-router'
import { computed } from 'vue'
import moment from '@nextcloud/moment'
import { DateTime } from 'luxon'
import { t } from '@nextcloud/l10n'
import {
usePollStore,
Expand Down Expand Up @@ -41,12 +41,13 @@ const closeToClosing = computed(
() =>
!poll.status.isExpired
&& poll.configuration.expire
&& moment.unix(poll.configuration.expire).diff() < 86400000,
&& DateTime.fromMillis(poll.configuration.expire * 1000).diffNow('hours')
.hours < 36,
)

const timeExpirationRelative = computed(() => {
if (poll.configuration.expire) {
return moment.unix(poll.configuration.expire).fromNow()
return DateTime.fromMillis(poll.configuration.expire * 1000).toRelative()
}
return t('polls', 'never')
})
Expand All @@ -67,14 +68,16 @@ const expiryClass = computed(() => {
return StatusResults.Success
})

const timeCreatedRelative = computed(() =>
moment.unix(poll.status.created).fromNow(),
const timeCreatedRelative = computed(
() => DateTime.fromMillis(poll.status.created * 1000).toRelative() as string,
)

const descriptionLine = computed(() => {
if (poll.status.isArchived) {
return t('polls', 'Archived {relativeTIme}', {
relativeTIme: moment.unix(poll.status.archivedDate).fromNow(),
return t('polls', 'Archived {relativeTime}', {
relativeTime: DateTime.fromMillis(
poll.status.archivedDate * 1000,
).toRelative() as string,
})
}
return t('polls', 'Started {relativeTime} from {ownerName}', {
Expand Down
19 changes: 19 additions & 0 deletions src/polls-reference.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* SPDX-FileCopyrightText: 2022 Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { createApp } from 'vue'
import { pinia } from './stores/index.ts'
import { registerWidget } from '@nextcloud/vue/components/NcRichText'
import Reference from './views/Reference.vue'
import './assets/scss/polls-icon.scss'

registerWidget('polls_reference_widget', async (el, { richObject }) => {
const PollsReference = createApp(Reference, {
richObject,
})
.use(pinia)
.mount(el)
return PollsReference
})
135 changes: 135 additions & 0 deletions src/views/Reference.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<!--
- SPDX-FileCopyrightText: 2022 Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->

<script setup lang="ts">
import NcUserBubble from '@nextcloud/vue/components/NcUserBubble'
import { PollsAppIcon } from '../components/AppIcons'
import ExpirationIcon from 'vue-material-design-icons/CalendarEnd.vue'
import BadgeSmallDiv from '../components/Base/modules/BadgeSmallDiv.vue'
import { t } from '@nextcloud/l10n'
import { DateTime } from 'luxon'
import { StatusResults } from '../Types'

type RichObject = {
id: number
poll: {
id: number
title: string
description: string
ownerDisplayName: string
ownerId: string
url: string
participated: boolean
expiry: number
expired: boolean
}
}

interface Props {
richObject?: RichObject
}

const { richObject } = defineProps<Props>()
// const expiryClass2 = (() => {
// if (!richObject?.poll?.expiry) {
// return ''
// }
// if (DateTime.fromMillis(richObject.poll.expiry * 1000).diffNow('hours').hours < 36) {
// return StatusResults.Warning
// }
// return StatusResults.Success
// })
const expiryClass = richObject?.poll?.expiry
? DateTime.fromMillis(richObject.poll.expiry * 1000).diffNow('hours').hours < 36
? StatusResults.Warning
: StatusResults.Success
: ''
</script>

<template>
<div v-if="richObject" class="polls_widget">
<div class="widget_header">
<PollsAppIcon :size="20" class="title-icon" />
<a class="title" :href="richObject.poll.url" target="_blank">
{{ richObject.poll.title }}
</a>
<BadgeSmallDiv v-if="richObject.poll.participated" class="success">
{{ t('polls', 'participated') }}
</BadgeSmallDiv>
<BadgeSmallDiv v-else-if="richObject.poll.expired" class="error">
{{ t('polls', 'closed') }}
</BadgeSmallDiv>
<BadgeSmallDiv
v-else-if="richObject.poll.expiry > 0"
:class="expiryClass">
<template #icon>
<ExpirationIcon :size="16" />
</template>
{{ DateTime.fromMillis(richObject.poll.expiry * 1000).toRelative() }}
</BadgeSmallDiv>
</div>
<div class="description">
<span class="clamped">
{{ richObject.poll.description }}
</span>
</div>
<div class="widget_footer">
<span>{{ t('polls', 'By:') }}</span>
<NcUserBubble
:user="richObject.poll.ownerId"
:display-name="richObject.poll.ownerDisplayName" />
<span
v-if="richObject.poll.expiry > 0 && !richObject.poll.expired"
class="expiration">
{{ t('polls', 'Ends in') }}
{{ DateTime.fromMillis(richObject.poll.expiry * 1000).toRelative() }}
</span>
</div>
</div>
</template>

<style lang="scss" scoped>
.polls_widget {
padding: 0.6rem;
}
.widget_header,
.widget_footer {
display: flex;
column-gap: 0.3rem;
}

.badge-small {
flex: 0;
}
.polls_app_icon {
flex: 0 0 1.4rem;
}
.title {
flex: 1;
font-weight: bold;
padding-left: 0.6rem;
text-wrap: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.description {
margin-left: 1.4rem;
padding: 0.6rem;
}
.owner {
margin-left: 1.4rem;
padding-left: 0.6rem;
}
.clamped {
display: -webkit-box !important;
-webkit-line-clamp: 4;
line-clamp: 4;
-webkit-box-orient: vertical;
text-wrap: wrap;
overflow: clip !important;
text-overflow: ellipsis !important;
padding: 0 !important;
}
</style>
Loading