[NOTE] Add mimetype to notes

This commit is contained in:
Diogo Peralta Cordeiro 2021-09-09 03:46:30 +01:00 committed by Hugo Sales
parent c69b28d894
commit 7c465bba5f
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
3 changed files with 61 additions and 38 deletions

View File

@ -73,20 +73,30 @@ class Posting extends Component
Event::handle('PostingPlaceHolderString', [&$placeholder_strings]); Event::handle('PostingPlaceHolderString', [&$placeholder_strings]);
$placeholder = $placeholder_strings[array_rand($placeholder_strings)]; $placeholder = $placeholder_strings[array_rand($placeholder_strings)];
$initial_content = '';
Event::handle('PostingInitialContent', [&$initial_content]);
$content_type = ['Plain Text' => 'text/plain'];
Event::handle('PostingAvailableContentTypes', [&$content_type]);
$request = $vars['request']; $request = $vars['request'];
$form = Form::create([ $form_params = [
['content', TextareaType::class, ['label' => ' ', 'data' => '', 'attr' => ['placeholder' => _m($placeholder)]]], ['content', TextareaType::class, ['label' => _m('Content') . ':', 'data' => $initial_content, 'attr' => ['placeholder' => _m($placeholder)]]],
['attachments', FileType::class, ['label' => ' ', 'data' => null, 'multiple' => true, 'required' => false]], ['attachments', FileType::class, ['label' => _m('Attachments') . ':', 'data' => null, 'multiple' => true, 'required' => false]],
['visibility', ChoiceType::class, ['label' => _m('Visibility:'), 'multiple' => false, 'expanded' => false, 'data' => 'public', 'choices' => [_m('Public') => 'public', _m('Instance') => 'instance', _m('Private') => 'private']]], ['visibility', ChoiceType::class, ['label' => _m('Visibility') . ':', 'multiple' => false, 'expanded' => false, 'data' => 'public', 'choices' => [_m('Public') => 'public', _m('Instance') => 'instance', _m('Private') => 'private']]],
['to', ChoiceType::class, ['label' => _m('To:'), 'multiple' => false, 'expanded' => false, 'choices' => $to_tags]], ['to', ChoiceType::class, ['label' => _m('To') . ':', 'multiple' => false, 'expanded' => false, 'choices' => $to_tags]],
['post_note', SubmitType::class, ['label' => _m('Post')]], ];
]); if (count($content_type) > 1) {
$form_params[] = ['content_type', ChoiceType::class, ['label' => _m('Text format') . ':', 'multiple' => false, 'expanded' => false, 'data' => 'text/plain', 'choices' => $content_type]];
}
$form_params[] = ['post_note', SubmitType::class, ['label' => _m('Post')]];
$form = Form::create($form_params);
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted()) { if ($form->isSubmitted()) {
$data = $form->getData(); $data = $form->getData();
if ($form->isValid()) { if ($form->isValid()) {
self::storeNote($actor_id, $data['content'], $data['attachments'], is_local: true); self::storeNote($actor_id, $data['content_type'] ?? array_key_first($content_type), $data['content'], $data['attachments'], is_local: true);
throw new RedirectException(); throw new RedirectException();
} else { } else {
throw new InvalidFormException(); throw new InvalidFormException();
@ -106,10 +116,11 @@ class Posting extends Component
* @throws DuplicateFoundException * @throws DuplicateFoundException
* @throws ClientException|ServerException * @throws ClientException|ServerException
*/ */
public static function storeNote(int $actor_id, ?string $content, array $attachments, bool $is_local, ?int $reply_to = null, ?int $repeat_of = null) public static function storeNote(int $actor_id, string $content_type, string $content, array $attachments, bool $is_local, ?int $reply_to = null, ?int $repeat_of = null)
{ {
$note = Note::create([ $note = Note::create([
'gsactor_id' => $actor_id, 'gsactor_id' => $actor_id,
'content_type' => $content_type,
'content' => $content, 'content' => $content,
'is_local' => $is_local, 'is_local' => $is_local,
'reply_to' => $reply_to, 'reply_to' => $reply_to,

View File

@ -62,7 +62,7 @@ class Reply extends Controller
'label' => _m('Reply'), 'label' => _m('Reply'),
'label_attr' => ['class' => 'section-form-label'], 'label_attr' => ['class' => 'section-form-label'],
'help' => _m('Please input your reply.'), 'help' => _m('Please input your reply.'),
] ],
], ],
['attachments', FileType::class, ['label' => ' ', 'multiple' => true, 'required' => false]], ['attachments', FileType::class, ['label' => ' ', 'multiple' => true, 'required' => false]],
['replyform', SubmitType::class, ['label' => _m('Submit')]], ['replyform', SubmitType::class, ['label' => _m('Submit')]],
@ -74,12 +74,13 @@ class Reply extends Controller
$data = $form->getData(); $data = $form->getData();
if ($form->isValid()) { if ($form->isValid()) {
Posting::storeNote( Posting::storeNote(
$actor_id, actor_id: $actor_id,
$data['content'], content_type: 'text/plain',
$data['attachments'], content: $data['content'],
$is_local = true, attachments: $data['attachments'],
$reply_to, is_local: true,
$repeat_of = null reply_to: $reply_to,
repeat_of: null
); );
$return = $this->string('return_to'); $return = $this->string('return_to');
if (!is_null($return)) { if (!is_null($return)) {

View File

@ -38,20 +38,21 @@ use DateTimeInterface;
* @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/ */
class Note extends Entity implements \JsonSerializable class Note extends Entity
{ {
// {{{ Autocode // {{{ Autocode
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
private int $id; private int $id;
private int $gsactor_id; private int $gsactor_id;
private ?string $content; private string $content_type = 'text/plain';
private ?string $rendered; private string $content;
private string $rendered;
private ?int $reply_to; private ?int $reply_to;
private ?bool $is_local; private ?bool $is_local;
private ?string $source; private ?string $source;
private ?int $conversation; private ?int $conversation;
private ?int $repeat_of; private ?int $repeat_of;
private int $scope = 1; private int $scope = VisibilityScope::PUBLIC;
private \DateTimeInterface $created; private \DateTimeInterface $created;
private \DateTimeInterface $modified; private \DateTimeInterface $modified;
@ -77,6 +78,23 @@ class Note extends Entity implements \JsonSerializable
return $this->gsactor_id; return $this->gsactor_id;
} }
/**
* @return string
*/
public function getContentType(): string
{
return $this->content_type;
}
/**
* @param string $content_type
*/
public function setContentType(string $content_type): self
{
$this->content_type = $content_type;
return $this;
}
public function setContent(?string $content): self public function setContent(?string $content): self
{ {
$this->content = $content; $this->content = $content;
@ -272,14 +290,6 @@ class Note extends Entity implements \JsonSerializable
['note_id' => $this->id, 'actor_id' => $a->getId()])); ['note_id' => $this->id, 'actor_id' => $a->getId()]));
} }
public function jsonSerialize()
{
return [
'content' => $this->getContent(),
'author' => $this->getGSActorId(),
];
}
public static function schemaDef(): array public static function schemaDef(): array
{ {
return [ return [
@ -287,7 +297,8 @@ class Note extends Entity implements \JsonSerializable
'fields' => [ 'fields' => [
'id' => ['type' => 'serial', 'not null' => true], 'id' => ['type' => 'serial', 'not null' => true],
'gsactor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'GSActor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'who made the note'], 'gsactor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'GSActor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'who made the note'],
'content' => ['type' => 'text', 'description' => 'note content'], 'content' => ['type' => 'text', 'not null' => true, 'description' => 'note content'],
'content_type' => ['type' => 'varchar', 'not null' => true, 'default' => 'text/plain', 'length' => 129, 'description' => 'A note can be written in a multitude of formats such as text/plain, text/markdown, application/x-latex, and text/html'],
'rendered' => ['type' => 'text', 'description' => 'rendered note content, so we can keep the microtags (if not local)'], 'rendered' => ['type' => 'text', 'description' => 'rendered note content, so we can keep the microtags (if not local)'],
'reply_to' => ['type' => 'int', 'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'description' => 'note replied to, null if root of a conversation'], 'reply_to' => ['type' => 'int', 'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'description' => 'note replied to, null if root of a conversation'],
'is_local' => ['type' => 'bool', 'description' => 'was this note generated by a local actor'], 'is_local' => ['type' => 'bool', 'description' => 'was this note generated by a local actor'],