Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion buildSrc/src/main/kotlin/common-conventions.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ kotlin {
repositories {
mavenCentral()
maven("https://cache-redirector.jetbrains.com/intellij-dependencies")
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/kotlin-js-wrappers")
maven("https://packages.jetbrains.team/maven/p/edu/maven")
maven("https://packages.jetbrains.team/maven/p/edu/educational-ml-library")
maven("https://packages.jetbrains.team/maven/p/grazi/grazie-platform-public")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ object SocialMediaUtils {

/**
* Hyperskill courses page the achievement points to. UTM tags are part of the agreed link and must be kept as is.
* Used both as the `url` parameter of the X share intent and embedded into the LinkedIn share text.
* Embedded into the share text for both X and LinkedIn.
*/
const val SHARE_URL: String =
"https://hyperskill.org/courses?source=ide_share&utm_source=jetbrains&utm_medium=social&utm_campaign=ide_plugin"
Expand Down Expand Up @@ -53,7 +53,7 @@ object SocialMediaUtils {
return allProjectTasksSolved
}

/** The achievement sentence without any link. Shown as the base of the dialog message and used as the X post text. */
/** The achievement sentence without any link. The base of both the dialog message and the share text. */
private fun achievementCore(solvedTask: Task): String {
val course = solvedTask.course
val projectName = (course as? HyperskillCourse)?.getProjectLesson()?.presentableName ?: course.presentableName
Expand All @@ -65,23 +65,26 @@ object SocialMediaUtils {
"${achievementCore(solvedTask)} ${EduCoreBundle.message("social.media.learn.more.at", COURSES_DISPLAY_LINK)}"

/**
* X share intent. The post text includes the "Learn more at ..." part (the same text shown in the dialog),
* and the tracked [SHARE_URL] is additionally passed via the `url` parameter.
* https://twitter.com/intent/tweet?text=...&url=...
* Text of the post itself: the achievement plus the tracked [SHARE_URL]. It must contain exactly one link,
* so it never includes [COURSES_DISPLAY_LINK]: X turns a bare `hyperskill.org/courses` into a second link.
*/
fun buildXShareUrl(solvedTask: Task): String {
val text = getDisplayMessage(solvedTask)
return "https://twitter.com/intent/tweet?text=${encode(text)}&url=${encode(SHARE_URL)}"
}
fun getShareText(solvedTask: Task): String =
"${achievementCore(solvedTask)} ${EduCoreBundle.message("social.media.learn.more.at", SHARE_URL)}"

/**
* LinkedIn share intent. LinkedIn has no separate `url` parameter, so the tracked [SHARE_URL] is embedded into the text.
* X share intent. The link is part of [getShareText], so the intent's `url` parameter is not passed:
* X appends it to the text, and the post would show the link twice.
* https://twitter.com/intent/tweet?text=...
*/
fun buildXShareUrl(solvedTask: Task): String =
"https://twitter.com/intent/tweet?text=${encode(getShareText(solvedTask))}"

/**
* LinkedIn share intent. LinkedIn has no separate `url` parameter, so the link is part of [getShareText].
* https://www.linkedin.com/feed/?shareActive=true&text=...
*/
fun buildLinkedInShareUrl(solvedTask: Task): String {
val text = "${achievementCore(solvedTask)} ${EduCoreBundle.message("social.media.learn.more.at", SHARE_URL)}"
return "https://www.linkedin.com/feed/?shareActive=true&text=${encode(text)}"
}
fun buildLinkedInShareUrl(solvedTask: Task): String =
"https://www.linkedin.com/feed/?shareActive=true&text=${encode(getShareText(solvedTask))}"

/**
* Loads the full-resolution banner and returns a DPI-aware icon that downscales it to the exact device size
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.hyperskill.academy.socialMedia

import org.hyperskill.academy.learning.EduTestCase
import org.hyperskill.academy.learning.courseFormat.tasks.Task
import org.hyperskill.academy.learning.stepik.hyperskill.hyperskillCourseWithFiles
import org.junit.Test
import java.net.URLDecoder
import java.nio.charset.StandardCharsets

class SocialMediaUtilsTest : EduTestCase() {

@Test
fun `test X share intent passes the link only inside the text`() {
val task = createProjectTask()

val params = queryParams(SocialMediaUtils.buildXShareUrl(task))

// X appends the `url` parameter to the text, so passing it would put a second link into the post
assertEquals(setOf("text"), params.keys)
assertSingleLink(params.getValue("text"))
assertEquals(SocialMediaUtils.getShareText(task), params.getValue("text"))
}

@Test
fun `test LinkedIn share intent passes the link only inside the text`() {
val task = createProjectTask()

val params = queryParams(SocialMediaUtils.buildLinkedInShareUrl(task))

assertEquals(setOf("shareActive", "text"), params.keys)
assertSingleLink(params.getValue("text"))
assertEquals(SocialMediaUtils.getShareText(task), params.getValue("text"))
}

@Test
fun `test dialog message shows the link without tracking parameters`() {
val task = createProjectTask()

val message = SocialMediaUtils.getDisplayMessage(task)

assertEquals(1, message.occurrencesOf(COURSES_LINK))
assertFalse("Tracking parameters must not be shown in the dialog", message.contains("utm_"))
}

private fun assertSingleLink(text: String) {
// A bare `hyperskill.org/courses` is turned into a link by X too, so it must not appear next to the tracked link
assertEquals("The post must contain exactly one link: $text", 1, text.occurrencesOf(COURSES_LINK))
assertTrue("The post must end with the tracked link: $text", text.endsWith(SocialMediaUtils.SHARE_URL))
}

private fun queryParams(url: String): Map<String, String> =
url.substringAfter('?').split('&').associate {
it.substringBefore('=') to URLDecoder.decode(it.substringAfter('='), StandardCharsets.UTF_8)
}

private fun String.occurrencesOf(substring: String): Int = split(substring).size - 1

private fun createProjectTask(): Task {
val course = hyperskillCourseWithFiles {
frameworkLesson("Project") {
eduTask("Task1") {
taskFile("task.txt")
}
}
}
return course.getProjectLesson()?.getTask("Task1") ?: error("No Task1")
}

companion object {
private const val COURSES_LINK = "hyperskill.org/courses"
}
}
Loading