]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Message.php
Merge branch 'master' of gitorious.org:statusnet/mainline
[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 Memcached_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     function getFrom()
31     {
32         return Profile::staticGet('id', $this->from_profile);
33     }
34
35     function getTo()
36     {
37         return Profile::staticGet('id', $this->to_profile);
38     }
39
40     static function saveNew($from, $to, $content, $source) {
41
42         $sender = Profile::staticGet('id', $from);
43
44         if (!$sender->hasRight(Right::NEWMESSAGE)) {
45             // TRANS: Client exception thrown when a user tries to send a direct message while being banned from sending them.
46             throw new ClientException(_('You are banned from sending direct messages.'));
47         }
48
49         $msg = new Message();
50
51         $msg->from_profile = $from;
52         $msg->to_profile = $to;
53         $msg->content = common_shorten_links($content);
54         $msg->rendered = common_render_text($content);
55         $msg->created = common_sql_now();
56         $msg->source = $source;
57
58         $result = $msg->insert();
59
60         if (!$result) {
61             common_log_db_error($msg, 'INSERT', __FILE__);
62             // TRANS: Message given when a message could not be stored on the server.
63             return _('Could not insert message.');
64         }
65
66         $orig = clone($msg);
67         $msg->uri = common_local_url('showmessage', array('message' => $msg->id));
68
69         $result = $msg->update($orig);
70
71         if (!$result) {
72             common_log_db_error($msg, 'UPDATE', __FILE__);
73             // TRANS: Message given when a message could not be updated on the server.
74             return _('Could not update message with new URI.');
75         }
76
77         return $msg;
78     }
79
80     static function maxContent()
81     {
82         $desclimit = common_config('message', 'contentlimit');
83         // null => use global limit (distinct from 0!)
84         if (is_null($desclimit)) {
85             $desclimit = common_config('site', 'textlimit');
86         }
87         return $desclimit;
88     }
89
90     static function contentTooLong($content)
91     {
92         $contentlimit = self::maxContent();
93         return ($contentlimit > 0 && !empty($content) && (mb_strlen($content) > $contentlimit));
94     }
95
96     function notify()
97     {
98         $from = User::staticGet('id', $this->from_profile);
99         $to   = User::staticGet('id', $this->to_profile);
100
101         mail_notify_message($this, $from, $to);
102     }
103 }