3 if (!defined('GNUSOCIAL')) { exit(1); }
6 * Table Definition for message
9 class Message extends Managed_DataObject
12 /* the code below is auto generated do not remove the above tag */
14 public $__table = 'message'; // table name
15 public $id; // int(4) primary_key not_null
16 public $uri; // varchar(255) unique_key
17 public $from_profile; // int(4) not_null
18 public $to_profile; // int(4) not_null
19 public $content; // text()
20 public $rendered; // text()
21 public $url; // varchar(255)
22 public $created; // datetime() not_null
23 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
24 public $source; // varchar(32)
26 /* the code above is auto generated do not remove the tag below */
29 public static function schemaDef()
33 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'),
34 'uri' => array('type' => 'varchar', 'length' => 255, 'description' => 'universally unique identifier'),
35 'from_profile' => array('type' => 'int', 'not null' => true, 'description' => 'who the message is from'),
36 'to_profile' => array('type' => 'int', 'not null' => true, 'description' => 'who the message is to'),
37 'content' => array('type' => 'text', 'description' => 'message content'),
38 'rendered' => array('type' => 'text', 'description' => 'HTML version of the content'),
39 'url' => array('type' => 'varchar', 'length' => 255, 'description' => 'URL of any attachment (image, video, bookmark, whatever)'),
40 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
41 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
42 'source' => array('type' => 'varchar', 'length' => 32, 'description' => 'source of comment, like "web", "im", or "clientname"'),
44 'primary key' => array('id'),
45 'unique keys' => array(
46 'message_uri_key' => array('uri'),
48 'foreign keys' => array(
49 'message_from_profile_fkey' => array('profile', array('from_profile' => 'id')),
50 'message_to_profile_fkey' => array('profile', array('to_profile' => 'id')),
53 // @fixme these are really terrible indexes, since you can only sort on one of them at a time.
54 // looks like we really need a (to_profile, created) for inbox and a (from_profile, created) for outbox
55 'message_from_idx' => array('from_profile'),
56 'message_to_idx' => array('to_profile'),
57 'message_created_idx' => array('created'),
64 return Profile::getKV('id', $this->from_profile);
69 return Profile::getKV('id', $this->to_profile);
72 static function saveNew($from, $to, $content, $source) {
73 $sender = Profile::getKV('id', $from);
75 if (!$sender->hasRight(Right::NEWMESSAGE)) {
76 // TRANS: Client exception thrown when a user tries to send a direct message while being banned from sending them.
77 throw new ClientException(_('You are banned from sending direct messages.'));
80 $user = User::getKV('id', $sender->id);
84 $msg->from_profile = $from;
85 $msg->to_profile = $to;
87 if ($user instanceof User) {
88 // Use the sender's URL shortening options.
89 $msg->content = $user->shortenLinks($content);
91 $msg->content = common_shorten_links($content);
94 $msg->rendered = common_render_text($msg->content);
95 $msg->created = common_sql_now();
96 $msg->source = $source;
98 $result = $msg->insert();
101 common_log_db_error($msg, 'INSERT', __FILE__);
102 // TRANS: Message given when a message could not be stored on the server.
103 throw new ServerException(_('Could not insert message.'));
107 $msg->uri = common_local_url('showmessage', array('message' => $msg->id));
109 $result = $msg->update($orig);
112 common_log_db_error($msg, 'UPDATE', __FILE__);
113 // TRANS: Message given when a message could not be updated on the server.
114 throw new ServerException(_('Could not update message with new URI.'));
120 static function maxContent()
122 $desclimit = common_config('message', 'contentlimit');
123 // null => use global limit (distinct from 0!)
124 if (is_null($desclimit)) {
125 $desclimit = common_config('site', 'textlimit');
130 static function contentTooLong($content)
132 $contentlimit = self::maxContent();
133 return ($contentlimit > 0 && !empty($content) && (mb_strlen($content) > $contentlimit));
138 $from = User::getKV('id', $this->from_profile);
139 $to = User::getKV('id', $this->to_profile);
141 mail_notify_message($this, $from, $to);
146 if (empty($this->source)) {
150 $ns = new Notice_source();
151 switch ($this->source) {
158 $ns->code = $this->source;
161 $ns = Notice_source::getKV($this->source);
162 if (!$ns instanceof Notice_source) {
163 $ns = new Notice_source();
164 $ns->code = $this->source;
165 $app = Oauth_application::getKV('name', $this->source);
167 $ns->name = $app->name;
168 $ns->url = $app->source_url;
176 function asActivity()
178 $act = new Activity();
180 if (Event::handle('StartMessageAsActivity', array($this, &$act))) {
182 $act->id = TagURI::mint(sprintf('activity:message:%d', $this->id));
183 $act->time = strtotime($this->created);
184 $act->link = $this->url;
186 $profile = Profile::getKV('id', $this->from_profile);
188 if (empty($profile)) {
189 throw new Exception(sprintf("Sender profile not found: %d", $this->from_profile));
192 $act->actor = $profile->asActivityObject();
193 $act->actor->extra[] = $profile->profileInfo();
195 $act->verb = ActivityVerb::POST;
197 $act->objects[] = ActivityObject::fromMessage($this);
199 $ctx = new ActivityContext();
201 $rprofile = Profile::getKV('id', $this->to_profile);
203 if (empty($rprofile)) {
204 throw new Exception(sprintf("Receiver profile not found: %d", $this->to_profile));
207 $ctx->attention[$rprofile->getUri()] = ActivityObject::PERSON;
209 $act->context = $ctx;
211 $source = $this->getSource();
213 if ($source instanceof Notice_source) {
214 $act->generator = ActivityObject::fromNoticeSource($source);
217 Event::handle('EndMessageAsActivity', array($this, &$act));