Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(openapi): Add more type/int limitations #12783

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
8 changes: 5 additions & 3 deletions lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ public function getCapabilities(): array {
return [];
}

/** @psalm-var non-empty-string $version */
$version = $this->appManager->getAppVersion('spreed');
$capabilities = [
'features' => self::FEATURES,
'features-local' => self::LOCAL_FEATURES,
Expand Down Expand Up @@ -211,15 +213,15 @@ public function getCapabilities(): array {
'only-trusted-servers' => true,
],
'previews' => [
'max-gif-size' => (int)$this->serverConfig->getAppValue('spreed', 'max-gif-size', '3145728'),
'max-gif-size' => max(0, (int)$this->serverConfig->getAppValue('spreed', 'max-gif-size', '3145728')),
],
'signaling' => [
'session-ping-limit' => max(0, (int)$this->serverConfig->getAppValue('spreed', 'session-ping-limit', '200')),
// 'hello-v2-token-key' => string,
],
],
'config-local' => self::LOCAL_CONFIGS,
'version' => $this->appManager->getAppVersion('spreed'),
'version' => $version,
];

if ($this->serverConfig->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'cron') {
Expand All @@ -246,7 +248,7 @@ public function getCapabilities(): array {
}

$pubKey = $this->talkConfig->getSignalingTokenPublicKey();
if ($pubKey) {
if ($pubKey !== '') {
$capabilities['config']['signaling']['hello-v2-token-key'] = $pubKey;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/Chat/ChatManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public function addSystemMessage(
* @return IComment
*/
public function addChangelogMessage(Room $chat, string $message): IComment {
$comment = $this->commentsManager->create(Attendee::ACTOR_GUESTS, Attendee::ACTOR_ID_CHANGELOG, 'chat', (string)$chat->getId());
$comment = $this->commentsManager->create(Attendee::ACTOR_GUESTS, Attendee::CHANGELOG_ACTOR_ID, 'chat', (string)$chat->getId());

$comment->setMessage($message, self::MAX_CHAT_LENGTH);
$comment->setCreationDateTime($this->timeFactory->getDateTime());
Expand Down Expand Up @@ -345,8 +345,8 @@ public function sendMessage(

// Update last_message
if ($comment->getActorType() !== Attendee::ACTOR_BOTS
|| $comment->getActorId() === Attendee::ACTOR_ID_CHANGELOG
|| str_starts_with($comment->getActorId(), Attendee::ACTOR_BOT_PREFIX)) {
|| $comment->getActorId() === Attendee::CHANGELOG_ACTOR_ID
|| str_starts_with($comment->getActorId(), Attendee::BOT_ACTOR_PREFIX)) {
$this->roomService->setLastMessage($chat, $comment);
$this->unreadCountCache->clear($chat->getId() . '-');
} else {
Expand Down
6 changes: 3 additions & 3 deletions lib/Chat/MessageParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ protected function getActorInformation(Message $message, string $actorType, stri
$displayName = $actorId;
$actorId = MatterbridgeManager::BRIDGE_BOT_USERID;
} elseif ($actorType === Attendee::ACTOR_GUESTS
&& !in_array($actorId, [Attendee::ACTOR_ID_CLI, Attendee::ACTOR_ID_CHANGELOG], true)) {
&& !in_array($actorId, [Attendee::CLI_ACTOR_ID, Attendee::CHANGELOG_ACTOR_ID], true)) {
if (isset($this->guestNames[$actorId])) {
$displayName = $this->guestNames[$actorId];
} else {
Expand All @@ -151,8 +151,8 @@ protected function getActorInformation(Message $message, string $actorType, stri
} elseif ($actorType === Attendee::ACTOR_BOTS) {
$displayName = $actorId . '-bot';
$token = $message->getRoom()->getToken();
if (str_starts_with($actorId, Attendee::ACTOR_BOT_PREFIX)) {
$urlHash = substr($actorId, strlen(Attendee::ACTOR_BOT_PREFIX));
if (str_starts_with($actorId, Attendee::BOT_ACTOR_PREFIX)) {
$urlHash = substr($actorId, strlen(Attendee::BOT_ACTOR_PREFIX));
$botName = $this->getBotNameByUrlHashForConversation($token, $urlHash);
if ($botName) {
$displayName = $botName . ' (Bot)';
Expand Down
4 changes: 2 additions & 2 deletions lib/Chat/Parser/Changelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public function handle(Event $event): void {
}

if ($chatMessage->getActorType() !== Attendee::ACTOR_GUESTS ||
$chatMessage->getActorId() !== Attendee::ACTOR_ID_CHANGELOG) {
$chatMessage->getActorId() !== Attendee::CHANGELOG_ACTOR_ID) {
return;
}

$l = $chatMessage->getL10n();
$chatMessage->setActor(Attendee::ACTOR_BOTS, Attendee::ACTOR_ID_CHANGELOG, $l->t('Talk updates ✅'));
$chatMessage->setActor(Attendee::ACTOR_BOTS, Attendee::CHANGELOG_ACTOR_ID, $l->t('Talk updates ✅'));
$event->stopPropagation();
}
}
8 changes: 4 additions & 4 deletions lib/Chat/Parser/SystemMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ protected function parseMessage(Message $chatMessage): void {
$participant->getAttendee()->getActorId() === $parsedParameters['actor']['id'];
}
$cliIsActor = $parsedParameters['actor']['type'] === 'guest' &&
'guest/' . Attendee::ACTOR_ID_CLI === $parsedParameters['actor']['id'];
'guest/' . Attendee::CLI_ACTOR_ID === $parsedParameters['actor']['id'];

if ($message === 'conversation_created') {
$systemIsActor = $parsedParameters['actor']['type'] === 'guest' &&
'guest/' . Attendee::ACTOR_ID_SYSTEM === $parsedParameters['actor']['id'];
'guest/' . Attendee::SYSTEM_ACTOR_ID === $parsedParameters['actor']['id'];

$parsedMessage = $this->l->t('{actor} created the conversation');
if ($currentUserIsActor) {
Expand Down Expand Up @@ -654,7 +654,7 @@ protected function parseMessage(Message $chatMessage): void {
$parsedMessage = $this->l->t('Someone voted on the poll {poll}');
unset($parsedParameters['actor']);

$chatMessage->setActor(Attendee::ACTOR_GUESTS, Attendee::ACTOR_ID_SYSTEM, '');
$chatMessage->setActor(Attendee::ACTOR_GUESTS, Attendee::SYSTEM_ACTOR_ID, '');
} else {
throw new \OutOfBoundsException('Unknown subject');
}
Expand Down Expand Up @@ -1024,7 +1024,7 @@ protected function getGuest(Room $room, string $actorType, string $actorId): arr
}

protected function getGuestName(Room $room, string $actorType, string $actorId): string {
if ($actorId === Attendee::ACTOR_ID_CLI) {
if ($actorId === Attendee::CLI_ACTOR_ID) {
return $this->l->t('Guest');
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Chat/ReactionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
}

/**
* @return array<string, TalkReaction[]>

Check failure on line 130 in lib/Chat/ReactionManager.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

MoreSpecificReturnType

lib/Chat/ReactionManager.php:130:13: MoreSpecificReturnType: The declared return type 'array<string, array<array-key, array{actorDisplayName: non-empty-string, actorId: non-empty-string, actorType: 'bots'|'bridged'|'circles'|'emails'|'federated_users'|'groups'|'guests'|'phones'|'users', timestamp: int<0, max>}>>' for OCA\Talk\Chat\ReactionManager::retrieveReactionMessages is more specific than the inferred return type 'array<string, non-empty-list<array{actorDisplayName: string, actorId: string, actorType: 'bots'|'bridged'|'circles'|'emails'|'federated_users'|'groups'|'guests'|'phones'|'users', timestamp: int}>>' (see https://psalm.dev/070)
* @throws PreConditionNotMetException
*/
public function retrieveReactionMessages(Room $chat, Participant $participant, int $messageId, ?string $reaction = null): array {
Expand All @@ -143,13 +143,13 @@
$this->messageParser->parseMessage($message);

$reactions[$comment->getMessage()][] = [
'actorType' => $comment->getActorType(),
'actorId' => $comment->getActorId(),
'actorType' => $message->getActorType(),
'actorId' => $message->getActorId(),
'actorDisplayName' => $message->getActorDisplayName(),
'timestamp' => $comment->getCreationDateTime()->getTimestamp(),
];
}
return $reactions;

Check failure on line 152 in lib/Chat/ReactionManager.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

LessSpecificReturnStatement

lib/Chat/ReactionManager.php:152:10: LessSpecificReturnStatement: The type 'array<string, non-empty-list<array{actorDisplayName: string, actorId: string, actorType: 'bots'|'bridged'|'circles'|'emails'|'federated_users'|'groups'|'guests'|'phones'|'users', timestamp: int}>>' is more general than the declared return type 'array<string, array<array-key, array{actorDisplayName: non-empty-string, actorId: non-empty-string, actorType: 'bots'|'bridged'|'circles'|'emails'|'federated_users'|'groups'|'guests'|'phones'|'users', timestamp: int<0, max>}>>' for OCA\Talk\Chat\ReactionManager::retrieveReactionMessages (see https://psalm.dev/129)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/Chat/SystemMessage/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,15 +428,15 @@ protected function sendSystemMessage(Room $room, string $message, array $paramet
$actorId = $participant->getAttendee()->getActorId();
} elseif ($forceSystemAsActor) {
$actorType = Attendee::ACTOR_GUESTS;
$actorId = Attendee::ACTOR_ID_SYSTEM;
$actorId = Attendee::SYSTEM_ACTOR_ID;
} else {
$user = $this->userSession->getUser();
if ($user instanceof IUser) {
$actorType = Attendee::ACTOR_USERS;
$actorId = $user->getUID();
} elseif (\OC::$CLI || $this->session->exists('talk-overwrite-actor-cli')) {
$actorType = Attendee::ACTOR_GUESTS;
$actorId = Attendee::ACTOR_ID_CLI;
$actorId = Attendee::CLI_ACTOR_ID;
} elseif ($this->session->exists('talk-overwrite-actor-type')) {
$actorType = $this->session->get('talk-overwrite-actor-type');
$actorId = $this->session->get('talk-overwrite-actor-id');
Expand Down
6 changes: 6 additions & 0 deletions lib/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,21 @@
return \is_array($groups) ? $groups : [];
}

/**
* @return Participant::PRIVACY_*

Check failure on line 68 in lib/Config.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

MoreSpecificReturnType

lib/Config.php:68:13: MoreSpecificReturnType: The declared return type '0|1' for OCA\Talk\Config::getUserReadPrivacy is more specific than the inferred return type 'int' (see https://psalm.dev/070)
*/
public function getUserReadPrivacy(string $userId): int {
return (int)$this->config->getUserValue(

Check failure on line 71 in lib/Config.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

LessSpecificReturnStatement

lib/Config.php:71:10: LessSpecificReturnStatement: The type 'int' is more general than the declared return type '0|1' for OCA\Talk\Config::getUserReadPrivacy (see https://psalm.dev/129)
$userId,
'spreed', 'read_status_privacy',
(string)Participant::PRIVACY_PUBLIC);
}

/**
* @return Participant::PRIVACY_*

Check failure on line 78 in lib/Config.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

MoreSpecificReturnType

lib/Config.php:78:13: MoreSpecificReturnType: The declared return type '0|1' for OCA\Talk\Config::getUserTypingPrivacy is more specific than the inferred return type 'int' (see https://psalm.dev/070)
*/
public function getUserTypingPrivacy(string $userId): int {
return (int)$this->config->getUserValue(

Check failure on line 81 in lib/Config.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

LessSpecificReturnStatement

lib/Config.php:81:10: LessSpecificReturnStatement: The type 'int' is more general than the declared return type '0|1' for OCA\Talk\Config::getUserTypingPrivacy (see https://psalm.dev/129)
$userId,
'spreed', 'typing_privacy',
(string)Participant::PRIVACY_PUBLIC);
Expand Down
6 changes: 3 additions & 3 deletions lib/Controller/BotController.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function sendMessage(string $token, string $message, string $referenceId
$room = $this->manager->getRoomByToken($token);

$actorType = Attendee::ACTOR_BOTS;
$actorId = Attendee::ACTOR_BOT_PREFIX . $bot->getBotServer()->getUrlHash();
$actorId = Attendee::BOT_ACTOR_PREFIX . $bot->getBotServer()->getUrlHash();

$parent = null;
if ($replyTo !== 0) {
Expand Down Expand Up @@ -210,7 +210,7 @@ public function react(string $token, int $messageId, string $reaction): DataResp
$room = $this->manager->getRoomByToken($token);

$actorType = Attendee::ACTOR_BOTS;
$actorId = Attendee::ACTOR_BOT_PREFIX . $bot->getBotServer()->getUrlHash();
$actorId = Attendee::BOT_ACTOR_PREFIX . $bot->getBotServer()->getUrlHash();

try {
$this->reactionManager->addReactionMessage(
Expand Down Expand Up @@ -263,7 +263,7 @@ public function deleteReaction(string $token, int $messageId, string $reaction):
$room = $this->manager->getRoomByToken($token);

$actorType = Attendee::ACTOR_BOTS;
$actorId = Attendee::ACTOR_BOT_PREFIX . $bot->getBotServer()->getUrlHash();
$actorId = Attendee::BOT_ACTOR_PREFIX . $bot->getBotServer()->getUrlHash();

try {
$this->reactionManager->deleteReactionMessage(
Expand Down
9 changes: 5 additions & 4 deletions lib/Model/Attendee.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
*/
class Attendee extends Entity {
public const ACTOR_USERS = 'users';

public const ACTOR_GROUPS = 'groups';
public const ACTOR_GUESTS = 'guests';
public const ACTOR_EMAILS = 'emails';
Expand All @@ -70,10 +71,10 @@ class Attendee extends Entity {
public const ACTOR_PHONES = 'phones';

// Special actor IDs
public const ACTOR_BOT_PREFIX = 'bot-';
public const ACTOR_ID_CLI = 'cli';
public const ACTOR_ID_SYSTEM = 'system';
public const ACTOR_ID_CHANGELOG = 'changelog';
public const BOT_ACTOR_PREFIX = 'bot-';
public const CLI_ACTOR_ID = 'cli';
public const SYSTEM_ACTOR_ID = 'system';
public const CHANGELOG_ACTOR_ID = 'changelog';

public const PERMISSIONS_DEFAULT = 0;
public const PERMISSIONS_CUSTOM = 1;
Expand Down
12 changes: 10 additions & 2 deletions lib/Model/BotServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,34 @@
use OCP\AppFramework\Db\Entity;

/**
* @psalm-method int<1, max> getId()
* @method void setName(string $name)
* @method string getName()
* @psalm-method non-empty-string getName()
* @method void setUrl(string $url)
* @method string getUrl()
* @psalm-method non-empty-string getUrl()
* @method void setUrlHash(string $urlHash)
* @method string getUrlHash()
* @psalm-method non-empty-string getUrlHash()
* @method void setDescription(?string $description)
* @method null|string getDescription()
* @method void setSecret(string $secret)
* @method string getSecret()
* @psalm-method non-empty-string getSecret()
* @method void setErrorCount(int $errorCount)
* @method int getErrorCount()
* @psalm-method int<0, max> getErrorCount()
* @method void setLastErrorDate(?\DateTimeImmutable $lastErrorDate)
* @method ?\DateTimeImmutable getLastErrorDate()
* @method null|\DateTimeImmutable getLastErrorDate()
* @method void setLastErrorMessage(string $lastErrorMessage)
* @method string getLastErrorMessage()
* @method void setState(int $state)
* @method int getState()
* @psalm-method Bot::STATE_* getState()
* @method void setFeatures(int $features)
* @method int getFeatures()
* @psalm-method int-mask<1, 2> getFeatures()
*
* @psalm-import-type TalkBotWithDetailsAndSecret from ResponseDefinitions
*/
Expand Down Expand Up @@ -72,7 +80,7 @@ public function jsonSerialize(): array {
'description' => $this->getDescription(),
'secret' => $this->getSecret(),
'error_count' => $this->getErrorCount(),
'last_error_date' => $this->getLastErrorDate() ? $this->getLastErrorDate()->getTimestamp() : 0,
'last_error_date' => $this->getLastErrorDate() ? max(0, $this->getLastErrorDate()->getTimestamp()) : 0,
'last_error_message' => $this->getLastErrorMessage(),
'state' => $this->getState(),
'features' => $this->getFeatures(),
Expand Down
3 changes: 3 additions & 0 deletions lib/Model/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ public function setLastEdit(string $type, string $id, string $displayName, int $
$this->lastEditTimestamp = $timestamp;
}

/**
* @return Attendee::ACTOR_*
*/
public function getActorType(): string {
return $this->actorType;
}
Expand Down
10 changes: 10 additions & 0 deletions lib/Model/Poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,37 @@
use OCP\AppFramework\Db\Entity;

/**
* @psalm-method int<1, max> getId()
* @method void setRoomId(int $roomId)
* @method int getRoomId()
* @method void setQuestion(string $question)
* @method string getQuestion()
* @psalm-method non-empty-string getQuestion()
* @method void setOptions(string $options)
* @method string getOptions()
* @method void setVotes(string $votes)
* @method string getVotes()
* @method void setNumVoters(int $numVoters)
* @method int getNumVoters()
* @psalm-method int<0, max> getNumVoters()
* @method void setActorType(string $actorType)
* @method string getActorType()
* @psalm-method Attendee::ACTOR_* getActorType()
* @method void setActorId(string $actorId)
* @method string getActorId()
* @psalm-method non-empty-string getActorId()
* @method void setDisplayName(string $displayName)
* @method string getDisplayName()
* @psalm-method non-empty-string getDisplayName()
* @method void setStatus(int $status)
* @method int getStatus()
* @psalm-method self::STATUS_* getStatus()
* @method void setResultMode(int $resultMode)
* @method int getResultMode()
* @psalm-method self::MODE_* getResultMode()
* @method void setMaxVotes(int $maxVotes)
* @method int getMaxVotes()
* @psalm-method int<0, max> getMaxVotes()
*
* @psalm-import-type TalkPoll from ResponseDefinitions
*/
Expand Down Expand Up @@ -80,6 +89,7 @@ public function asArray(): array {
// Because PHP is turning arrays with sequent numeric keys "{"0":x,"1":y,"2":z}" into "[x,y,z]"
// when json_encode() is used we have to prefix the keys with a string,
// to prevent breaking in the mobile apps.
/** @var array<string, int<0, max>> $prefixedVotes */
$prefixedVotes = [];
foreach ($votes as $option => $count) {
$prefixedVotes['option-' . $option] = $count;
Expand Down
5 changes: 4 additions & 1 deletion lib/Model/Reminder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
/**
* @method void setUserId(string $userId)
* @method string getUserId()
* @psalm-method non-empty-string getUserId()
* @method void setToken(string $token)
* @method string getToken()
* @psalm-method non-empty-string getToken()
* @method void setMessageId(int $messageId)
* @method int getMessageId()
* @psalm-method int<1, max> getMessageId()
* @method void setDateTime(\DateTime $dateTime)
* @method \DateTime getDateTime()
*
Expand All @@ -44,7 +47,7 @@ public function jsonSerialize(): array {
'userId' => $this->getUserId(),
'token' => $this->getToken(),
'messageId' => $this->getMessageId(),
'timestamp' => $this->getDateTime()->getTimestamp(),
'timestamp' => max(0, $this->getDateTime()->getTimestamp()),
];
}
}
3 changes: 3 additions & 0 deletions lib/Model/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
* @method string getSessionId()
* @method void setInCall(int $inCall)
* @method int getInCall()
* @psalm-method int-mask<1, 2, 4, 8> getInCall()
* @method void setLastPing(int $lastPing)
* @method int getLastPing()
* @psalm-method int<0, max> getLastPing()
* @method void setState(int $state)
* @method int getState()
* @psalm-method self::STATE_* getState()
*/
class Session extends Entity {
public const STATE_INACTIVE = 0;
Expand Down
2 changes: 1 addition & 1 deletion lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ protected function parseChatMessage(INotification $notification, Room $room, Par
} elseif ($subjectParameters['userType'] === Attendee::ACTOR_BOTS) {
$botId = $subjectParameters['userId'];
try {
$bot = $this->botServerMapper->findByUrlHash(substr($botId, strlen(Attendee::ACTOR_BOT_PREFIX)));
$bot = $this->botServerMapper->findByUrlHash(substr($botId, strlen(Attendee::BOT_ACTOR_PREFIX)));
$richSubjectUser = [
'type' => 'highlight',
'id' => $botId,
Expand Down
Loading
Loading