aboutsummaryrefslogtreecommitdiffhomepage
path: root/routes/actor_profile_fragment.php
blob: 0a2a881c59e866200629e7647f283ff838434919 (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
<?php

use Digitigrade\Model\Actor;
use Digitigrade\Model\StorageObject;
use Digitigrade\Model\UserAccount;
use Digitigrade\Router;
use Digitigrade\StorageProvider\FilesystemStorage;

Router::getInstance()->mount('/fragment/actor/:id', function (array $args) {
    render_template('actor_profile', ['actor' => Actor::find($args['id'])]);
});

Router::getInstance()->mount('/fragment/actor/:id/followButton', function (array $args) {
    $user = UserAccount::requireByCurrentSession();
    $actor = Actor::find($args['id']);

    if ($user->actor->follows($actor)) {
        // already following, therefore unfollow
        $user->actor->unfollow($actor);
    } elseif ($user->actor->followsPending($actor)) {
        // can't actually do anything at this point
        // TODO: cancel request? is this possible?? do i need to modify the protocol
    } else {
        // follow
        $user->actor->follow($actor);
    }

    render_template('actor_profile_follow_button', ['user' => $user, 'actor' => $actor]);
});

Router::getInstance()->mount('/fragment/actor/:id/acceptPending', function (array $args) {
    $user = UserAccount::requireByCurrentSession();
    $actor = Actor::find($args['id']);

    $user->actor->acceptPendingFollowFrom($actor);

    render_template('actor_profile_pending_follow', ['response' => 'accepted']);
});

Router::getInstance()->mount('/fragment/actor/:id/rejectPending', function (array $args) {
    $user = UserAccount::requireByCurrentSession();
    $actor = Actor::find($args['id']);

    $user->actor->rejectPendingFollowFrom($actor);

    render_template('actor_profile_pending_follow', ['response' => 'rejected']);
});

Router::getInstance()->mount('/fragment/profile', function (array $args) {
    $user = UserAccount::requireByCurrentSession();
    $actor = $user->actor;
    if (isset($_POST['displayName'])) {
        // save changes and put the normal profile back
        $actor->displayName = trim($_POST['displayName']);
        if (strlen($actor->displayName) <= 0) {
            $actor->displayName = $actor->handle;
        }
        $actor->bio = trim($_POST['bio'] ?? '');

        if (isset($_FILES['avatar'])) {
            // TODO: allow using other backends
            $obj = StorageObject::createFromUpload(FilesystemStorage::class, $_FILES['avatar']);
            $actor->avatar = $obj->getUrl();
        }

        $actor->save();
        $actor->publish();

        render_template('actor_profile', ['actor' => $actor]);
    } else {
        render_template('actor_profile_editable', ['actor' => $actor]);
    }
});

Router::getInstance()->mount('/fragment/profile/resetAvatar', function (array $args) {
    $user = UserAccount::requireByCurrentSession();
    $user->actor->avatar = null;
    $user->actor->save();
    render_template('actor_profile_reset_avatar', ['reset' => true]);
});