Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Commit

Permalink
fix(ghost): fixed bug when opening ghost thread while not authenticated
Browse files Browse the repository at this point in the history
  • Loading branch information
oed committed Jan 10, 2020
1 parent 41027bd commit 2e9e334
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 156 deletions.
85 changes: 44 additions & 41 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
</head>

<body>
<!-- Open Box -->
<button id="bopen" type="button" class="btn btn-primary" style="margin: 50px 100px;">Open 3box</button>
<!-- Auth Box -->
<button id="bauth" type="button" class="btn btn-primary" style="margin: 50px 100px;" disabled=true>Auth 3box</button>

<div id="controlls" style="display: none; padding: 5px 100px;">

Expand Down Expand Up @@ -97,45 +97,6 @@ <h5 style="padding: 20px 0px 10px 0px" id="spacePriv"> </h5>
<span id="spaceDataPriv"></span>
</p>

<h3> Threads: </h3>
<input type="text" id="threadName" placeholder="Name or Address"></br>
<input type="text" id="threadfirstModerator" placeholder="Thread Root Moderator"></br>
<span > Members Only Thread </span>
<input type="radio" name="members" id='members' value="true"> </br>
<span > Ghost ? </span>
<input type="radio" name="ghostCheck" id='ghostCheck' value="true"> </br>
<button id="joinThread" type="button" class="btn btn btn-primary" >Join thread</button> </br></br>

<div id="threadModeration" style="display: none;">
<input type="text" id="threadMod" placeholder="Thread Moderator">
<button id="addThreadMod" type="button" class="btn btn btn-primary" >Add Thread Moderator</button></br>
<h5> Moderators: </h5>
<p>
<span id="threadModeratorList"></span>
</p>
</div>
<div id="threadMembers" style="display: none;">
<input type="text" id="threadMember" placeholder="Thread Member">
<button id="addThreadMember" type="button" class="btn btn btn-primary" >Add Thread Member</button></br>
<h5> Members: </h5>
<p>
<span id="threadMemberList"></span>
</p>
</div></br>

<p>
<span id="threadACError"></span>
</p>

<div id="posts" style="display: none;">
<h4> Posts: </h4>
<p>
<span id="threadData"></span>
</p>
<input type="text" id="postMsg" placeholder="Type your message">
<button id="postThread" type="button" class="btn btn btn-primary">Post</button>
</div>

</div>
</div>

Expand All @@ -145,6 +106,48 @@ <h4> Posts: </h4>
</div>
</div>

<div style="padding: 0px 0px 25px 100px;">
<h3> Threads: </h3>
<input type="text" id="threadName" placeholder="Name of thread"></br>
<input type="text" id="threadSpaceName" placeholder="Space name"></br>
<input type="text" id="threadfirstModerator" placeholder="Thread Root Moderator"></br>
<span > Members Only Thread </span>
<input type="radio" name="members" id='members' value="true"> </br>
<span > Ghost ? </span>
<input type="radio" name="ghostCheck" id='ghostCheck' value="true"> </br>
<button id="openThread" type="button" class="btn btn btn-primary" disabled=true>Open thread</button> </br></br>

<div id="threadModeration" style="display: none;">
<input type="text" id="threadMod" placeholder="Thread Moderator">
<button id="addThreadMod" type="button" class="btn btn btn-primary" >Add Thread Moderator</button></br>
<h5> Moderators: </h5>
<p>
<span id="threadModeratorList"></span>
</p>
</div>
<div id="threadMembers" style="display: none;">
<input type="text" id="threadMember" placeholder="Thread Member">
<button id="addThreadMember" type="button" class="btn btn btn-primary" >Add Thread Member</button></br>
<h5> Members: </h5>
<p>
<span id="threadMemberList"></span>
</p>
</div></br>

<p>
<span id="threadACError"></span>
</p>

<div id="posts" style="display: none;">
<h4> Posts: </h4>
<p>
<span id="threadData"></span>
</p>
<input type="text" id="postMsg" placeholder="Type your message">
<button id="postThread" type="button" class="btn btn btn-primary">Post</button>
</div>
</div>

<hr />
<div style="padding: 5px 100px;">
<!-- Get Profile Div -->
Expand Down
189 changes: 98 additions & 91 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ const syncComplete = (res) => {
console.log('Sync Complete')
updateProfileData(window.box)
}
bopen.addEventListener('click', event => {

Box.create(window.ethereum).then(box => {
window.box = box
bauth.disabled = false
openThread.disabled = false
})

bauth.addEventListener('click', event => {

window.ethereum.enable().then(addresses => {
window.Box.openBox(addresses[0], window.ethereum, {}).then(box => {
window.box.auth([], { address: addresses[0] }).then(() => {
box.onSyncDone(syncComplete)
window.box = box
console.log(box)
console.log('authed')

controlls.style.display = 'block'
updateProfileData(box)
Expand Down Expand Up @@ -95,104 +101,105 @@ bopen.addEventListener('click', event => {
})
})
}
})

joinThread.addEventListener('click', () => {
const name = threadName.value
const firstModerator = threadfirstModerator.value
const membersBool = members.checked
const ghostBool = ghostCheck.checked
console.log('ghost?', ghostBool);
posts.style.display = 'block'
threadModeration.style.display = 'block'
if (members.checked) threadMembers.style.display = 'block'
if (ghostCheck.checked) {
addThreadMod.disabled = true
}
box.spaces[window.currentSpace].joinThread(name, {firstModerator, members: membersBool, ghost: ghostBool}).then(thread => {
window.currentThread = thread
thread.onUpdate(() => {
updateThreadData()
})
thread.onNewCapabilities(() => {
updateThreadCapabilities()
})
if (window.currentThread._room == undefined) {
updateThreadData()
updateThreadCapabilities()
}
}).catch(updateThreadError)
})
bclose.addEventListener('click', () => {
logout(box)
})
})
})

addThreadMod.addEventListener('click', () => {
const id = threadMod.value
window.currentThread.addModerator(id).then(res => {
updateThreadCapabilities()
}).catch(updateThreadError)
})
openThread.addEventListener('click', () => {
const name = threadName.value
const space = threadSpaceName.value
const firstModerator = threadfirstModerator.value
const membersBool = members.checked
const ghostBool = ghostCheck.checked
console.log('ghost?', ghostBool);
posts.style.display = 'block'
threadModeration.style.display = 'block'
if (members.checked) threadMembers.style.display = 'block'
if (ghostCheck.checked) {
addThreadMod.disabled = true
}
box.openThread(space, name, {firstModerator, members: membersBool, ghost: ghostBool}).then(thread => {
window.currentThread = thread
thread.onUpdate(() => {
updateThreadData()
})
thread.onNewCapabilities(() => {
updateThreadCapabilities()
})
if (window.currentThread._room == undefined) {
updateThreadData()
updateThreadCapabilities()
}
}).catch(updateThreadError)
})

addThreadMember.addEventListener('click', () => {
const id = threadMember.value
window.currentThread.addMember(id).then(res => {
updateThreadCapabilities()
}).catch(updateThreadError)
})
addThreadMod.addEventListener('click', () => {
const id = threadMod.value
window.currentThread.addModerator(id).then(res => {
updateThreadCapabilities()
}).catch(updateThreadError)
})

window.deletePost = (el) => {
window.currentThread.deletePost(el.id).then(res => {
updateThreadData()
}).catch(updateThreadError)
}
addThreadMember.addEventListener('click', () => {
const id = threadMember.value
window.currentThread.addMember(id).then(res => {
updateThreadCapabilities()
}).catch(updateThreadError)
})

const updateThreadError = (e = '') => {
threadACError.innerHTML = e
}
window.deletePost = (el) => {
window.currentThread.deletePost(el.id).then(res => {
updateThreadData()
}).catch(updateThreadError)
}

const updateThreadData = () => {
threadData.innerHTML = ''
updateThreadError()
window.currentThread.getPosts().then(posts => {
posts.map(post => {
threadData.innerHTML += post.author + ': <br />' + post.message + '<br /><br />'
threadData.innerHTML += `<button id="` + post.postId + `"onClick="window.deletePost(` + post.postId + `)" type="button" class="btn btn btn-primary" >Delete</button>` + '<br /><br />'
})
})
}
const updateThreadError = (e = '') => {
threadACError.innerHTML = e
}

const updateThreadCapabilities = () => {
threadMemberList.innerHTML = ''
// if else statement cause ghost thread can't list moderators
if (window.currentThread._peerId) {
window.currentThread.listMembers().then(members => {
members.map(member => {
threadMemberList.innerHTML += member + '<br />'
})
})
} else {
if (window.currentThread._members) {
window.currentThread.listMembers().then(members => {
members.map(member => {
threadMemberList.innerHTML += member + '<br />'
})
})
}
threadModeratorList.innerHTML = ''
window.currentThread.listModerators().then(moderators => {
moderators.map(moderator => {
threadModeratorList.innerHTML += moderator + '<br />'
})
})
}
}
const updateThreadData = () => {
threadData.innerHTML = ''
updateThreadError()
window.currentThread.getPosts().then(posts => {
posts.map(post => {
threadData.innerHTML += post.author + ': <br />' + post.message + '<br /><br />'
threadData.innerHTML += `<button id="` + post.postId + `"onClick="window.deletePost(` + post.postId + `)" type="button" class="btn btn btn-primary" >Delete</button>` + '<br /><br />'
})
})
}

postThread.addEventListener('click', () => {
window.currentThread.post(postMsg.value).catch(updateThreadError)
const updateThreadCapabilities = () => {
threadMemberList.innerHTML = ''
// if else statement cause ghost thread can't list moderators
if (window.currentThread._peerId) {
window.currentThread.listMembers().then(members => {
members.map(member => {
threadMemberList.innerHTML += member + '<br />'
})

bclose.addEventListener('click', () => {
logout(box)
})
} else {
if (window.currentThread._members) {
window.currentThread.listMembers().then(members => {
members.map(member => {
threadMemberList.innerHTML += member + '<br />'
})
})
}
threadModeratorList.innerHTML = ''
window.currentThread.listModerators().then(moderators => {
moderators.map(moderator => {
threadModeratorList.innerHTML += moderator + '<br />'
})
})
})
}
}

postThread.addEventListener('click', () => {
window.currentThread.post(postMsg.value).catch(updateThreadError)
})

getProfile.addEventListener('click', () => {
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "3box",
"version": "1.15.0",
"version": "1.16.0-beta.2",
"description": "Interact with user data",
"main": "lib/3box.js",
"directories": {
Expand Down Expand Up @@ -77,7 +77,7 @@
"babel-core": "7.0.0-bridge.0",
"babel-loader": "^8.0.6",
"express": "^4.17.0",
"identity-wallet": "^1.1.0-beta.1",
"identity-wallet": "^1.1.0",
"jest": "^23.6.0",
"jsdoc-to-markdown": "^5.0.0",
"standard": "^14.3.1",
Expand Down
6 changes: 3 additions & 3 deletions src/3box.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ class Box extends BoxApi {
await this._3id.authenticate(spaces)
}
// make sure we are authenticated to threads
spaces.forEach(space => {
await Promise.all(spaces.map(async space => {
if (this.spaces[space]) {
this.spaces[space]._authThreads(this._3id)
await this.spaces[space]._authThreads(this._3id)
}
})
}))
}

/**
Expand Down
Loading

0 comments on commit 2e9e334

Please sign in to comment.