]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
refactor notice-adding code to one static method on Notice
authorEvan Prodromou <evan@prodromou.name>
Wed, 30 Jul 2008 02:28:56 +0000 (22:28 -0400)
committerEvan Prodromou <evan@prodromou.name>
Wed, 30 Jul 2008 02:28:56 +0000 (22:28 -0400)
darcs-hash:20080730022856-84dde-f19e4ff5d5ae2603b63b8aebd8f878ec90b3ce22.gz

actions/newnotice.php
actions/postnotice.php
actions/twitapistatuses.php
classes/Notice.php
maildaemon.php
xmppdaemon.php

index 5f6a224848d2baa976de75aa97f0900dbb5629c8..8de6643a0f29b5f437c0d49f4873cf8f449a4991 100644 (file)
@@ -38,44 +38,27 @@ class NewnoticeAction extends Action {
 
                $user = common_current_user();
                assert($user); # XXX: maybe an error instead...
-               $notice = new Notice();
-               assert($notice);
-               $notice->profile_id = $user->id; # user id *is* profile id
-               $notice->is_local = 1;
-               $notice->created = DB_DataObject_Cast::dateTime();
-               # Default theme uses 'content' for something else
-               $notice->content = $this->trimmed('status_textarea');
-
-               if (!$notice->content) {
+               $content = $this->trimmed('status_textarea');
+               
+               if (!$content) {
                        $this->show_form(_('No content!'));
                        return;
-               } else if (strlen($notice->content) > 140) {
+               } else if (strlen($content) > 140) {
                        $this->show_form(_('That\'s too long. Max notice size is 140 chars.'));
                        return;
                }
 
-               $notice->rendered = common_render_content($notice->content, $notice);
-
-               $id = $notice->insert();
-
-               if (!$id) {
-                       common_server_error(_('Problem saving notice.'));
+               $notice = Notice::saveNew($user->id, $content, 'web');
+               
+               if (is_string($notice)) {
+                       $this->show_form($notice);
                        return;
                }
-
-               $orig = clone($notice);
-               $notice->uri = common_notice_uri($notice);
-
-               if (!$notice->update($orig)) {
-                       common_server_error(_('Problem saving notice.'));
-                       return;
-               }
-
-               common_save_replies($notice);
-               $notice->saveTags();
+               
                common_broadcast_notice($notice);
-
+               
                $returnto = $this->trimmed('returnto');
+               
                if ($returnto) {
                        $url = common_local_url($returnto,
                                                                        array('nickname' => $user->nickname));
index b08f27ac4b4ebca81e2de4bf98aa3e6968373005..9540d553648c7a9f8e1cd4853935419f380a8562 100644 (file)
@@ -74,24 +74,11 @@ class PostnoticeAction extends Action {
                }
                $notice = Notice::staticGet('uri', $notice_uri);
                if (!$notice) {
-                       $notice = new Notice();
-                       $notice->is_local = 0;
-                       $notice->profile_id = $remote_profile->id;
-                       $notice->uri = $notice_uri;
-                       $notice->content = $content;
-                       $notice->rendered = common_render_content($notice->content, $notice);
-                       if ($notice_url) {
-                               $notice->url = $notice_url;
-                       }
-                       $notice->created = DB_DataObject_Cast::dateTime(); # current time
-                       $id = $notice->insert();
-                       if (!$id) {
-                               common_server_error(_('Error inserting notice'), 500);
+                       $notice = Notice::saveNew($remote_profile->id, $content, 'omb', 0);
+                       if (is_string($notice)) {
+                               common_server_serror($notice, 500);
                                return false;
                        }
-                       common_save_replies($notice);
-                       $notice->saveTags();
-                       common_broadcast_notice($notice, true);
                }
                return true;
        }
index 0c97ada0b115b8327725b864fff495b3a36b8d75..f791a0cc864a2192a2e3a67400e8e9ed121b839b 100644 (file)
@@ -371,20 +371,19 @@ class TwitapistatusesAction extends TwitterapiAction {
        }
                
        function update($args, $apidata) {
+               
                parent::handle($args);
                
                $user = $apidata['user'];
-                               
-               $this->is_readonly();
                
-                               
-               $notice = DB_DataObject::factory('notice');             
+               $status = $this->trimmed('status');
+               $source = $this->trimmed('source');
                
-               $notice->profile_id = $user->id; # user id *is* profile id
-               $notice->created = DB_DataObject_Cast::dateTime();      
-               $notice->content = $this->trimmed('status');
-
-               if (!$notice->content) {
+               if (!$source) {
+                       $source = 'api';
+               }
+               
+               if (!$status) {
                        
                        // XXX: Note: In this case, Twitter simply returns '200 OK'
                        // No error is given, but the status is not posted to the 
@@ -392,7 +391,7 @@ class TwitapistatusesAction extends TwitterapiAction {
                        // errror? -- Zach
                        exit();
                        
-               } else if (strlen($notice->content) > 140) {
+               } else if (strlen($status) > 140) {
 
                        // XXX: Twitter truncates anything over 140, flags the status 
                    // as "truncated."  Sending this error may screw up some clients
@@ -402,28 +401,14 @@ class TwitapistatusesAction extends TwitterapiAction {
                        print "That's too long. Max notice size is 140 chars.\n";
                        exit();
                }
-
-               $notice->rendered = common_render_content($notice->content, $notice);
-               $notice->is_local = 1;
                
-               $id = $notice->insert();
-
-               if (!$id) {
-                       common_server_error('Could not update status!', 500);
-                       exit();
-               }
+               $notice = Notice::saveNew($user->id, $status, $source);
 
-               $orig = clone($notice);
-               $notice->uri = common_notice_uri($notice);
-
-               if (!$notice->update($orig)) {
-                       common_server_error('Could not save status!', 500);
-                       exit();
+               if (is_string($notice)) {
+                       $this->server_error($notice);
+                       return;
                }
-
-        common_save_replies($notice);
-               common_broadcast_notice($notice);
-
+               
                // FIXME: Bad Hack 
                // I should be able to just sent this notice off for display,
                // but $notice->created does not contain a string at this
index 02526bae22532f33864de5357c5f118966399c00..ef8f7046e61d17bef693b33745241dc2ce2d4041 100644 (file)
@@ -75,4 +75,34 @@ class Notice extends DB_DataObject
                }
                return true;
        }
+       
+       static function saveNew($profile_id, $content, $source=NULL, $is_local=1) {
+               
+               $notice = new Notice();
+               $notice->profile_id = $profile_id;
+               $notice->is_local = $is_local;
+               $notice->created = DB_DataObject_Cast::dateTime();
+               # Default theme uses 'content' for something else
+               $notice->content = $content;
+
+               $notice->rendered = common_render_content($notice->content, $notice);
+
+               $id = $notice->insert();
+
+               if (!$id) {
+                       return _('Problem saving notice.');
+               }
+
+               $orig = clone($notice);
+               $notice->uri = common_notice_uri($notice);
+
+               if (!$notice->update($orig)) {
+                       return _('Problem saving notice.');
+               }
+
+               common_save_replies($notice);
+               $notice->saveTags();
+               
+               return $notice;
+       }
 }
index f8fd7c11aede6440a36a1538b5a9873b1387641c..5be62efb88683f61b8e6a2af92eefb9eab6b6062 100755 (executable)
@@ -115,35 +115,11 @@ class MailerDaemon {
        }
 
        function add_notice($user, $msg) {
-               $notice = new Notice();
-               $notice->is_local = 1;
-               $notice->profile_id = $user->id;
-               $notice->content = trim(substr($msg, 0, 140));
-               $notice->rendered = common_render_content($notice->content, $notice);
-               $notice->created = DB_DataObject_Cast::dateTime();
-               $notice->query('BEGIN');
-               $id = $notice->insert();
-               if (!$id) {
-                       $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
-                       $this->log(LOG_ERR,
-                                          'Could not insert ' . common_log_objstring($notice) .
-                                          ' for user ' . common_log_objstring($user) .
-                                          ': ' . $last_error->message);
+               $notice = Notice::saveNew($user->id, $msg, 'mail');
+               if (is_string($notice)) {
+                       $this->log(LOG_ERR, $notice);
                        return;
                }
-               $orig = clone($notice);
-               $notice->uri = common_notice_uri($notice);
-               $result = $notice->update($orig);
-               if (!$result) {
-                       $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
-                       $this->log(LOG_ERR,
-                                          'Could not add URI to ' . common_log_objstring($notice) .
-                                          ' for user ' . common_log_objstring($user) .
-                                          ': ' . $last_error->message);
-                       return;
-               }
-               $notice->query('COMMIT');
-        common_save_replies($notice);
                common_broadcast_notice($notice);
                $this->log(LOG_INFO,
                                   'Added notice ' . $notice->id . ' from user ' . $user->nickname);
index d9b7064543f599326d40e1ff501ad3e167415bcc..7977dc804d2e51b980c208d55e4923631e27c9be 100755 (executable)
@@ -223,36 +223,11 @@ class XMPPDaemon {
        }
 
        function add_notice(&$user, &$pl) {
-               $notice = new Notice();
-               $notice->is_local = 1;
-               $notice->profile_id = $user->id;
-               $notice->content = trim(substr($pl['body'], 0, 140));
-               $notice->rendered = common_render_content($notice->content, $notice);
-               $notice->created = DB_DataObject_Cast::dateTime();
-               $notice->query('BEGIN');
-               $id = $notice->insert();
-               if (!$id) {
-                       $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
-                       $this->log(LOG_ERR,
-                                          'Could not insert ' . common_log_objstring($notice) .
-                                          ' for user ' . common_log_objstring($user) .
-                                          ': ' . $last_error->message);
-                       return;
-               }
-               $orig = clone($notice);
-               $notice->uri = common_notice_uri($notice);
-               $result = $notice->update($orig);
-               if (!$result) {
-                       $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
-                       $this->log(LOG_ERR,
-                                          'Could not add URI to ' . common_log_objstring($notice) .
-                                          ' for user ' . common_log_objstring($user) .
-                                          ': ' . $last_error->message);
+               $notice = Notice::saveNew($user->id, trim(substr($pl['body'], 0, 140)), 'xmpp');
+               if (is_string($notice)) {
+                       $this->log(LOG_ERR, $notice);
                        return;
                }
-               $notice->query('COMMIT');
-               common_save_replies($notice);
-               $notice->saveTags();
                common_real_broadcast($notice);
                $this->log(LOG_INFO,
                                   'Added notice ' . $notice->id . ' from user ' . $user->nickname);