aboutsummaryrefslogtreecommitdiff
path: root/app/routes/Blubber.php
blob: 14450887b707bf169cc01db68f842f2b1565dd1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
namespace RESTAPI\Routes;

/**
 * @license GPL 2 or later
 * @deprecated Since Stud.IP 5.0. Will be removed in Stud.IP 6.0.
 *
 * @condition course_id ^[a-f0-9]{1,32}$
 * @condition stream_id ^(global|[a-f0-9]{1,32})$
 * @condition user_id ^[a-f0-9]{1,32}$
 * @condition blubber_id ^[a-f0-9]{1,32}$
 */
class Blubber extends \RESTAPI\RouteMap
{

    /**
     * Get content and some comments for a blubber-thread or for the "global" thread all "public" threads.
     *
     * @get /blubber/threads/:thread_id
     * @param string $thread_id   id of the blubber thread or "global" if you want public threads (not comments). Remind the global thread is a virtual thread with a special behaviour.
     * @return array   the blubber as array
     */
    public function getThreadData($thread_id)
    {
        if (!$GLOBALS['perm']->have_perm('autor')) {
            $this->error(401);
        }
        $GLOBALS['user']->cfg->store('BLUBBER_DEFAULT_THREAD', $thread_id);

        $thread = new \BlubberThread($thread_id);
        $thread = \BlubberThread::upgradeThread($thread);
        if (!$thread->isReadable()) {
            $this->error(401);
        }

        $json = $thread->getJSONData(50, null, \Request::get("search"));
        $thread->markAsRead();

        $this->etag(md5(serialize($json)));

        return $json;
    }

    /**
     * Get threads
     *
     * @get /blubber/threads
     * @return array   the stream as array
     */
    public function getMyThreads()
    {
        $threads_data = [
            'threads'   => [],
            'more_down' => 0,
        ];
        $limit = \Request::int('limit', 50);

        $threads = \BlubberThread::findMyGlobalThreads(
            $limit + 1,
            null,
            \Request::int('timestamp'),
            null,
            \Request::get("search") ?: null
        );
        if (count($threads) > $limit) {
            array_pop($threads);
            $threads_data['more_down'] = 1;
        }
        foreach ($threads as $thread) {
            $threads_data['threads'][] = [
                'thread_id' => $thread->getId(),
                'avatar'    => $thread->getAvatar(),
                'name'      => $thread->getName(),
                'timestamp' => (int) $thread->getLatestActivity(),
            ];
        }
        return $threads_data;
    }

    /**
     * Write a comment to a thread
     *
     * @post /blubber/threads/:thread_id/comments
     * @param string $thread_id   id of the blubber thread
     * @return array   the comment as array
     */
    public function postComment($thread_id)
    {
        if (!$GLOBALS['perm']->have_perm('autor')) {
            $this->error(401);
        }

        if (!trim($this->data['content'])) {
            $this->error(406);
        }

        $thread = \BlubberThread::find($thread_id);
        if (!$thread->isCommentable()) {
            $this->error(401);
        }

        $comment = new \BlubberComment();
        $comment['thread_id'] = $thread_id;
        $comment['content'] = $this->data['content'];
        $comment['user_id'] = $GLOBALS['user']->id;
        $comment['external_contact'] = 0;
        $comment->store();

        $thread->setLastVisit();

        return $comment->getJSONData();
    }

    /**
     * Write a comment to a thread
     *
     * @put /blubber/threads/:thread_id/comments/:comment_id
     *
     * @param string $thread_id   id of the blubber thread
     * @param string $comment     id of the comment
     *
     * @return array   the comment as array
     */
    public function editComment($thread_id, $comment_id)
    {
        $comment = \BlubberComment::find($comment_id);
        if (!$comment->isWritable()) {
            $this->error(401);
        }
        $old_content = $comment['content'];
        $comment['content'] = $this->data['content'];

        if ($comment['user_id'] !== $GLOBALS['user']->id) {
            $messaging = new \messaging();
            $message = sprintf(
                _("%s hat als Moderator gerade Ihren Beitrag in Blubber editiert.\n\nDie alte Version des Beitrags lautete:\n\n%s\n\nDie neue lautet:\n\n%s\n"),
                get_fullname(), $old_content, $comment['content']
            );

            $message .= "\n\n";

            $message .= '[' . _('Link zu diesem Beitrag') . ']';
            $message .= \URLHelper::getURL(
                "{$GLOBALS['ABSOLUTE_URI_STUDIP']}dispatch.php/blubber/index/{$comment->thread_id}",
                [],
                true
            );

            $messaging->insert_message(
                $message,
                get_username($comment['user_id']),
                $GLOBALS['user']->id,
                null, null, null, null,
                _("Änderungen an Ihrem Blubber.")
            );
        }

        if (!trim($this->data['content'])) {
            $data = $comment->getJSONData();
            $comment->delete();
        } else {
            $comment->store();
            $data = $comment->getJSONData();
        }
        return $data;
    }

    /**
     * Write a comment to a thread
     *
     * @get /blubber/threads/:thread_id/comments
     *
     * @param string $thread_id   id of the blubber thread
     *
     * @return array   the comments as array
     */
    public function getComments($thread_id)
    {
        if (!$GLOBALS['perm']->have_perm('autor')) {
            $this->error(401);
        }

        $thread = new \BlubberThread($thread_id);
        if (!$thread->isReadable()) {
            $this->error(401);
        }

        $modifier = \Request::get('modifier');
        if ($modifier === 'olderthan') {
            $limit = \Request::int('limit', 50);

            $query = "SELECT blubber_comments.*
                      FROM blubber_comments
                      WHERE blubber_comments.thread_id = :thread_id
                        AND blubber_comments.mkdate <= :timestamp
                      ORDER BY mkdate DESC
                      LIMIT :limit";
            $result = \DBManager::get()->fetchAll($query, [
                'thread_id' => $thread_id,
                'timestamp' => \Request::int('timestamp', time()),
                'limit'     => $limit + 1,
            ]);

            $output = ['comments' => []];

            if (count($result) > $limit) {
                array_pop($result);
                $output['more_up'] = 1;
            } else {
                $output['more_up'] = 0;
            }
            foreach ($result as $data) {
                $comment = \BlubberComment::buildExisting($data);
                $output['comments'][] = $comment->getJSONData();
            }
            return $output;
        }

        if ($modifier === 'newerthan') {
            $limit = \Request::int('limit', 50);

            $query = "SELECT blubber_comments.*
                      FROM blubber_comments
                      WHERE blubber_comments.thread_id = :thread_id
                        AND blubber_comments.mkdate >= :timestamp
                      ORDER BY mkdate
                      LIMIT :limit";
            $comments = \DBManager::get()->fetchAll($query, [
                'thread_id' => $thread_id,
                'timestamp' => \Request::int('timestamp', time()),
                'limit'     => $limit + 1,
            ], function ($comment) {
                return \BlubberComment::buildExisting($comment)->getJSONData();
            });

            $output = ['comments' => $comments];

            if (count($comments) > $limit) {
                array_pop($output['comments']);
                $output['more_down'] = 1;
            } else {
                $output['more_down'] = 0;
            }

            return $output;
        }

        $query = "SELECT blubber_comments.*
                  FROM blubber_comments
                  WHERE blubber_comments.thread_id = :thread_id ";
        $parameters = ['thread_id' => $thread_id];

        if (\Request::get('search')) {
            $query .= " AND blubber_comments.content LIKE :search ";
            $parameters['search'] = '%'.\Request::get('search').'%';
        }
        $query .= " ORDER BY mkdate ASC ";

        $output['comments'] = \DBManager::get()->fetchAll($query, $parameters, function ($comment) {
            return \BlubberComment::buildExisting($comment)->getJSONData();
        });
        $output['more_up'] = 0;
        $output['more_down'] = 0;

        return $output;
    }

    /**
     * Does the current user follow the thread?
     *
     * @get /blubber/threads/:thread_id/follow
     */
    public function threadIsFollowed($thread_id)
    {
        return $this->requireThread($thread_id)->isFollowedByUser();
    }

    /**
     * User follows a thread.
     *
     * @post /blubber/threads/:thread_id/follow
     *
     * @param string $thread_id   id of the blubber thread
     */
    public function followThread($thread_id)
    {
        $this->requireThread($thread_id)->addFollowingByUser();
    }

    /**
     * User unfollows a thread.
     *
     * @delete /blubber/threads/:thread_id/follow
     *
     * @param string $thread_id   id of the blubber thread
     */
    public function unfollowThread($thread_id)
    {
        $this->requireThread($thread_id)->removeFollowingByUser();
    }

    /**
     * Returns a blubber thread and checks permissions.
     *
     * @param string $thread_id Id of the blubber thread
     * @return \BlubberThread
     */
    private function requireThread($thread_id)
    {
        if (!$GLOBALS['perm']->have_perm('autor')) {
            $this->error(401);
        }

        $thread = new \BlubberThread($thread_id);
        if (!$thread->isReadable()) {
            $this->error(401);
        }

        return \BlubberThread::upgradeThread($thread);
    }
}