Compare commits

..

No commits in common. "main" and "899b034013c89f13ecd5d2e444be2ab026326242" have entirely different histories.

10 changed files with 1027 additions and 1174 deletions

View File

@ -1,12 +1,8 @@
{
"com.thorlaksson.phpcs.formatOnSave" : true,
"com.thorlaksson.phpcs.runOnChange" : "onSave",
"com.thorlaksson.phpcs.standard" : "phpcs.xml",
"editor.default_syntax" : "php",
"php.validate" : "onSave",
"prettier.format-on-save" : "Disable",
"prettier.format-on-save.ignore-remote" : "Disable",
"prettier.format-on-save.ignore-without-config" : "Disable",
"workspace.color" : 1,
"workspace.name" : "APnutI"
}

View File

@ -3,7 +3,7 @@
"ignoredFilePatterns" : [
"logs"
],
"remotePath" : "\/var\/www\/dragonpolls\/vendor\/hutattedonmyarm\/apnuti",
"server" : "Rabenberger Photos",
"remotePath" : "\/var\/www\/html\/Dragonpolls\/vendor\/hutattedonmyarm\/apnuti",
"server" : "Phlaym",
"usesPublishing" : true
}

View File

@ -6,9 +6,9 @@
"keywords": ["pnut", "api"],
"license": "MIT",
"require": {
"monolog/monolog": "^3.0",
"psr/log": "^2.0 || ^3.0",
"php": "^8.3",
"monolog/monolog": "^2.0",
"psr/log": "^1.1",
"php": "^7.4|^8.0",
"ext-pdo": "*",
"ext-curl": "*",
"ext-json": "*",

83
composer.lock generated
View File

@ -4,70 +4,63 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "fd959c4dda47577b06ccb0f69ab89de8",
"content-hash": "3c041ce633113232f43e991a77d802f9",
"packages": [
{
"name": "monolog/monolog",
"version": "3.9.0",
"version": "2.2.0",
"source": {
"type": "git",
"url": "https://github.com/Seldaek/monolog.git",
"reference": "10d85740180ecba7896c87e06a166e0c95a0e3b6"
"reference": "1cb1cde8e8dd0f70cc0fe51354a59acad9302084"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Seldaek/monolog/zipball/10d85740180ecba7896c87e06a166e0c95a0e3b6",
"reference": "10d85740180ecba7896c87e06a166e0c95a0e3b6",
"url": "https://api.github.com/repos/Seldaek/monolog/zipball/1cb1cde8e8dd0f70cc0fe51354a59acad9302084",
"reference": "1cb1cde8e8dd0f70cc0fe51354a59acad9302084",
"shasum": ""
},
"require": {
"php": ">=8.1",
"psr/log": "^2.0 || ^3.0"
"php": ">=7.2",
"psr/log": "^1.0.1"
},
"provide": {
"psr/log-implementation": "3.0.0"
"psr/log-implementation": "1.0.0"
},
"require-dev": {
"aws/aws-sdk-php": "^3.0",
"aws/aws-sdk-php": "^2.4.9 || ^3.0",
"doctrine/couchdb": "~1.0@dev",
"elasticsearch/elasticsearch": "^7 || ^8",
"ext-json": "*",
"graylog2/gelf-php": "^1.4.2 || ^2.0",
"guzzlehttp/guzzle": "^7.4.5",
"guzzlehttp/psr7": "^2.2",
"elasticsearch/elasticsearch": "^7",
"graylog2/gelf-php": "^1.4.2",
"mongodb/mongodb": "^1.8",
"php-amqplib/php-amqplib": "~2.4 || ^3",
"php-console/php-console": "^3.1.8",
"phpstan/phpstan": "^2",
"phpstan/phpstan-deprecation-rules": "^2",
"phpstan/phpstan-strict-rules": "^2",
"phpunit/phpunit": "^10.5.17 || ^11.0.7",
"predis/predis": "^1.1 || ^2",
"rollbar/rollbar": "^4.0",
"ruflin/elastica": "^7 || ^8",
"symfony/mailer": "^5.4 || ^6",
"symfony/mime": "^5.4 || ^6"
"php-amqplib/php-amqplib": "~2.4",
"php-console/php-console": "^3.1.3",
"phpspec/prophecy": "^1.6.1",
"phpstan/phpstan": "^0.12.59",
"phpunit/phpunit": "^8.5",
"predis/predis": "^1.1",
"rollbar/rollbar": "^1.3",
"ruflin/elastica": ">=0.90 <7.0.1",
"swiftmailer/swiftmailer": "^5.3|^6.0"
},
"suggest": {
"aws/aws-sdk-php": "Allow sending log messages to AWS services like DynamoDB",
"doctrine/couchdb": "Allow sending log messages to a CouchDB server",
"elasticsearch/elasticsearch": "Allow sending log messages to an Elasticsearch server via official client",
"ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)",
"ext-curl": "Required to send log messages using the IFTTTHandler, the LogglyHandler, the SendGridHandler, the SlackWebhookHandler or the TelegramBotHandler",
"ext-mbstring": "Allow to work properly with unicode symbols",
"ext-mongodb": "Allow sending log messages to a MongoDB server (via driver)",
"ext-openssl": "Required to send log messages using SSL",
"ext-sockets": "Allow sending log messages to a Syslog server (via UDP driver)",
"graylog2/gelf-php": "Allow sending log messages to a GrayLog2 server",
"mongodb/mongodb": "Allow sending log messages to a MongoDB server (via library)",
"php-amqplib/php-amqplib": "Allow sending log messages to an AMQP server using php-amqplib",
"php-console/php-console": "Allow sending log messages to Google Chrome",
"rollbar/rollbar": "Allow sending log messages to Rollbar",
"ruflin/elastica": "Allow sending log messages to an Elastic Search server"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "3.x-dev"
"dev-main": "2.x-dev"
}
},
"autoload": {
@ -95,7 +88,7 @@
],
"support": {
"issues": "https://github.com/Seldaek/monolog/issues",
"source": "https://github.com/Seldaek/monolog/tree/3.9.0"
"source": "https://github.com/Seldaek/monolog/tree/2.2.0"
},
"funding": [
{
@ -107,34 +100,34 @@
"type": "tidelift"
}
],
"time": "2025-03-24T10:02:05+00:00"
"time": "2020-12-14T13:15:25+00:00"
},
{
"name": "psr/log",
"version": "3.0.2",
"version": "1.1.3",
"source": {
"type": "git",
"url": "https://github.com/php-fig/log.git",
"reference": "f16e1d5863e37f8d8c2a01719f5b34baa2b714d3"
"reference": "0f73288fd15629204f9d42b7055f72dacbe811fc"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/log/zipball/f16e1d5863e37f8d8c2a01719f5b34baa2b714d3",
"reference": "f16e1d5863e37f8d8c2a01719f5b34baa2b714d3",
"url": "https://api.github.com/repos/php-fig/log/zipball/0f73288fd15629204f9d42b7055f72dacbe811fc",
"reference": "0f73288fd15629204f9d42b7055f72dacbe811fc",
"shasum": ""
},
"require": {
"php": ">=8.0.0"
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.x-dev"
"dev-master": "1.1.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Log\\": "src"
"Psr\\Log\\": "Psr/Log/"
}
},
"notification-url": "https://packagist.org/downloads/",
@ -144,7 +137,7 @@
"authors": [
{
"name": "PHP-FIG",
"homepage": "https://www.php-fig.org/"
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for logging libraries",
@ -155,24 +148,24 @@
"psr-3"
],
"support": {
"source": "https://github.com/php-fig/log/tree/3.0.2"
"source": "https://github.com/php-fig/log/tree/1.1.3"
},
"time": "2024-09-11T13:17:53+00:00"
"time": "2020-03-23T09:12:05+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": {},
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
"php": "^8.3",
"php": "^7.4|^8.0",
"ext-pdo": "*",
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*"
},
"platform-dev": {},
"plugin-api-version": "2.6.0"
"platform-dev": [],
"plugin-api-version": "2.0.0"
}

View File

@ -18,10 +18,10 @@
<property name="eolChar" value="\n"/>
</properties>
</rule>
<arg name="tab-width" value="4"/>
<arg name="tab-width" value="2"/>
<rule ref="Generic.WhiteSpace.ScopeIndent">
<properties>
<property name="indent" value="4"/>
<property name="indent" value="2"/>
</properties>
</rule>

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,4 @@
<?php
namespace APnutI\Entities;
use APnutI\Entities\PollOption;
@ -10,227 +9,216 @@ use APnutI\Exceptions\NotSupportedPollException;
class Poll
{
public \DateTime $created_at;
public \DateTime $closed_at;
public int $id = 0;
public int $max_options = 0;
public bool $is_anonymous = false;
public bool $is_public = false;
public array $options = [];
public ?string $token = null;
public string $prompt = '';
public ?User $user = null;
public ?Source $source = null;
public string $type;
public \DateTime $created_at;
public \DateTime $closed_at;
public int $id = 0;
public int $max_options = 0;
public bool $is_anonymous = false;
public bool $is_public = false;
public array $options = [];
public ?string $token = null;
public string $prompt = "";
public ?User $user = null;
public ?Source $source = null;
public string $type;
private APnutI $api;
private APnutI $api;
public static string $notice_type = 'io.pnut.core.poll-notice';
public static array $poll_types = [
'general.poll',
'net.unsweets.beta',
'io.pnut.core.poll',
'io.broadsword.poll',
'nl.chimpnut.quizbot.attachment.poll',
];
public static string $notice_type = 'io.pnut.core.poll-notice';
public static array $poll_types = [
'general.poll',
'net.unsweets.beta',
'io.pnut.core.poll',
'io.broadsword.poll',
'nl.chimpnut.quizbot.attachment.poll'
];
public function __construct(array $data, APnutI $api)
{
$this->api = $api;
$this->options = [];
$type = '';
if (array_key_exists('type', $data) &&
$data['type'] === Poll::$notice_type
) {
$val = $data['value'];
$this->closed_at = new \DateTime($val['closed_at']);
foreach ($val['options'] as $option) {
$this->options[] = new PollOption($option);
}
$this->id = (int) $val['poll_id'];
$this->token = $val['poll_token'];
$this->prompt = $val['prompt'];
} elseif (array_key_exists('type', $data) &&
in_array($data['type'], Poll::$poll_types)
) {
$this->parsePoll($data);
} elseif (array_key_exists('type', $data) &&
strpos($data['type'], '.poll') !== false
) {
// Try parsing unknown types if they *might* be a poll
try {
$this->parsePoll($data);
} catch (\Exception $e) {
throw new NotSupportedPollException($data['type']);
}
} elseif (array_key_exists('raw', $data) && #Polls included in posts
array_key_exists(Poll::$notice_type, $data['raw']) &&
count($data['raw'][Poll::$notice_type]) > 0
) {
$poll_data = $data['raw'][Poll::$notice_type][0];
if (!empty($data['source'])) {
#Source is attached to post, not to poll raw
$poll_data['source'] = $data['source'];
}
if (!empty($data['user'])) {
#User is attached to post, not to poll raw
$poll_data['user'] = $data['user'];
}
$type = Poll::$notice_type;
$this->parsePoll($poll_data);
} else {
throw new NotSupportedPollException($data['type']);
}
$this->type = empty($type) ? $data['type'] : $type;
public function __construct(array $data, APnutI $api)
{
$this->api = $api;
$this->options = [];
$type = '';
if (array_key_exists('type', $data) && $data['type'] === Poll::$notice_type) {
$val = $data['value'];
$this->closed_at = new \DateTime($val['closed_at']);
foreach ($val['options'] as $option) {
$this->options[] = new PollOption($option);
}
$this->id = (int)$val['poll_id'];
$this->token = $val['poll_token'];
$this->prompt = $val['prompt'];
} elseif (array_key_exists('type', $data) &&in_array($data['type'], Poll::$poll_types)) {
$this->parsePoll($data);
} elseif (array_key_exists('type', $data) &&strpos($data['type'], '.poll') !== false) {
// Try parsing unknown types if they *might* be a poll
try {
$this->parsePoll($data);
} catch (\Exception $e) {
throw new NotSupportedPollException($data['type']);
}
} elseif (array_key_exists('raw', $data) && #Polls included in posts
array_key_exists(Poll::$notice_type, $data['raw']) &&
count($data['raw'][Poll::$notice_type]) > 0
) {
$poll_data = $data['raw'][Poll::$notice_type][0];
if (!empty($data['source'])) { #Source is attached to post, not to poll raw
$poll_data['source'] = $data['source'];
}
if (!empty($data['user'])) { #User is attached to post, not to poll raw
$poll_data['user'] = $data['user'];
}
$type = Poll::$notice_type;
$this->parsePoll($poll_data);
} else {
throw new NotSupportedPollException($data['type']);
}
$this->type = empty($type) ? $data['type'] : $type;
}
private function parsePoll(array $data)
{
$this->created_at = new \DateTime($data['created_at']);
$this->closed_at = new \DateTime($data['closed_at']);
$this->id = (int) $data['id'];
$this->is_anonymous = array_key_exists('is_anonymous', $data)
? (bool) $data['is_anonymous']
: false;
$this->max_options = array_key_exists('max_options', $data)
? (int) $data['max_options']
: 1;
$this->is_public = array_key_exists('is_public', $data)
? (bool) $data['is_public']
: false;
foreach ($data['options'] as $option) {
$this->options[] = new PollOption($option);
}
if (!empty($data['poll_token'])) {
$this->token = $data['poll_token'];
}
$this->prompt = $data['prompt'];
if (!empty($data['user'])) {
$this->user = new User($data['user'], $this->api);
}
if (!empty($data['source'])) {
$this->source = new Source($data['source']);
}
private function parsePoll(array $data)
{
$this->created_at = new \DateTime($data['created_at']);
$this->closed_at = new \DateTime($data['closed_at']);
$this->id = (int)$data['id'];
$this->is_anonymous = array_key_exists('is_anonymous', $data) ? (bool)$data['is_anonymous'] : false;
$this->max_options = array_key_exists('max_options', $data) ? (int)$data['max_options'] : 1;
$this->is_public = array_key_exists('is_public', $data) ? (bool)$data['is_public'] : false;
foreach ($data['options'] as $option) {
$this->options[] = new PollOption($option);
}
if (!empty($data['poll_token'])) {
$this->token = $data['poll_token'];
}
$this->prompt = $data['prompt'];
if (!empty($data['user'])) {
$this->user = new User($data['user'], $this->api);
}
if (!empty($data['source'])) {
$this->source = new Source($data['source']);
}
}
/**
* Returns the most voted option. If multiple options have the same amount
* of voted, return all of them. Always returns an array!
*/
public function getMostVotedOption(): array
{
if (count($this->options) === 0) {
return [];
}
/**
* Returns the most voted option. If multiple options have the same amount
* of voted, return all of them. Always returns an array!
*/
public function getMostVotedOption(): array
{
if (count($this->options) === 0) {
return [];
}
$optns = [];
//$most_voted_option = $this->options[0];
$most_voted_option = null;
foreach ($this->options as $option) {
if ($option->greaterThan($most_voted_option)) {
$optns = [];
//$most_voted_option = $this->options[0];
$most_voted_option = null;
foreach ($this->options as $option) {
if ($option->greaterThan($most_voted_option)) {
$optns = [];
$most_voted_option = $option;
$optns[] = $option;
} elseif ($option->greaterThanOrSame($most_voted_option)) {
$optns[] = $option;
}
}
return $optns;
$most_voted_option = $option;
$optns[] = $option;
} elseif ($option->greaterThanOrSame($most_voted_option)) {
$optns[] = $option;
}
}
return $optns;
}
public function getMyVotes(): array
{
$optns = [];
foreach ($this->options as $option) {
if ($option->is_your_response) {
$optns[] = $option;
}
}
return $optns;
public function getMyVotes(): array
{
$optns = [];
foreach ($this->options as $option) {
if ($option->is_your_response) {
$optns[] = $option;
}
}
return $optns;
}
public static function isValidPoll(string $type): bool
{
return $type === Poll::$notice_type || in_array($type, Poll::$poll_types);
}
public static function isValidPoll(string $type): bool
{
return $type === Poll::$notice_type || in_array($type, Poll::$poll_types);
}
public function canVote()
{
$is_authenticated = $this->api->isAuthenticated(false, true);
return $is_authenticated && !$this->isClosed();
}
public function canVote()
{
$is_authenticated = $this->api->isAuthenticated(false, true);
return $is_authenticated && !$this->isClosed();
}
public function isClosed()
{
return $this->closed_at <= new \DateTime();
}
public function isClosed()
{
return $this->closed_at <= new \DateTime();
}
public function vote(array $options): Poll
{
return $this->api->voteInPoll($this->id, $options, $this->token);
}
public function vote(array $options): Poll
{
return $this->api->voteInPoll($this->id, $options, $this->token);
}
/*
/*
* This is inconsistend with most other functions (which are provided directly by the API object.
* I should probably settle on one of the two styles.
* I prefer having the methods in here, but having to pass the API object along is not so great,
* neither is having to make the API's logger public
* TODO for v2 I guess
*/
public static function create(
APnutI $api,
string $prompt,
array $options,
int $max_options,
int $duration_minutes,
bool $is_anonymous,
bool $is_public
): Poll {
$options = array_filter($options);
$options = array_map(function ($v) {
return ['text' => $v];
}, $options);
$params = [
'duration' => $duration_minutes,
'options' => array_filter($options), #filters empty options
'prompt' => $prompt,
'type' => 'io.pnut.core.poll',
'is_anonymous' => $is_anonymous,
'is_public' => $is_public,
'max_options' => $max_options,
];
$api->logger->debug('Creating poll');
$api->logger->debug(json_encode($params));
$resp = $api->post('/polls', $params, 'application/json');
#TODO: Use getPollFromEndpoint
return new Poll($resp, $api);
}
public static function create(
APnutI $api,
string $prompt,
array $options,
int $max_options,
int $duration_minutes,
bool $is_anonymous,
bool $is_public
): Poll {
$options = array_filter($options);
$options = array_map(
function ($v) {
return ['text' => $v];
},
$options
);
$params = [
'duration' => $duration_minutes,
'options' => array_filter($options), #filters empty options
'prompt' => $prompt,
'type' => 'io.pnut.core.poll',
'is_anonymous' => $is_anonymous,
'is_public' => $is_public,
'max_options' => $max_options
];
$api->logger->debug('Creating poll');
$api->logger->debug(json_encode($params));
$resp = $api->post('/polls', $params, 'application/json');
#TODO: Use getPollFromEndpoint
return new Poll($resp, $api);
}
public static function makePollNoticeRaw(int $poll_id, string $poll_token)
{
return [
'io.pnut.core.poll-notice' => [
[
'+io.pnut.core.poll' => [
'poll_id' => $poll_id,
'poll_token' => $poll_token,
],
],
],
];
}
public static function makePollNoticeRaw(int $poll_id, string $poll_token)
{
return [
'io.pnut.core.poll-notice' => [
[
'+io.pnut.core.poll' => [
'poll_id' => $poll_id,
'poll_token' => $poll_token
]
]
]
];
}
public function __toString(): string
{
if (!empty($this->user)) {
$str = $this->user->username;
#$str = 'Unknown user';
} else {
$str = 'Unknown user';
}
return $str .
" asked: '" .
$this->prompt .
"', closed at " .
$this->closed_at->format('Y-m-d H:i:s T');
public function __toString(): string
{
if (!empty($this->user)) {
$str = $this->user->username;
#$str = 'Unknown user';
} else {
$str = 'Unknown user';
}
return $str
. " asked: '"
. $this->prompt
. "', closed at "
. $this->closed_at->format('Y-m-d H:i:s T');
}
}

View File

@ -52,17 +52,6 @@ class User
}
}
public function getPresenceString()
{
if ($this->presence === true) {
return "online";
} elseif ($this->presence === false) {
return "offline";
} else {
return "presence unknown";
}
}
public function getAvatarUrl(
?int $width = null,
?int $height = null

View File

@ -1,17 +0,0 @@
<?php
namespace APnutI\Logger;
use Monolog\Processor\ProcessorInterface;
use Monolog\LogRecord;
// See: https://stackoverflow.com/a/78901964
class LevelNamePaddingProcessor implements ProcessorInterface
{
public function __invoke(LogRecord $record): LogRecord
{
$record->extra['level_padded'] = str_pad($record->level->getName(), 5, ' ', STR_PAD_RIGHT);
return $record;
}
}

View File

@ -9,43 +9,43 @@ use APnutI\Exceptions\HttpPnutForbiddenException;
class Meta
{
public bool $more = false;
public ?int $max_id = null;
public ?int $min_id = null;
public ?int $code = -1;
public bool $more = false;
public ?int $max_id = null;
public ?int $min_id = null;
public ?int $code = -1;
public function __construct(array $json)
{
if (empty($json['meta'])) {
return;
}
$meta = (array)$json['meta'];
if (!empty($meta['more'])) {
$this->more = (bool)$meta['more'];
}
if (!empty($meta['max_id'])) {
$this->max_id = (int)$meta['max_id'];
}
if (!empty($meta['min_id'])) {
$this->min_id = (int)$meta['min_id'];
}
if (!empty($meta['code'])) {
$this->code = $meta['code'];
if ($this->code === 400) {
throw new PnutException($meta['error_message']);
}
if ($this->code === 401) {
throw new NotAuthorizedException($meta['error_message']);
}
if ($this->code === 403) {
throw new HttpPnutForbiddenException();
}
if ($this->code === 404) {
throw new NotFoundException();
}
if ($meta['code'] < 200 || $meta['code'] >= 300) {
throw new PnutException($meta['error_message']);
}
}
public function __construct(array $json)
{
if (empty($json['meta'])) {
return;
}
$meta = (array)$json['meta'];
if (!empty($meta['more'])) {
$this->more = (bool)$meta['more'];
}
if (!empty($meta['max_id'])) {
$this->max_id = (int)$meta['max_id'];
}
if (!empty($meta['min_id'])) {
$this->min_id = (int)$meta['min_id'];
}
if (!empty($meta['code'])) {
$this->code = $meta['code'];
if ($this->code === 400) {
throw new PnutException($meta['error_message']);
}
if ($this->code === 401) {
throw new NotAuthorizedException($meta['error_message']);
}
if ($this->code === 403) {
throw new HttpPnutForbiddenException();
}
if ($this->code === 404) {
throw new NotFoundException();
}
if ($meta['code'] < 200 || $meta['code'] >= 300) {
throw new PnutException($meta['error_message']);
}
}
}
}