Skip to content

Commit

Permalink
Permanently hide Try It Now menu item and mfT notifications after o…
Browse files Browse the repository at this point in the history
…perator was used (#264)

* Menu item and notifications hidden, removed MirrordRunCounter

* Lint
  • Loading branch information
Razz4780 authored May 21, 2024
1 parent d53229a commit ede99e3
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 59 deletions.
1 change: 1 addition & 0 deletions changelog.d/256.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`Try It Now` menu item and mirrord for Teams notifications are now hidden permanently after operator use was detected.
56 changes: 34 additions & 22 deletions modules/core/src/main/kotlin/com/metalbear/mirrord/MirrordApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ data class Error(val message: String, val severity: String, val causes: List<Str
data class MirrordExecution(
val environment: MutableMap<String, String>,
@SerializedName("patched_path") val patchedPath: String?,
@SerializedName("env_to_unset") val envToUnset: List<String>?
@SerializedName("env_to_unset") val envToUnset: List<String>?,
@SerializedName("uses_operator") val usesOperator: Boolean?
)

/**
Expand All @@ -118,7 +119,7 @@ private class SafeParser {
}

/**
* How many times mirrord can be run before displaying a notification asking for marketplace review.
* How many times mirrord can be run before asking for marketplace review.
*/
private const val FEEDBACK_COUNTER_REVIEW_AFTER = 100

Expand All @@ -127,6 +128,16 @@ private const val FEEDBACK_COUNTER_REVIEW_AFTER = 100
*/
private const val DISCORD_COUNTER_INVITE_AFTER = 10

/**
* How many times mirrord can be run before inviting the user to mirrord for Teams **for the first time**.
*/
private const val MIRRORD_FOR_TEAMS_INVITE_AFTER = 100

/**
* How many times mirrord can run before inviting the user to mirrord for Teams **again**.
*/
private const val MIRRORD_FOR_TEAMS_INVITE_EVERY = 30

/**
* Interact with mirrord CLI using this API.
*/
Expand Down Expand Up @@ -249,7 +260,6 @@ class MirrordApi(private val service: MirrordProjectService, private val project
val stderr = process.errorStream.reader().buffered()
MirrordLogger.logger.debug(stderr.readText())

val warningHandler = MirrordWarningHandler(project.service<MirrordProjectService>())
return bufferedReader.readText()
}
}
Expand Down Expand Up @@ -277,8 +287,7 @@ class MirrordApi(private val service: MirrordProjectService, private val project
* @return environment for the user's application
*/
fun exec(cli: String, target: String?, configFile: String?, executable: String?, wslDistribution: WSLDistribution?): MirrordExecution {
bumpFeedbackCounter()
checkDiscordCounter()
bumpRunCounter()

val task = MirrordExtTask(cli, projectEnvVars).apply {
this.target = target
Expand All @@ -290,36 +299,39 @@ class MirrordApi(private val service: MirrordProjectService, private val project
val result = task.run(service.project)
service.notifier.notifySimple("mirrord starting...", NotificationType.INFORMATION)

result.usesOperator?.let { usesOperator ->
if (usesOperator) {
MirrordSettingsState.instance.mirrordState.operatorUsed = true
}
}

return result
}

/**
* Increments the mirrord run counter. Occasionally displays a notification asking for marketplace review.
* Increments the mirrord run counter.
* Can display some notifications (asking for feedback, discord invite, mirrord for Teams invite).
*/
private fun bumpFeedbackCounter() {
private fun bumpRunCounter() {
val previousRuns = MirrordSettingsState.instance.mirrordState.runsCounter
val currentRuns = previousRuns + 1

MirrordSettingsState.instance.mirrordState.runsCounter = currentRuns

if ((currentRuns % FEEDBACK_COUNTER_REVIEW_AFTER) != 0) {
return
}
val operatorUsed = MirrordSettingsState.instance.mirrordState.operatorUsed

service.notifier.notification("Enjoying mirrord? Don't forget to leave a review or star us on GitHub!", NotificationType.INFORMATION).withLink("Review", "https://plugins.jetbrains.com/plugin/19772-mirrord/reviews").withLink("Star us on GitHub", GITHUB_URL).withDontShowAgain(MirrordSettingsState.NotificationId.PLUGIN_REVIEW).fire()
}

/**
* Invite user to MetalBear Discord server after a few usages.
*/
private fun checkDiscordCounter() {
val currentRuns = MirrordSettingsState.instance.mirrordState.runsCounter
if ((currentRuns % FEEDBACK_COUNTER_REVIEW_AFTER) == 0) {
service.notifier.notification("Enjoying mirrord? Don't forget to leave a review or star us on GitHub!", NotificationType.INFORMATION).withLink("Review", "https://plugins.jetbrains.com/plugin/19772-mirrord/reviews").withLink("Star us on GitHub", GITHUB_URL).withDontShowAgain(MirrordSettingsState.NotificationId.PLUGIN_REVIEW).fire()
}

if ((currentRuns - DISCORD_COUNTER_INVITE_AFTER) != 0) {
return
if (currentRuns == DISCORD_COUNTER_INVITE_AFTER) {
service.notifier.notification("Need any help with mirrord? Come chat with our team on Discord!", NotificationType.INFORMATION).withLink("Join us", "https://discord.gg/metalbear").withDontShowAgain(MirrordSettingsState.NotificationId.DISCORD_INVITE).fire()
}

service.notifier.notification("Need any help with mirrord? Come chat with our team on Discord!", NotificationType.INFORMATION).withLink("Join us", "https://discord.gg/metalbear").withDontShowAgain(MirrordSettingsState.NotificationId.DISCORD_INVITE).fire()
if (previousRuns >= MIRRORD_FOR_TEAMS_INVITE_AFTER && !operatorUsed) {
if ((previousRuns - MIRRORD_FOR_TEAMS_INVITE_AFTER) % MIRRORD_FOR_TEAMS_INVITE_EVERY == 0) {
service.notifier.notification("For more features of mirrord, including multi-pod impersonation, check out mirrord for Teams!", NotificationType.INFORMATION).withLink("Try it now", MIRRORD_FOR_TEAMS_URL).withDontShowAgain(MirrordSettingsState.NotificationId.MIRRORD_FOR_TEAMS).fire()
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,12 @@ class MirrordDropDown : ComboBoxAction(), DumbAware {
service.activeConfig?.let { add(ShowActiveConfigAction(it, project)) }
add(SelectActiveConfigAction())
add(SettingsAction())
addSeparator("mirrord for Teams")
add(NavigateToMirrodForTeamsIntroAction())

if (!MirrordSettingsState.instance.mirrordState.operatorUsed) {
addSeparator("mirrord for Teams")
add(NavigateToMirrodForTeamsIntroAction())
}

addSeparator("Help")
add(DiscordAction())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,6 @@ class MirrordExecManager(private val service: MirrordProjectService) {
null
}

service.runCounter.bump()

val executionInfo = mirrordApi.exec(
cli,
target,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ class MirrordProjectService(val project: Project) : Disposable {

val versionCheck: MirrordVersionCheck = MirrordVersionCheck(this)

val runCounter: MirrordRunCounter = MirrordRunCounter(this)

fun mirrordApi(environment: Map<String, String>?): MirrordApi {
return MirrordApi(this, environment)
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ open class MirrordSettingsState : PersistentStateComponent<MirrordSettingsState.
var disabledNotifications: Set<NotificationId>? = null
var showUsageBanner: Boolean = true
var runsCounter: Int = 0
var operatorUsed: Boolean = false

fun disableNotification(id: NotificationId) {
disabledNotifications = disabledNotifications.orEmpty() + id
Expand Down

0 comments on commit ede99e3

Please sign in to comment.