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
|
package net.lacolaco.smileessence.entity
import android.net.Uri
import twitter4j.Status
import java.util.*
class Tweet private constructor(
override val id: Long,
val user: User,
val createdAt: Date,
val source: String,
val inReplyToStatusId: Long?,
private val retweetedTweet: Tweet?,
val entities: Entities,
val text: String
) : IdObject {
var favoriteCount: Int = -1
get() = retweetedTweet?.favoriteCount ?: field
private set
var retweetCount: Int = -1
get() = retweetedTweet?.retweetCount ?: field
private set
val favoriters: MutableSet<Long> = retweetedTweet?.favoriters ?: HashSet()
val retweets: MutableMap<Long, Long> = retweetedTweet?.retweets ?: HashMap()
// Utility functions
val isRetweet = retweetedTweet != null
val originalTweet = retweetedTweet ?: this
// XXX
val embeddedStatusIDs: List<Long> by lazy {
entities.urlsExpanded.mapNotNull {
val uri = Uri.parse(it)
if ("twitter.com" == uri.host) {
val segments = uri.pathSegments
if (segments.size >= 3 && segments[1] == "status") {
return@mapNotNull segments[2].toLongOrNull()
}
}
null
}
}
companion object {
private val storage = HashMap<Long, Tweet>()
@Synchronized
fun cached(statusId: Long): Tweet? {
return storage[statusId]
}
@Synchronized
fun fromTwitter(st: twitter4j.Status, myUserId: Long): Tweet {
var t = cached(st.id)
if (t == null) {
val retweetedTweet = st.retweetedStatus?.let { Tweet.fromTwitter(it, myUserId) }
val entities = retweetedTweet?.entities ?: Entities.fromTwitter(st)
t = Tweet(
st.id,
User.fromTwitter(st.user),
st.createdAt,
st.source,
st.inReplyToStatusId.takeIf { it != -1L },
retweetedTweet,
entities,
retweetedTweet?.text ?: entities.extractText(st, st.text)
).apply {
if (st.isFavorited)
favoriters += myUserId
if (st.currentUserRetweetId != -1L)
retweets[myUserId] = st.currentUserRetweetId
favoriteCount = st.favoriteCount
retweetCount = st.retweetCount
}
storage[st.id] = t
} else {
User.fromTwitter(st.user)
st.retweetedStatus?.let { Tweet.fromTwitter(it, myUserId) }
if (st.isFavorited)
t.favoriters += myUserId
else
t.favoriters -= myUserId
if (st.currentUserRetweetId != -1L)
t.retweets[myUserId] = st.currentUserRetweetId
t.favoriteCount = st.favoriteCount
t.retweetCount = st.retweetCount
}
return t
}
@Synchronized
fun fromTwitter(sts: List<Status>, myUserId: Long): List<Tweet> {
return sts.map { st -> fromTwitter(st, myUserId) }
}
val placeHolder = Tweet(
-1,
User.placeHolder,
Date(),
"System",
null,
null,
Entities.placeHolder,
"Place holder"
)
}
}
|