-
Notifications
You must be signed in to change notification settings - Fork 12.2k
/
MediaInfoHydrator.scala
73 lines (61 loc) · 2.23 KB
/
MediaInfoHydrator.scala
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
package com.twitter.tweetypie
package hydrator
import com.twitter.stitch.Stitch
import com.twitter.tweetypie.core._
import com.twitter.tweetypie.media.MediaKeyUtil
import com.twitter.tweetypie.media.MediaMetadataRequest
import com.twitter.tweetypie.repository._
import com.twitter.tweetypie.thriftscala._
import java.nio.ByteBuffer
object MediaInfoHydrator {
type Ctx = MediaEntityHydrator.Uncacheable.Ctx
type Type = MediaEntityHydrator.Uncacheable.Type
private[this] val log = Logger(getClass)
def apply(repo: MediaMetadataRepository.Type, stats: StatsReceiver): Type = {
val attributableUserCounter = stats.counter("attributable_user")
ValueHydrator[MediaEntity, Ctx] { (curr, ctx) =>
val request =
toMediaMetadataRequest(
mediaEntity = curr,
tweetId = ctx.tweetId,
extensionsArgs = ctx.opts.extensionsArgs
)
request match {
case None => Stitch.value(ValueState.unmodified(curr))
case Some(req) =>
repo(req).liftToTry.map {
case Return(metadata) =>
if (metadata.attributableUserId.nonEmpty) attributableUserCounter.incr()
ValueState.delta(
curr,
metadata.updateEntity(
mediaEntity = curr,
tweetUserId = ctx.userId,
includeAdditionalMetadata = ctx.includeAdditionalMetadata
)
)
case Throw(ex) if !PartialEntityCleaner.isPartialMedia(curr) =>
log.info("Ignored media info repo failure, media entity already hydrated", ex)
ValueState.unmodified(curr)
case Throw(ex) =>
log.error("Media info hydration failed", ex)
ValueState.partial(curr, MediaEntityHydrator.hydratedField)
}
}
}
}
def toMediaMetadataRequest(
mediaEntity: MediaEntity,
tweetId: TweetId,
extensionsArgs: Option[ByteBuffer]
): Option[MediaMetadataRequest] =
mediaEntity.isProtected.map { isProtected =>
val mediaKey = MediaKeyUtil.get(mediaEntity)
MediaMetadataRequest(
tweetId = tweetId,
mediaKey = mediaKey,
isProtected = isProtected,
extensionsArgs = extensionsArgs
)
}
}