updated user count calculation functions

This commit is contained in:
Martina 2021-08-23 16:41:34 -07:00
parent c7f18df245
commit ab80643e4b
19 changed files with 118 additions and 55 deletions

View File

@ -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,

View File

@ -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,

View File

@ -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;

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -27,7 +27,7 @@ export default async ({ fileId }) => {
]);
let rows = updatedFile.rows;
if (rows?.length) {
return rows.first();
return rows.pop();
}
return true;

View 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",
};
},
});
};

View 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",
};
},
});
};

View File

@ -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;

View File

@ -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,