]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ActivityModeration/classes/Deleted_notice.php
Return the correct URI in Deleted_notice
[quix0rs-gnu-social.git] / plugins / ActivityModeration / classes / Deleted_notice.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, Control Yourself, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.     If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('GNUSOCIAL')) { exit(1); }
21
22 /**
23  * Table Definition for deleted_notice
24  */
25
26 class Deleted_notice extends Managed_DataObject
27 {
28     public $__table = 'deleted_notice';      // table name
29     public $id;                              // int(4)  primary_key not_null
30     public $profile_id;                      // int(4)   not_null
31     public $uri;                             // varchar(191)  unique_key   not 255 because utf8mb4 takes more space
32     public $act_uri;                         // varchar(191)  unique_key   not 255 because utf8mb4 takes more space
33     public $created;                         // datetime()   not_null
34     public $deleted;                         // datetime()   not_null
35
36     public static function schemaDef()
37     {
38         return array(
39             'fields' => array(
40                 'id' => array('type' => 'int', 'not null' => true, 'description' => 'identity of notice'),
41                 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'author of the notice'),
42                 'uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'URI of the deleted notice'),
43                 'act_uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'URI of the delete activity, may exist in notice table'),
44                 'act_created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date the notice record was created'),
45                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date the notice record was deleted'),
46             ),
47             'primary key' => array('id'),
48             'unique keys' => array(
49                 'deleted_notice_act_uri_key' => array('act_uri'),
50             ),
51             'indexes' => array(
52                 'deleted_notice_profile_id_idx' => array('profile_id'),
53             ),
54         );
55     }
56
57     public static function addNew(Notice $notice, Profile $actor=null)
58     {
59         if (is_null($actor)) {
60             $actor = $notice->getProfile();
61         }
62
63         if ($notice->getProfile()->hasRole(Profile_role::DELETED)) {
64             // Don't emit notices if the notice author is (being) deleted
65             return false;
66         }
67
68         $act = new Activity();
69         $act->type = ActivityObject::ACTIVITY;
70         $act->verb = ActivityVerb::DELETE;
71         $act->time = time();
72         $act->id   = self::newUri($actor, $notice);
73
74         $act->content = sprintf(_m('<a href="%1$s">%2$s</a> deleted notice <a href="%3$s">{{%4$s}}</a>.'),
75                             htmlspecialchars($actor->getUrl()),
76                             htmlspecialchars($actor->getBestName()),
77                             htmlspecialchars($notice->getUrl()),
78                             htmlspecialchars($notice->getUri())
79                            );
80
81         $act->actor = $actor->asActivityObject();
82         $act->target = new ActivityObject();    // We don't save the notice object, as it's supposed to be removed!
83         $act->target->id = $notice->getUri();
84         $act->objects = array(clone($act->target));
85
86         $url = $notice->getUrl();
87         $act->selfLink = $url;
88         $act->editLink = $url;
89
90         // This will make ActivityModeration run saveObjectFromActivity which adds
91         // a new Deleted_notice entry in the database as well as deletes the notice
92         // if the actor has permission to do so.
93         $stored = Notice::saveActivity($act, $actor);
94
95         return $stored;
96     }
97
98     static public function fromStored(Notice $stored)
99     {
100         $class = get_called_class();
101         $object = new $class;
102         $object->act_uri = $stored->getUri();
103         if (!$object->find(true)) {
104             throw new NoResultException($object);
105         }
106         return $object;
107     }
108
109     public function getActor()
110     {
111         return Profile::getByID($this->profile_id);
112     }
113
114     static public function getObjectType()
115     {
116         return 'activity';
117     }
118
119     protected $_stored = array();
120
121     public function getStored()
122     {
123         $uri = $this->getTargetUri();
124         if (!isset($this->_stored[$uri])) {
125             $stored = new Notice();
126             $stored->uri = $uri;
127             if (!$stored->find(true)) {
128                 throw new NoResultException($stored);
129             }
130             $this->_stored[$uri] = $stored;
131         }
132         return $this->_stored[$uri];
133     }
134
135     public function getTargetUri()
136     {
137         return $this->act_uri;
138     }
139
140     public function getUri()
141     {
142         return $this->uri;
143     }
144
145     public function asActivityObject(Profile $scoped=null)
146     {
147         $actobj = new ActivityObject();
148         $actobj->id = $this->getUri();
149         $actobj->type = ActivityObject::ACTIVITY;
150         $actobj->actor = $this->getActorObject();
151         $actobj->target = new ActivityObject();
152         $actobj->target->id = $this->getTargetUri();
153         $actobj->objects = array(clone($actobj->target));
154         $actobj->verb = ActivityVerb::DELETE;
155         $actobj->title = ActivityUtils::verbToTitle($actobj->verb);
156
157         $actor = $this->getActor();
158         $actobj->content = sprintf(_m('<a href="%1$s">%2$s</a> deleted notice {{%3$s}}.'),
159                             htmlspecialchars($actor->getUrl()),
160                             htmlspecialchars($actor->getBestName()),
161                             htmlspecialchars($actor->getTargetUri())
162                            );
163
164         return $actobj;
165     }
166
167     static public function extendActivity(Notice $stored, Activity $act, Profile $scoped=null)
168     {
169         $object = self::fromStored($stored);
170
171         $act->target = $object->asActivityObject();
172         $act->objects = array(clone($act->target));
173
174         $act->context->replyToID = $object->getTargetUri();
175         $act->title = ActivityUtils::verbToTitle($act->verb);
176     }
177
178     static function newUri(Profile $actor, Managed_DataObject $object, $created=null)
179     {
180         if (is_null($created)) {
181             $created = common_sql_now();
182         }
183         return TagURI::mint(strtolower(get_called_class()).':%d:%s:%d:%s',
184                                         $actor->getID(),
185                                         ActivityUtils::resolveUri($object->getObjectType(), true),
186                                         $object->getID(),
187                                         common_date_iso8601($created));
188     }
189
190     static public function beforeSchemaUpdate()
191     {
192         $table = strtolower(get_called_class());
193         $schema = Schema::get();
194         $schemadef = $schema->getTableDef($table);
195
196         // 2015-10-03 We change the meaning of the 'uri' field and move its 
197         // content to the 'act_uri' for the deleted activity. act_created is
198         // added too.
199         if (isset($schemadef['fields']['act_uri'])) {
200             // We already have the act_uri field, so no need to migrate to it.
201             return;
202         }
203         echo "\nFound old $table table, upgrading it to contain 'act_uri' and 'act_created' field...";
204
205         $schemadef['fields']['act_uri'] = array('type' => 'varchar', 'not null' => true, 'length' => 191, 'description' => 'URI of the delete activity, may exist in notice table');
206         $schemadef['fields']['act_created'] = array('type' => 'datetime', 'not null' => true, 'description' => 'date the notice record was created');
207         unset($schemadef['unique keys']);
208         $schema->ensureTable($table, $schemadef);
209
210         $deleted = new Deleted_notice();
211         $result = $deleted->find();
212         if ($result === false) {
213             print "\nFound no deleted_notice entries, continuing...";
214             return true;
215         }
216         print "\nFound $result deleted_notice entries, aligning with new database layout: ";
217         while($deleted->fetch()) {
218             $orig = clone($deleted);
219             $deleted->act_uri = $deleted->uri;
220             // this is a fake URI just to have something to put there to avoid NULL
221             $deleted->uri = TagURI::mint(strtolower(get_called_class()).':%d:%s:%s:%s',
222                                 $deleted->profile_id,
223                                 ActivityUtils::resolveUri(self::getObjectType(), true),
224                                 'unknown',
225                                 common_date_iso8601($deleted->created));
226             $deleted->act_created = $deleted->created;  // we don't actually know when the notice was created
227             $deleted->updateWithKeys($orig, 'id');
228             print ".";
229         }
230         print "DONE.\n";
231         print "Resuming core schema upgrade...";
232     }
233
234 }