mirror of
https://github.com/filecoin-project/slate.git
synced 2024-11-22 21:45:56 +03:00
updated user count calculation functions
This commit is contained in:
parent
c7f18df245
commit
ab80643e4b
@ -7,6 +7,8 @@ import getUserByUsername from "~/node_common/data/methods/get-user-by-username";
|
||||
import getUserById from "~/node_common/data/methods/get-user-by-id";
|
||||
import getUserByEmail from "~/node_common/data/methods/get-user-by-email";
|
||||
import getUserByTwitterId from "~/node_common/data/methods/get-user-by-twitter-id";
|
||||
import recalcUserSlatecount from "~/node_common/data/methods/recalc-user-slatecount";
|
||||
import recalcUserFollowercount from "~/node_common/data/methods/recalc-user-followercount";
|
||||
|
||||
// NOTE(amine)
|
||||
// TwitterTokens postgres queries
|
||||
@ -45,7 +47,7 @@ import updateSlateById from "~/node_common/data/methods/update-slate-by-id";
|
||||
import updateSlatePrivacy from "~/node_common/data/methods/update-slate-privacy";
|
||||
import deleteSlatesByUserId from "~/node_common/data/methods/delete-slates-by-user-id";
|
||||
import deleteSlateById from "~/node_common/data/methods/delete-slate-by-id";
|
||||
import recalcSlateSubscribers from "~/node_common/data/methods/recalc-slate-subscribers";
|
||||
import recalcSlateSubscribercount from "~/node_common/data/methods/recalc-slate-subscribercount";
|
||||
import recalcSlateFilecount from "~/node_common/data/methods/recalc-slate-filecount";
|
||||
|
||||
// NOTE(jim):
|
||||
@ -105,6 +107,8 @@ export {
|
||||
getUserById,
|
||||
getUserByEmail,
|
||||
getUserByTwitterId,
|
||||
recalcUserSlatecount,
|
||||
recalcUserFollowercount,
|
||||
//NOTE(martina): File operations
|
||||
createFile,
|
||||
getFileByCid,
|
||||
@ -132,7 +136,7 @@ export {
|
||||
deleteSlatesByUserId,
|
||||
deleteSlateById,
|
||||
recalcSlateFilecount,
|
||||
recalcSlateSubscribers,
|
||||
recalcSlateSubscribercount,
|
||||
// NOTE(jim): API key operations
|
||||
createAPIKey,
|
||||
deleteAPIKeyById,
|
||||
|
@ -1,3 +1,5 @@
|
||||
import * as Data from "~/node_common/data";
|
||||
|
||||
import { runQuery } from "~/node_common/data/utilities";
|
||||
|
||||
export default async ({ ownerId, slatename, isPublic, data = {} }) => {
|
||||
@ -23,9 +25,7 @@ export default async ({ ownerId, slatename, isPublic, data = {} }) => {
|
||||
type: "CREATE_SLATE",
|
||||
}).into("activity");
|
||||
|
||||
const summaryQuery = await DB.from("users")
|
||||
.where("id", ownerId)
|
||||
.increment("slateCount", 1);
|
||||
await Data.recalcUserSlatecount({ userId: ownerId });
|
||||
} else {
|
||||
const activityQuery = await DB.insert({
|
||||
ownerId,
|
||||
|
@ -21,7 +21,7 @@ export default async ({ ownerId, slateId, userId }) => {
|
||||
type: "SUBSCRIBE_SLATE",
|
||||
}).into("activity");
|
||||
|
||||
await Data.recalcSlateSubscribers({ slateId });
|
||||
await Data.recalcSlateSubscribercount({ slateId });
|
||||
} else if (userId) {
|
||||
const activityQuery = await DB.insert({
|
||||
ownerId,
|
||||
@ -29,9 +29,7 @@ export default async ({ ownerId, slateId, userId }) => {
|
||||
type: "SUBSCRIBE_USER",
|
||||
}).into("activity");
|
||||
|
||||
let summaryQuery = await DB.from("users")
|
||||
.where({ id: userId })
|
||||
.increment("followerCount", 1);
|
||||
await Data.recalcUserFollowercount({ userId });
|
||||
}
|
||||
|
||||
const index = query ? query.pop() : null;
|
||||
|
@ -4,9 +4,11 @@ export default async ({ id }) => {
|
||||
return await runQuery({
|
||||
label: "DELETE_API_KEY_BY_ID",
|
||||
queryFn: async (DB) => {
|
||||
const data = await DB.from("keys").where({ id }).del();
|
||||
const data = await DB.from("keys").where({ id }).del().returning("*");
|
||||
|
||||
return 1 === data;
|
||||
let key = data ? data.pop() : data;
|
||||
|
||||
return key;
|
||||
},
|
||||
errorFn: async (e) => {
|
||||
return {
|
||||
|
@ -29,7 +29,7 @@ export default async ({ ids, ownerId }) => {
|
||||
|
||||
const files = await DB("files").whereIn("id", ids).del().returning("*");
|
||||
|
||||
return files.length === ids.length;
|
||||
return files;
|
||||
},
|
||||
errorFn: async (e) => {
|
||||
return {
|
||||
|
@ -34,9 +34,9 @@ export default async ({ ownerId }) => {
|
||||
|
||||
const activity = await DB("activity").whereIn("fileId", fileIds).del();
|
||||
|
||||
const files = await DB("files").whereIn("id", fileIds).del();
|
||||
const files = await DB("files").whereIn("id", fileIds).del().returning("*");
|
||||
|
||||
return true;
|
||||
return files;
|
||||
},
|
||||
errorFn: async (e) => {
|
||||
return {
|
||||
|
@ -1,3 +1,5 @@
|
||||
import * as Data from "~/node_common/data";
|
||||
|
||||
import { runQuery } from "~/node_common/data/utilities";
|
||||
|
||||
export default async ({ id }) => {
|
||||
@ -10,18 +12,14 @@ export default async ({ id }) => {
|
||||
|
||||
const activity = await DB("activity").where({ slateId: id }).del();
|
||||
|
||||
const slates = await DB("slates").where({ id }).del().returning("*");
|
||||
const data = await DB("slates").where({ id }).del().returning("*");
|
||||
|
||||
if (slates) {
|
||||
const slate = slates.pop();
|
||||
if (slate.isPublic) {
|
||||
const summaryQuery = await DB.from("users")
|
||||
.where("id", slate.ownerId)
|
||||
.decrement("slateCount", 1);
|
||||
}
|
||||
let slate = data ? data.pop() : data;
|
||||
if (slate?.isPublic) {
|
||||
await Data.recalcUserSlatecount({ userId: slate.ownerId });
|
||||
}
|
||||
|
||||
return slates;
|
||||
return slate;
|
||||
},
|
||||
errorFn: async (e) => {
|
||||
return {
|
||||
|
@ -9,7 +9,8 @@ export default async ({ slateId, ids }) => {
|
||||
const slateFiles = await DB("slate_files")
|
||||
.where("slateId", slateId)
|
||||
.whereIn("fileId", ids)
|
||||
.del();
|
||||
.del()
|
||||
.returning("*");
|
||||
|
||||
const activityQuery = await DB("activity")
|
||||
.where({ slateId, type: "CREATE_SLATE_OBJECT" })
|
||||
@ -18,7 +19,7 @@ export default async ({ slateId, ids }) => {
|
||||
|
||||
await Data.recalcSlateFilecount({ slateId });
|
||||
|
||||
return true;
|
||||
return slateFiles;
|
||||
},
|
||||
errorFn: async (e) => {
|
||||
return {
|
||||
|
@ -18,9 +18,9 @@ export default async ({ ownerId }) => {
|
||||
|
||||
const activity = await DB("activity").whereIn("slateId", slateIds).del();
|
||||
|
||||
const slates = await DB("slates").whereIn("id", slateIds).del();
|
||||
const slates = await DB("slates").whereIn("id", slateIds).del().returning("*");
|
||||
|
||||
return true;
|
||||
return slates;
|
||||
},
|
||||
errorFn: async (e) => {
|
||||
return {
|
||||
|
@ -6,25 +6,33 @@ export default async ({ id, ownerId, userId, slateId }) => {
|
||||
return await runQuery({
|
||||
label: "DELETE_SUBSCRIPTION_BY_ID",
|
||||
queryFn: async (DB) => {
|
||||
const data = await DB.from("subscriptions").where({ id }).del();
|
||||
const data = await DB.from("subscriptions").where({ id }).del().returning("*");
|
||||
|
||||
if (userId) {
|
||||
let subscription = data ? data.pop() : data;
|
||||
|
||||
if (subscription?.userId) {
|
||||
const activityQuery = await DB("activity")
|
||||
.where({ type: "SUBSCRIBE_USER", ownerId, userId })
|
||||
.where({
|
||||
type: "SUBSCRIBE_USER",
|
||||
ownerId: subscription.ownerId,
|
||||
userId: subscription.userId,
|
||||
})
|
||||
.del();
|
||||
|
||||
let summaryQuery = await DB.from("users")
|
||||
.where({ id: userId })
|
||||
.decrement("followerCount", 1);
|
||||
} else if (slateId) {
|
||||
await Data.recalcUserFollowercount({ userId });
|
||||
} else if (subscription?.slateId) {
|
||||
const activityQuery = await DB("activity")
|
||||
.where({ type: "SUBSCRIBE_SLATE", ownerId, slateId })
|
||||
.where({
|
||||
type: "SUBSCRIBE_SLATE",
|
||||
ownerId: subscription.slateId,
|
||||
userId: subscription.slateId,
|
||||
})
|
||||
.del();
|
||||
|
||||
await Data.recalcSlateSubscribers({ slateId });
|
||||
await Data.recalcSlateSubscribercount({ slateId });
|
||||
}
|
||||
|
||||
return 1 === data;
|
||||
return subscription;
|
||||
},
|
||||
errorFn: async (e) => {
|
||||
return {
|
||||
|
@ -17,7 +17,7 @@ export default async ({ id }) => {
|
||||
.returning("slateId");
|
||||
|
||||
for (let slateId of deletedSubscriptions) {
|
||||
await Data.recalcSlateSubscribers({ slateId });
|
||||
await Data.recalcSlateSubscribercount({ slateId });
|
||||
}
|
||||
|
||||
const deletedFollowing = await DB.from("subscriptions")
|
||||
@ -25,10 +25,10 @@ export default async ({ id }) => {
|
||||
.whereNotNull("userId")
|
||||
.del()
|
||||
.returning("userId");
|
||||
|
||||
const followingSummaryQuery = await DB.from("users")
|
||||
.whereIn("id", deletedFollowing)
|
||||
.decrement("followerCount", 1);
|
||||
console.log({ deletedFollowing }); //double confirm this
|
||||
for (let userId of deletedFollowing) {
|
||||
await Data.recalcUserFollowercount({ userId });
|
||||
}
|
||||
|
||||
const activity = await DB.from("activity")
|
||||
.where({ ownerId: id })
|
||||
@ -37,9 +37,11 @@ export default async ({ id }) => {
|
||||
|
||||
const usage = await DB.from("usage").where("userId", id).del();
|
||||
|
||||
const data = await DB.from("users").where({ id }).del();
|
||||
const data = await DB.from("users").where({ id }).del().returning("*");
|
||||
|
||||
return 1 === data;
|
||||
let user = data ? data.pop() : data;
|
||||
|
||||
return user;
|
||||
},
|
||||
errorFn: async (e) => {
|
||||
return {
|
||||
|
@ -4,9 +4,9 @@ export default async ({ email }) => {
|
||||
return await runQuery({
|
||||
label: "DELETE_VERIFICATION_BY_EMAIL",
|
||||
queryFn: async (DB) => {
|
||||
const query = await DB.from("verifications").where({ email }).del();
|
||||
const data = await DB.from("verifications").where({ email }).del().returning("*");
|
||||
|
||||
return 1 === query;
|
||||
return data;
|
||||
},
|
||||
errorFn: async (e) => {
|
||||
return {
|
||||
|
@ -4,9 +4,9 @@ export default async ({ sid }) => {
|
||||
return await runQuery({
|
||||
label: "DELETE_VERIFICATION_BY_SID",
|
||||
queryFn: async (DB) => {
|
||||
const query = await DB.from("verifications").where({ sid }).del();
|
||||
const data = await DB.from("verifications").where({ sid }).del().returning("*");
|
||||
|
||||
return 1 === query;
|
||||
return data;
|
||||
},
|
||||
errorFn: async (e) => {
|
||||
return {
|
||||
|
@ -27,7 +27,7 @@ export default async ({ fileId }) => {
|
||||
]);
|
||||
let rows = updatedFile.rows;
|
||||
if (rows?.length) {
|
||||
return rows.first();
|
||||
return rows.pop();
|
||||
}
|
||||
|
||||
return true;
|
||||
|
25
node_common/data/methods/recalc-user-followercount.js
Normal file
25
node_common/data/methods/recalc-user-followercount.js
Normal file
@ -0,0 +1,25 @@
|
||||
import { runQuery } from "~/node_common/data/utilities";
|
||||
|
||||
export default async ({ userId }) => {
|
||||
return await runQuery({
|
||||
label: "RECALC_USER_FOLLOWERCOUNT",
|
||||
queryFn: async (DB) => {
|
||||
const followerCountFields = ["id", "subscriptions", "userId", userId];
|
||||
const followerCount = `(SELECT COUNT(??) FROM ?? WHERE ?? = ?)`;
|
||||
|
||||
const updateFields = ["users", "followerCount", ...followerCountFields, "id", userId];
|
||||
const update = await DB.raw(
|
||||
`UPDATE ?? SET ?? = ${followerCount} WHERE ?? = ? RETURNING *`,
|
||||
updateFields
|
||||
);
|
||||
|
||||
return true;
|
||||
},
|
||||
errorFn: async (e) => {
|
||||
return {
|
||||
error: true,
|
||||
decorator: "RECALC_USER_FOLLOWERCOUNT",
|
||||
};
|
||||
},
|
||||
});
|
||||
};
|
25
node_common/data/methods/recalc-user-slatecount.js
Normal file
25
node_common/data/methods/recalc-user-slatecount.js
Normal file
@ -0,0 +1,25 @@
|
||||
import { runQuery } from "~/node_common/data/utilities";
|
||||
|
||||
export default async ({ userId }) => {
|
||||
return await runQuery({
|
||||
label: "RECALC_USER_SLATECOUNT",
|
||||
queryFn: async (DB) => {
|
||||
const slateCountFields = ["id", "slates", "ownerId", userId, "isPublic", true];
|
||||
const slateCount = `(SELECT COUNT(??) FROM ?? WHERE ?? = ? AND ?? = ?)`;
|
||||
|
||||
const updateFields = ["users", "slateCount", ...slateCountFields, "id", userId];
|
||||
const update = await DB.raw(
|
||||
`UPDATE ?? SET ?? = ${slateCount} WHERE ?? = ? RETURNING *`,
|
||||
updateFields
|
||||
);
|
||||
|
||||
return true;
|
||||
},
|
||||
errorFn: async (e) => {
|
||||
return {
|
||||
error: true,
|
||||
decorator: "RECALC_USER_SLATECOUNT",
|
||||
};
|
||||
},
|
||||
});
|
||||
};
|
@ -1,3 +1,5 @@
|
||||
import * as Data from "~/node_common/data";
|
||||
|
||||
import { runQuery } from "~/node_common/data/utilities";
|
||||
|
||||
export default async ({ ownerId, id, isPublic }) => {
|
||||
@ -10,22 +12,20 @@ export default async ({ ownerId, id, isPublic }) => {
|
||||
|
||||
const response = await DB.from("slates").where("id", id).update({ isPublic }).returning("*");
|
||||
|
||||
await Data.recalcUserSlatecount({ userId: ownerId });
|
||||
|
||||
if (isPublic) {
|
||||
let activityQuery = await DB.insert({
|
||||
type: "SLATE_VISIBLE",
|
||||
ownerId,
|
||||
slateId: id,
|
||||
}).into("activity");
|
||||
|
||||
const summaryQuery = await DB.from("users").where("id", ownerId).increment("slateCount", 1);
|
||||
} else {
|
||||
let activityQuery = await DB("activity")
|
||||
.where({
|
||||
slateId: id,
|
||||
})
|
||||
.update({ ignore: true });
|
||||
|
||||
const summaryQuery = await DB.from("users").where("id", ownerId).decrement("slateCount", 1);
|
||||
}
|
||||
|
||||
const index = response ? response.pop() : null;
|
||||
|
@ -50,8 +50,8 @@ export default class SceneEditAccount extends React.Component {
|
||||
photo: this.props.viewer.data.photo,
|
||||
name: this.props.viewer.data.name,
|
||||
deleting: false,
|
||||
allow_filecoin_directory_listing:
|
||||
this.props.viewer.data.settings?.allow_filecoin_directory_listing,
|
||||
allow_filecoin_directory_listing: this.props.viewer.data.settings
|
||||
?.allow_filecoin_directory_listing,
|
||||
allow_automatic_data_storage: this.props.viewer.data.settings?.allow_automatic_data_storage,
|
||||
allow_encrypted_data_storage: this.props.viewer.data.settings?.allow_encrypted_data_storage,
|
||||
changingPassword: false,
|
||||
|
Loading…
Reference in New Issue
Block a user