Skip to content
Merged
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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ The server loads `.env` automatically. Real environment variables override `.env
| HISTORY_MIGRATION_WORKERS | 4 | Concurrent background workers used to backfill missing project histories (clamped to 1-8) |
| APP_URL | https://api.mistwarp.org | Public URL of this API |
| ROTUR_APP_KEY | mistwarp | The old validator key, for editors that predate validators keyed to the Rotur App |
| COMMERCE_SERVICE_KEY | | Key registered for `mistwarp` in Rotur's `COMMERCE_SERVICE_KEYS` |
| COMMERCE_SERVICE_KEY | | Key registered for `mistwarp` in Rotur's `COMMERCE_SERVICE_KEYS`. Only bounty awards and the groups integration still use it |
| ROTUR_CLIENT_ID | | The MistWarp Rotur App's client ID (`app_1938b6a87799f862`). With the secret, mistwarp-api calls the apps API as MistWarp |
| ROTUR_CLIENT_SECRET | | One of the MistWarp Rotur App's secrets, made with **New secret** on rotur.dev/me/developer |
| MISTWARP_ROTUR_USER | | Rotur account that gets MistWarp's share of sales. Without it, the MistWarp Rotur App's owner gets it |
| ROTUR_WEBHOOK_SECRET | | Signing secret (`whsec_…`) of the MistWarp Rotur App's webhook. Without it `POST /v1/rotur/webhook` answers 503 |
| R2_ENDPOINT | | https://accountid.r2.cloudflarestorage.com |
| R2_BUCKET | mistwarp | R2 bucket name |
Expand Down Expand Up @@ -52,12 +53,23 @@ The MistWarp Rotur App declares that people can talk and can spend credits. Befo

Rotur only answers about people who have used MistWarp through Sign in with Rotur, or made their account on MistWarp. For anyone else, or if Rotur can't be reached, MistWarp carries on as it did before. Rotur counts credits towards a parent's monthly limit when it moves them, so the purchase signal only asks.

## Payments

Buying a project or a game product, donating with a comment and refunding a game product are Rotur payment requests (`POST /v2/apps/<app>/payment-requests`). mistwarp-api never holds a permission to spend anyone's credits.

1. The editor asks for an intent. mistwarp-api asks Rotur for the payment, with a purchase key (`mwbuy_`, `mwgame_`, `mwdonate_` or `mwrefund_`) as its `reference`, and answers with the key and Rotur's `approveUrl`.
2. The person approves it on rotur.dev with their password. Rotur moves the credits, shared between everyone the sale pays: the creator, collaborators, the remixed project's creator and MistWarp's fee.
3. Rotur's `payment.completed` webhook marks the key paid and delivers projects and game products. The editor's confirm call does the same if it gets there first, asking Rotur whether the request was paid. Whichever comes second finds it done.

Donations are delivered when their comment is posted, and refunds when the product is revoked. Paid keys waiting for that are kept for 30 days, and unpaid ones for an hour (Rotur's requests expire after 15 minutes).

## Rotur webhooks

Set the MistWarp Rotur App's webhook to `https://api.mistwarp.org/v1/rotur/webhook` on rotur.dev/me/developer, with privacy requests on, and put its signing secret in `ROTUR_WEBHOOK_SECRET`.

- `user.deleted`, `user.left` and `user.banned` erase everything MistWarp holds about that person, the same as deleting their data from Settings.
- A `privacy.request` for `erasure` does the same. One for `access` sends them a MistWarp notification pointing to the download in Settings.
- `payment.completed` finishes the purchase it names (see Payments).

Deliveries are checked against `Rotur-Signature` and refused if their timestamp is more than five minutes out. Each is saved to `data/rotur-webhooks.json`, answered, then handled in the background, and a retried delivery is only handled once. Anything not yet handled when the server stops is handled when it starts.

Expand Down
8 changes: 0 additions & 8 deletions admin.osl
Original file line number Diff line number Diff line change
Expand Up @@ -674,12 +674,6 @@ def handleAdminStats(*serve.Context c) (
object bans = banData.bans
number bannedUsers = bans.getKeys().len
array news = loadNews()
object payoutStore = loadPendingPayouts()
array pendingPayouts = payoutStore.payouts.assert(array)
number pendingPayoutAmount = 0
for i pendingPayouts.len (
pendingPayoutAmount += pendingPayouts[i].amount.toNum()
)
c.json(200, {
ok: true,
totalProjects: totalProjects,
Expand All @@ -694,8 +688,6 @@ def handleAdminStats(*serve.Context c) (
openErrors: openErrors,
bannedUsers: bannedUsers,
newsPosts: news.len,
pendingPayouts: pendingPayouts.len,
pendingPayoutAmount: pendingPayoutAmount,
days: days,
averageProjectBytes: if totalProjects > 0 then totalBytes / totalProjects else 0,
averageViews: if totalProjects > 0 then totalViews / totalProjects else 0,
Expand Down
57 changes: 17 additions & 40 deletions comments.osl
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,7 @@ def handleGetProjectComments(*serve.Context c) (
)

def handleProjectCommentDonationIntent(*serve.Context c) (
if commerceServiceKey == "" (
c.json(503, {ok: false, error: "donations are not available right now"})
if refuseOldPaymentClient(c) (
return
)
auto result = loadProject(c.param("id"))
Expand Down Expand Up @@ -443,22 +442,14 @@ def handleProjectCommentDonationIntent(*serve.Context c) (
return
)

string key = "mwdonate_" ++ randomString(16)
object store = prunePurchaseKeys(loadPurchaseKeys())
object keys = store.keys
keys[key] = {kind: "comment_donation", projectId: project.id.toStr(), buyer: donor, amount: amount, at: timestamp}
store.keys = keys
savePurchaseKeys(store)
c.json(200, {
ok: true,
key: key,
amount: amount,
projectId: project.id,
title: project.title,
recipient: project.owner,
splits: [{username: project.owner, basis_points: 10000}]
})
)
object started = startPurchase("mwdonate_", {kind: "comment_donation", projectId: project.id.toStr(), buyer: donor, amount: amount}, donor, project.owner.toStr(), [], amount, "Donation to " ++ project.title.toStr(), (body.returnUrl ?? "").toStr())
if !started.ok (
c.json(503, started)
return
)
started.amount = amount
started.recipient = project.owner
c.json(200, started))

def handleCreateProjectComment(*serve.Context c) (
auto result = loadProject(c.param("id"))
Expand Down Expand Up @@ -513,13 +504,10 @@ def handleCreateProjectComment(*serve.Context c) (
return
)
object donation = body.donation.assertElse(object, {})
if donation.contains("key") and donation.key != null (
donationKey = donation.key.toStr().strip()
)
if donation.contains("paymentId") and donation.paymentId != null (
donationPaymentId = donation.paymentId.toStr().strip()
)
if donationKey == "" or donationPaymentId == "" (
donationKey = (donation.key ?? "").toStr().strip()
object pendingDonation = paidPurchase(donationKey)
donationPaymentId = (pendingDonation.requestId ?? "").toStr()
if !donationKey.startsWith("mwdonate_") (
c.badRequest("missing donation confirmation")
return
)
Expand All @@ -533,22 +521,15 @@ def handleCreateProjectComment(*serve.Context c) (
)
)

object donationStore = loadPurchaseKeys()
object donationKeys = donationStore.keys
if !donationKeys.contains(donationKey) (
c.badRequest("this donation expired, start again")
if donationPaymentId == "" (
c.json(402, {ok: false, pending: true, error: "donation not received yet, try again in a moment"})
return
)
object pendingDonation = donationKeys[donationKey].assert(object)
if !pendingDonation.contains("kind") or pendingDonation.kind.toStr() != "comment_donation" or pendingDonation.projectId.toStr() != project.id.toStr() or normalizeUsername(pendingDonation.buyer.toStr()) != normalizeUsername(c.getString("username")) (
if pendingDonation.projectId.toStr() != project.id.toStr() or normalizeUsername(pendingDonation.buyer.toStr()) != normalizeUsername(c.getString("username")) (
c.badRequest("this donation key does not match")
return
)
donationAmount = pendingDonation.amount.toNum()
if !verifyCommerceCommentDonation(donationPaymentId, c.getString("username"), project, donationAmount) (
c.json(402, {ok: false, pending: true, error: "donation not received yet, try again in a moment"})
return
)
)

object created = createComment("project-" ++ project.id.toStr(), c.getString("username"), c.getString("userId"), body.content.toStr(), parentId, kind, donationAmount, donationPaymentId)
Expand All @@ -557,11 +538,7 @@ def handleCreateProjectComment(*serve.Context c) (
return
)
if donationKey != "" (
object donationStore = loadPurchaseKeys()
object donationKeys = donationStore.keys
donationKeys.delete(donationKey)
donationStore.keys = donationKeys
savePurchaseKeys(donationStore)
forgetPurchase(donationKey)
)
if created.contains("duplicate") and created.duplicate == true (
created.comment = commentResponse(created.comment.assert(object), c.getString("username"))
Expand Down
Loading