]> git.mxchange.org Git - friendica.git/blob - src/Model/GContact.php
Add common relationship methods to Model\ContactRelation
[friendica.git] / src / Model / GContact.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 use Exception;
25 use Friendica\Core\Protocol;
26 use Friendica\Database\DBA;
27 use Friendica\Util\DateTimeFormat;
28
29 /**
30  * This class handles GlobalContact related functions
31  */
32 class GContact
33 {
34         /**
35          * @param integer $uid id
36          * @param integer $cid id
37          * @return integer
38          * @throws Exception
39          */
40         public static function countCommonFriends($uid, $cid)
41         {
42                 $sourceId = Contact::getPublicIdByUserId($uid);
43
44                 $targetIds = Contact::getPublicAndUserContacID($cid, $uid);
45
46                 $condition = [
47                         'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
48                         $sourceId,
49                 ];
50
51                 return ContactRelation::countCommonFollows($sourceId, $targetIds['public'] ?? 0, $condition);
52         }
53
54         /**
55          * @param integer $uid  id
56          * @param integer $zcid zcid
57          * @return integer
58          * @throws Exception
59          */
60         public static function countCommonFriendsZcid($uid, $zcid)
61         {
62                 $sourceId = Contact::getPublicIdByUserId($uid);
63
64                 $targetPublicContact = DI::dba()->fetchFirst("
65 SELECT `id`
66 FROM `contact` c 
67 JOIN `gcontact` z ON z.`nurl` = c.`nurl`
68 AND z.`id` = ?
69 AND c.`uid` = 0
70 LIMIT 1",
71                         $zcid
72                 );
73
74                 return ContactRelation::countCommonFollowers($sourceId, $targetPublicContact['id'] ?? 0);
75         }
76
77         /**
78          * Returns the cross-section between the local user contacts and one of their contact's own relationships
79          * as known by the local node.
80          *
81          * @param integer $uid     local user id
82          * @param integer $cid     user contact id to compare friends with
83          * @param integer $start   optional, default 0
84          * @param integer $limit   optional, default 9999
85          * @param boolean $shuffle optional, default false
86          * @return array
87          * @throws Exception
88          */
89         public static function commonFriends($uid, $cid, $start = 0, $limit = 9999, $shuffle = false)
90         {
91                 $sourceId = Contact::getPublicIdByUserId($uid);
92
93                 $targetIds = Contact::getPublicAndUserContacID($cid, $uid);
94
95                 $condition = [
96                         'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
97                         $sourceId,
98                 ];
99
100                 return ContactRelation::listCommonFollows($sourceId, $targetIds['public'] ?? 0, $condition, $limit, $start, $shuffle);
101         }
102
103         /**
104          * Returns the cross-section between a local user and a remote visitor contact's own relationships
105          * as known by the local node.
106          *
107          * @param integer $uid     local user id
108          * @param integer $zcid    remote visitor contact zcid
109          * @param integer $start   optional, default 0
110          * @param integer $limit   optional, default 9999
111          * @param boolean $shuffle optional, default false
112          * @return array
113          * @throws Exception
114          */
115         public static function commonFriendsZcid($uid, $zcid, $start = 0, $limit = 9999, $shuffle = false)
116         {
117                 $sourceId = Contact::getPublicIdByUserId($uid);
118
119                 $targetPublicContact = DI::dba()->fetchFirst("
120 SELECT c.`id`
121 FROM `contact` c 
122 JOIN `gcontact` z ON z.`nurl` = c.`nurl`
123 AND z.`id` = ?
124 AND c.`uid` = 0
125 LIMIT 1",
126                         $zcid
127                 );
128
129                 return ContactRelation::listCommonFollows($sourceId, $targetPublicContact['id'] ?? 0, [], $limit, $start, $shuffle);
130         }
131
132         /**
133          * @param integer $uid user
134          * @param integer $cid cid
135          * @return integer
136          * @throws Exception
137          */
138         public static function countAllFriends($uid, $cid)
139         {
140                 $cids = Contact::getPublicAndUserContacID($cid, $uid);
141
142                 return ContactRelation::countFollows($cids['public'] ?? 0);
143         }
144
145         /**
146          * @param integer $uid   user
147          * @param integer $cid   cid
148          * @param integer $start optional, default 0
149          * @param integer $limit optional, default 80
150          * @return array
151          * @throws Exception
152          */
153         public static function allFriends($uid, $cid, $start = 0, $limit = 80)
154         {
155                 $cids = Contact::getPublicAndUserContacID($cid, $uid);
156
157                 return ContactRelation::listFollows($cids['public'] ?? 0, [], $limit, $start);
158         }
159 }