aboutsummaryrefslogtreecommitdiff
path: root/lib/models/BlubberGlobalThread.php
blob: b19a7f96e6129c0f2ce70cdf8a701a2536b02801 (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
<?php

class BlubberGlobalThread extends BlubberThread
{
    public function isReadable(string $user_id = null)
    {
        return true;
    }

    public function getName()
    {
        return _("Globaler Blubber");
    }

    public function getContextTemplate()
    {
        $template = $GLOBALS['template_factory']->open('blubber/global_context');
        $template->thread = $this;
        $template->hashtags = $this->getHashtags(time() - 86400 * 365);
        $template->unfollowed = !$this->isFollowedByUser();
        return $template;
    }

    /**
     * Lets a user follow a thread
     *
     * @param string|null $user_id Id of the user (optional, defaults to current user
     */
    public function addFollowingByUser($user_id = null)
    {
        if (Config::get()->BLUBBER_GLOBAL_THREAD_OPTOUT) {
            return parent::addFollowingByUser($user_id);
        }

        $query = "REPLACE INTO `blubber_threads_followstates`
                  VALUES (:thread_id, :user_id, 'followed', UNIX_TIMESTAMP())";
        DBManager::get()->execute($query, [
            ':thread_id' => $this->id,
            ':user_id'   => $user_id ?? $GLOBALS['user']->id,
        ]);
    }

    /**
     * Lets a user unfollow a thread
     *
     * @param string|null $user_id Id of the user (optional, defaults to current user
     */
    public function removeFollowingByUser($user_id = null)
    {
        if (Config::get()->BLUBBER_GLOBAL_THREAD_OPTOUT) {
            return parent::removeFollowingByUser($user_id);
        }

        $query = "DELETE FROM `blubber_threads_followstates`
                  WHERE `thread_id` = :thread_id
                    AND `user_id` = :user_id";
        DBManager::get()->execute($query, [
            ':thread_id' => $this->id,
            ':user_id'   => $user_id ?? $GLOBALS['user']->id,
        ]);
    }

    /**
     * Returns whether a user follows a thread.
     *
     * @param string|null $user_id Id of the user (optional, defaults to current user
     * @return bool
     */
    public function isFollowedByUser($user_id = null)
    {
        if (Config::get()->BLUBBER_GLOBAL_THREAD_OPTOUT) {
            return parent::isFollowedByUser($user_id);
        }

        $query = "SELECT 1
                  FROM `blubber_threads_followstates`
                  WHERE `thread_id` = :thread_id
                    AND `user_id` = :user_id
                    AND `state` = 'followed'";
        $followed = (bool) DBManager::get()->fetchColumn($query, [
            ':thread_id' => $this->getId(),
            ':user_id'   => $user_id ?? $GLOBALS['user']->id,
        ]);

        return $followed;
    }

    /**
     * {@inheritdoc}
     */
    protected function getNotificationUsersQueryAndParameters()
    {
        $parameters = [
            ':user_id'   => $GLOBALS['user']->id,
            ':thread_id' => $this->id,
        ];

        if (Config::get()->BLUBBER_GLOBAL_THREAD_OPTOUT) {
            $query = "SELECT `auth_user_md5`.`user_id`
                      FROM `auth_user_md5`
                      LEFT JOIN `blubber_threads_followstates` ON (
                          `blubber_threads_followstates`.`thread_id` = :thread_id
                          AND `blubber_threads_followstates`.`user_id` = `auth_user_md5`.`user_id`
                          AND `blubber_threads_followstates`.`state` = 'unfollowed'
                      )
                      WHERE auth_user_md5.user_id != :user_id
                        AND `blubber_threads_followstates`.`user_id` IS NULL";
            return compact('query', 'parameters');
        }

        $query = "SELECT `auth_user_md5`.`user_id`
                  FROM `auth_user_md5`
                  JOIN `blubber_threads_followstates` ON (
                      `blubber_threads_followstates`.`thread_id` = :thread_id
                      AND `blubber_threads_followstates`.`user_id` = `auth_user_md5`.`user_id`
                      AND `blubber_threads_followstates`.`state` = 'followed'
                  )
                  WHERE auth_user_md5.user_id != :user_id";

        return compact('query', 'parameters');
    }

    public function getAvatar()
    {
        return Icon::create('blubber')->asImagePath();
    }
}