From 34f4aedb87e403ef276fa780bcaaeb280336c0f4 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Fri, 31 Jan 2020 23:50:46 +0100 Subject: [PATCH 1/4] Move mod/fsuggest to src/Module/SuggestFriends --- src/Collection/FSuggests.php | 10 +++ src/DI.php | 8 ++ src/Model/FSuggest.php | 22 +++++ src/Module/FriendSuggest.php | 105 +++++++++++++++++++++++ src/Protocol/ActivityPub/Transmitter.php | 6 +- src/Repository/FSuggest.php | 74 ++++++++++++++++ src/Worker/Notifier.php | 8 +- static/routes.config.php | 1 + view/templates/fsuggest.tpl | 9 ++ 9 files changed, 236 insertions(+), 7 deletions(-) create mode 100644 src/Collection/FSuggests.php create mode 100644 src/Model/FSuggest.php create mode 100644 src/Module/FriendSuggest.php create mode 100644 src/Repository/FSuggest.php create mode 100644 view/templates/fsuggest.tpl diff --git a/src/Collection/FSuggests.php b/src/Collection/FSuggests.php new file mode 100644 index 000000000..4ab51b648 --- /dev/null +++ b/src/Collection/FSuggests.php @@ -0,0 +1,10 @@ +create(Repository\FSuggest::class); + } + /** * @return Repository\Introduction */ diff --git a/src/Model/FSuggest.php b/src/Model/FSuggest.php new file mode 100644 index 000000000..e7b8df7e2 --- /dev/null +++ b/src/Model/FSuggest.php @@ -0,0 +1,22 @@ +t('Permission denied.')); + } + } + + public static function post(array $parameters = []) + { + $cid = intval($parameters['contact']); + + // We do query the "uid" as well to ensure that it is our contact + if (!DI::dba()->exists('contact', ['id' => $cid, 'uid' => local_user()])) { + throw new NotFoundException(DI::l10n()->t('Contact not found.')); + } + + $suggest_contact_id = intval($_POST['suggest']); + if (empty($suggest_contact_id)) { + return; + } + + // We do query the "uid" as well to ensure that it is our contact + $contact = DI::dba()->selectFirst('contact', ['name', 'url', 'request', 'avatar'], ['id' => $suggest_contact_id, 'uid' => local_user()]); + if (empty($contact)) { + notice(DI::l10n()->t('Suggested contact not found.')); + return; + } + + $note = Strings::escapeHtml(trim($_POST['note'] ?? '')); + + $suggest = DI::fsuggest()->insert([ + 'uid' => local_user(), + 'cid' => $cid, + 'name' => $contact['name'], + 'url' => $contact['url'], + 'request' => $contact['request'], + 'photo' => $contact['avatar'], + 'note' => $note, + 'created' => DateTimeFormat::utcNow() + ]); + + Worker::add(PRIORITY_HIGH, 'Notifier', Delivery::SUGGESTION, $suggest->id); + + info(DI::l10n()->t('Friend suggestion sent.')); + } + + public static function content(array $parameters = []) + { + $cid = intval($parameters['contact']); + + $contact = DI::dba()->selectFirst('contact', [], ['id' => $cid, 'uid' => local_user()]); + if (empty($contact)) { + notice(DI::l10n()->t('Contact not found.')); + DI::baseUrl()->redirect(); + } + + $stmtContacts = ContactModel::select(['id', 'name'], [ + '`uid` = ? AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND NOT `deleted` AND `notify` != "" AND `id` != ? AND `networks` = ?', + local_user(), + $cid, + Protocol::DFRN, + ]); + + $formattedContacts = []; + + while ($contact = DI::dba()->fetch($stmtContacts)) { + $formattedContacts[$contact['id']] = $contact['name']; + } + + $tpl = Renderer::getMarkupTemplate('fsuggest.tpl'); + return Renderer::replaceMacros($tpl, [ + '$contact_id' => $cid, + '$fsuggest_title' => DI::l10n()->t('Suggest Friends'), + '$fsuggest_select' => [ + 'suggest', + DI::l10n()->t('Suggest a friend for %s', $contact['name']), + '', + '', + $formattedContacts, + ], + '$submit' => DI::l10n()->t('Submit'), + ]); + } +} diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index 23301456a..1883fb162 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -1510,14 +1510,14 @@ class Transmitter { $owner = User::getOwnerDataById($uid); - $suggestion = DBA::selectFirst('fsuggest', ['url', 'note', 'created'], ['id' => $suggestion_id]); + $suggestion = DI::fsuggest()->getById($suggestion_id); $data = ['@context' => ActivityPub::CONTEXT, 'id' => DI::baseUrl() . '/activity/' . System::createGUID(), 'type' => 'Announce', 'actor' => $owner['url'], - 'object' => $suggestion['url'], - 'content' => $suggestion['note'], + 'object' => $suggestion->url, + 'content' => $suggestion->note, 'instrument' => self::getService(), 'to' => [ActivityPub::PUBLIC_COLLECTION], 'cc' => []]; diff --git a/src/Repository/FSuggest.php b/src/Repository/FSuggest.php new file mode 100644 index 000000000..4567f0a5a --- /dev/null +++ b/src/Repository/FSuggest.php @@ -0,0 +1,74 @@ +dba, $this->logger, $data); + } + + /** + * Returns the Friend Suggest based on it's ID + * + * @param int $id The id of the fsuggest + * + * @return Model\FSuggest + * + * @throws \Friendica\Network\HTTPException\NotFoundException + */ + public function getById(int $id) + { + return $this->selectFirst(['id' => $id]); + } + + /** + * @param array $condition + * @return Model\FSuggest + * @throws \Friendica\Network\HTTPException\NotFoundException + */ + public function selectFirst(array $condition) + { + return parent::selectFirst($condition); + } + + /** + * @param array $condition + * @param array $params + * @return Collection\FSuggests + * @throws \Exception + */ + public function select(array $condition = [], array $params = []) + { + return parent::select($condition, $params); + } + + /** + * @param array $condition + * @param array $params + * @param int|null $max_id + * @param int|null $since_id + * @param int $limit + * @return Collection\FSuggests + * @throws \Exception + */ + public function selectByBoundaries(array $condition = [], array $params = [], int $max_id = null, int $since_id = null, int $limit = self::LIMIT) + { + return parent::selectByBoundaries($condition, $params, $max_id, $since_id, $limit); + } +} diff --git a/src/Worker/Notifier.php b/src/Worker/Notifier.php index d8c424ed0..d611c54c9 100644 --- a/src/Worker/Notifier.php +++ b/src/Worker/Notifier.php @@ -70,12 +70,12 @@ class Notifier 'APDelivery', $cmd, $target_id, $inbox, $uid); } } elseif ($cmd == Delivery::SUGGESTION) { - $suggest = DBA::selectFirst('fsuggest', ['uid', 'cid'], ['id' => $target_id]); - if (!DBA::isResult($suggest)) { + $suggest = DI::fsuggest()->getById($target_id); + if (empty($suggest)) { return; } - $uid = $suggest['uid']; - $recipients[] = $suggest['cid']; + $uid = $suggest->uid; + $recipients[] = $suggest->cid; } elseif ($cmd == Delivery::REMOVAL) { return self::notifySelfRemoval($target_id, $a->queue['priority'], $a->queue['created']); } elseif ($cmd == Delivery::RELOCATION) { diff --git a/static/routes.config.php b/static/routes.config.php index 4aad69d8c..f3e8fdf83 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -128,6 +128,7 @@ return [ '/followers/{owner}' => [Module\Followers::class, [R::GET]], '/following/{owner}' => [Module\Following::class, [R::GET]], '/friendica[/json]' => [Module\Friendica::class, [R::GET]], + '/fsuggest/{contact}' => [Module\FriendSuggest::class, [R::GET, R::POST]], '/group' => [ '[/]' => [Module\Group::class, [R::GET, R::POST]], diff --git a/view/templates/fsuggest.tpl b/view/templates/fsuggest.tpl new file mode 100644 index 000000000..b6ee1fcb3 --- /dev/null +++ b/view/templates/fsuggest.tpl @@ -0,0 +1,9 @@ +
+

{{$fsuggest_title}}

+
+ {{include file="field_select.tpl" field=$fsuggest_select}} +
+ +
+
+
From d4e836855bffb49c23f2d59f55b892080fc39559 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Fri, 31 Jan 2020 23:51:11 +0100 Subject: [PATCH 2/4] Remove deprecated code --- mod/fsuggest.php | 96 ----------------------------- src/Core/ACL.php | 113 ----------------------------------- src/Module/FriendSuggest.php | 26 ++++---- 3 files changed, 13 insertions(+), 222 deletions(-) delete mode 100644 mod/fsuggest.php diff --git a/mod/fsuggest.php b/mod/fsuggest.php deleted file mode 100644 index 73b0f09e8..000000000 --- a/mod/fsuggest.php +++ /dev/null @@ -1,96 +0,0 @@ -argc != 2) { - return; - } - - $contact_id = intval($a->argv[1]); - if (empty($contact_id)) { - return; - } - - // We do query the "uid" as well to ensure that it is our contact - if (!DBA::exists('contact', ['id' => $contact_id, 'uid' => local_user()])) { - notice(DI::l10n()->t('Contact not found.') . EOL); - return; - } - - $suggest_contact_id = intval($_POST['suggest']); - if (empty($suggest_contact_id)) { - return; - } - - // We do query the "uid" as well to ensure that it is our contact - $contact = DBA::selectFirst('contact', ['name', 'url', 'request', 'avatar'], ['id' => $suggest_contact_id, 'uid' => local_user()]); - if (!DBA::isResult($contact)) { - notice(DI::l10n()->t('Suggested contact not found.') . EOL); - return; - } - - $note = Strings::escapeHtml(trim($_POST['note'] ?? '')); - - $fields = ['uid' => local_user(),'cid' => $contact_id, 'name' => $contact['name'], - 'url' => $contact['url'], 'request' => $contact['request'], - 'photo' => $contact['avatar'], 'note' => $note, 'created' => DateTimeFormat::utcNow()]; - DBA::insert('fsuggest', $fields); - - Worker::add(PRIORITY_HIGH, 'Notifier', Delivery::SUGGESTION, DBA::lastInsertId()); - - info(DI::l10n()->t('Friend suggestion sent.') . EOL); -} - -function fsuggest_content(App $a) -{ - if (! local_user()) { - notice(DI::l10n()->t('Permission denied.') . EOL); - return; - } - - if ($a->argc != 2) { - return; - } - - $contact_id = intval($a->argv[1]); - - $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => local_user()]); - if (! DBA::isResult($contact)) { - notice(DI::l10n()->t('Contact not found.') . EOL); - return; - } - - $o = '

' . DI::l10n()->t('Suggest Friends') . '

'; - - $o .= '
' . DI::l10n()->t('Suggest a friend for %s', $contact['name']) . '
'; - - $o .= '
'; - - $o .= ACL::getSuggestContactSelectHTML( - 'suggest', - 'suggest-select', - ['size' => 4, 'exclude' => $contact_id, 'networks' => 'DFRN_ONLY', 'single' => true] - ); - - - $o .= '
'; - $o .= '
'; - - return $o; -} diff --git a/src/Core/ACL.php b/src/Core/ACL.php index cfe46e770..b60421877 100644 --- a/src/Core/ACL.php +++ b/src/Core/ACL.php @@ -19,119 +19,6 @@ use Friendica\Model\Group; */ class ACL { - /** - * Returns a select input tag with all the contact of the local user - * - * @param string $selname Name attribute of the select input tag - * @param string $selclass Class attribute of the select input tag - * @param array $options Available options: - * - size: length of the select box - * - mutual_friends: Only used for the hook - * - single: Only used for the hook - * - exclude: Only used for the hook - * @param array $preselected Contact ID that should be already selected - * @return string - * @throws \Exception - */ - public static function getSuggestContactSelectHTML($selname, $selclass, array $options = [], array $preselected = []) - { - $a = DI::app(); - - $networks = null; - - $size = ($options['size'] ?? 0) ?: 4; - $mutual = !empty($options['mutual_friends']); - $single = !empty($options['single']) && empty($options['multiple']); - $exclude = $options['exclude'] ?? false; - - switch (($options['networks'] ?? '') ?: Protocol::PHANTOM) { - case 'DFRN_ONLY': - $networks = [Protocol::DFRN]; - break; - - case 'PRIVATE': - $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA]; - break; - - case 'TWO_WAY': - if (!empty($a->user['prvnets'])) { - $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA]; - } else { - $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA, Protocol::OSTATUS]; - } - break; - - default: /// @TODO Maybe log this call? - break; - } - - $x = ['options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks]; - - Hook::callAll('contact_select_options', $x); - - $o = ''; - - $sql_extra = ''; - - if (!empty($x['mutual'])) { - $sql_extra .= sprintf(" AND `rel` = %d ", intval(Contact::FRIEND)); - } - - if (!empty($x['exclude'])) { - $sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude'])); - } - - if (!empty($x['networks'])) { - /// @TODO rewrite to foreach() - array_walk($x['networks'], function (&$value) { - $value = "'" . DBA::escape($value) . "'"; - }); - $str_nets = implode(',', $x['networks']); - $sql_extra .= " AND `network` IN ( $str_nets ) "; - } - - $tabindex = (!empty($options['tabindex']) ? 'tabindex="' . $options["tabindex"] . '"' : ''); - - if (!empty($x['single'])) { - $o .= "\r\n"; - } - - $stmt = DBA::p("SELECT `id`, `name`, `url`, `network` FROM `contact` - WHERE `uid` = ? AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND NOT `deleted` AND `notify` != '' - $sql_extra - ORDER BY `name` ASC ", intval(local_user()) - ); - - $contacts = DBA::toArray($stmt); - - $arr = ['contact' => $contacts, 'entry' => $o]; - - // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow' - Hook::callAll(DI::module()->getName() . '_pre_' . $selname, $arr); - - if (DBA::isResult($contacts)) { - foreach ($contacts as $contact) { - if (in_array($contact['id'], $preselected)) { - $selected = ' selected="selected" '; - } else { - $selected = ''; - } - - $trimmed = mb_substr($contact['name'], 0, 20); - - $o .= "\r\n"; - } - } - - $o .= '' . PHP_EOL; - - Hook::callAll(DI::module()->getName() . '_post_' . $selname, $o); - - return $o; - } - /** * Returns a select input tag with all the contact of the local user * diff --git a/src/Module/FriendSuggest.php b/src/Module/FriendSuggest.php index 027a43b36..b2a76300a 100644 --- a/src/Module/FriendSuggest.php +++ b/src/Module/FriendSuggest.php @@ -21,7 +21,7 @@ class FriendSuggest extends BaseModule { public static function init(array $parameters = []) { - if (! local_user()) { + if (!local_user()) { throw new ForbiddenException(DI::l10n()->t('Permission denied.')); } } @@ -50,13 +50,13 @@ class FriendSuggest extends BaseModule $note = Strings::escapeHtml(trim($_POST['note'] ?? '')); $suggest = DI::fsuggest()->insert([ - 'uid' => local_user(), - 'cid' => $cid, - 'name' => $contact['name'], - 'url' => $contact['url'], + 'uid' => local_user(), + 'cid' => $cid, + 'name' => $contact['name'], + 'url' => $contact['url'], 'request' => $contact['request'], - 'photo' => $contact['avatar'], - 'note' => $note, + 'photo' => $contact['avatar'], + 'note' => $note, 'created' => DateTimeFormat::utcNow() ]); @@ -75,8 +75,8 @@ class FriendSuggest extends BaseModule DI::baseUrl()->redirect(); } - $stmtContacts = ContactModel::select(['id', 'name'], [ - '`uid` = ? AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND NOT `deleted` AND `notify` != "" AND `id` != ? AND `networks` = ?', + $contacts = ContactModel::selectToArray(['id', 'name'], [ + '`uid` = ? AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND NOT `deleted` AND `notify` != "" AND `id` != ? AND `network` = ?', local_user(), $cid, Protocol::DFRN, @@ -84,14 +84,14 @@ class FriendSuggest extends BaseModule $formattedContacts = []; - while ($contact = DI::dba()->fetch($stmtContacts)) { + foreach ($contacts as $contact) { $formattedContacts[$contact['id']] = $contact['name']; } $tpl = Renderer::getMarkupTemplate('fsuggest.tpl'); return Renderer::replaceMacros($tpl, [ - '$contact_id' => $cid, - '$fsuggest_title' => DI::l10n()->t('Suggest Friends'), + '$contact_id' => $cid, + '$fsuggest_title' => DI::l10n()->t('Suggest Friends'), '$fsuggest_select' => [ 'suggest', DI::l10n()->t('Suggest a friend for %s', $contact['name']), @@ -99,7 +99,7 @@ class FriendSuggest extends BaseModule '', $formattedContacts, ], - '$submit' => DI::l10n()->t('Submit'), + '$submit' => DI::l10n()->t('Submit'), ]); } } From c5653d30f332e4148539fe575bc754e411739f56 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Sat, 1 Feb 2020 00:22:56 +0100 Subject: [PATCH 3/4] add regex for route --- static/routes.config.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/static/routes.config.php b/static/routes.config.php index f3e8fdf83..eaaa3bd94 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -128,7 +128,8 @@ return [ '/followers/{owner}' => [Module\Followers::class, [R::GET]], '/following/{owner}' => [Module\Following::class, [R::GET]], '/friendica[/json]' => [Module\Friendica::class, [R::GET]], - '/fsuggest/{contact}' => [Module\FriendSuggest::class, [R::GET, R::POST]], + + '/fsuggest/{contact:\d+}' => [Module\FriendSuggest::class, [R::GET, R::POST]], '/group' => [ '[/]' => [Module\Group::class, [R::GET, R::POST]], From f21196df7ca32bc19802db35d098eeae3e87a0d2 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Sat, 1 Feb 2020 00:27:46 +0100 Subject: [PATCH 4/4] improvements --- src/Model/FSuggest.php | 2 +- src/Module/FriendSuggest.php | 10 +++++++++- src/Worker/Notifier.php | 3 --- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Model/FSuggest.php b/src/Model/FSuggest.php index e7b8df7e2..a910eb30c 100644 --- a/src/Model/FSuggest.php +++ b/src/Model/FSuggest.php @@ -5,7 +5,7 @@ namespace Friendica\Model; use Friendica\BaseModel; /** - * Model for interacting with a friend suggest + * Model for interacting with a friend suggestion * * @property int uid * @property int cid diff --git a/src/Module/FriendSuggest.php b/src/Module/FriendSuggest.php index b2a76300a..dcb93ee7e 100644 --- a/src/Module/FriendSuggest.php +++ b/src/Module/FriendSuggest.php @@ -76,7 +76,15 @@ class FriendSuggest extends BaseModule } $contacts = ContactModel::selectToArray(['id', 'name'], [ - '`uid` = ? AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND NOT `deleted` AND `notify` != "" AND `id` != ? AND `network` = ?', + '`uid` = ? + AND `id` != ? + AND `network` = ? + AND NOT `self` + AND NOT `blocked` + AND NOT `pending` + AND NOT `archive` + AND NOT `deleted` + AND `notify` != ""', local_user(), $cid, Protocol::DFRN, diff --git a/src/Worker/Notifier.php b/src/Worker/Notifier.php index d611c54c9..85098d1de 100644 --- a/src/Worker/Notifier.php +++ b/src/Worker/Notifier.php @@ -71,9 +71,6 @@ class Notifier } } elseif ($cmd == Delivery::SUGGESTION) { $suggest = DI::fsuggest()->getById($target_id); - if (empty($suggest)) { - return; - } $uid = $suggest->uid; $recipients[] = $suggest->cid; } elseif ($cmd == Delivery::REMOVAL) {