aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/entity/User.kt
blob: 6bd3254fc695e79383c2324174c3d4b4899e31bd (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
package net.lacolaco.smileessence.entity

import java.util.HashMap

class User private constructor(override val id: Long, screenName: String) : IdObject {
    var screenName = screenName
        private set
    var profileImageUrl: String = "<not set>"
        private set(value) {
            var url = value.substring(0, value.lastIndexOf("_"))
            val extIndex = value.lastIndexOf(".")
            if (extIndex > value.lastIndexOf("/")) {
                url += value.substring(extIndex)
            }
            field = url
        }
    var name: String? = null
        private set
    var profileBannerUrl: String? = null
        private set
    var description: String = "<not set>"
        private set
    var location: String = "<not set>"
        private set
    var url: String = "<not set>"
        private set
    var favoritesCount: Int = 0
        private set
    var statusesCount: Int = 0
        private set
    var friendsCount: Int = 0
        private set
    var followersCount: Int = 0
        private set
    var isProtected: Boolean = false
        private set
    var isVerified: Boolean = false
        private set

    private fun update(user: twitter4j.User) {
        // FIXME
        isProtected = user.isProtected
        if (user.screenName != null)
            screenName = user.screenName
        if (user.name != null)
            name = user.name
        if (user.profileImageURLHttps != null)
            this.profileImageUrl = user.profileImageURLHttps

        isVerified = user.isVerified
        if (user.profileBannerURL != null)
            profileBannerUrl = user.profileBannerURL
        if (user.description != null)
            description = user.description
        if (user.location != null)
            location = user.location
        if (user.url != null)
            url = user.url
        if (user.favouritesCount != -1)
            favoritesCount = user.favouritesCount
        if (user.statusesCount != -1)
            statusesCount = user.statusesCount
        if (user.friendsCount != -1)
            friendsCount = user.friendsCount
        if (user.followersCount != -1)
            followersCount = user.followersCount
    }

    // helper methods
    val userHomeURL: String
        get() = String.format("https://twitter.com/%s", screenName)

    val aclogTimelineURL: String
        get() = String.format("https://aclog.rhe.jp/%s/timeline", screenName)

    val formattedName: String
        get() = screenName + " / " + name

    companion object {
        private val storage = HashMap<Long, User>()

        @Synchronized
        fun fetch(userId: Long): User? {
            return storage[userId]
        }

        @Synchronized
        fun fromTwitter(st: twitter4j.User): User {
            var u: User? = fetch(st.id)
            if (u == null) {
                u = User(st.id, st.screenName)
                storage.put(st.id, u)
            }

            u.update(st)
            return u
        }

        // only for initialization; DO NOT have reference for this object
        @Synchronized
        fun _makeSkeleton(id: Long, screenName: String): User {
            var u: User? = fetch(id)
            if (u != null) {
                return u
            } else {
                u = User(id, screenName)
                storage.put(id, u)
                return u
            }
        }
    }
}