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