Handle users.demote event (#6315)

* fix: Handle users.demote event

* fix: fetchAll

* fix: fetch based on total
This commit is contained in:
Apoorv Mishra
2023-12-27 08:33:44 +05:30
committed by GitHub
parent 4fd0e99909
commit 08aacdb302
6 changed files with 56 additions and 19 deletions

View File

@@ -18,6 +18,7 @@ import Pin from "~/models/Pin";
import Star from "~/models/Star"; import Star from "~/models/Star";
import Subscription from "~/models/Subscription"; import Subscription from "~/models/Subscription";
import Team from "~/models/Team"; import Team from "~/models/Team";
import User from "~/models/User";
import withStores from "~/components/withStores"; import withStores from "~/components/withStores";
import { import {
PartialWithId, PartialWithId,
@@ -460,6 +461,13 @@ class WebsocketProvider extends React.Component<Props> {
} }
); );
this.socket.on("users.demote", async (event: PartialWithId<User>) => {
if (auth.user && event.id === auth.user.id) {
documents.all.forEach((document) => policies.remove(document.id));
await collections.fetchAll();
}
});
// received a message from the API server that we should request // received a message from the API server that we should request
// to join a specific room. Forward that to the ws server. // to join a specific room. Forward that to the ws server.
this.socket.on("join", (event) => { this.socket.on("join", (event) => {

View File

@@ -1,8 +1,10 @@
import invariant from "invariant"; import invariant from "invariant";
import flatten from "lodash/flatten";
import lowerFirst from "lodash/lowerFirst"; import lowerFirst from "lodash/lowerFirst";
import orderBy from "lodash/orderBy"; import orderBy from "lodash/orderBy";
import { observable, action, computed, runInAction } from "mobx"; import { observable, action, computed, runInAction } from "mobx";
import pluralize from "pluralize"; import pluralize from "pluralize";
import { Pagination } from "@shared/constants";
import RootStore from "~/stores/RootStore"; import RootStore from "~/stores/RootStore";
import Policy from "~/models/Policy"; import Policy from "~/models/Policy";
import Model from "~/models/base/Model"; import Model from "~/models/base/Model";
@@ -268,6 +270,20 @@ export default abstract class Store<T extends Model> {
} }
}; };
@action
fetchAll = async (): Promise<T[]> => {
const limit = Pagination.defaultLimit;
const response = await this.fetchPage({ limit });
const pages = Math.ceil(response[PAGINATION_SYMBOL].total / limit);
const fetchPages = [];
for (let page = 1; page < pages; page++) {
fetchPages.push(this.fetchPage({ offset: page * limit, limit }));
}
const results = await Promise.all(fetchPages);
return flatten(results);
};
@computed @computed
get orderedData(): T[] { get orderedData(): T[] {
return orderBy(Array.from(this.data.values()), "createdAt", "desc"); return orderBy(Array.from(this.data.values()), "createdAt", "desc");

View File

@@ -139,6 +139,7 @@ class Event extends IdModel {
"documents.restore", "documents.restore",
"revisions.create", "revisions.create",
"users.create", "users.create",
"users.demote",
]; ];
static AUDIT_EVENTS: TEvent["name"][] = [ static AUDIT_EVENTS: TEvent["name"][] = [

View File

@@ -616,6 +616,12 @@ export default class WebsocketsProcessor {
.emit(event.name, presentTeam(team)); .emit(event.name, presentTeam(team));
} }
case "users.demote": {
return socketio
.to(`user-${event.userId}`)
.emit(event.name, { id: event.userId });
}
default: default:
return; return;
} }

View File

@@ -759,17 +759,20 @@ router.post(
teamId: user.teamId, teamId: user.teamId,
id: collectionIds, id: collectionIds,
}; };
const collections = await Collection.scope({ const [collections, total] = await Promise.all([
method: ["withMembership", user.id], Collection.scope({
}).findAll({ method: ["withMembership", user.id],
where, }).findAll({
order: [ where,
Sequelize.literal('"collection"."index" collate "C"'), order: [
["updatedAt", "DESC"], Sequelize.literal('"collection"."index" collate "C"'),
], ["updatedAt", "DESC"],
offset: ctx.state.pagination.offset, ],
limit: ctx.state.pagination.limit, offset: ctx.state.pagination.offset,
}); limit: ctx.state.pagination.limit,
}),
Collection.count({ where }),
]);
const nullIndex = collections.findIndex( const nullIndex = collections.findIndex(
(collection) => collection.index === null (collection) => collection.index === null
@@ -783,7 +786,7 @@ router.post(
} }
ctx.body = { ctx.body = {
pagination: ctx.state.pagination, pagination: { ...ctx.state.pagination, total },
data: collections.map(presentCollection), data: collections.map(presentCollection),
policies: presentPolicies(user, collections), policies: presentPolicies(user, collections),
}; };

View File

@@ -152,12 +152,15 @@ router.post(
sort = "updatedAt"; sort = "updatedAt";
} }
const documents = await Document.defaultScopeWithUser(user.id).findAll({ const [documents, total] = await Promise.all([
where, Document.defaultScopeWithUser(user.id).findAll({
order: [[sort, direction]], where,
offset: ctx.state.pagination.offset, order: [[sort, direction]],
limit: ctx.state.pagination.limit, offset: ctx.state.pagination.offset,
}); limit: ctx.state.pagination.limit,
}),
Document.count({ where }),
]);
// index sort is special because it uses the order of the documents in the // index sort is special because it uses the order of the documents in the
// collection.documentStructure rather than a database column // collection.documentStructure rather than a database column
@@ -172,7 +175,7 @@ router.post(
); );
const policies = presentPolicies(user, documents); const policies = presentPolicies(user, documents);
ctx.body = { ctx.body = {
pagination: ctx.state.pagination, pagination: { ...ctx.state.pagination, total },
data, data,
policies, policies,
}; };