Browse Source

deleteContact when u are tribe admin

feature/dockerfile-arm
Evan Feenstra 5 years ago
parent
commit
22db051e25
  1. 39
      api/controllers/contacts.ts
  2. 5
      api/network/receive.ts
  3. 38
      dist/api/controllers/contacts.js
  4. 2
      dist/api/controllers/contacts.js.map
  5. 5
      dist/api/network/receive.js
  6. 2
      dist/api/network/receive.js.map

39
api/controllers/contacts.ts

@ -110,6 +110,14 @@ const createContact = async (req, res) => {
const owner = await models.Contact.findOne({ where: { isOwner: true }})
const existing = attrs['public_key'] && await models.Contact.findOne({where:{publicKey:attrs['public_key']}})
if(existing) {
const updateObj:{[k:string]:any} = {from_group:false}
if(attrs['alias']) updateObj.alias = attrs['alias']
await existing.update(updateObj)
return success(res, jsonUtils.contactToJson(existing))
}
const createdContact = await models.Contact.create(attrs)
const contact = await createdContact.update(jsonUtils.jsonToContact(attrs))
@ -130,13 +138,30 @@ const deleteContact = async (req, res) => {
}
const contact = await models.Contact.findOne({ where: { id } })
await contact.update({
deleted:true,
publicKey:'',
photoUrl:'',
alias:'Unknown',
contactKey:'',
})
if(!contact) return
const owner = await models.Contact.findOne({ where: { isOwner: true }})
const tribesImAdminOf = await models.Chat.findAll({where:{ownerPubkey:owner.publicKey}})
const tribesIdArray = tribesImAdminOf && tribesImAdminOf.length && tribesImAdminOf.map(t=>t.id)
let okToDelete = true
if(tribesIdArray && tribesIdArray.length) {
const thisContactMembers = await models.ChatMember.findAll({where:{id:{in:tribesIdArray}}})
if(thisContactMembers&&thisContactMembers.length){
// IS A MEMBER! dont delete, instead just set from_group=true
okToDelete=false
await contact.update({fromGroup:true})
}
}
if(okToDelete){
await contact.update({
deleted:true,
publicKey:'',
photoUrl:'',
alias:'Unknown',
contactKey:'',
})
}
// find and destroy chat & messages
const chats = await models.Chat.findAll({where:{deleted:false}})

5
api/network/receive.ts

@ -29,9 +29,12 @@ async function onReceive(payload){
const tribeOwnerPubKey = chat.ownerPubkey
const owner = await models.Contact.findOne({where: {isOwner:true}})
if(owner.publicKey===tribeOwnerPubKey){
// CHECK THEY ARE IN THE GROUP
const senderContact = await models.Contact.findOne({where:{publicKey:payload.sender.pub_key}})
const senderMember = senderContact && await models.ChatMember.findOne({where:{contactId:senderContact.id, chatId:chat.id}})
if(!senderMember) doAction=false
// CHECK PRICES
toAddIn.isTribeOwner = true
if(payload.type===msgtypes.group_join) {
if(payload.message.amount<chat.priceToJoin) doAction=false
}

38
dist/api/controllers/contacts.js

@ -100,6 +100,14 @@ const createContact = (req, res) => __awaiter(void 0, void 0, void 0, function*
console.log('=> createContact called', { body: req.body, params: req.params, query: req.query });
let attrs = extractAttrs(req.body);
const owner = yield models_1.models.Contact.findOne({ where: { isOwner: true } });
const existing = attrs['public_key'] && (yield models_1.models.Contact.findOne({ where: { publicKey: attrs['public_key'] } }));
if (existing) {
const updateObj = { from_group: false };
if (attrs['alias'])
updateObj.alias = attrs['alias'];
yield existing.update(updateObj);
return res_1.success(res, jsonUtils.contactToJson(existing));
}
const createdContact = yield models_1.models.Contact.create(attrs);
const contact = yield createdContact.update(jsonUtils.jsonToContact(attrs));
res_1.success(res, jsonUtils.contactToJson(contact));
@ -117,13 +125,29 @@ const deleteContact = (req, res) => __awaiter(void 0, void 0, void 0, function*
return;
}
const contact = yield models_1.models.Contact.findOne({ where: { id } });
yield contact.update({
deleted: true,
publicKey: '',
photoUrl: '',
alias: 'Unknown',
contactKey: '',
});
if (!contact)
return;
const owner = yield models_1.models.Contact.findOne({ where: { isOwner: true } });
const tribesImAdminOf = yield models_1.models.Chat.findAll({ where: { ownerPubkey: owner.publicKey } });
const tribesIdArray = tribesImAdminOf && tribesImAdminOf.length && tribesImAdminOf.map(t => t.id);
let okToDelete = true;
if (tribesIdArray && tribesIdArray.length) {
const thisContactMembers = yield models_1.models.ChatMember.findAll({ where: { id: { in: tribesIdArray } } });
if (thisContactMembers && thisContactMembers.length) {
// IS A MEMBER! dont delete, instead just set from_group=true
okToDelete = false;
yield contact.update({ fromGroup: true });
}
}
if (okToDelete) {
yield contact.update({
deleted: true,
publicKey: '',
photoUrl: '',
alias: 'Unknown',
contactKey: '',
});
}
// find and destroy chat & messages
const chats = yield models_1.models.Chat.findAll({ where: { deleted: false } });
chats.map((chat) => __awaiter(void 0, void 0, void 0, function* () {

2
dist/api/controllers/contacts.js.map

File diff suppressed because one or more lines are too long

5
dist/api/network/receive.js

@ -39,6 +39,11 @@ function onReceive(payload) {
const tribeOwnerPubKey = chat.ownerPubkey;
const owner = yield models_1.models.Contact.findOne({ where: { isOwner: true } });
if (owner.publicKey === tribeOwnerPubKey) {
// CHECK THEY ARE IN THE GROUP
const senderContact = yield models_1.models.Contact.findOne({ where: { publicKey: payload.sender.pub_key } });
const senderMember = senderContact && (yield models_1.models.ChatMember.findOne({ where: { contactId: senderContact.id, chatId: chat.id } }));
if (!senderMember)
doAction = false;
// CHECK PRICES
toAddIn.isTribeOwner = true;
if (payload.type === msgtypes.group_join) {

2
dist/api/network/receive.js.map

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save