Browse Source

Cache the display name for polls and votes

Signed-off-by: Joas Schilling <coding@schilljs.com>
pull/7306/head
Joas Schilling 4 years ago
parent
commit
30c41a8fff
No known key found for this signature in database GPG Key ID: 7076EA9751AACDDA
  1. 5
      lib/Controller/PollController.php
  2. 19
      lib/GuestManager.php
  3. 12
      lib/Listener/UserDisplayNameListener.php
  4. 8
      lib/Migration/Version15000Date20220503121308.php
  5. 5
      lib/Model/Poll.php
  6. 5
      lib/Model/Vote.php
  7. 24
      lib/Service/PollService.php
  8. 8
      tests/integration/features/chat/poll.feature

5
lib/Controller/PollController.php

@ -31,6 +31,7 @@ use OCA\Talk\Exceptions\WrongPermissionsException;
use OCA\Talk\Model\Poll;
use OCA\Talk\Model\Vote;
use OCA\Talk\Service\PollService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Utility\ITimeFactory;
@ -77,12 +78,14 @@ class PollController extends AEnvironmentAwareController {
$this->room->getId(),
$attendee->getActorType(),
$attendee->getActorId(),
$attendee->getDisplayName(),
$question,
$options,
$resultMode,
$maxVotes
);
} catch (\Exception $e) {
$this->logger->error('Error creating poll', ['exception' => $e]);
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
@ -119,7 +122,7 @@ class PollController extends AEnvironmentAwareController {
public function showPoll(int $pollId): DataResponse {
try {
$poll = $this->pollService->getPoll($this->room->getId(), $pollId);
} catch (\Exception $e) {
} catch (DoesNotExistException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}

19
lib/GuestManager.php

@ -27,6 +27,7 @@ use OCA\Talk\Events\AddEmailEvent;
use OCA\Talk\Events\ModifyParticipantEvent;
use OCA\Talk\Model\Attendee;
use OCA\Talk\Service\ParticipantService;
use OCA\Talk\Service\PollService;
use OCP\Defaults;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IL10N;
@ -42,19 +43,13 @@ class GuestManager {
public const EVENT_AFTER_NAME_UPDATE = self::class . '::updateName';
protected Config $talkConfig;
protected IMailer $mailer;
protected Defaults $defaults;
protected IUserSession $userSession;
private ParticipantService $participantService;
protected ParticipantService $participantService;
protected PollService $pollService;
protected IURLGenerator $url;
protected IL10N $l;
protected IEventDispatcher $dispatcher;
public function __construct(Config $talkConfig,
@ -62,6 +57,7 @@ class GuestManager {
Defaults $defaults,
IUserSession $userSession,
ParticipantService $participantService,
PollService $pollService,
IURLGenerator $url,
IL10N $l,
IEventDispatcher $dispatcher) {
@ -70,6 +66,7 @@ class GuestManager {
$this->defaults = $defaults;
$this->userSession = $userSession;
$this->participantService = $participantService;
$this->pollService = $pollService;
$this->url = $url;
$this->l = $l;
$this->dispatcher = $dispatcher;
@ -89,6 +86,12 @@ class GuestManager {
$displayName
);
$this->pollService->updateDisplayNameForActor(
$attendee->getActorType(),
$attendee->getActorId(),
$displayName
);
$event = new ModifyParticipantEvent($room, $participant, 'name', $displayName);
$this->dispatcher->dispatch(self::EVENT_AFTER_NAME_UPDATE, $event);
}

12
lib/Listener/UserDisplayNameListener.php

@ -25,15 +25,19 @@ namespace OCA\Talk\Listener;
use OCA\Talk\Model\Attendee;
use OCA\Talk\Service\ParticipantService;
use OCA\Talk\Service\PollService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\User\Events\UserChangedEvent;
class UserDisplayNameListener implements IEventListener {
private ParticipantService $participantService;
private PollService $pollService;
public function __construct(ParticipantService $participantService) {
public function __construct(ParticipantService $participantService,
PollService $pollService) {
$this->participantService = $participantService;
$this->pollService = $pollService;
}
public function handle(Event $event): void {
@ -52,5 +56,11 @@ class UserDisplayNameListener implements IEventListener {
$event->getUser()->getUID(),
(string) $event->getValue()
);
$this->pollService->updateDisplayNameForActor(
Attendee::ACTOR_USERS,
$event->getUser()->getUID(),
(string) $event->getValue()
);
}
}

8
lib/Migration/Version15000Date20220503121308.php

@ -71,6 +71,10 @@ class Version15000Date20220503121308 extends SimpleMigrationStep {
'notnull' => true,
'length' => 64,
]);
$table->addColumn('display_name', Types::STRING, [
'notnull' => false,
'length' => 255,
]);
$table->addColumn('status', Types::SMALLINT, [
'notnull' => false,
'default' => 0,
@ -112,6 +116,10 @@ class Version15000Date20220503121308 extends SimpleMigrationStep {
'notnull' => true,
'length' => 64,
]);
$table->addColumn('display_name', Types::STRING, [
'notnull' => false,
'length' => 255,
]);
$table->addColumn('option_id', Types::INTEGER, [
'notnull' => true,
'length' => 6,

5
lib/Model/Poll.php

@ -39,6 +39,8 @@ use OCP\AppFramework\Db\Entity;
* @method string getActorType()
* @method void setActorId(string $actorId)
* @method string getActorId()
* @method void setDisplayName(string $displayName)
* @method string getDisplayName()
* @method void setStatus(int $status)
* @method int getStatus()
* @method void setResultMode(int $resultMode)
@ -58,6 +60,7 @@ class Poll extends Entity {
protected string $options = '';
protected string $actorType = '';
protected string $actorId = '';
protected ?string $displayName = null;
protected int $status = self::STATUS_OPEN;
protected int $resultMode = self::MODE_PUBLIC;
protected int $maxVotes = self::MAX_VOTES_UNLIMITED;
@ -68,6 +71,7 @@ class Poll extends Entity {
$this->addType('options', 'string');
$this->addType('actorType', 'string');
$this->addType('actorId', 'string');
$this->addType('displayName', 'string');
$this->addType('status', 'int');
$this->addType('resultMode', 'int');
$this->addType('maxVotes', 'int');
@ -84,6 +88,7 @@ class Poll extends Entity {
'options' => $this->getOptions(),
'actorType' => $this->getActorType(),
'actorId' => $this->getActorId(),
'actorDisplayName' => $this->getDisplayName(),
'status' => $this->getStatus(),
'resultMode' => $this->getResultMode(),
'maxVotes' => $this->getMaxVotes(),

5
lib/Model/Vote.php

@ -37,6 +37,8 @@ use OCP\AppFramework\Db\Entity;
* @method string getActorType()
* @method void setActorId(string $actorId)
* @method string getActorId()
* @method void setDisplayName(string $displayName)
* @method string getDisplayName()
* @method void setOptionId(int $optionId)
* @method int getOptionId()
*/
@ -45,6 +47,7 @@ class Vote extends Entity {
protected int $roomId = 0;
protected string $actorType = '';
protected string $actorId = '';
protected ?string $displayName = null;
protected int $optionId = 0;
public function __construct() {
@ -52,6 +55,7 @@ class Vote extends Entity {
$this->addType('roomId', 'int');
$this->addType('actorType', 'string');
$this->addType('actorId', 'string');
$this->addType('displayName', 'string');
$this->addType('optionId', 'int');
}
@ -65,6 +69,7 @@ class Vote extends Entity {
// 'roomId' => $this->getRoomId(),
'actorType' => $this->getActorType(),
'actorId' => $this->getActorId(),
'actorDisplayName' => $this->getDisplayName(),
'optionId' => $this->getOptionId(),
];
}

24
lib/Service/PollService.php

@ -32,9 +32,7 @@ use OCA\Talk\Model\Vote;
use OCA\Talk\Model\VoteMapper;
use OCA\Talk\Participant;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\IMapperException;
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
class PollService {
@ -50,11 +48,12 @@ class PollService {
$this->voteMapper = $voteMapper;
}
public function createPoll(int $roomId, string $actorType, string $actorId, string $question, array $options, int $resultMode, int $maxVotes): Poll {
public function createPoll(int $roomId, string $actorType, string $actorId, string $displayName, string $question, array $options, int $resultMode, int $maxVotes): Poll {
$poll = new Poll();
$poll->setRoomId($roomId);
$poll->setActorType($actorType);
$poll->setActorId($actorId);
$poll->setDisplayName($displayName);
$poll->setQuestion($question);
$poll->setOptions(json_encode($options));
$poll->setResultMode($resultMode);
@ -69,7 +68,7 @@ class PollService {
* @param int $roomId
* @param int $pollId
* @return Poll
* @throws IMapperException
* @throws DoesNotExistException
*/
public function getPoll(int $roomId, int $pollId): Poll {
$poll = $this->pollMapper->getByPollId($pollId);
@ -134,6 +133,7 @@ class PollService {
$vote->setRoomId($poll->getRoomId());
$vote->setActorType($participant->getAttendee()->getActorType());
$vote->setActorId($participant->getAttendee()->getActorId());
$vote->setDisplayName($participant->getAttendee()->getDisplayName());
$vote->setOptionId($optionId);
$this->voteMapper->insert($vote);
@ -152,4 +152,20 @@ class PollService {
$this->voteMapper->deleteByRoomId($roomId);
$this->pollMapper->deleteByRoomId($roomId);
}
public function updateDisplayNameForActor(string $actorType, string $actorId, string $displayName): void {
$update = $this->connection->getQueryBuilder();
$update->update('talk_polls')
->set('display_name', $update->createNamedParameter($displayName))
->where($update->expr()->eq('actor_type', $update->createNamedParameter($actorType)))
->andWhere($update->expr()->eq('actor_id', $update->createNamedParameter($actorId)));
$update->executeStatement();
$update = $this->connection->getQueryBuilder();
$update->update('talk_poll_votes')
->set('display_name', $update->createNamedParameter($displayName))
->where($update->expr()->eq('actor_type', $update->createNamedParameter($actorType)))
->andWhere($update->expr()->eq('actor_id', $update->createNamedParameter($actorId)));
$update->executeStatement();
}
}

8
tests/integration/features/chat/poll.feature

@ -23,6 +23,7 @@ Feature: chat/poll
| maxVotes | unlimited |
| actorType | users |
| actorId | participant1 |
| actorDisplayName | participant1-displayname |
| status | open |
| voted | not voted |
Then user "participant1" votes for options "[1]" on poll "What is the question?" in room "room" with 200
@ -33,6 +34,7 @@ Feature: chat/poll
| maxVotes | unlimited |
| actorType | users |
| actorId | participant1 |
| actorDisplayName | participant1-displayname |
| status | open |
| voted | [1] |
Then user "participant1" closes poll "What is the question?" in room "room" with 200
@ -43,6 +45,7 @@ Feature: chat/poll
| maxVotes | unlimited |
| actorType | users |
| actorId | participant1 |
| actorDisplayName | participant1-displayname |
| status | closed |
| voted | [1] |
@ -63,6 +66,7 @@ Feature: chat/poll
| maxVotes | unlimited |
| actorType | users |
| actorId | participant1 |
| actorDisplayName | participant1-displayname |
| status | open |
| voted | [1] |
Then user "participant1" votes for options "[0]" on poll "What is the question?" in room "room" with 200
@ -73,6 +77,7 @@ Feature: chat/poll
| maxVotes | unlimited |
| actorType | users |
| actorId | participant1 |
| actorDisplayName | participant1-displayname |
| status | open |
| voted | [0] |
@ -116,6 +121,7 @@ Feature: chat/poll
| maxVotes | unlimited |
| actorType | users |
| actorId | participant1 |
| actorDisplayName | participant1-displayname |
| status | open |
| voted | [0,1] |
@ -140,6 +146,7 @@ Feature: chat/poll
| maxVotes | unlimited |
| actorType | users |
| actorId | participant2 |
| actorDisplayName | participant2-displayname |
| status | closed |
| voted | not voted |
@ -174,6 +181,7 @@ Feature: chat/poll
| maxVotes | unlimited |
| actorType | users |
| actorId | participant2 |
| actorDisplayName | participant2-displayname |
| status | closed |
| voted | not voted |

Loading…
Cancel
Save