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)
24 /* the code above is auto generated do not remove the tag below */
27 public static function schemaDef()
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"'),
42 'primary key' => array('id'),
43 'unique keys' => array(
44 'message_uri_key' => array('uri'),
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')),
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'),
62 return Profile::getKV('id', $this->from_profile);
67 return Profile::getKV('id', $this->to_profile);
70 static function saveNew($from, $to, $content, $source) {
71 $sender = Profile::getKV('id', $from);
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.'));
78 $user = User::getKV('id', $sender->id);
82 $msg->from_profile = $from;
83 $msg->to_profile = $to;
85 if ($user instanceof User) {
86 // Use the sender's URL shortening options.
87 $msg->content = $user->shortenLinks($content);
89 $msg->content = common_shorten_links($content);
92 $msg->rendered = common_render_text($msg->content);
93 $msg->created = common_sql_now();
94 $msg->source = $source;
96 $result = $msg->insert();
99 common_log_db_error($msg, 'INSERT', __FILE__);
100 // TRANS: Message given when a message could not be stored on the server.
101 throw new ServerException(_('Could not insert message.'));
105 $msg->uri = common_local_url('showmessage', array('message' => $msg->id));
107 $result = $msg->update($orig);
110 common_log_db_error($msg, 'UPDATE', __FILE__);
111 // TRANS: Message given when a message could not be updated on the server.
112 throw new ServerException(_('Could not update message with new URI.'));
118 static function maxContent()
120 $desclimit = common_config('message', 'contentlimit');
121 // null => use global limit (distinct from 0!)
122 if (is_null($desclimit)) {
123 $desclimit = common_config('site', 'textlimit');
128 static function contentTooLong($content)
130 $contentlimit = self::maxContent();
131 return ($contentlimit > 0 && !empty($content) && (mb_strlen($content) > $contentlimit));
136 $from = User::getKV('id', $this->from_profile);
137 $to = User::getKV('id', $this->to_profile);
139 mail_notify_message($this, $from, $to);
144 $ns = new Notice_source();
145 if (!empty($this->source)) {
146 switch ($this->source) {
153 $ns->code = $this->source;
156 $ns = Notice_source::getKV($this->source);
158 $ns = new Notice_source();
159 $ns->code = $this->source;
160 $app = Oauth_application::getKV('name', $this->source);
162 $ns->name = $app->name;
163 $ns->url = $app->source_url;
172 function asActivity()
174 $act = new Activity();
176 if (Event::handle('StartMessageAsActivity', array($this, &$act))) {
178 $act->id = TagURI::mint(sprintf('activity:message:%d', $this->id));
179 $act->time = strtotime($this->created);
180 $act->link = $this->url;
182 $profile = Profile::getKV('id', $this->from_profile);
184 if (empty($profile)) {
185 throw new Exception(sprintf("Sender profile not found: %d", $this->from_profile));
188 $act->actor = $profile->asActivityObject();
189 $act->actor->extra[] = $profile->profileInfo();
191 $act->verb = ActivityVerb::POST;
193 $act->objects[] = ActivityObject::fromMessage($this);
195 $ctx = new ActivityContext();
197 $rprofile = Profile::getKV('id', $this->to_profile);
199 if (empty($rprofile)) {
200 throw new Exception(sprintf("Receiver profile not found: %d", $this->to_profile));
203 $ctx->attention[$rprofile->getUri()] = ActivityObject::PERSON;
205 $act->context = $ctx;
207 $source = $this->getSource();
210 $act->generator = ActivityObject::fromNoticeSource($source);
213 Event::handle('EndMessageAsActivity', array($this, &$act));