]> git.mxchange.org Git - friendica.git/blob - src/Model/UserItem.php
1888906fa75e1b33e62398b9bf6382d514116de6
[friendica.git] / src / Model / UserItem.php
1 <?php
2
3 /**
4  * @file src/Model/UserItem.php
5  */
6
7 namespace Friendica\Model;
8
9 use Friendica\Core\Hook;
10 use Friendica\Database\DBA;
11 use Friendica\DI;
12 use Friendica\Util\Strings;
13
14 class UserItem
15 {
16         /**
17          * Checks an item for notifications and sets the "notification-type" field
18          *
19          * @param array $item The message array that is checked for notifications
20          * @param int   $uid  User ID
21          */
22         public static function setNotification($item, $uid)
23         {
24                 if (self::checkShared($item, $uid)) {
25                         echo "shared\n";
26                 }
27
28                 $profiles = self::getProfileForUser($uid);
29
30                 if (self::checkTagged($item, $uid, $profiles)) {
31                         echo "tagged\n";
32                 }
33
34                 if (self::checkCommented($item, $uid, $profiles)) {
35                         echo "commented\n";
36                 }
37         }
38
39         private static function getProfileForUser($uid)
40         {
41                 $notification_data = ["uid" => $uid, "profiles" => []];
42                 Hook::callAll('check_item_notification', $notification_data);
43
44                 $profiles = $notification_data["profiles"];
45
46                 $fields = ['nickname'];
47                 $user = DBA::selectFirst('user', $fields, ['uid' => $uid]);
48                 if (!DBA::isResult($user)) {
49                         return false;
50                 }
51
52                 $owner = DBA::selectFirst('contact', ['url'], ['self' => true, 'uid' => $uid]);
53                 if (!DBA::isResult($owner)) {
54                         return false;
55                 }
56
57                 // This is our regular URL format
58                 $profiles[] = $owner["url"];
59
60                 // Notifications from Diaspora are often with an URL in the Diaspora format
61                 $profiles[] = DI::baseUrl()."/u/".$user["nickname"];
62
63                 $profiles2 = [];
64
65                 foreach ($profiles AS $profile) {
66                         // Check for invalid profile urls. 13 should be the shortest possible profile length:
67                         // http://a.bc/d
68                         // Additionally check for invalid urls that would return the normalised value "http:"
69                         if ((strlen($profile) >= 13) && (Strings::normaliseLink($profile) != "http:")) {
70                                 if (!in_array($profile, $profiles2))
71                                         $profiles2[] = $profile;
72
73                                 $profile = Strings::normaliseLink($profile);
74                                 if (!in_array($profile, $profiles2))
75                                         $profiles2[] = $profile;
76
77                                 $profile = str_replace("http://", "https://", $profile);
78                                 if (!in_array($profile, $profiles2))
79                                         $profiles2[] = $profile;
80                         }
81                 }
82
83                 return $profiles2;
84         }
85
86         private static function checkShared($item, $uid)
87         {
88                 if ($item['gravity'] != GRAVITY_PARENT) {
89                         return false;
90                 }
91
92                 // Send a notification for every new post?
93                 // Either the contact had posted something directly
94                 if (DBA::exists('contact', ['id' => $item['contact-id'], 'notify_new_posts' => true])) {
95                         return true;
96                 }
97
98                 // Or the contact is a mentioned forum
99                 $tags = DBA::select('term', ['url'], ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => TERM_MENTION, 'uid' => $uid]);
100                 while ($tag = DBA::fetch($tags)) {
101                         $condition = ['nurl' => Strings::normaliseLink($tag["url"]), 'uid' => $uid, 'notify_new_posts' => true, 'contact-type' => Contact::TYPE_COMMUNITY];
102                         if (DBA::exists('contact', $condition)) {
103                                 return true;
104                         }
105                 }
106
107                 return false;
108         }
109
110         // Is the user mentioned in this post?
111         private static function checkTagged($item, $uid, $profiles)
112         {
113                 foreach ($profiles AS $profile) {
114                         if (strpos($item["tag"], "=".$profile."]") || strpos($item["body"], "=".$profile."]"))
115                                 return true;
116                 }
117
118                 return false;
119         }
120
121         // Fetch all contacts for the given profiles
122         private static function checkCommented($item, $uid, $profiles)
123         {
124                 // Is it a post that the user had started?
125                 $fields = ['ignored', 'mention'];
126                 $thread = Item::selectFirstThreadForUser($uid, $fields, ['iid' => $item["parent"], 'deleted' => false]);
127                 if ($thread['mention'] && !$thread['ignored']) {
128                         return true;
129                 }
130
131                 $contacts = [];
132                 $ret = DBA::select('contact', ['id'], ['uid' => 0, 'nurl' => $profiles]);
133                 while ($contact = DBA::fetch($ret)) {
134                         $contacts[] = $contact['id'];
135                 }
136                 DBA::close($ret);
137
138                 // And now we check for participation of one of our contacts in the thread
139                 $condition = ['parent' => $item["parent"], 'author-id' => $contacts, 'deleted' => false];
140
141                 if (!$thread['ignored'] && Item::exists($condition)) {
142                         return true;
143                 }
144
145                 return false;
146         }
147 }