]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Message.php
3986836836ad4e2e13077d8f4d2ce5329f64af9d
[quix0rs-gnu-social.git] / classes / Message.php
1 <?php
2 /**
3  * Table Definition for message
4  */
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
6
7 class Message extends Managed_DataObject
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'message';                         // table name
13     public $id;                              // int(4)  primary_key not_null
14     public $uri;                             // varchar(255)  unique_key
15     public $from_profile;                    // int(4)   not_null
16     public $to_profile;                      // int(4)   not_null
17     public $content;                         // text()
18     public $rendered;                        // text()
19     public $url;                             // varchar(255)
20     public $created;                         // datetime()   not_null
21     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
22     public $source;                          // varchar(32)
23
24     /* the code above is auto generated do not remove the tag below */
25     ###END_AUTOCODE
26
27     public static function schemaDef()
28     {
29         return array(
30             'fields' => array(
31                 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'),
32                 'uri' => array('type' => 'varchar', 'length' => 255, 'description' => 'universally unique identifier'),
33                 'from_profile' => array('type' => 'int', 'not null' => true, 'description' => 'who the message is from'),
34                 'to_profile' => array('type' => 'int', 'not null' => true, 'description' => 'who the message is to'),
35                 'content' => array('type' => 'text', 'description' => 'message content'),
36                 'rendered' => array('type' => 'text', 'description' => 'HTML version of the content'),
37                 'url' => array('type' => 'varchar', 'length' => 255, 'description' => 'URL of any attachment (image, video, bookmark, whatever)'),
38                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
39                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
40                 'source' => array('type' => 'varchar', 'length' => 32, 'description' => 'source of comment, like "web", "im", or "clientname"'),
41             ),
42             'primary key' => array('id'),
43             'unique keys' => array(
44                 'message_uri_key' => array('uri'),
45             ),
46             'foreign keys' => array(
47                 'message_from_profile_fkey' => array('profile', array('from_profile' => 'id')),
48                 'message_to_profile_fkey' => array('profile', array('to_profile' => 'id')),
49             ),
50             'indexes' => array(
51                 // @fixme these are really terrible indexes, since you can only sort on one of them at a time.
52                 // looks like we really need a (to_profile, created) for inbox and a (from_profile, created) for outbox
53                 'message_from_idx' => array('from_profile'),
54                 'message_to_idx' => array('to_profile'),
55                 'message_created_idx' => array('created'),
56             ),
57         );
58     }
59
60     function getFrom()
61     {
62         return Profile::staticGet('id', $this->from_profile);
63     }
64
65     function getTo()
66     {
67         return Profile::staticGet('id', $this->to_profile);
68     }
69
70     static function saveNew($from, $to, $content, $source) {
71         $sender = Profile::staticGet('id', $from);
72
73         if (!$sender->hasRight(Right::NEWMESSAGE)) {
74             // TRANS: Client exception thrown when a user tries to send a direct message while being banned from sending them.
75             throw new ClientException(_('You are banned from sending direct messages.'));
76         }
77
78         $user = User::staticGet('id', $sender->id);
79
80         $msg = new Message();
81
82         $msg->from_profile = $from;
83         $msg->to_profile = $to;
84         if ($user) {
85             // Use the sender's URL shortening options.
86             $msg->content = $user->shortenLinks($content);
87         } else {
88             $msg->content = common_shorten_links($content);
89         }
90         $msg->rendered = common_render_text($msg->content);
91         $msg->created = common_sql_now();
92         $msg->source = $source;
93
94         $result = $msg->insert();
95
96         if (!$result) {
97             common_log_db_error($msg, 'INSERT', __FILE__);
98             // TRANS: Message given when a message could not be stored on the server.
99             return _('Could not insert message.');
100         }
101
102         $orig = clone($msg);
103         $msg->uri = common_local_url('showmessage', array('message' => $msg->id));
104
105         $result = $msg->update($orig);
106
107         if (!$result) {
108             common_log_db_error($msg, 'UPDATE', __FILE__);
109             // TRANS: Message given when a message could not be updated on the server.
110             return _('Could not update message with new URI.');
111         }
112
113         return $msg;
114     }
115
116     static function maxContent()
117     {
118         $desclimit = common_config('message', 'contentlimit');
119         // null => use global limit (distinct from 0!)
120         if (is_null($desclimit)) {
121             $desclimit = common_config('site', 'textlimit');
122         }
123         return $desclimit;
124     }
125
126     static function contentTooLong($content)
127     {
128         $contentlimit = self::maxContent();
129         return ($contentlimit > 0 && !empty($content) && (mb_strlen($content) > $contentlimit));
130     }
131
132     function notify()
133     {
134         $from = User::staticGet('id', $this->from_profile);
135         $to   = User::staticGet('id', $this->to_profile);
136
137         mail_notify_message($this, $from, $to);
138     }
139
140     function getSource()
141     {
142         $ns = new Notice_source();
143         if (!empty($this->source)) {
144             switch ($this->source) {
145             case 'web':
146             case 'xmpp':
147             case 'mail':
148             case 'omb':
149             case 'system':
150             case 'api':
151                 $ns->code = $this->source;
152                 break;
153             default:
154                 $ns = Notice_source::staticGet($this->source);
155                 if (!$ns) {
156                     $ns = new Notice_source();
157                     $ns->code = $this->source;
158                     $app = Oauth_application::staticGet('name', $this->source);
159                     if ($app) {
160                         $ns->name = $app->name;
161                         $ns->url  = $app->source_url;
162                     }
163                 }
164                 break;
165             }
166         }
167         return $ns;
168     }
169
170     function asActivity()
171     {
172         $act = new Activity();
173
174         if (Event::handle('StartMessageAsActivity', array($this, &$act))) {
175
176             $act->id      = TagURI::mint(sprintf('activity:message:%d', $this->id));
177             $act->time    = strtotime($this->created);
178             $act->link    = $this->url;
179
180             $profile = Profile::staticGet('id', $this->from_profile);
181
182             if (empty($profile)) {
183                 throw new Exception(sprintf("Sender profile not found: %d", $this->from_profile));
184             }
185             
186             $act->actor            = ActivityObject::fromProfile($profile);
187             $act->actor->extra[]   = $profile->profileInfo(null);
188
189             $act->verb = ActivityVerb::POST;
190
191             $act->objects[] = ActivityObject::fromMessage($this);
192
193             $ctx = new ActivityContext();
194
195             $rprofile = Profile::staticGet('id', $this->to_profile);
196
197             if (empty($rprofile)) {
198                 throw new Exception(sprintf("Receiver profile not found: %d", $this->to_profile));
199             }
200
201             $ctx->attention[] = $rprofile->getUri();
202             $ctx->attentionType[$rprofile->getUri()] = ActivityObject::PERSON;
203
204             $act->context = $ctx;
205
206             $source = $this->getSource();
207
208             if ($source) {
209                 $act->generator = ActivityObject::fromNoticeSource($source);
210             }
211
212             Event::handle('EndMessageAsActivity', array($this, &$act));
213         }
214
215         return $act;
216     }
217 }