Skip to content

Commit

Permalink
fear: remove archived data when leave the group (#1747)
Browse files Browse the repository at this point in the history
  • Loading branch information
Silver-IT authored Oct 10, 2023
1 parent 79f0387 commit 7071d99
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 7 deletions.
30 changes: 28 additions & 2 deletions contracts/0.2.0/group-slim.js
Original file line number Diff line number Diff line change
Expand Up @@ -8398,7 +8398,7 @@ ${this.getErrorInfo()}`;
process({ data, meta, contractID }, { state, getters }) {
memberLeaves({ username: data.member, dateLeft: meta.createdDate }, { contractID, meta, state, getters });
},
sideEffect({ data, meta, contractID }, { state, getters }) {
async sideEffect({ data, meta, contractID }, { state, getters }) {
const rootState = (0, import_sbp6.default)("state/vuex/state");
const rootGetters = (0, import_sbp6.default)("state/vuex/getters");
const contracts = rootState.contracts || {};
Expand All @@ -8407,6 +8407,8 @@ ${this.getErrorInfo()}`;
if ((0, import_sbp6.default)("okTurtles.data/get", "JOINING_GROUP-" + contractID)) {
return;
}
await (0, import_sbp6.default)("gi.contracts/group/removeArchivedProposals", contractID);
await (0, import_sbp6.default)("gi.contracts/group/removeArchivedPayments", contractID);
const groupIdToSwitch = Object.keys(contracts).find((cID) => contracts[cID].type === "gi.contracts/group" && cID !== contractID && rootState[cID].settings) || null;
(0, import_sbp6.default)("state/vuex/commit", "setCurrentChatRoomId", {});
(0, import_sbp6.default)("state/vuex/commit", "setCurrentGroupId", groupIdToSwitch);
Expand Down Expand Up @@ -8656,7 +8658,14 @@ ${this.getErrorInfo()}`;
const rootState = (0, import_sbp6.default)("state/vuex/state");
if (meta.username === rootState.loggedIn.username && !(0, import_sbp6.default)("okTurtles.data/get", "JOINING_GROUP-" + contractID)) {
const sendingData = data.leavingGroup ? { member: data.member } : { member: data.member, username: meta.username };
await (0, import_sbp6.default)("gi.actions/chatroom/leave", { contractID: data.chatRoomID, data: sendingData });
await (0, import_sbp6.default)("gi.actions/chatroom/leave", {
contractID: data.chatRoomID,
data: sendingData,
...data.leavingGroup && {
signingKeyId: (0, import_sbp6.default)("chelonia/contract/currentKeyIdByName", state, "csk"),
innerSigningContractID: null
}
});
}
}
},
Expand Down Expand Up @@ -8803,6 +8812,23 @@ ${this.getErrorInfo()}`;
await (0, import_sbp6.default)("gi.db/archive/save", archSentOrReceivedPaymentsKey, archSentOrReceivedPayments);
(0, import_sbp6.default)("okTurtles.events/emit", PAYMENTS_ARCHIVED, { paymentsByPeriod, payments });
},
"gi.contracts/group/removeArchivedProposals": async function(contractID) {
const { username } = (0, import_sbp6.default)("state/vuex/state").loggedIn;
const key = `proposals/${username}/${contractID}`;
await (0, import_sbp6.default)("gi.db/archive/delete", key);
},
"gi.contracts/group/removeArchivedPayments": async function(contractID) {
const { username } = (0, import_sbp6.default)("state/vuex/state").loggedIn;
const archPaymentsByPeriodKey = `paymentsByPeriod/${username}/${contractID}`;
const periods = Object.keys(await (0, import_sbp6.default)("gi.db/archive/load", archPaymentsByPeriodKey) || {});
const archSentOrReceivedPaymentsKey = `sentOrReceivedPayments/${username}/${contractID}`;
for (const period of periods) {
const archPaymentsKey = `payments/${username}/${period}/${contractID}`;
await (0, import_sbp6.default)("gi.db/archive/delete", archPaymentsKey);
}
await (0, import_sbp6.default)("gi.db/archive/delete", archPaymentsByPeriodKey);
await (0, import_sbp6.default)("gi.db/archive/delete", archSentOrReceivedPaymentsKey);
},
"gi.contracts/group/sendMincomeChangedNotification": async function(contractID, meta, data) {
const myProfile = (0, import_sbp6.default)("state/vuex/getters").ourGroupProfile;
if (isActionYoungerThanUser(meta, myProfile) && myProfile.incomeDetailsType) {
Expand Down
2 changes: 1 addition & 1 deletion contracts/0.2.0/group.0.2.0.manifest.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"head":{"manifestVersion":"1.0.0"},"body":"{\"version\":\"0.2.0\",\"contract\":{\"hash\":\"21XWnNNeUjMPNg6nMYJGuLti2V4HaWxCY2HbgKPo5hWzvNLcPi\",\"file\":\"group.js\"},\"authors\":[{\"cipher\":\"algo\",\"key\":\"<pubkey from deploy-key.json>\"},{\"cipher\":\"algo\",\"key\":\"<pubkey from alex.json>\"}],\"contractSlim\":{\"file\":\"group-slim.js\",\"hash\":\"21XWnNJaYwsVEVWpT3i87GnukfSfZMGDJGZ67oKvKuBw4y7jTG\"}}","signature":{"key":"<which of the 'authors' keys was used to sign 'body'>","signature":"<signature>"}}
{"head":{"manifestVersion":"1.0.0"},"body":"{\"version\":\"0.2.0\",\"contract\":{\"hash\":\"21XWnNL4g66rNK5wkSH9vZD6H6ZDJXJ84ecvCzPkBoaiCHkgmM\",\"file\":\"group.js\"},\"authors\":[{\"cipher\":\"algo\",\"key\":\"<pubkey from deploy-key.json>\"},{\"cipher\":\"algo\",\"key\":\"<pubkey from alex.json>\"}],\"contractSlim\":{\"file\":\"group-slim.js\",\"hash\":\"21XWnNMhWKeNnBG1upPDBdvrKWuERWGaKdNpCUcyfQ7Bj6QG3y\"}}","signature":{"key":"<which of the 'authors' keys was used to sign 'body'>","signature":"<signature>"}}
30 changes: 28 additions & 2 deletions contracts/0.2.0/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -17519,7 +17519,7 @@ ${this.getErrorInfo()}`;
process({ data, meta, contractID }, { state, getters }) {
memberLeaves({ username: data.member, dateLeft: meta.createdDate }, { contractID, meta, state, getters });
},
sideEffect({ data, meta, contractID }, { state, getters }) {
async sideEffect({ data, meta, contractID }, { state, getters }) {
const rootState = (0, import_sbp7.default)("state/vuex/state");
const rootGetters = (0, import_sbp7.default)("state/vuex/getters");
const contracts = rootState.contracts || {};
Expand All @@ -17528,6 +17528,8 @@ ${this.getErrorInfo()}`;
if ((0, import_sbp7.default)("okTurtles.data/get", "JOINING_GROUP-" + contractID)) {
return;
}
await (0, import_sbp7.default)("gi.contracts/group/removeArchivedProposals", contractID);
await (0, import_sbp7.default)("gi.contracts/group/removeArchivedPayments", contractID);
const groupIdToSwitch = Object.keys(contracts).find((cID) => contracts[cID].type === "gi.contracts/group" && cID !== contractID && rootState[cID].settings) || null;
(0, import_sbp7.default)("state/vuex/commit", "setCurrentChatRoomId", {});
(0, import_sbp7.default)("state/vuex/commit", "setCurrentGroupId", groupIdToSwitch);
Expand Down Expand Up @@ -17777,7 +17779,14 @@ ${this.getErrorInfo()}`;
const rootState = (0, import_sbp7.default)("state/vuex/state");
if (meta.username === rootState.loggedIn.username && !(0, import_sbp7.default)("okTurtles.data/get", "JOINING_GROUP-" + contractID)) {
const sendingData = data.leavingGroup ? { member: data.member } : { member: data.member, username: meta.username };
await (0, import_sbp7.default)("gi.actions/chatroom/leave", { contractID: data.chatRoomID, data: sendingData });
await (0, import_sbp7.default)("gi.actions/chatroom/leave", {
contractID: data.chatRoomID,
data: sendingData,
...data.leavingGroup && {
signingKeyId: (0, import_sbp7.default)("chelonia/contract/currentKeyIdByName", state, "csk"),
innerSigningContractID: null
}
});
}
}
},
Expand Down Expand Up @@ -17924,6 +17933,23 @@ ${this.getErrorInfo()}`;
await (0, import_sbp7.default)("gi.db/archive/save", archSentOrReceivedPaymentsKey, archSentOrReceivedPayments);
(0, import_sbp7.default)("okTurtles.events/emit", PAYMENTS_ARCHIVED, { paymentsByPeriod, payments });
},
"gi.contracts/group/removeArchivedProposals": async function(contractID) {
const { username } = (0, import_sbp7.default)("state/vuex/state").loggedIn;
const key = `proposals/${username}/${contractID}`;
await (0, import_sbp7.default)("gi.db/archive/delete", key);
},
"gi.contracts/group/removeArchivedPayments": async function(contractID) {
const { username } = (0, import_sbp7.default)("state/vuex/state").loggedIn;
const archPaymentsByPeriodKey = `paymentsByPeriod/${username}/${contractID}`;
const periods = Object.keys(await (0, import_sbp7.default)("gi.db/archive/load", archPaymentsByPeriodKey) || {});
const archSentOrReceivedPaymentsKey = `sentOrReceivedPayments/${username}/${contractID}`;
for (const period of periods) {
const archPaymentsKey = `payments/${username}/${period}/${contractID}`;
await (0, import_sbp7.default)("gi.db/archive/delete", archPaymentsKey);
}
await (0, import_sbp7.default)("gi.db/archive/delete", archPaymentsByPeriodKey);
await (0, import_sbp7.default)("gi.db/archive/delete", archSentOrReceivedPaymentsKey);
},
"gi.contracts/group/sendMincomeChangedNotification": async function(contractID, meta, data) {
const myProfile = (0, import_sbp7.default)("state/vuex/getters").ourGroupProfile;
if (isActionYoungerThanUser(meta, myProfile) && myProfile.incomeDetailsType) {
Expand Down
24 changes: 23 additions & 1 deletion frontend/model/contracts/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ sbp('chelonia/defineContract', {
{ contractID, meta, state, getters }
)
},
sideEffect ({ data, meta, contractID }, { state, getters }) {
async sideEffect ({ data, meta, contractID }, { state, getters }) {
const rootState = sbp('state/vuex/state')
const rootGetters = sbp('state/vuex/getters')
const contracts = rootState.contracts || {}
Expand All @@ -912,6 +912,11 @@ sbp('chelonia/defineContract', {
return
}

// NOTE: should remove archived data from IndexedStorage
// regarding the current group (proposals, payments)
await sbp('gi.contracts/group/removeArchivedProposals', contractID)
await sbp('gi.contracts/group/removeArchivedPayments', contractID)

const groupIdToSwitch = Object.keys(contracts)
.find(cID => contracts[cID].type === 'gi.contracts/group' &&
cID !== contractID && rootState[cID].settings) || null
Expand Down Expand Up @@ -1443,6 +1448,23 @@ sbp('chelonia/defineContract', {

sbp('okTurtles.events/emit', PAYMENTS_ARCHIVED, { paymentsByPeriod, payments })
},
'gi.contracts/group/removeArchivedProposals': async function (contractID) {
const { username } = sbp('state/vuex/state').loggedIn
const key = `proposals/${username}/${contractID}`
await sbp('gi.db/archive/delete', key)
},
'gi.contracts/group/removeArchivedPayments': async function (contractID) {
const { username } = sbp('state/vuex/state').loggedIn
const archPaymentsByPeriodKey = `paymentsByPeriod/${username}/${contractID}`
const periods = Object.keys(await sbp('gi.db/archive/load', archPaymentsByPeriodKey) || {})
const archSentOrReceivedPaymentsKey = `sentOrReceivedPayments/${username}/${contractID}`
for (const period of periods) {
const archPaymentsKey = `payments/${username}/${period}/${contractID}`
await sbp('gi.db/archive/delete', archPaymentsKey)
}
await sbp('gi.db/archive/delete', archPaymentsByPeriodKey)
await sbp('gi.db/archive/delete', archSentOrReceivedPaymentsKey)
},
'gi.contracts/group/sendMincomeChangedNotification': async function (contractID, meta, data) {
// NOTE: When group's mincome has changed, below actions should be taken.
// - When mincome has increased, send 'MINCOME_CHANGED' notification to both receiving/pledging members.
Expand Down
2 changes: 1 addition & 1 deletion frontend/model/contracts/manifests.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifests": {
"gi.contracts/chatroom": "21XWnNGb27BabxLKJcHvKPjxLbzETfrcthDshd5GRsrpTXvY54",
"gi.contracts/group": "21XWnNRzearY9CzKmKiquHvXbyPTLKif6AhsJgipUHo1dBNS99",
"gi.contracts/group": "21XWnNNBbhSoianhSffVBT45mEsobrrcH7Pp2JuBVjwcuc3FDt",
"gi.contracts/identity": "21XWnNSxNrVFTMBCUVcZV3CG833gTbJ9V8ZyX7Fxgmpj9rNy4r"
}
}

0 comments on commit 7071d99

Please sign in to comment.