Skip to content

Commit

Permalink
problem: can't claim problems to work on them
Browse files Browse the repository at this point in the history
  • Loading branch information
gsovereignty committed Nov 10, 2023
1 parent e0039a4 commit 50effbf
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/lib/stores/event_sources/kinds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const problemKindRecord: Record<number, string> = {
// 15171972: "Problem COMMIT",
// 15171973: "Problem TEXT",
// 31971: "Problem HEAD",
1971: "Problem Event"
1971: "Problem Event",
1972: "Problem Status"
};

export const problemKinds = Object.keys(problemKindRecord).map((k) =>
Expand All @@ -25,7 +26,8 @@ const kinds: Record<number, string> = {
31108: "Rocket Metadata",
15172008: "Consensus Event",
31009: "Identity Tree Replaceable Event",
1971: "Problem Event"
1971: "Problem Event",
1972: "Problem Status"
// 15171971: "Problem ANCHOR",
// 15171972: "Problem COMMIT",
// 15171973: "Problem TEXT",
Expand All @@ -51,6 +53,8 @@ export function kindToDescription(kind: number): string {
return "This is a list of pubkeys to be included in an Identity Tree";
case 1971:
return "This is an event that describes a Problem"
case 1972:
return "This is a problem status update event"
// case 15171971:
// return "This is a Problem ANCHOR event.";
// case 15171972:
Expand Down
1 change: 1 addition & 0 deletions src/lib/stores/nostrocket_state/master_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ function processSoftStateChangeReqeustsFromMempool(currentState: Nostrocket, eli
handled.push(e);
}
}
case 1972:
case 1971:
if (HandleProblemEvent(e, currentState)) {
handled.push(e)
Expand Down
56 changes: 51 additions & 5 deletions src/lib/stores/nostrocket_state/soft_state/simplifiedProblems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,61 @@ import { labelledTag } from "$lib/helpers/shouldBeInNDK";

export function HandleProblemEvent(ev:NDKEvent, state:Nostrocket):boolean {
let success = false
if (ev.getMatchingTags("new").length > 0) {
success = handleNewProblemEvent(ev, state)
}
if (labelledTag(ev, "problem", "e")) {
success = handleProblemModification(ev, state)
switch (ev.kind) {
case 1971:
if (ev.getMatchingTags("new").length > 0) {
success = handleNewProblemEvent(ev, state)
}
if (labelledTag(ev, "problem", "e")) {
success = handleProblemModification(ev, state)
}
break;
case 1972:
let [err, s] = handleProblemStatus(ev, state)
success = s
}

return success
}

function handleProblemStatus(ev:NDKEvent, state:Nostrocket):[string, boolean] {
let success = false
let error = ""
if (!state.Problems) {
state.Problems = new Map<string, Problem>();
}
let problemID = labelledTag(ev, "problem", "e")
let statusTag = ev.getMatchingTags("status")
let newStatus = statusTag[0][1] //todo try/catch or some javascripty way to handle error
if (!statusTag) {error = "could not find a status update tag"}
if (problemID) {
let problem = state.Problems.get(problemID)
if (problem) {
if (!problem) {
error = "problem is missing"
}
if (!state.RocketMap.get(nostrocketIgnitionEvent)?.isParticipant(ev.pubkey)) {
error = "current user is not in the Identity Tree"
}
if (newStatus == "claimed" && problem?.Status != "open") {
error = "cannot claim a problem that isn't open"
}
if (newStatus == "close" && problem?.CreatedBy != ev.pubkey) {
//todo also check if maintainer
error = "you cannot close a problem unless you are the creator of it or a maintainer on its rocket"
}
if (newStatus == "patched" && (problem?.Status !== "claimed" || problem?.ClaimedBy != ev.pubkey)) {
error = "you cannot mark this problem as patched unless you are the one who claimed it"
}
if (error == "") {
problem.Status = newStatus
success = true
}
}
}
return [error, success]
}

function handleNewProblemEvent(ev:NDKEvent, state:Nostrocket):boolean {
let success = false
if (!state.Problems) {
Expand Down
1 change: 0 additions & 1 deletion src/routes/problems/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import type { Account, Problem, ProblemStatus } from "$lib/stores/nostrocket_state/types";
import { Accordion, Column, Row, Search, Select, SelectItem, SelectItemGroup } from "carbon-components-svelte";
import { derived, writable } from "svelte/store";
import LogNewProblemModal from "../../components/problems/LogNewProblemModal.svelte";
import ProblemComponent from "../../components/problems/ProblemComponent.svelte";
let rootNodes: Map<string, Problem>
Expand Down
36 changes: 33 additions & 3 deletions src/routes/problems/[id]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
import type {NDKUserProfile} from "@nostr-dev-kit/ndk";
import {makeHtml} from "$lib/helpers/mundane";
import LogNewProblemModal from "../../../components/problems/LogNewProblemModal.svelte";
import makeEvent from "$lib/helpers/eventMaker";
import { currentUser } from "$lib/stores/hot_resources/current-user";
import { nostrocketIgnitionEvent } from "../../../settings";
let problem: Problem
let problem: Problem | undefined
let createdBy: NDKUserProfile | undefined
let claimedBy: NDKUserProfile | undefined
let claimable = false
$: {
problem = $consensusTipState.Problems.get($page.params.id)
claimable = (problem.Children.size == 0 && problem.Status == "open")
claimable = (problem?.Children.size == 0 && problem.Status == "open")
}
Expand All @@ -38,6 +41,33 @@
})()
}
function updateStatus(newStatus:string):Promise<string> {
return new Promise<string>((resolve, reject)=>{
if (!problem) {
reject("problem is missing")
}
if (!$currentUser) {
reject("user not logged in")
}
if (!$consensusTipState.RocketMap.get(nostrocketIgnitionEvent)?.isParticipant($currentUser!.pubkey)) {
reject("current user is not in the Identity Tree")
}
if (newStatus == "claimed" && problem?.Status != "open") {
reject("cannot claim a problem that isn't open")
}
if (newStatus == "close" && problem?.CreatedBy != $currentUser?.pubkey) {
//todo also check if maintainer
reject("you cannot close a problem unless you are the creator of it or a maintainer on its rocket")
}
if (newStatus == "patched" && (problem?.Status !== "claimed" || problem?.ClaimedBy != $currentUser?.pubkey)) {
reject("you cannot mark this problem as patched unless you are the one who claimed it")
}
let e = makeEvent({kind:1972})
e.tags.push(["e", problem!.UID, "problem"])
e.tags.push(["status", newStatus])
e.publish().then(()=>{console.log(e);resolve("published")}).catch((err)=>{reject(err)})
})
}
</script>


Expand Down Expand Up @@ -122,7 +152,7 @@

<Row>
<Column>
<Button disabled={!claimable} icon={PlayFilledAlt} size="small" kind="primary">Claim this problem and work on it now</Button>
<Button disabled={!claimable} icon={PlayFilledAlt} size="small" kind="primary" on:click={()=>{updateStatus("claimed").then((response)=>{console.log(response)}).catch((response)=>{console.log(response)})}}>Claim this problem and work on it now</Button>
</Column>
</Row>
</Column>
Expand Down

0 comments on commit 50effbf

Please sign in to comment.