the-algorithm/visibilitylib/src/main/scala/com/twitter/visibility/builder/users/RelationshipVerbHelpers.scala
twitter-team ef4c5eb65e Twitter Recommendation Algorithm
Please note we have force-pushed a new initial commit in order to remove some publicly-available Twitter user information. Note that this process may be required in the future.
2023-03-31 17:36:31 -05:00

80 lines
2.3 KiB
Scala

package com.twitter.visibility.builder.users
import com.twitter.finagle.stats.Counter
import com.twitter.gizmoduck.thriftscala.Perspective
import com.twitter.gizmoduck.thriftscala.User
import com.twitter.stitch.Stitch
import com.twitter.visibility.common.UserId
case object ViewerVerbsAuthor {
def apply(
authorId: UserId,
viewerIdOpt: Option[UserId],
relationship: (UserId, UserId) => Stitch[Boolean],
relationshipCounter: Counter
): Stitch[Boolean] = {
relationshipCounter.incr()
viewerIdOpt match {
case Some(viewerId) => relationship(viewerId, authorId)
case _ => Stitch.False
}
}
def apply(
author: User,
viewerId: Option[UserId],
checkPerspective: Perspective => Option[Boolean],
relationship: (UserId, UserId) => Stitch[Boolean],
relationshipCounter: Counter
): Stitch[Boolean] = {
author.perspective match {
case Some(perspective) =>
checkPerspective(perspective) match {
case Some(status) =>
relationshipCounter.incr()
Stitch.value(status)
case None =>
ViewerVerbsAuthor(author.id, viewerId, relationship, relationshipCounter)
}
case None => ViewerVerbsAuthor(author.id, viewerId, relationship, relationshipCounter)
}
}
}
case object AuthorVerbsViewer {
def apply(
authorId: UserId,
viewerIdOpt: Option[UserId],
relationship: (UserId, UserId) => Stitch[Boolean],
relationshipCounter: Counter
): Stitch[Boolean] = {
relationshipCounter.incr()
viewerIdOpt match {
case Some(viewerId) => relationship(authorId, viewerId)
case _ => Stitch.False
}
}
def apply(
author: User,
viewerId: Option[UserId],
checkPerspective: Perspective => Option[Boolean],
relationship: (UserId, UserId) => Stitch[Boolean],
relationshipCounter: Counter
): Stitch[Boolean] = {
author.perspective match {
case Some(perspective) =>
checkPerspective(perspective) match {
case Some(status) =>
relationshipCounter.incr()
Stitch.value(status)
case None =>
AuthorVerbsViewer(author.id, viewerId, relationship, relationshipCounter)
}
case None => AuthorVerbsViewer(author.id, viewerId, relationship, relationshipCounter)
}
}
}