]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ActivityModeration/classes/Deleted_notice.php
Some work on ActivityModeration with notice deletion
[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)
58     {
59         $actor = $notice->getProfile();
60
61         if ($actor->hasRole(Profile_role::DELETED)) {
62             // Don't emit notices if the user is deleted
63             return true;
64         }
65
66         $act = new Activity();
67         $act->type = ActivityObject::ACTIVITY;
68         $act->verb = ActivityVerb::DELETE;
69         $act->time = time();
70         $act->id   = TagURI::mint('deleted_notice:%d:%d:%s',
71                             $actor->getID(),
72                             $notice->getID(),
73                             common_date_iso8601(common_sql_now()));
74
75         $act->content = sprintf(_m('<a href="%1$s">%2$s</a> deleted notice <a href="%3$s">{{%4$s}}</a>.'),
76                             htmlspecialchars($actor->getUrl()),
77                             htmlspecialchars($actor->getBestName()),
78                             htmlspecialchars($notice->getUrl()),
79                             htmlspecialchars($notice->getUri())
80                            );
81
82         $act->actor = $actor->asActivityObject();
83         $act->target = new ActivityObject();
84         $act->target->id = $notice->getUri();
85         $act->objects = array(clone($act->target));
86
87         $url = $notice->getUrl();
88         $act->selfLink = $url;
89         $act->editLink = $url;
90
91         // This will make ActivityModeration run saveObjectFromActivity which adds
92         // a new Deleted_notice entry in the database as well as deletes the notice
93         // if the actor has permission to do so.
94         $stored = Notice::saveActivity($act, $actor);
95
96         return $stored;
97     }
98
99     static public function fromStored(Notice $stored)
100     {
101         $class = get_called_class();
102         $object = new $class;
103         $object->act_uri = $stored->getUri();
104         if (!$object->find(true)) {
105             throw new NoResultException($object);
106         }
107         return $object;
108     }
109
110     public function getActor()
111     {
112         return Profile::getByID($this->profile_id);
113     }
114
115     static public function getObjectType()
116     {
117         return 'activity';
118     }
119
120     protected $_stored = array();
121
122     public function getStored()
123     {
124         $uri = $this->getTargetUri();
125         if (!isset($this->_stored[$uri])) {
126             $stored = new Notice();
127             $stored->uri = $uri;
128             if (!$stored->find(true)) {
129                 throw new NoResultException($stored);
130             }
131             $this->_stored[$uri] = $stored;
132         }
133         return $this->_stored[$uri];
134     }
135
136     public function getTargetUri()
137     {
138         return $this->uri;
139     }
140
141     public function getUri()
142     {
143         return $this->act_uri;
144     }
145
146     public function asActivityObject(Profile $scoped=null)
147     {
148         $actobj = new ActivityObject();
149         $actobj->id = $this->getUri();
150         $actobj->type = ActivityUtils::resolveUri(self::getObjectType());
151         $actobj->actor = $this->getActorObject();
152         $actobj->target = new ActivityObject();
153         $actobj->target->id = $this->getTargetUri();
154         $actobj->objects = array(clone($actobj->target));
155         $actobj->verb = ActivityVerb::DELETE;
156         $actobj->title = ActivityUtils::verbToTitle($actobj->verb);
157
158         $actor = $this->getActor();
159         $actobj->content = sprintf(_m('<a href="%1$s">%2$s</a> deleted notice {{%3$s}}.'),
160                             htmlspecialchars($actor->getUrl()),
161                             htmlspecialchars($actor->getBestName()),
162                             htmlspecialchars($actor->getTargetUri())
163                            );
164
165         return $actobj;
166     }
167
168     static function newUri(Profile $actor, Managed_DataObject $object, $created=null)
169     {
170         if (is_null($created)) {
171             $created = common_sql_now();
172         }
173         return TagURI::mint(strtolower(get_called_class()).':%d:%s:%d:%s',
174                                         $actor->getID(),
175                                         ActivityUtils::resolveUri(self::getObjectType(), true),
176                                         $object->getID(),
177                                         common_date_iso8601($created));
178     }
179
180     static public function beforeSchemaUpdate()
181     {
182         $table = strtolower(get_called_class());
183         $schema = Schema::get();
184         $schemadef = $schema->getTableDef($table);
185
186         // 2015-10-03 We change the meaning of the 'uri' field and move its 
187         // content to the 'act_uri' for the deleted activity. act_created is
188         // added too.
189         if (isset($schemadef['fields']['act_uri'])) {
190             // We already have the act_uri field, so no need to migrate to it.
191             return;
192         }
193         echo "\nFound old $table table, upgrading it to contain 'act_uri' and 'act_created' field...";
194
195         $schemadef['fields']['act_uri'] = array('type' => 'varchar', 'not null' => true, 'length' => 191, 'description' => 'URI of the delete activity, may exist in notice table');
196         $schemadef['fields']['act_created'] = array('type' => 'datetime', 'not null' => true, 'description' => 'date the notice record was created');
197         unset($schemadef['unique keys']);
198         $schema->ensureTable($table, $schemadef);
199
200         $deleted = new Deleted_notice();
201         $result = $deleted->find();
202         if ($result === false) {
203             print "\nFound no deleted_notice entries, continuing...";
204             return true;
205         }
206         print "\nFound $result deleted_notice entries, aligning with new database layout: ";
207         while($deleted->fetch()) {
208             $orig = clone($deleted);
209             $deleted->act_uri = $deleted->uri;
210             // this is a fake URI just to have something to put there to avoid NULL
211             $deleted->uri = TagURI::mint(strtolower(get_called_class()).':%d:%s:%s:%s',
212                                 $deleted->profile_id,
213                                 ActivityUtils::resolveUri(self::getObjectType(), true),
214                                 'unknown',
215                                 common_date_iso8601($deleted->created));
216             $deleted->act_created = $deleted->created;  // we don't actually know when the notice was created
217             $deleted->updateWithKeys($orig, 'id');
218             print ".";
219         }
220         print "DONE.\n";
221         print "Resuming core schema upgrade...";
222     }
223
224 }