]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ActivityModeration/classes/Deleted_notice.php
f834f88bb7dd7e3f337d1c8a62affdf679410357
[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_created;                     // datetime()   not_null
33     public $created;                         // datetime()   not_null
34
35     public static function schemaDef()
36     {
37         return array(
38             'fields' => array(
39                 'id' => array('type' => 'int', 'not null' => true, 'description' => 'notice ID'),
40                 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'profile that deleted the notice'),
41                 'uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'URI of the deleted notice'),
42                 'act_created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date the notice record was created'),
43                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date the notice record was deleted'),
44             ),
45             'primary key' => array('id'),
46             'unique keys' => array(
47                 'deleted_notice_uri_key' => array('uri'),
48             ),
49             'indexes' => array(
50                 'deleted_notice_profile_id_idx' => array('profile_id'),
51             ),
52         );
53     }
54
55     public static function addNew(Notice $notice, Profile $actor=null)
56     {
57         if (is_null($actor)) {
58             $actor = $notice->getProfile();
59         }
60
61         if ($notice->getProfile()->hasRole(Profile_role::DELETED)) {
62             // Don't emit notices if the notice author is (being) deleted
63             return false;
64         }
65
66         $act = new Activity();
67         $act->verb = ActivityVerb::DELETE;
68         $act->time = time();
69         $act->id   = $notice->getUri();
70
71         $act->content = sprintf(_m('<a href="%1$s">%2$s</a> deleted notice <a href="%3$s">{{%4$s}}</a>.'),
72                             htmlspecialchars($actor->getUrl()),
73                             htmlspecialchars($actor->getBestName()),
74                             htmlspecialchars($notice->getUrl()),
75                             htmlspecialchars($notice->getUri())
76                            );
77
78         $act->actor = $actor->asActivityObject();
79         $act->target = new ActivityObject();    // We don't save the notice object, as it's supposed to be removed!
80         $act->target->id = $notice->getUri();
81         $act->target->type = $notice->getObjectType();
82         $act->objects = array(clone($act->target));
83
84         $url = $notice->getUrl();
85         $act->selfLink = $url;
86         $act->editLink = $url;
87
88         // This will make ActivityModeration run saveObjectFromActivity which adds
89         // a new Deleted_notice entry in the database as well as deletes the notice
90         // if the actor has permission to do so.
91         $stored = Notice::saveActivity($act, $actor);
92
93         return $stored;
94     }
95
96     static public function fromStored(Notice $stored)
97     {
98         $class = get_called_class();
99         return self::getByPK(array('uri' => $stored->getUri()));
100     }
101
102     // The one who deleted the notice, not the notice's author
103     public function getActor()
104     {
105         return Profile::getByID($this->profile_id);
106     }
107
108     // As above: The one who deleted the notice, not the notice's author
109     public function getActorObject()
110     {
111         return $this->getActor()->asActivityObject();
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->getUri();
124         if (!isset($this->_stored[$uri])) {
125             $this->_stored[$uri] = Notice::getByPK(array('uri' => $uri));
126         }
127         return $this->_stored[$uri];
128     }
129
130     public function getUri()
131     {
132         return $this->uri;
133     }
134
135     public function asActivityObject(Profile $scoped=null)
136     {
137         $actobj = new ActivityObject();
138         $actobj->id = $this->getUri();
139         $actobj->type = ActivityObject::ACTIVITY;
140         $actobj->actor = $this->getActorObject();
141         $actobj->target = new ActivityObject();
142         $actobj->target->id = $this->getUri();
143         // FIXME: actobj->target->type? as in extendActivity, and actobj->target->actor maybe?
144         $actobj->objects = array(clone($actobj->target));
145         $actobj->verb = ActivityVerb::DELETE;
146         $actobj->title = ActivityUtils::verbToTitle($actobj->verb);
147
148         $actor = $this->getActor();
149         $actobj->content = sprintf(_m('<a href="%1$s">%2$s</a> deleted notice {{%3$s}}.'),
150                             htmlspecialchars($actor->getUrl()),
151                             htmlspecialchars($actor->getBestName()),
152                             htmlspecialchars($this->getUri())
153                            );
154
155         return $actobj;
156     }
157
158     static public function extendActivity(Notice $stored, Activity $act, Profile $scoped=null)
159     {
160         // the original notice id and type is still stored in the Notice table
161         // so we use that information to describe the delete activity
162         $act->target = new ActivityObject();
163         $act->target->id = $stored->getUri();
164         $act->target->type = $stored->getObjectType();
165         $act->objects = array(clone($act->target));
166
167         $act->title = ActivityUtils::verbToTitle($act->verb);
168     }
169
170     static public function beforeSchemaUpdate()
171     {
172         $table = strtolower(get_called_class());
173         $schema = Schema::get();
174         $schemadef = $schema->getTableDef($table);
175
176         // 2015-12-31 If we have the act_uri field we want to remove it
177         // since there's no difference in delete verbs and the original URI
178         // but the act_created field stays.
179         if (!isset($schemadef['fields']['act_uri']) && isset($schemadef['fields']['act_created'])) {
180             // We don't have an act_uri field, and we also have act_created, so no need to migrate.
181             return;
182         } elseif (isset($schemadef['fields']['act_uri']) && !isset($schemadef['fields']['act_created'])) {
183             throw new ServerException('Something is wrong with your database, you have the act_uri field but NOT act_created in deleted_notice!');
184         }
185
186         if (!isset($schemadef['fields']['act_created'])) {
187             // this is a "normal" upgrade from StatusNet for example
188             echo "\nFound old $table table, upgrading it to add 'act_created' field...";
189
190             $schemadef['fields']['act_created'] = array('type' => 'datetime', 'not null' => true, 'description' => 'datetime the notice record was created');
191             $schema->ensureTable($table, $schemadef);
192
193             $deleted = new Deleted_notice();
194             // we don't actually know when the notice was created for the old ones
195             $deleted->query('UPDATE deleted_notice SET act_created=created;');
196         } else {
197             // 2015-10-03 For a while we had act_uri and act_created fields which
198             // apparently wasn't necessary.
199             echo "\nFound old $table table, upgrading it to remove 'act_uri' field...";
200
201             // we stored what should be in 'uri' in the 'act_uri' field for some night-coding reason.
202             $deleted = new Deleted_notice();
203             $deleted->query('UPDATE deleted_notice SET uri=act_uri;');
204         }
205         print "DONE.\n";
206         print "Resuming core schema upgrade...";
207     }
208
209 }