]> git.mxchange.org Git - friendica.git/blob - mod/subthread.php
Merge pull request #5022 from Rudloff/feature/test_api
[friendica.git] / mod / subthread.php
1 <?php
2 /**
3  * @file mod/subthread.php
4  */
5 use Friendica\App;
6 use Friendica\Core\Addon;
7 use Friendica\Core\L10n;
8 use Friendica\Core\System;
9 use Friendica\Database\DBM;
10 use Friendica\Model\Item;
11
12 require_once 'include/security.php';
13 require_once 'include/items.php';
14
15 function subthread_content(App $a) {
16
17         if(! local_user() && ! remote_user()) {
18                 return;
19         }
20
21         $activity = ACTIVITY_FOLLOW;
22
23         $item_id = (($a->argc > 1) ? notags(trim($a->argv[1])) : 0);
24
25         $r = q("SELECT * FROM `item` WHERE `parent` = '%s' OR `parent-uri` = '%s' and parent = id LIMIT 1",
26                 dbesc($item_id),
27                 dbesc($item_id)
28         );
29
30         if(! $item_id || (! DBM::is_result($r))) {
31                 logger('subthread: no item ' . $item_id);
32                 return;
33         }
34
35         $item = $r[0];
36
37         $owner_uid = $item['uid'];
38
39         if(! can_write_wall($owner_uid)) {
40                 return;
41         }
42
43         $remote_owner = null;
44
45         if(! $item['wall']) {
46                 // The top level post may have been written by somebody on another system
47                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
48                         intval($item['contact-id']),
49                         intval($item['uid'])
50                 );
51                 if (! DBM::is_result($r)) {
52                         return;
53                 }
54                 if (! $r[0]['self']) {
55                         $remote_owner = $r[0];
56                 }
57         }
58
59         $owner = null;
60         // this represents the post owner on this system.
61
62         $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
63                 WHERE `contact`.`self` = 1 AND `contact`.`uid` = %d LIMIT 1",
64                 intval($owner_uid)
65         );
66
67         if (DBM::is_result($r)) {
68                 $owner = $r[0];
69         }
70
71         if (! $owner) {
72                 logger('like: no owner');
73                 return;
74         }
75
76         if (! $remote_owner) {
77                 $remote_owner = $owner;
78         }
79
80         $contact = null;
81         // This represents the person posting
82
83         if ((local_user()) && (local_user() == $owner_uid)) {
84                 $contact = $owner;
85         } else {
86                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
87                         intval($_SESSION['visitor_id']),
88                         intval($owner_uid)
89                 );
90
91                 if (DBM::is_result($r)) {
92                         $contact = $r[0];
93                 }
94         }
95         if (! $contact) {
96                 return;
97         }
98
99         $uri = item_new_uri($a->get_hostname(),$owner_uid);
100
101         $post_type = (($item['resource-id']) ? L10n::t('photo') : L10n::t('status'));
102         $objtype = (($item['resource-id']) ? ACTIVITY_OBJ_IMAGE : ACTIVITY_OBJ_NOTE );
103         $link = xmlify('<link rel="alternate" type="text/html" href="' . System::baseUrl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . '" />' . "\n") ;
104         $body = $item['body'];
105
106         $obj = <<< EOT
107
108         <object>
109                 <type>$objtype</type>
110                 <local>1</local>
111                 <id>{$item['uri']}</id>
112                 <link>$link</link>
113                 <title></title>
114                 <content>$body</content>
115         </object>
116 EOT;
117         $bodyverb = L10n::t('%1$s is following %2$s\'s %3$s');
118
119         if (! isset($bodyverb)) {
120                 return;
121         }
122
123         $arr = [];
124
125         $arr['guid'] = get_guid(32);
126         $arr['uri'] = $uri;
127         $arr['uid'] = $owner_uid;
128         $arr['contact-id'] = $contact['id'];
129         $arr['type'] = 'activity';
130         $arr['wall'] = $item['wall'];
131         $arr['origin'] = 1;
132         $arr['gravity'] = GRAVITY_LIKE;
133         $arr['parent'] = $item['id'];
134         $arr['parent-uri'] = $item['uri'];
135         $arr['thr-parent'] = $item['uri'];
136         $arr['owner-name'] = $remote_owner['name'];
137         $arr['owner-link'] = $remote_owner['url'];
138         $arr['owner-avatar'] = $remote_owner['thumb'];
139         $arr['author-name'] = $contact['name'];
140         $arr['author-link'] = $contact['url'];
141         $arr['author-avatar'] = $contact['thumb'];
142
143         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
144         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
145         $plink = '[url=' . System::baseUrl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . ']' . $post_type . '[/url]';
146         $arr['body'] =  sprintf( $bodyverb, $ulink, $alink, $plink );
147
148         $arr['verb'] = $activity;
149         $arr['object-type'] = $objtype;
150         $arr['object'] = $obj;
151         $arr['allow_cid'] = $item['allow_cid'];
152         $arr['allow_gid'] = $item['allow_gid'];
153         $arr['deny_cid'] = $item['deny_cid'];
154         $arr['deny_gid'] = $item['deny_gid'];
155         $arr['visible'] = 1;
156         $arr['unseen'] = 1;
157
158         $post_id = Item::insert($arr);
159
160         if (!$item['visible']) {
161                 Item::update(['visible' => true], ['id' => $item['id']]);
162         }
163
164         $arr['id'] = $post_id;
165
166         Addon::callHooks('post_local_end', $arr);
167
168         killme();
169
170 }
171
172