Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
Revert "wip: code review"
Browse files Browse the repository at this point in the history
This reverts commit 92b2da3.
  • Loading branch information
ricardobrg committed May 17, 2024
1 parent 92b2da3 commit e18eba7
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,21 @@ import org.json.JSONObject

class LicenseRequest {
private val userSignature: ByteArray?
// TODO add parameters
// ptr
// tags
// uses
// description
// origin
// expiry
// terms

init {
val keys = TikiClient.auth.getKey()
val address = TikiClient.auth.address(keys)
userSignature = address?.let { TikiClient.auth.signMessage(it, keys.private) }
}

// TODO create a constructor for License Request

fun toJSON(context: Context, use: Use?, tag: List<Tag>?): JSONObject {
val usecaseJson = JSONArray().apply { use?.usecases?.forEach { put(it) } }
val destinationJson = JSONArray().apply { use?.destinations?.forEach { put(it) } }
val tagsJson = JSONArray().apply { tag?.forEach { put(it.value) } }

val jsonBody =
JSONObject()
.put("ptr", TikiClient.userID) // TODO use PTR as parameter
.put("ptr", TikiClient.userID)
.put("tags", tagsJson)
.put(
"uses",
Expand All @@ -43,10 +34,10 @@ class LicenseRequest {
put("usecases", usecaseJson)
put("destinations", destinationJson)
}))
.put("description", "") // TODO use description as parameter
.put("description", "")
.put("origin", context.packageName)
.put("expiry", JSONObject.NULL) // TODO use expiry as parameter
.put("terms", TikiClient.license.terms(context)) // TODO use terms as parameter
.put("expiry", JSONObject.NULL)
.put("terms", TikiClient.license.terms(context))

val privateKey = TikiClient.auth.getKey().private ?: throw Exception("Private key not found")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class LicenseService {
* @throws Exception if there is an error creating the license. The exception message contains
* details about the error.
*/
suspend fun create(context: Context, ptr: String, use: List<Use>, tags: List<Tag>) =
suspend fun create(context: Context, use: Use?, tags: List<Tag>) =
manageLicense(context, use, tags)

/**
Expand All @@ -37,7 +37,7 @@ class LicenseService {
* @throws Exception if there is an error revoking the license. The exception message contains
* details about the error.
*/
suspend fun revoke(context: Context, prt: String, tags: List<Tag>) = manageLicense(context, null, null)
suspend fun revoke(context: Context) = manageLicense(context, null, null)

/**
* Manages the license for the user. This is a private function used by the create and revoke
Expand Down Expand Up @@ -79,7 +79,7 @@ class LicenseService {
* @return Boolean indicating whether the license is valid.
* @throws Exception if there is an error verifying the license.
*/
suspend fun verify(ptr: String): Boolean {
suspend fun verify(): Boolean {
val response =
ApiService.post(
header =
Expand Down
48 changes: 48 additions & 0 deletions client/src/main/java/com/mytiki/publish/client/offer/Offer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,32 @@ private constructor(
val ptr: String,
val permissions: List<Permission>? = null,
val mutable: Boolean = true,
active: Boolean = false,
) {
var active: Boolean = active
private set

private var isFirstChange = true

var id = UUID.randomUUID().toString()
private set

private constructor(
description: String,
rewards: List<Reward>,
use: Use,
tags: List<Tag>,
ptr: String,
permissions: List<Permission>? = null,
mutable: Boolean = true,
active: Boolean = false,
isFirstChange: Boolean = true,
id: String = UUID.randomUUID().toString()
) : this(description, rewards, use, tags, ptr, permissions, mutable) {
this.active = active
this.isFirstChange = isFirstChange
this.id = id
}

class Builder {
private var description: String = ""
Expand All @@ -26,6 +49,8 @@ private constructor(
private var ptr: String = ""
private var permissions: List<Permission>? = null
private var mutable: Boolean = true
private var active: Boolean = false
private var isFirstChange: Boolean = true
private var id: String = UUID.randomUUID().toString()

fun description(description: String) = apply { this.description = description }
Expand Down Expand Up @@ -94,4 +119,27 @@ private constructor(
.put("ptr", ptr)
}

internal fun activate(): Offer {
if (mutable) {
active = true
} else {
if (isFirstChange) {
active = true
isFirstChange = false
}
}
return this
}

internal fun deactivate(): Offer {
if (mutable) {
active = false
} else {
if (isFirstChange) {
active = false
isFirstChange = false
}
}
return this
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,22 @@ class OfferService {
* @return The deactivated offer.
*/
fun decline(context: Context, offer: Offer): Offer {
MainScope().async { TikiClient.license.create(context, null, listOf(), offer.tags) }
MainScope().async { TikiClient.license.create(context, null, offer.tags) }
return offer.deactivate()
}

/**
* Updates the status of an offer. If the offer is active, it will be declined. If the offer is
* inactive, it will be accepted. However, if the offer is not mutable, the status of the offer
* will not be changed.
*
* @param context The context. This is typically the application context or the current activity.
* @param offer The offer whose status is to be updated. This is an instance of the Offer class.
* @return The updated offer. If the offer was mutable and active, the returned offer will be
* inactive, and vice versa. If the offer was not mutable, the same offer will be returned
* without any changes.
*/
fun updateOfferStatus(context: Context, offer: Offer): Offer {
return if (offer.active) decline(context, offer) else accept(context, offer)
}
}

0 comments on commit e18eba7

Please sign in to comment.