package net.lacolaco.smileessence.entity import java.util.* class User private constructor(override val id: Long, screenName: String, rawProfileImageUrl: String) : IdObject { var screenName = screenName private set var profileImageUrl: String = rawProfileImageUrl 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 = "" private set var location: String = "" private set var url: String = "" 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) // XXX val decoratedDescription: String get() { var html = description html = html.replace("https?://[\\w/:%#$&?()~.=+-]+".toRegex(), "$0") html = html.replace("@([a-zA-Z0-9_]+)".toRegex(), "$0") html = html.replace("\r\n".toRegex(), "
") return html } companion object { private val storage = HashMap() @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 } @Synchronized fun makeSkeletonForInternalUseOnly(id: Long, screenName: String, profileImageUrl: String) = fetch(id) ?: User(id, screenName, profileImageUrl).apply { storage.put(id, this) } } }