aboutsummaryrefslogtreecommitdiff
path: root/app/routes/Contacts.php
blob: d7fd01040bb9b79d6f6fd35598ea647553caa9d1 (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
<?php
namespace RESTAPI\Routes;

/**
 * @author  <mlunzena@uos.de>
 * @license GPL 2 or later
 * @deprecated Since Stud.IP 5.0. Will be removed in Stud.IP 6.0.
 *
 * @condition user_id ^[a-f0-9]{1,32}$
 * @condition friend_id ^[a-f0-9]{1,32}$
 * @condition group_id ^[a-f0-9]{1,32}$
 */
class Contacts extends \RESTAPI\RouteMap
{

    public static function before()
    {
        require_once 'User.php';
        require_once 'lib/statusgruppe.inc.php';
    }

    /**
     * Lists all contacts of a user
     *
     * @get /user/:user_id/contacts
     */
    public function getUserContacts($user_id)
    {
        if ($GLOBALS['user']->id !== $user_id) {
            $this->error(401);
        }

        // quite degenerated as long as we can only see our own contacts
        $user = $this->requireUser($user_id);

        $total = count($user->contacts);
        $contacts = $user->contacts->limit($this->offset, $this->limit);

        $contacts_json = $this->contactsToJSON($contacts);
        $this->etag(md5(serialize($contacts_json)));

        return $this->paginated($contacts_json,
                                $total, compact('user_id'));
    }

    /**
     * Adds/Updates a contact to user's list of contacts
     *
     * @put /user/:user_id/contacts/:friend_id
     */
    public function addUserContact($user_id, $buddy_user_id)
    {
        if ($GLOBALS['user']->id !== $user_id) {
            $this->error(401);
        }

        $user = $this->requireUser($user_id);
        $friend = $this->requireUser($buddy_user_id);

        // prevent duplicates
        if ($user->isFriendOf($friend)) {
            $this->error(409, sprintf('User "%s" is already a contact', htmlReady($friend->id)));
        }

        $user->contacts[] = $friend;
        $user->store();

        $this->status(201);
    }

    /**
     * Deletes a contact
     *
     * @delete /user/:user_id/contacts/:friend_id
     */
    public function removeUserContact($user_id, $buddy_user_id)
    {
        if ($GLOBALS['user']->id !== $user_id) {
            $this->error(401);
        }

        $user = $this->requireUser($user_id);
        $friend = $this->requireUser($buddy_user_id);

        if (!$user->isFriendOf($friend)) {
            $this->notFound("Contact not found");
        }

        $user->contacts->unsetByPK($friend->id);
        $user->store();

        $this->status(204);
    }


    /**
     * List all contact groups of a user
     *
     * @get /user/:user_id/contact_groups
     */
    public function getUserContactGroups($user_id)
    {
        if ($GLOBALS['user']->id !== $user_id) {
            $this->error(401);
        }

        $contact_groups = \SimpleCollection::createFromArray(
                \Statusgruppen::findByRange_id($GLOBALS['user']->id))
            ->orderBy('name ASC');

        $total = count($contact_groups);
        $contact_groups = $contact_groups->limit($this->offset, $this->limit);

        $contact_groups_json = $this->contactGroupsToJSON($contact_groups);
        $this->etag(md5(serialize($contact_groups_json)));

        return $this->paginated($contact_groups_json,
                                $total, compact('user_id'));
    }

    /**
     * Create a new contact group for a user.
     *
     * @post /user/:user_id/contact_groups
     */
    public function createContactGroup($user_id)
    {
        if ($GLOBALS['user']->id !== $user_id) {
            $this->error(401);
        }

        if (!isset($this->data['name']) || !mb_strlen($name = trim($this->data['name']))) {
            $this->error(400, 'Contact group name required.');
        }

        $group = new \Statusgruppen();
        $group->range_id       = $GLOBALS['user']->id;
        $group->name           = $name;
        $group->size           = 0;
        $group->selfassign     = 0;
        $group->calendar_group = 0;
        $group->store();
        $this->redirect('contact_group/' . $group->id, 201, 'ok');
    }

    /**
     * Show a single contact group
     *
     * @get /contact_group/:group_id
     */
    public function showContactGroup($group_id)
    {
        $group = $this->requireContactGroup($group_id);
        $contact_group_json = $this->contactGroupToJSON($group);
        $this->etag(md5(serialize($contact_group_json)));
        return $contact_group_json;
    }

    /**
     * Remove a contact group
     *
     * @delete /contact_group/:group_id
     */
    public function destroyContactGroup($group_id)
    {
        $group = $this->requireContactGroup($group_id);

        $group->remove();

        $this->status(204);
    }

    /**
     * List all members of a contact group
     *
     * @get /contact_group/:group_id/members
     */
    public function indexOfContactGroupMembers($group_id)
    {
        $group = $this->requireContactGroup($group_id);
        $contacts = $group->members->limit($this->offset, $this->limit);

        $json = [];
        foreach ($contacts as $contact) {
            $url = $this->urlf('/contact_group/%s/members/%s', [$group_id, $contact->user_id]);
            $json[$url] = User::getMiniUser($this, $contact->user);
        }

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

        return $this->paginated($json, count($group->members), compact('group_id'));
    }

    /**
     * Add a user to a contact group
     *
     * @put /contact_group/:group_id/members/:user_id
     */
    public function addToContactGroup($group_id, $user_id)
    {
        $group = $this->requireContactGroup($group_id);
        $user = $this->requireUser($user_id);

        // prevent duplicates
        $exists = $group->members->findBy('user_id', $user_id)->first();
        if ($exists) {
            $this->halt(204);
        }

        $new_contact = [
            'owner_id' => $GLOBALS['user']->id,
            'user_id'  => $user->id];

        $new_contact['group_assignments'][] = ['statusgruppe_id' => $group->id,
                                                    'user_id'         => $user->id];

        $success = (bool)\Contact::import($new_contact)->store();


        if (!$success) {
            $this->error(500);
        }

        $this->status(201);
    }

    /**
     * Remove a user from a contact group
     *
     * @delete /contact_group/:group_id/members/:user_id
     */
    public function removeFromContactGroup($group_id, $user_id)
    {
        $group = $this->requireContactGroup($group_id);
        $membership = $group->members->findBy('user_id', $user_id)->first();
        if (!$membership) {
            $this->notFound();
        }

        $membership->delete();

        $this->status(204);
    }


    /**************************************************/
    /* PRIVATE HELPER METHODS                         */
    /**************************************************/

    private function requireUser($user_id)
    {
        $user = \User::find($user_id);
        // TODO: checks visibility using the global perm object!
        if (!$user || !get_visibility_by_id($user_id)) {
            $this->notFound(sprintf("Could not find user with id: %s", htmlReady($user_id)));
        }

        return $user;
    }

    private function requireContactGroup($group_id)
    {
        $group = \Statusgruppen::find($group_id);
        if (!$group) {
            $this->notFound();
        }

        if ($group->range_id !== $GLOBALS['user']->id) {
            $this->error(401);
        }
        return $group;
    }

    private function contactsToJSON($contacts) {
        $result = [];
        foreach ($contacts as $contact) {
            $result[] = User::getMiniUser($this, $contact);
        }
        return $result;
    }

    private function contactGroupsToJSON($contact_groups)
    {
        $result = [];
        foreach ($contact_groups as $cg) {
            $url = $this->urlf('/contact_group/%s', [htmlReady($cg->id)]);
            $result[$url] = $this->contactGroupToJSON($cg);
        }
        return $result;
    }

    private function contactGroupToJSON($group)
    {
        $json = [
            'id'             => $group->id,
            'name'           => (string) $group->name,
            'contacts'       => $this->urlf('/contact_group/%s/members', [htmlReady($group->id)]),
            'contacts_count' => sizeof($group->members)
        ];
        return $json;
    }
}