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