123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- defined('GNUSOCIAL') || die();
- class Message extends Managed_DataObject
- {
-
-
- public $__table = 'message';
- public $id;
- public $uri;
- public $from_profile;
- public $to_profile;
- public $content;
- public $rendered;
- public $url;
- public $created;
- public $modified;
- public $source;
-
-
- public static function schemaDef()
- {
- return array(
- 'fields' => array(
- 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'),
- 'uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'universally unique identifier'),
- 'from_profile' => array('type' => 'int', 'not null' => true, 'description' => 'who the message is from'),
- 'to_profile' => array('type' => 'int', 'not null' => true, 'description' => 'who the message is to'),
- 'content' => array('type' => 'text', 'description' => 'message content'),
- 'rendered' => array('type' => 'text', 'description' => 'HTML version of the content'),
- 'url' => array('type' => 'varchar', 'length' => 191, 'description' => 'URL of any attachment (image, video, bookmark, whatever)'),
- 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
- 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
- 'source' => array('type' => 'varchar', 'length' => 32, 'description' => 'source of comment, like "web", "im", or "clientname"'),
- ),
- 'primary key' => array('id'),
- 'unique keys' => array(
- 'message_uri_key' => array('uri'),
- ),
- 'foreign keys' => array(
- 'message_from_profile_fkey' => array('profile', array('from_profile' => 'id')),
- 'message_to_profile_fkey' => array('profile', array('to_profile' => 'id')),
- ),
- 'indexes' => array(
- 'message_from_profile_created_id_idx' => array('from_profile', 'created', 'id'),
- 'message_to_profile_created_id_idx' => array('to_profile', 'created', 'id'),
- ),
- );
- }
- public function getFrom()
- {
- return Profile::getKV('id', $this->from_profile);
- }
- public function getTo()
- {
- return Profile::getKV('id', $this->to_profile);
- }
- public function getSource()
- {
- if (empty($this->source)) {
- return false;
- }
- $ns = new Notice_source();
- switch ($this->source) {
- case 'web':
- case 'xmpp':
- case 'mail':
- case 'omb':
- case 'system':
- case 'api':
- $ns->code = $this->source;
- break;
- default:
- $ns = Notice_source::getKV($this->source);
- if (!$ns instanceof Notice_source) {
- $ns = new Notice_source();
- $ns->code = $this->source;
- $app = Oauth_application::getKV('name', $this->source);
- if ($app) {
- $ns->name = $app->name;
- $ns->url = $app->source_url;
- }
- }
- break;
- }
- return $ns;
- }
- public function asActivity()
- {
- $act = new Activity();
- if (Event::handle('StartMessageAsActivity', array($this, &$act))) {
- $act->verb = ActivityVerb::POST;
- $act->time = strtotime($this->created);
- $actor_profile = $this->getFrom();
- if (is_null($actor_profile)) {
- throw new Exception(sprintf("Sender profile not found: %d", $this->from_profile));
- }
- $act->actor = $actor_profile->asActivityObject();
-
- $act->context = new ActivityContext();
- $options = ['source' => $this->source,
- 'uri' => TagURI::mint(sprintf('activity:message:%d', $this->id)),
- 'url' => $this->uri,
- 'scope' => Notice::MESSAGE_SCOPE];
- $to_profile = $this->getTo();
- if (is_null($to_profile)) {
- throw new Exception(sprintf("Receiver profile not found: %d", $this->to_profile));
- }
- $act->context->attention[$to_profile->getUri()] = ActivityObject::PERSON;
- $act->objects[] = ActivityObject::fromMessage($this);
- $source = $this->getSource();
- if ($source instanceof Notice_source) {
- $act->generator = ActivityObject::fromNoticeSource($source);
- }
- Event::handle('EndMessageAsActivity', array($this, &$act));
- }
- return $act;
- }
- }
|