slates: adds an easy way to get every user and every slate (use with caution)

This commit is contained in:
@wwwjim 2020-08-27 15:05:20 -07:00
parent 44d5458d3f
commit 8039d31192
9 changed files with 117 additions and 29 deletions

View File

@ -46,6 +46,12 @@ export const sendFilecoin = async (data) => {
});
};
export const getNetworkDirectory = async () => {
return await returnJSON(`/api/directory`, {
...DEFAULT_OPTIONS,
});
};
export const getSlateBySlatename = async (data) => {
return await returnJSON(`/api/search/slates/${data.query}`, {
...DEFAULT_OPTIONS,

26
common/serializers.js Normal file
View File

@ -0,0 +1,26 @@
export const user = (entity) => {
return {
type: "USER",
id: entity.id,
username: entity.username,
data: {
name: entity.data.name,
photo: entity.data.photo,
body: entity.data.body,
},
};
};
export const slate = (entity) => {
return {
type: "SLATE",
id: entity.id,
slatename: entity.slatename,
data: {
ownerId: entity.data.ownerId,
name: entity.data.name,
body: entity.data.body,
objects: entity.data.objects,
},
};
};

View File

@ -56,6 +56,8 @@ import deleteActivityById from "~/node_common/data/methods/delete-activity-by-id
// Search postgres queries
import querySlates from "~/node_common/data/methods/query-slates";
import queryUsers from "~/node_common/data/methods/query-users";
import getEverySlate from "~/node_common/data/methods/get-every-slate";
import getEveryUser from "~/node_common/data/methods/get-every-user";
export {
// NOTE(jim): User operations.
@ -103,4 +105,6 @@ export {
// NOTE(jim): Search
queryUsers,
querySlates,
getEverySlate,
getEveryUser,
};

View File

@ -0,0 +1,27 @@
import * as Serializers from "~/common/serializers";
import { runQuery } from "~/node_common/data/utilities";
export default async () => {
return await runQuery({
label: "GET_EVERY_SLATE",
queryFn: async (DB) => {
const r = await DB.select("id", "slatename", "data").from("slates");
if (!r || r.error) {
return [];
}
const sanitized = r
.filter((each) => each.data.public)
.map((each) => Serializers.slate(each));
return JSON.parse(JSON.stringify(sanitized));
},
errorFn: async (e) => {
return {
error: "GET_EVERY_SLATE",
source: e,
};
},
});
};

View File

@ -0,0 +1,25 @@
import * as Serializers from "~/common/serializers";
import { runQuery } from "~/node_common/data/utilities";
export default async () => {
return await runQuery({
label: "GET_EVERY_USER",
queryFn: async (DB) => {
const r = await DB.select("id", "username", "data").from("users");
if (!r || r.error) {
return [];
}
const sanitized = r.map((each) => Serializers.user(each));
return JSON.parse(JSON.stringify(sanitized));
},
errorFn: async (e) => {
return {
error: "GET_EVERY_USER",
source: e,
};
},
});
};

View File

@ -1,3 +1,5 @@
import * as Serializers from "~/common/serializers";
import { runQuery } from "~/node_common/data/utilities";
export default async ({ query }) => {
@ -15,20 +17,7 @@ export default async ({ query }) => {
const sanitized = r
.filter((each) => each.data.public)
.map((each) => {
return {
id: each.id,
slatename: each.slatename,
data: {
ownerId: each.data.ownerId,
name: each.data.name,
body: each.data.body,
objects: each.data.objects,
},
type: "SLATE",
};
});
.map((each) => Serializers.slate(each));
return JSON.parse(JSON.stringify(sanitized));
},
errorFn: async (e) => {

View File

@ -1,3 +1,5 @@
import * as Serializers from "~/common/serializers";
import { runQuery } from "~/node_common/data/utilities";
export default async ({ query }) => {
@ -13,20 +15,7 @@ export default async ({ query }) => {
return [];
}
// TODO(jim): Not a fan of this. Need something more secure.
const sanitized = r.map((each) => {
return {
type: "USER",
id: each.id,
username: each.username,
data: {
name: each.data.name,
photo: each.data.photo,
body: each.data.body,
},
};
});
const sanitized = r.map((each) => Serializers.user(each));
return JSON.parse(JSON.stringify(sanitized));
},
errorFn: async (e) => {

View File

@ -38,7 +38,10 @@ export default class IntegrationPage extends React.Component {
viewer: this.props.viewer,
};
componentDidMount() {}
async componentDidMount() {
const response = await Actions.getNetworkDirectory();
console.log(response);
}
_handleChange = async (e) => {
const query = e.target.value;

19
pages/api/directory.js Normal file
View File

@ -0,0 +1,19 @@
import * as MW from "~/node_common/middleware";
import * as Strings from "~/common/strings";
import * as Data from "~/node_common/data";
const initCORS = MW.init(MW.CORS);
const initAuth = MW.init(MW.RequireCookieAuthentication);
export default async (req, res) => {
initCORS(req, res);
initAuth(req, res);
const users = await Data.getEveryUser();
const slates = await Data.getEverySlate();
return res.status(200).send({
decorator: "SERVER_DIRECTORY",
data: { users, slates },
});
};