3 * Table Definition for message
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
7 class Message extends Managed_DataObject
10 /* the code below is auto generated do not remove the above tag */
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)
25 function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Message',$k,$v); }
27 /* the code above is auto generated do not remove the tag below */
30 public static function schemaDef()
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"'),
45 'primary key' => array('id'),
46 'unique keys' => array(
47 'message_uri_key' => array('uri'),
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')),
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'),
65 return Profile::staticGet('id', $this->from_profile);
70 return Profile::staticGet('id', $this->to_profile);
73 static function saveNew($from, $to, $content, $source) {
74 $sender = Profile::staticGet('id', $from);
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.'));
81 $user = User::staticGet('id', $sender->id);
85 $msg->from_profile = $from;
86 $msg->to_profile = $to;
88 // Use the sender's URL shortening options.
89 $msg->content = $user->shortenLinks($content);
91 $msg->content = common_shorten_links($content);
93 $msg->rendered = common_render_text($msg->content);
94 $msg->created = common_sql_now();
95 $msg->source = $source;
97 $result = $msg->insert();
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.');
106 $msg->uri = common_local_url('showmessage', array('message' => $msg->id));
108 $result = $msg->update($orig);
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.');
119 static function maxContent()
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');
129 static function contentTooLong($content)
131 $contentlimit = self::maxContent();
132 return ($contentlimit > 0 && !empty($content) && (mb_strlen($content) > $contentlimit));
137 $from = User::staticGet('id', $this->from_profile);
138 $to = User::staticGet('id', $this->to_profile);
140 mail_notify_message($this, $from, $to);
145 $ns = new Notice_source();
146 if (!empty($this->source)) {
147 switch ($this->source) {
154 $ns->code = $this->source;
157 $ns = Notice_source::staticGet($this->source);
159 $ns = new Notice_source();
160 $ns->code = $this->source;
161 $app = Oauth_application::staticGet('name', $this->source);
163 $ns->name = $app->name;
164 $ns->url = $app->source_url;
173 function asActivity()
175 $act = new Activity();
177 if (Event::handle('StartMessageAsActivity', array($this, &$act))) {
179 $act->id = TagURI::mint(sprintf('activity:message:%d', $this->id));
180 $act->time = strtotime($this->created);
181 $act->link = $this->url;
183 $profile = Profile::staticGet('id', $this->from_profile);
185 if (empty($profile)) {
186 throw new Exception(sprintf("Sender profile not found: %d", $this->from_profile));
189 $act->actor = ActivityObject::fromProfile($profile);
190 $act->actor->extra[] = $profile->profileInfo($cur);
192 $act->verb = ActivityVerb::POST;
194 $act->objects[] = ActivityObject::fromMessage($this);
196 $ctx = new ActivityContext();
198 $rprofile = Profile::staticGet('id', $this->to_profile);
200 if (empty($rprofile)) {
201 throw new Exception(sprintf("Receiver profile not found: %d", $this->to_profile));
204 $ctx->attention[] = $rprofile->getUri();
205 $ctx->attentionType[$rprofile->getUri()] = ActivityObject::PERSON;
207 $act->context = $ctx;
209 $source = $this->getSource();
212 $act->generator = ActivityObject::fromNoticeSource($source);
215 Event::handle('EndMessageAsActivity', array($this, &$act));