Skip to content

Commit

Permalink
Ability to define rooms per group
Browse files Browse the repository at this point in the history
  • Loading branch information
arj03 committed Nov 25, 2021
1 parent dbf3f57 commit 09240ac
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
10 changes: 6 additions & 4 deletions chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = function (ssbSingleton, group, crut, getChatFeedHelper,
data: function() {
return {
title: '',
rooms: [],
message: '',
messages: [],
componentStillLoaded: false,
Expand Down Expand Up @@ -67,7 +68,7 @@ module.exports = function (ssbSingleton, group, crut, getChatFeedHelper,
},

editGroup: function() {
editGroupHelper(group, this.title)
editGroupHelper(group, this.title, this.rooms)
},

load: function() {
Expand All @@ -82,9 +83,10 @@ module.exports = function (ssbSingleton, group, crut, getChatFeedHelper,

groupConfigChanges(SSB, crut, group.id, (err, record) => {
if (!err) {
const title = record.states[0].title
this.title = title
document.title = 'Groupies chat - ' + title
const state = record.states[0]
this.title = state.title
document.title = 'Groupies chat - ' + state.title
this.rooms = state.rooms
}
})

Expand Down
10 changes: 10 additions & 0 deletions dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@
<div class="modal-body">
Title: <input id="title" type="text" v-model="groupTitle"><br>
<button class="clickButton" v-on:click="copyGroupKey()" title="Copy secret group key to clipboard">Copy secret group key to clipboard</Button>
<div>
<h2>Room servers</h2>
<form v-on:submit.prevent="addRoomToConfig">
Room address: <input id="room" type="text" style="width: 350px; margin-right: 10px;" v-model="newRoomAddress"><button>Add room</Button>
</form>
<div v-for="(room, index) in rooms" style="padding-top: 10px;">
{{ room }}
<button v-on:click="rooms.splice(index, 1)">Remove</button>
</div>
</div>
</div>

<div class="modal-footer">
Expand Down
55 changes: 48 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const crypto = require('crypto')

const Crut = require('ssb-crut')
const Overwrite = require('@tangle/overwrite')
const SimpleSet = require('@tangle/simple-set')

const { getGroupKeysFeed, getChatFeed, groupConfigChanges,
replicateSubfeeds, dumpDB } = require('./helpers')
Expand Down Expand Up @@ -49,7 +50,8 @@ let config = {
const spec = {
type: 'groupconfig',
props: {
title: Overwrite()
title: Overwrite(),
rooms: SimpleSet()
}
}
let crut
Expand Down Expand Up @@ -90,7 +92,9 @@ const menu = new Vue({
groupKey: '',
showGroupEdit: false,
groupSaveText: 'Create group',
groupTitle: ''
groupTitle: '',
rooms: [],
newRoomAddress: ''
}
},

Expand All @@ -102,12 +106,17 @@ const menu = new Vue({
groupConfigChanges, this.editGroupConfig)
new Vue(app).$mount("#app")
},
editGroupConfig: function(group, title) {
addRoomToConfig: function() {
this.rooms.push(this.newRoomAddress)
this.newRoomAddress = ''
},
editGroupConfig: function(group, title, rooms) {
afterGroupSave = () => { this.showGroupEdit = false }

this.groupKey = group.key
this.groupSaveText = 'Save group config'
this.groupTitle = title
this.rooms = rooms
this.showGroupEdit = true
},
copyGroupKey: function() {
Expand Down Expand Up @@ -143,11 +152,35 @@ const menu = new Vue({
const content = { title: this.groupTitle }

if (msgs.length > 0) {
crut.update(msgs[0].key, content, chatFeed.keys, (err) => {
crut.read(msgs[0].key, (err, record) => {
if (err) return console.error(err)
else afterGroupSave()

const currentRooms = new Set(record.states[0].rooms)
const newRooms = new Set(this.rooms)

const added = []
const removed = []

for (let room of currentRooms)
if (!newRooms.has(room))
removed.push(room)

for (let room of newRooms)
if (!currentRooms.has(room))
added.push(room)

content.rooms = {
add: added,
remove: removed
}

crut.update(msgs[0].key, content, chatFeed.keys, (err) => {
if (err) return console.error(err)
else afterGroupSave()
})
})
} else {
content.rooms = this.rooms
content.recps = [group.id]
crut.create(content, chatFeed.keys, (err) => {
if (err) return console.error(err)
Expand Down Expand Up @@ -277,8 +310,16 @@ function setupApp(SSB) {
menu.groups.push(group)

groupConfigChanges(SSB, crut, id, (err, record) => {
if (!err)
group.title = record.states[0].title
if (!err) {
const state = record.states[0]
group.title = state.title
if (state.rooms.length > 0) {
state.rooms.forEach(room => {
console.log("connecting to", room)
SSB.conn.connect(room, () => {})
})
}
}
})
})
)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"author": "Anders Rune Jensen <[email protected]>",
"license": "AGPL-3.0",
"dependencies": {
"@tangle/simple-set": "^2.1.0",
"ssb-browser-core": "^13.0.0",
"ssb-crut": "git+https://gitlab.com/ahau/lib/ssb-crut/#metafeeds",
"ssb-db2-box2": "^0.5.0",
Expand Down

0 comments on commit 09240ac

Please sign in to comment.