Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
10 / 10
CRAP
100.00% covered (success)
100.00%
41 / 41
Note
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
10 / 10
41
100.00% covered (success)
100.00%
41 / 41
 setId
n/a
0 / 0
1
n/a
0 / 0
 getId
n/a
0 / 0
1
n/a
0 / 0
 setGSActorId
n/a
0 / 0
1
n/a
0 / 0
 getGSActorId
n/a
0 / 0
1
n/a
0 / 0
 setContent
n/a
0 / 0
1
n/a
0 / 0
 getContent
n/a
0 / 0
1
n/a
0 / 0
 setRendered
n/a
0 / 0
1
n/a
0 / 0
 getRendered
n/a
0 / 0
1
n/a
0 / 0
 setReplyTo
n/a
0 / 0
1
n/a
0 / 0
 getReplyTo
n/a
0 / 0
1
n/a
0 / 0
 setIsLocal
n/a
0 / 0
1
n/a
0 / 0
 getIsLocal
n/a
0 / 0
1
n/a
0 / 0
 setSource
n/a
0 / 0
1
n/a
0 / 0
 getSource
n/a
0 / 0
1
n/a
0 / 0
 setConversation
n/a
0 / 0
1
n/a
0 / 0
 getConversation
n/a
0 / 0
1
n/a
0 / 0
 setRepeatOf
n/a
0 / 0
1
n/a
0 / 0
 getRepeatOf
n/a
0 / 0
1
n/a
0 / 0
 setScope
n/a
0 / 0
1
n/a
0 / 0
 getScope
n/a
0 / 0
1
n/a
0 / 0
 setCreated
n/a
0 / 0
1
n/a
0 / 0
 getCreated
n/a
0 / 0
1
n/a
0 / 0
 setModified
n/a
0 / 0
1
n/a
0 / 0
 getModified
n/a
0 / 0
1
n/a
0 / 0
 getActorNickname
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getAvatarUrl
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 getAllNotes
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 getAttachments
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
5 / 5
 getLinks
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
5 / 5
 getReplies
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 getReplyToNickname
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
7 / 7
 isVisibleTo
100.00% covered (success)
100.00%
1 / 1
7
100.00% covered (success)
100.00%
9 / 9
 jsonSerialize
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 schemaDef
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
1<?php
2
3// {{{ License
4
5// This file is part of GNU social - https://www.gnu.org/software/social
6//
7// GNU social is free software: you can redistribute it and/or modify
8// it under the terms of the GNU Affero General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// GNU social is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU Affero General Public License for more details.
16//
17// You should have received a copy of the GNU Affero General Public License
18// along with GNU social.  If not, see <http://www.gnu.org/licenses/>.
19
20// }}}
21
22namespace App\Entity;
23
24use App\Core\Cache;
25use App\Core\DB\DB;
26use App\Core\Entity;
27use App\Core\Event;
28use App\Core\VisibilityScope;
29use DateTimeInterface;
30
31/**
32 * Entity for notices
33 *
34 * @category  DB
35 * @package   GNUsocial
36 *
37 * @author    Hugo Sales <hugo@hsal.es>
38 * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
39 * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
40 */
41class Note extends Entity
42{
43    // {{{ Autocode
44    // @codeCoverageIgnoreStart
45    private int $id;
46    private int $gsactor_id;
47    private ?string $content;
48    private ?string $rendered;
49    private ?int $reply_to;
50    private ?bool $is_local;
51    private ?string $source;
52    private ?int $conversation;
53    private ?int $repeat_of;
54    private int $scope = 1;
55    private \DateTimeInterface $created;
56    private \DateTimeInterface $modified;
57
58    public function setId(int $id): self
59    {
60        $this->id = $id;
61        return $this;
62    }
63
64    public function getId(): int
65    {
66        return $this->id;
67    }
68
69    public function setGSActorId(int $gsactor_id): self
70    {
71        $this->gsactor_id = $gsactor_id;
72        return $this;
73    }
74
75    public function getGSActorId(): int
76    {
77        return $this->gsactor_id;
78    }
79
80    public function setContent(?string $content): self
81    {
82        $this->content = $content;
83        return $this;
84    }
85
86    public function getContent(): ?string
87    {
88        return $this->content;
89    }
90
91    public function setRendered(?string $rendered): self
92    {
93        $this->rendered = $rendered;
94        return $this;
95    }
96
97    public function getRendered(): ?string
98    {
99        return $this->rendered;
100    }
101
102    public function setReplyTo(?int $reply_to): self
103    {
104        $this->reply_to = $reply_to;
105        return $this;
106    }
107
108    public function getReplyTo(): ?int
109    {
110        return $this->reply_to;
111    }
112
113    public function setIsLocal(?bool $is_local): self
114    {
115        $this->is_local = $is_local;
116        return $this;
117    }
118
119    public function getIsLocal(): ?bool
120    {
121        return $this->is_local;
122    }
123
124    public function setSource(?string $source): self
125    {
126        $this->source = $source;
127        return $this;
128    }
129
130    public function getSource(): ?string
131    {
132        return $this->source;
133    }
134
135    public function setConversation(?int $conversation): self
136    {
137        $this->conversation = $conversation;
138        return $this;
139    }
140
141    public function getConversation(): ?int
142    {
143        return $this->conversation;
144    }
145
146    public function setRepeatOf(?int $repeat_of): self
147    {
148        $this->repeat_of = $repeat_of;
149        return $this;
150    }
151
152    public function getRepeatOf(): ?int
153    {
154        return $this->repeat_of;
155    }
156
157    public function setScope(int $scope): self
158    {
159        $this->scope = $scope;
160        return $this;
161    }
162
163    public function getScope(): int
164    {
165        return $this->scope;
166    }
167
168    public function setCreated(DateTimeInterface $created): self
169    {
170        $this->created = $created;
171        return $this;
172    }
173
174    public function getCreated(): DateTimeInterface
175    {
176        return $this->created;
177    }
178
179    public function setModified(DateTimeInterface $modified): self
180    {
181        $this->modified = $modified;
182        return $this;
183    }
184
185    public function getModified(): DateTimeInterface
186    {
187        return $this->modified;
188    }
189
190    // @codeCoverageIgnoreEnd
191    // }}} Autocode
192
193    public function getActorNickname()
194    {
195        return GSActor::getNicknameFromId($this->gsactor_id);
196    }
197
198    public function getAvatarUrl()
199    {
200        $url = null;
201        Event::handle('GetAvatarUrl', [$this->getGSActorId(), &$url]);
202        return $url;
203    }
204    public static function getAllNotes(int $noteScope): array
205    {
206        return DB::sql('select * from note n ' .
207            'where n.reply_to is null and (n.scope & :notescope) <> 0 ' .
208            'order by n.created DESC',
209            ['n'         => 'App\Entity\Note'],
210            ['notescope' => $noteScope]
211        );
212    }
213
214    public function getAttachments(): array
215    {
216        return Cache::get('note-attachments-' . $this->id, function () {
217            return DB::dql(
218                    'select att from App\Entity\Attachment att ' .
219                    'join App\Entity\AttachmentToNote atn with atn.attachment_id = att.id ' .
220                    'where atn.note_id = :note_id',
221                    ['note_id' => $this->id]
222                );
223        });
224    }
225
226    public function getLinks(): array
227    {
228        return Cache::get('note-links-' . $this->id, function () {
229            return DB::dql(
230                'select l from App\Entity\Link l ' .
231                'join App\Entity\NoteToLink ntl with ntl.link_id = l.id ' .
232                'where ntl.note_id = :note_id',
233                ['note_id' => $this->id]
234            );
235        });
236    }
237
238    public function getReplies(): array
239    {
240        return Cache::getList('note-replies-' . $this->id, function () {
241            return DB::dql('select n from App\Entity\Note n where n.reply_to = :id', ['id' => $this->id]);
242        });
243    }
244
245    public function getReplyToNickname(): ?string
246    {
247        if (!empty($this->reply_to)) {
248            return Cache::get('note-reply-to-' . $this->id, function () {
249                return DB::dql('select g from App\Entity\Note n join ' .
250                                   'App\Entity\GSActor g with n.gsactor_id = g.id where n.reply_to = :reply',
251                                   ['reply' => $this->reply_to])[0]->getNickname();
252            });
253        }
254        return null;
255    }
256
257    /**
258     * Whether this note is visible to the given actor
259     *
260     * @param mixed $a
261     */
262    public function isVisibleTo(GSActor | LocalUser $a): bool
263    {
264        $scope = VisibilityScope::create($this->scope);
265        return $scope->public
266            || ($scope->follower
267                && null != DB::find('follow', ['follower' => $a->getId(), 'followed' => $this->gsactor_id]))
268            || ($scope->addressee
269                && null != DB::find('notification', ['activity_id' => $this->id, 'gsactor_id' => $a->getId()]))
270            || ($scope->group && [] != DB::dql('select m from group_member m ' .
271                                               'join group_inbox i with m.group_id = i.group_id ' .
272                                               'join note n with i.activity_id = n.id ' .
273                                               'where n.id = :note_id and m.gsactor_id = :actor_id',
274                                               ['note_id' => $this->id, 'actor_id' => $a->getId()]));
275    }
276
277    public function jsonSerialize()
278    {
279        return [
280            'content' => $this->getContent(),
281            'author'  => $this->getGSActorId(),
282        ];
283    }
284
285    public static function schemaDef(): array
286    {
287        return [
288            'name'   => 'note',
289            'fields' => [
290                'id'           => ['type' => 'serial',    'not null' => true],
291                'gsactor_id'   => ['type' => 'int',       'foreign key' => true, 'target' => 'GSActor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'who made the note'],
292                'content'      => ['type' => 'text',      'description' => 'note content'],
293                'rendered'     => ['type' => 'text',      'description' => 'rendered note content, so we can keep the microtags (if not local)'],
294                'reply_to'     => ['type' => 'int',       'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'description' => 'note replied to, null if root of a conversation'],
295                'is_local'     => ['type' => 'bool',      'description' => 'was this note generated by a local actor'],
296                'source'       => ['type' => 'varchar',   'foreign key' => true, 'length' => 32, 'target' => 'NoteSource.code', 'multiplicity' => 'many to one', 'description' => 'fkey to source of note, like "web", "im", or "clientname"'],
297                'conversation' => ['type' => 'int',       'foreign key' => true, 'target' => 'Conversation.id', 'multiplicity' => 'one to one', 'description' => 'the local conversation id'],
298                'repeat_of'    => ['type' => 'int',       'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'description' => 'note this is a repeat of'],
299                'scope'        => ['type' => 'int',       'not null' => true, 'default' => VisibilityScope::PUBLIC, 'description' => 'bit map for distribution scope; 0 = everywhere; 1 = this server only; 2 = addressees; 4 = groups; 8 = followers; 16 = messages; null = default'],
300                'created'      => ['type' => 'datetime',  'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
301                'modified'     => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
302            ],
303            'primary key' => ['id'],
304            'indexes'     => [
305                'note_created_id_is_local_idx'      => ['created', 'is_local'],
306                'note_gsactor_created_idx'          => ['gsactor_id', 'created'],
307                'note_is_local_created_gsactor_idx' => ['is_local', 'created', 'gsactor_id'],
308                'note_repeat_of_created_idx'        => ['repeat_of', 'created'],
309                'note_conversation_created_idx'     => ['conversation', 'created'],
310                'note_reply_to_idx'                 => ['reply_to'],
311            ],
312            'fulltext indexes' => ['notice_fulltext_idx' => ['content']],
313        ];
314    }
315}