|
| 1 | +package org.codeoverflow.chatoverflow.requirement.service.streamelements.impl |
| 2 | + |
| 3 | +import java.time.format.DateTimeFormatter |
| 4 | +import java.time.{LocalDateTime, OffsetDateTime, ZoneOffset} |
| 5 | +import java.util.Currency |
| 6 | + |
| 7 | +import org.codeoverflow.chatoverflow.api.io.dto.User |
| 8 | +import org.codeoverflow.chatoverflow.api.io.dto.stat.stream.SubscriptionTier |
| 9 | +import org.codeoverflow.chatoverflow.api.io.dto.stat.stream.streamelements._ |
| 10 | +import org.codeoverflow.chatoverflow.api.io.event.stream.streamelements._ |
| 11 | +import org.codeoverflow.chatoverflow.api.io.input.event.StreamElementsEventInput |
| 12 | +import org.codeoverflow.chatoverflow.registry.Impl |
| 13 | +import org.codeoverflow.chatoverflow.requirement.impl.EventInputImpl |
| 14 | +import org.codeoverflow.chatoverflow.requirement.service.streamelements.StreamElementsConnector |
| 15 | +import org.codeoverflow.chatoverflow.requirement.service.streamelements.StreamElementsConnector._ |
| 16 | +import org.json.{JSONException, JSONObject} |
| 17 | + |
| 18 | +import scala.reflect.ClassTag |
| 19 | + |
| 20 | +@Impl(impl = classOf[StreamElementsEventInput], connector = classOf[StreamElementsConnector]) |
| 21 | +class StreamElementsEventInputImpl extends EventInputImpl[StreamElementsEvent, StreamElementsConnector] with StreamElementsEventInput { |
| 22 | + |
| 23 | + override def start(): Boolean = { |
| 24 | + sourceConnector.get.registerEventHandler(handleExceptions(onFollow)) |
| 25 | + sourceConnector.get.registerEventHandler(handleExceptions(onSubscription)) |
| 26 | + sourceConnector.get.registerEventHandler(handleExceptions(onDonation)) |
| 27 | + sourceConnector.get.registerEventHandler(handleExceptions(onCheer)) |
| 28 | + sourceConnector.get.registerEventHandler(handleExceptions(onRaid)) |
| 29 | + sourceConnector.get.registerEventHandler(handleExceptions(onHost)) |
| 30 | + true |
| 31 | + } |
| 32 | + |
| 33 | + private def handleExceptions[T: ClassTag](handler: T => Unit): T => Unit = event => { |
| 34 | + try { |
| 35 | + handler(event) |
| 36 | + } catch { |
| 37 | + case e@(_: JSONException | _: IllegalArgumentException) => |
| 38 | + val jsonClass = implicitly[ClassTag[T]].runtimeClass |
| 39 | + logger warn s"Error while parsing follow json of type ${jsonClass.getSimpleName}:" |
| 40 | + logger warn s"${e.getClass.getName} - ${e.getMessage}" |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private def onFollow(event: FollowEventJSON): Unit = { |
| 45 | + val json = event.json |
| 46 | + val data = json.getJSONObject("data") |
| 47 | + |
| 48 | + val follow = new StreamElementsFollow( |
| 49 | + parseUser(data), |
| 50 | + parseTime(json), |
| 51 | + parseProvider(json) |
| 52 | + ) |
| 53 | + call(new StreamElementsFollowEvent(follow)) |
| 54 | + } |
| 55 | + |
| 56 | + private def onSubscription(event: SubscriptionEventJSON): Unit = { |
| 57 | + val json = event.json |
| 58 | + val data = json.getJSONObject("data") |
| 59 | + |
| 60 | + val gifted = data.optBoolean("gifted", false) |
| 61 | + val sub = new StreamElementsSubscription( |
| 62 | + parseUser(data), |
| 63 | + parseTime(json), |
| 64 | + data.optDouble("amount", 1).toInt, |
| 65 | + { |
| 66 | + // (judging based on the events from the event simulator that can be seen in the browser console) |
| 67 | + // "plan" can be either a string or a number, so we need to handle both cases |
| 68 | + val plan = Option(data.opt("plan")).getOrElse("1000").toString |
| 69 | + if (plan.toLowerCase == "prime") |
| 70 | + SubscriptionTier.PRIME |
| 71 | + else |
| 72 | + SubscriptionTier.parse(plan.toInt / 1000) |
| 73 | + }, |
| 74 | + gifted, |
| 75 | + if (gifted) new User(data.optString("sender")) else null, |
| 76 | + parseProvider(json) |
| 77 | + ) |
| 78 | + call(new StreamElementsSubscriptionEvent(sub)) |
| 79 | + } |
| 80 | + |
| 81 | + private def onDonation(event: DonationEventJSON): Unit = { |
| 82 | + val json = event.json |
| 83 | + val data = json.getJSONObject("data") |
| 84 | + |
| 85 | + val donation = new StreamElementsDonation( |
| 86 | + parseUser(data), |
| 87 | + data.getDouble("amount").toFloat, |
| 88 | + Currency.getInstance(data.getString("currency")), |
| 89 | + parseTime(json), |
| 90 | + data.optString("message") |
| 91 | + ) |
| 92 | + call(new StreamElementsDonationEvent(donation)) |
| 93 | + } |
| 94 | + |
| 95 | + private def onCheer(event: CheerEventJSON): Unit = { |
| 96 | + val json = event.json |
| 97 | + val data = json.getJSONObject("data") |
| 98 | + |
| 99 | + val cheer = new StreamElementsCheer( |
| 100 | + parseUser(data), |
| 101 | + data.getInt("amount"), |
| 102 | + data.optString("message"), |
| 103 | + parseTime(json) |
| 104 | + ) |
| 105 | + call(new StreamElementsCheerEvent(cheer)) |
| 106 | + } |
| 107 | + |
| 108 | + private def onRaid(event: RaidEventJSON): Unit = { |
| 109 | + val json = event.json |
| 110 | + val data = json.getJSONObject("data") |
| 111 | + |
| 112 | + val raid = new StreamElementsRaid( |
| 113 | + parseUser(data), |
| 114 | + data.optString("message"), |
| 115 | + data.getInt("amount"), |
| 116 | + parseTime(json) |
| 117 | + ) |
| 118 | + call(new StreamElementsRaidEvent(raid)) |
| 119 | + } |
| 120 | + |
| 121 | + private def onHost(event: HostEventJSON): Unit = { |
| 122 | + val json = event.json |
| 123 | + val data = json.getJSONObject("data") |
| 124 | + |
| 125 | + val host = new StreamElementsHost( |
| 126 | + parseUser(data), |
| 127 | + data.optString("message"), |
| 128 | + data.getInt("amount"), |
| 129 | + parseTime(json) |
| 130 | + ) |
| 131 | + call(new StreamElementsHostEvent(host)) |
| 132 | + } |
| 133 | + |
| 134 | + override def stop(): Boolean = { |
| 135 | + sourceConnector.get.unregisterAllEventListeners |
| 136 | + true |
| 137 | + } |
| 138 | + |
| 139 | + // Common methods for JSON processing: |
| 140 | + |
| 141 | + private def parseProvider(json: JSONObject): StreamElementsProvider = StreamElementsProvider.parse(json.optString("provider")) |
| 142 | + |
| 143 | + private def parseUser(json: JSONObject): User = { |
| 144 | + val username = json.getString("username") |
| 145 | + val displayName = json.optString("displayName", username) |
| 146 | + new User(username, displayName) |
| 147 | + } |
| 148 | + |
| 149 | + private def parseTime(json: JSONObject): OffsetDateTime = { |
| 150 | + val utcString = json.getString("createdAt") |
| 151 | + LocalDateTime.parse(utcString, DateTimeFormatter.ISO_DATE_TIME).atOffset(ZoneOffset.UTC) |
| 152 | + } |
| 153 | +} |
0 commit comments