Skip to content
This repository has been archived by the owner on Nov 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #51 from /issues/42
Browse files Browse the repository at this point in the history
Fix #42: First time?
  • Loading branch information
madhead authored Mar 20, 2021
2 parents 52314be + 34ecd81 commit a66dcb2
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ Triggered by a pattern "Not sure if <A>, or just <B>" or by macro ids: "not sure
+
image::./demos/not sure.jpeg[]

https://knowyourmeme.com/memes/james-franco-first-time[**First Time?**]::
Triggered by a macro id: "first time", accepting one argument, bottom text:
+
image::./demos/first time.jpeg[]

== Running locally

. Define the required environment variables in `.env` file in the root of the project.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package me.madhead.imgmacrobot.core.generators

import dev.inmo.tgbotapi.types.InlineQueries.abstracts.InlineQuery
import io.micrometer.core.instrument.MeterRegistry
import me.madhead.imgmacrobot.core.ParagraphsImageMacroGenerator
import me.madhead.imgmacrobot.core.ParsedInlineQuery
import me.madhead.imgmacrobot.core.dao.CachedInlineQueryResultDAO
import me.madhead.imgmacrobot.imgur.Imgur
import org.jetbrains.skija.Canvas
import org.jetbrains.skija.Image
import org.jetbrains.skija.paragraph.FontCollection
import java.nio.file.Path
import kotlin.io.path.ExperimentalPathApi

/**
* Never try your luck twice.
*/
@ExperimentalPathApi
class FirstTime(
templatesDir: Path,
imgur: Imgur,
fontCollection: FontCollection,
cachedInlineQueryResultDAO: CachedInlineQueryResultDAO,
registry: MeterRegistry,
) : ParagraphsImageMacroGenerator<FirstTimeParsedInlineQuery>(
templatesDir,
"first time.png",
imgur,
fontCollection,
cachedInlineQueryResultDAO,
registry,
) {
companion object {
private val macroIdRegex =
"(first time):(\\s*)(?<bottom>.+?)"
.toRegex(RegexOption.IGNORE_CASE)
}

@Suppress("ReturnCount")
override fun parseInlineQuery(inlineQuery: InlineQuery): FirstTimeParsedInlineQuery? {
return macroIdRegex.matchEntire(inlineQuery.query)?.groups?.let { groups ->
FirstTimeParsedInlineQuery(
groups["bottom"]?.value ?: return null,
)
}
}

override fun drawParagraphs(template: Image, canvas: Canvas, parsedInlineQuery: FirstTimeParsedInlineQuery) {
imageMacroParagraph(parsedInlineQuery.bottom.toUpperCase()) {
layout(@Suppress("MagicNumber") 0.9F * template.width.toFloat())
paint(canvas, @Suppress("MagicNumber") 0.05F * template.width, template.height - @Suppress("MagicNumber") 10 - height)
}
}
}

/**
* [ParsedInlineQuery] implementation for [FirstTime].
*
* @property bottom bottom text.
*/
data class FirstTimeParsedInlineQuery(
val bottom: String,
) : ParsedInlineQuery {
override val discriminator: String
get() = bottom
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package me.madhead.imgmacrobot.core.generators

import dev.inmo.tgbotapi.types.ChatId
import dev.inmo.tgbotapi.types.CommonUser
import dev.inmo.tgbotapi.types.InlineQueries.abstracts.InlineQuery
import dev.inmo.tgbotapi.types.InlineQueries.query.BaseInlineQuery
import io.micrometer.core.instrument.simple.SimpleMeterRegistry
import io.mockk.mockk
import me.madhead.imgmacrobot.core.dao.CachedInlineQueryResultDAO
import me.madhead.imgmacrobot.imgur.Imgur
import org.jetbrains.skija.paragraph.FontCollection
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource
import java.nio.file.Path
import kotlin.io.path.ExperimentalPathApi

@ExperimentalPathApi
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
internal class FirstTimeTest {
private val templatesDir = mockk<Path>()
private val imgur = mockk<Imgur>()
private val fontCollection = mockk<FontCollection>()
private val cachedInlineQueryResultDAO = mockk<CachedInlineQueryResultDAO>()
private val registry = SimpleMeterRegistry()
private val sut = FirstTime(templatesDir, imgur, fontCollection, cachedInlineQueryResultDAO, registry)

@ParameterizedTest
@MethodSource("parseInlineQueryParams")
fun parseInlineQuery(inlineQuery: InlineQuery, parsedInlineQuery: FirstTimeParsedInlineQuery?) {
val actual = sut.parseInlineQuery(inlineQuery)

Assertions.assertEquals(parsedInlineQuery, actual)
}

@Suppress("unused")
private fun parseInlineQueryParams(): List<Arguments> {
val user = CommonUser(
id = ChatId(0),
firstName = "user"
)

return listOf(
Arguments.of(
BaseInlineQuery(id = "", from = user, query = "first time: First time?", offset = ""),
FirstTimeParsedInlineQuery("First time?")
),
Arguments.of(
BaseInlineQuery(id = "", from = user, query = "Молодой человек, мы, русские, не обманываем друг друга!", offset = ""),
null
),
)
}
}
3 changes: 3 additions & 0 deletions demos/first time.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.ktor.config.ApplicationConfig
import io.micrometer.prometheus.PrometheusMeterRegistry
import me.madhead.imgmacrobot.core.ImageMacroGenerationPipeline
import me.madhead.imgmacrobot.core.generators.Artemon
import me.madhead.imgmacrobot.core.generators.FirstTime
import me.madhead.imgmacrobot.core.generators.GoodGoodPalpatine
import me.madhead.imgmacrobot.core.generators.GoodGoodPalpatineRu
import me.madhead.imgmacrobot.core.generators.IronicPalpatine
Expand Down Expand Up @@ -153,6 +154,13 @@ val pipelineModule = module {
get(),
get<PrometheusMeterRegistry>()
),
FirstTime(
Path(get<ApplicationConfig>().property("templatesDir").getString()),
get(),
get(),
get(),
get<PrometheusMeterRegistry>()
),
),
get(),
)
Expand Down
3 changes: 3 additions & 0 deletions templates/first time.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a66dcb2

Please sign in to comment.