]> git.mxchange.org Git - friendica.git/commitdiff
Move mod/fsuggest to src/Module/SuggestFriends
authornupplaPhil <admin+github@philipp.info>
Fri, 31 Jan 2020 22:50:46 +0000 (23:50 +0100)
committernupplaPhil <admin+github@philipp.info>
Fri, 31 Jan 2020 22:50:46 +0000 (23:50 +0100)
src/Collection/FSuggests.php [new file with mode: 0644]
src/DI.php
src/Model/FSuggest.php [new file with mode: 0644]
src/Module/FriendSuggest.php [new file with mode: 0644]
src/Protocol/ActivityPub/Transmitter.php
src/Repository/FSuggest.php [new file with mode: 0644]
src/Worker/Notifier.php
static/routes.config.php
view/templates/fsuggest.tpl [new file with mode: 0644]

diff --git a/src/Collection/FSuggests.php b/src/Collection/FSuggests.php
new file mode 100644 (file)
index 0000000..4ab51b6
--- /dev/null
@@ -0,0 +1,10 @@
+<?php
+
+namespace Friendica\Collection;
+
+use Friendica\BaseCollection;
+
+class FSuggests extends BaseCollection
+{
+
+}
index 65c82225d54c1f3757283e6781094701cd1b2724..1d092439f2d9560902b336f088285accddaeb794 100644 (file)
@@ -292,6 +292,14 @@ abstract class DI
        // "Repository" namespace
        //
 
+       /**
+        * @return Repository\FSuggest;
+        */
+       public static function fsuggest()
+       {
+               return self::$dice->create(Repository\FSuggest::class);
+       }
+
        /**
         * @return Repository\Introduction
         */
diff --git a/src/Model/FSuggest.php b/src/Model/FSuggest.php
new file mode 100644 (file)
index 0000000..e7b8df7
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+
+namespace Friendica\Model;
+
+use Friendica\BaseModel;
+
+/**
+ * Model for interacting with a friend suggest
+ *
+ * @property int uid
+ * @property int cid
+ * @property string name
+ * @property string url
+ * @property string request
+ * @property string photo
+ * @property string note
+ * @property string created
+ */
+class FSuggest extends BaseModel
+{
+
+}
diff --git a/src/Module/FriendSuggest.php b/src/Module/FriendSuggest.php
new file mode 100644 (file)
index 0000000..027a43b
--- /dev/null
@@ -0,0 +1,105 @@
+<?php
+
+namespace Friendica\Module;
+
+use Friendica\BaseModule;
+use Friendica\Core\Protocol;
+use Friendica\Core\Renderer;
+use Friendica\Core\Worker;
+use Friendica\DI;
+use Friendica\Model\Contact as ContactModel;
+use Friendica\Network\HTTPException\ForbiddenException;
+use Friendica\Network\HTTPException\NotFoundException;
+use Friendica\Util\DateTimeFormat;
+use Friendica\Util\Strings;
+use Friendica\Worker\Delivery;
+
+/**
+ * Suggest friends to a known contact
+ */
+class FriendSuggest extends BaseModule
+{
+       public static function init(array $parameters = [])
+       {
+               if (! local_user()) {
+                       throw new ForbiddenException(DI::l10n()->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'),
+               ]);
+       }
+}
index 23301456a98b178291927b1174f5a066b88822fc..1883fb1623538f73bd9317add10aed75b4df5995 100644 (file)
@@ -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 (file)
index 0000000..4567f0a
--- /dev/null
@@ -0,0 +1,74 @@
+<?php
+
+namespace Friendica\Repository;
+
+use Friendica\BaseRepository;
+use Friendica\Collection;
+use Friendica\Model;
+
+class FSuggest extends BaseRepository
+{
+       protected static $table_name = 'fsuggest';
+
+       protected static $model_class = Model\FSuggest::class;
+
+       protected static $collection_class = Collection\FSuggests::class;
+
+       /**
+        * @param array $data
+        * @return Model\FSuggest
+        */
+       protected function create(array $data)
+       {
+               return new Model\FSuggest($this->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);
+       }
+}
index d8c424ed036ed5d0310e755206965f4ed9a6d6c7..d611c54c96649fb02b928599e27b266d0bc1a0eb 100644 (file)
@@ -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) {
index 4aad69d8c7f5ec8e2162d973845f0c65d5cf6010..f3e8fdf83acd671056577833c9169bc3d466bcfb 100644 (file)
@@ -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 (file)
index 0000000..b6ee1fc
--- /dev/null
@@ -0,0 +1,9 @@
+<div class="generic-page-wrapper">
+       <h2>{{$fsuggest_title}}</h2>
+       <form id="fsuggest-form" action="fsuggest/{{$contact_id}}" method="post">
+               {{include file="field_select.tpl" field=$fsuggest_select}}
+               <div id="fsuggest-submit-wrapper">
+                       <input id="fsuggest-submit" type="submit" name="submit" value="{{$submit}}" />
+               </div>
+       </form>
+</div>