Skip to content

Commit

Permalink
Merge pull request #63 from stakwork/private-tribes
Browse files Browse the repository at this point in the history
Private tribes
  • Loading branch information
Evanfeenstra authored Jul 29, 2020
2 parents 43072f2 + d8c9d5a commit 2d68f9e
Show file tree
Hide file tree
Showing 23 changed files with 141 additions and 114 deletions.
41 changes: 21 additions & 20 deletions api/controllers/chatTribes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ export async function joinTribe(req, res){
export async function editTribe(req, res) {
const {
name,
is_listed,
price_per_message,
price_to_join,
escrow_amount,
escrow_millis,
img,
description,
tags,
unlisted,
} = req.body
const { id } = req.params

Expand All @@ -117,24 +117,23 @@ export async function editTribe(req, res) {
const owner = await models.Contact.findOne({ where: { isOwner: true } })

let okToUpdate = true
if(is_listed) {
try{
await tribes.edit({
uuid: chat.uuid,
name: name,
host: chat.host,
price_per_message: price_per_message||0,
price_to_join: price_to_join||0,
escrow_amount: escrow_amount||0,
escrow_millis: escrow_millis||0,
description,
tags,
img,
owner_alias: owner.alias,
})
} catch(e) {
okToUpdate = false
}
try{
await tribes.edit({
uuid: chat.uuid,
name: name,
host: chat.host,
price_per_message: price_per_message||0,
price_to_join: price_to_join||0,
escrow_amount: escrow_amount||0,
escrow_millis: escrow_millis||0,
description,
tags,
img,
owner_alias: owner.alias,
unlisted,
})
} catch(e) {
okToUpdate = false
}

if(okToUpdate) {
Expand All @@ -145,6 +144,7 @@ export async function editTribe(req, res) {
priceToJoin: price_to_join||0,
escrowAmount: escrow_amount||0,
escrowMillis: escrow_millis||0,
unlisted: unlisted||false,
})
success(res, jsonUtils.chatToJson(chat))
} else {
Expand Down Expand Up @@ -206,7 +206,7 @@ export async function replayChatHistory(chat, contact) {
})
}

export async function createTribeChatParams(owner, contactIds, name, img, price_per_message, price_to_join, escrow_amount, escrow_millis): Promise<{[k:string]:any}> {
export async function createTribeChatParams(owner, contactIds, name, img, price_per_message, price_to_join, escrow_amount, escrow_millis, unlisted): Promise<{[k:string]:any}> {
let date = new Date()
date.setMilliseconds(0)
if (!(owner && contactIds && Array.isArray(contactIds))) {
Expand All @@ -233,6 +233,7 @@ export async function createTribeChatParams(owner, contactIds, name, img, price_
priceToJoin: price_to_join||0,
escrowMillis: escrow_millis||0,
escrowAmount: escrow_amount||0,
unlisted: unlisted||false,
}
}

Expand Down
7 changes: 4 additions & 3 deletions api/controllers/chats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@ export async function createGroupChat(req, res) {
const {
name,
is_tribe,
is_listed,
price_per_message,
price_to_join,
escrow_amount,
escrow_millis,
img,
description,
tags,
unlisted,
} = req.body
const contact_ids = req.body.contact_ids||[]

Expand All @@ -165,8 +165,8 @@ export async function createGroupChat(req, res) {
let chatParams:any = null
let okToCreate = true
if(is_tribe){
chatParams = await createTribeChatParams(owner, contact_ids, name, img, price_per_message, price_to_join, escrow_amount, escrow_millis)
if(is_listed && chatParams.uuid){
chatParams = await createTribeChatParams(owner, contact_ids, name, img, price_per_message, price_to_join, escrow_amount, escrow_millis, unlisted)
if(chatParams.uuid){
// publish to tribe server
try {
await tribes.declare({
Expand All @@ -181,6 +181,7 @@ export async function createGroupChat(req, res) {
description, tags, img,
owner_pubkey: owner.publicKey,
owner_alias: owner.alias,
unlisted: unlisted||false,
})
} catch(e) {
okToCreate = false
Expand Down
2 changes: 1 addition & 1 deletion api/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const sendHubCall = (params) => {
headers: { 'Content-Type': 'application/json' }
})
.catch(error => {
console.log('[hub error]', error)
console.log('[hub warning]: cannot reach hub',)
})
}

Expand Down
10 changes: 10 additions & 0 deletions api/models/ts/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ export default class Chat extends Model<Chat> {
@Column(DataType.BIGINT)
escrowMillis: number

@Column({ // dont show on tribes list
type: DataType.BOOLEAN,
defaultValue: false,
// allowNull: false
})
unlisted: boolean

@Column
private: boolean // joining requires approval of admin

@Column
ownerPubkey: string

Expand Down
3 changes: 3 additions & 0 deletions api/models/ts/chatMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ export default class ChatMember extends Model<ChatMember> {
@Column
lastActive: Date

@Column
approved: boolean

}
47 changes: 20 additions & 27 deletions api/utils/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as publicIp from 'public-ip'
import password from '../utils/password'
import {checkTag, checkCommitHash} from '../utils/gitinfo'

const USER_VERSION = 5
const USER_VERSION = 6

const setupDatabase = async () => {
console.log('=> [db] starting setup...')
Expand All @@ -31,40 +31,33 @@ async function setVersion(){
}

async function migrate(){
addTableColumn('sphinx_chats', 'private', 'BOOLEAN')
addTableColumn('sphinx_chats', 'unlisted', 'BOOLEAN')
addTableColumn('sphinx_chat_members', 'approved', 'BOOLEAN')

addTableColumn('sphinx_chats', 'seen', 'BOOLEAN')

try{
await sequelize.query(`CREATE INDEX idx_messages_sender ON sphinx_messages (sender);`)
}catch(e){
console.log(e)
}
}catch(e){}

addTableColumn('sphinx_contacts', 'notification_sound')

try{
await sequelize.query(`
CREATE TABLE sphinx_timers (
id BIGINT,
chat_id BIGINT,
receiver BIGINT,
millis BIGINT,
msg_id BIGINT,
amount DECIMAL
)`)
} catch(e){}
addTableColumn('sphinx_chats', 'escrow_amount', 'BIGINT')
addTableColumn('sphinx_chats', 'escrow_millis', 'BIGINT')
// try{
// await sequelize.query(`
// CREATE TABLE sphinx_timers (
// id BIGINT,
// chat_id BIGINT,
// receiver BIGINT,
// millis BIGINT,
// msg_id BIGINT,
// amount DECIMAL
// )`)
// } catch(e){}
// addTableColumn('sphinx_chats', 'escrow_amount', 'BIGINT')
// addTableColumn('sphinx_chats', 'escrow_millis', 'BIGINT')

addTableColumn('sphinx_contacts', 'private_photo', 'BOOLEAN')

// addTableColumn('sphinx_media_keys', 'media_type')
// addTableColumn('sphinx_media_keys', 'original_muid')
// addTableColumn('sphinx_messages', 'original_muid')

// addTableColumn('sphinx_messages', 'uuid')
// addTableColumn('sphinx_messages', 'reply_uuid')

// addTableColumn('sphinx_media_keys', 'sender', 'BIGINT')
// addTableColumn('sphinx_contacts', 'private_photo', 'BOOLEAN')
}

async function addTableColumn(table:string, column:string, type='TEXT') {
Expand Down
6 changes: 4 additions & 2 deletions api/utils/tribes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export function publish(topic, msg, cb) {
})
}

export async function declare({ uuid, name, description, tags, img, group_key, host, price_per_message, price_to_join, owner_alias, owner_pubkey, escrow_amount, escrow_millis }) {
export async function declare({ uuid, name, description, tags, img, group_key, host, price_per_message, price_to_join, owner_alias, owner_pubkey, escrow_amount, escrow_millis, unlisted }) {
try {
await fetch('https://' + host + '/tribes', {
method: 'POST',
Expand All @@ -84,6 +84,7 @@ export async function declare({ uuid, name, description, tags, img, group_key, h
owner_alias, owner_pubkey,
escrow_amount: escrow_amount || 0,
escrow_millis: escrow_millis || 0,
unlisted: unlisted||false,
}),
headers: { 'Content-Type': 'application/json' }
})
Expand All @@ -94,7 +95,7 @@ export async function declare({ uuid, name, description, tags, img, group_key, h
}
}

export async function edit({ uuid, host, name, description, tags, img, price_per_message, price_to_join, owner_alias, escrow_amount, escrow_millis }) {
export async function edit({ uuid, host, name, description, tags, img, price_per_message, price_to_join, owner_alias, escrow_amount, escrow_millis, unlisted }) {
try {
const token = await genSignedTimestamp()
await fetch('https://' + host + '/tribe?token=' + token, {
Expand All @@ -107,6 +108,7 @@ export async function edit({ uuid, host, name, description, tags, img, price_per
escrow_amount: escrow_amount || 0,
escrow_millis: escrow_millis || 0,
owner_alias,
unlisted: unlisted||false,
}),
headers: { 'Content-Type': 'application/json' }
})
Expand Down
4 changes: 3 additions & 1 deletion config/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
"group_leave": 15,
"group_kick": 16,
"delete": 17,
"repayment": 18
"repayment": 18,
"member_approve": 19,
"member_reject": 20
},
"payment_errors": {
"timeout": "Timed Out",
Expand Down
43 changes: 22 additions & 21 deletions dist/api/controllers/chatTribes.js

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

Loading

0 comments on commit 2d68f9e

Please sign in to comment.