added max length for body and extended database length for body & edited migration script

This commit is contained in:
Martina 2021-09-10 12:09:36 -07:00
parent d0d0d356c9
commit ef725d8040
12 changed files with 117 additions and 37 deletions

View File

@ -118,6 +118,7 @@ export const error = {
SERVER_UPDATE_SLATE_NAME_TAKEN:
"You already have a collection with that name. Collection names must be unique",
SERVER_UPDATE_SLATE_FAILED: "We are having trouble updating that collection right now",
SERVER_UPDATE_SLATE_MAX_BODY_LENGTH: "Descriptions can be a maximum of 2000 characters",
//Create user
SERVER_CREATE_USER_NOT_ALLOWED: "You can only create users while on slate.host",
@ -194,6 +195,7 @@ export const error = {
SERVER_USER_UPDATE_DEFAULT_ARCHIVE_CONFIG:
"We're having trouble updating your settings right now",
SERVER_USER_UPDATE_INVALID_PASSWORD: "Please choose a valid password",
SERVER_USER_UPDATE_MAX_BODY_LENGTH: "Descriptions can be a maximum of 2000 characters",
//Zip files
GET_ZIP_FILES_PATHS_BUCKET_CHECK_FAILED: "We're having trouble locating those files right now",

View File

@ -552,6 +552,7 @@ class CarouselSidebar extends React.Component {
...STYLES_INPUT,
}}
textStyle={{ color: Constants.system.white }}
maxlength="255"
/>
<Textarea
@ -560,6 +561,7 @@ class CarouselSidebar extends React.Component {
value={this.state.body}
onChange={this._handleChange}
style={STYLES_INPUT}
maxlength="2000"
/>
<Input
full
@ -570,6 +572,7 @@ class CarouselSidebar extends React.Component {
id={`sidebar-label-source`}
style={STYLES_INPUT}
textStyle={{ color: Constants.system.white }}
maxlength="255"
/>
<Input
full
@ -580,6 +583,7 @@ class CarouselSidebar extends React.Component {
id={`sidebar-label-author`}
style={{ ...STYLES_INPUT, marginBottom: 12 }}
textStyle={{ color: Constants.system.white }}
maxlength="255"
/>
{/* <div css={STYLES_OPTIONS_SECTION}>
<Tag

View File

@ -112,14 +112,14 @@ export default class SidebarCreateSlate extends React.Component {
<div css={STYLES_GROUPING}>
<System.P1 css={STYLES_HEADER}>Name</System.P1>
{/* <System.P1
<System.P1
css={STYLES_TEXT}
style={{
marginTop: 12,
}}
>
Give your collection a name so you and others can find it on Slate and on the web.
</System.P1> */}
Note: Changing the collection name will break any old links to the collection.
</System.P1>
<System.Input
autoFocus
@ -131,6 +131,7 @@ export default class SidebarCreateSlate extends React.Component {
onSubmit={this._handleSubmit}
descriptionStyle={{ fontSize: "20px !important" }}
labelStyle={{ fontSize: "20px" }}
maxlength="255"
/>
<System.P1
style={{
@ -161,6 +162,7 @@ export default class SidebarCreateSlate extends React.Component {
value={this.state.body}
onChange={this._handleChange}
onSubmit={this._handleSubmit}
maxlength="2000"
/>
</div>

View File

@ -169,6 +169,7 @@ export default class SidebarSingleSlateSettings extends React.Component {
onSubmit={this._handleSubmit}
descriptionStyle={{ fontSize: "20px !important" }}
labelStyle={{ fontSize: "20px" }}
maxlength="255"
/>
<System.P1
style={{
@ -199,6 +200,7 @@ export default class SidebarSingleSlateSettings extends React.Component {
value={this.state.body}
onChange={this._handleChange}
onSubmit={this._handleSubmit}
maxlength="2000"
/>
</div>

View File

@ -269,6 +269,7 @@ export class Input extends React.Component {
disabled={this.props.disabled}
readOnly={this.props.readOnly}
required={this.props.required}
maxlength={this.props.maxlength}
style={{
width: this.props.copyable || this.props.icon ? "calc(100% - 32px)" : "100%",
...this.props.textStyle,

View File

@ -54,6 +54,7 @@ export class Textarea extends React.Component {
name={this.props.name}
value={this.props.value}
readOnly={this.props.readOnly}
maxlength={this.props.maxlength}
/>
);
}

View File

@ -24,6 +24,10 @@ export default async (req, res) => {
.send({ decorator: "SERVER_CREATE_SLATE_EXISTING_SLATE_NAME", error: true });
}
if (req.body.data.body && req.body.data.body.length > 2000) {
return res.status(400).send({ decorator: "SERVER_CREATE_SLATE_MAX_BODY_LENGTH", error: true });
}
const slate = await Data.createSlate({
ownerId: id,
slatename: Strings.createSlug(req.body.data.name),

View File

@ -30,6 +30,10 @@ export default async (req, res) => {
return res.status(500).send({ decorator: "SERVER_UPDATE_SLATE_NOT_FOUND", error: true });
}
if (updates.body && updates.body.length > 2000) {
return res.status(400).send({ decorator: "SERVER_UPDATE_SLATE_MAX_BODY_LENGTH", error: true });
}
if (typeof updates.isPublic !== "undefined" && slate.isPublic !== updates.isPublic) {
let privacyResponse = await Data.updateSlatePrivacy({
ownerId: id,

View File

@ -110,6 +110,10 @@ export default async (req, res) => {
updates.password = hash;
}
if (updates.body && updates.body.length > 2000) {
return res.status(400).send({ decorator: "SERVER_USER_UPDATE_MAX_BODY_LENGTH", error: true });
}
let unsafeResponse = await Data.updateUserById({ id, ...updates });
if (unsafeResponse && !unsafeResponse.error) {

View File

@ -218,6 +218,7 @@ export default class SceneEditAccount extends React.Component {
<div css={STYLES_HEADER}>Bio</div>
<System.Textarea
maxlength="2000"
name="body"
value={this.state.body}
placeholder="A bit about yourself..."

View File

@ -113,7 +113,7 @@ const printFilesTable = async () => {
const addUserColumns = async () => {
await DB.schema.table("users", function (table) {
table.string("body").nullable();
table.string("body", 2000).nullable();
table.string("photo").nullable();
table.string("name").nullable();
table.string("twitterUsername").nullable();
@ -128,7 +128,7 @@ const addUserColumns = async () => {
const addSlateColumns = async () => {
await DB.schema.table("slates", function (table) {
table.string("body").nullable();
table.string("body", 2000).nullable();
table.string("name").nullable();
table.string("preview").nullable();
});
@ -144,29 +144,49 @@ const addFileColumns = async () => {
table.string("type").notNullable().defaultTo("link");
table.string("blurhash").nullable();
table.string("source").nullable();
table.string("body").nullable();
table.string("body", 2000).nullable();
table.string("author").nullable();
table.jsonb("coverImage").nullable();
table.jsonb("data").nullable(); //where you'll move unity stuff
table.string("linkName").nullable();
table.string("linkBody").nullable();
table.string("linkBody", 2000).nullable();
table.string("linkAuthor").nullable();
table.string("linkSource").nullable();
table.string("linkDomain").nullable();
table.string("linkImage").nullable();
table.string("linkFavicon").nullable();
table.string("linkHtml").nullable();
table.text("linkHtml").nullable();
table.boolean("linkIFrameAllowed").nullable().defaultTo(false);
table.jsonb("tags").nullable();
});
};
const editColumns = async () => {
await DB.schema.alterTable("files", function (table) {
table.text("linkHtml").nullable().alter();
});
};
/* Migrate over to new columns (and denormalize tags) */
const defaultPhotos = [
"https://slate.host/static/a1.jpg",
"https://slate.textile.io/ipfs/bafkreigjofaa2wvmcoi5vbe3h32cbqh35d35wdhodfxwgmfky3gcur6s5i",
"https://slate.textile.io/ipfs/bafkreiaycwt6m3jabhetfqnsb63z2lx65vzepp5ejk2b56vxypwxiet6c4",
"https://slate.textile.io/ipfs/bafkreifvdvygknj66bfqdximxmxobqelwhd3xejiq3vfplhzkopcfdetrq",
"https://slate.textile.io/ipfs/bafkreihm3srxvhfppm2dvldw224v4m3prcc3b6pwe3rtuxpsu6wwjpgzpa",
"https://slate.textile.io/ipfs/bafkreiardkkfxj3ip373ee2tf6ffivjqclq7ionemt6pw55e6hv7ws5pvu",
"https://slate.textile.io/ipfs/bafkreick3nscgixwfpq736forz7kzxvvhuej6kszevpsgmcubyhsx2pf7i",
"https://slate.textile.io/ipfs/bafkreibf3hoiyuk2ywjyoy24ywaaclo4k5rz53flesvr5h4qjlyzxamozm",
];
const defaultBody = ["A user of Slate.", "", "A slate."];
const migrateUserTable = async () => {
const users = await DB.select("id", "data").from("users");
for (let user of users) {
let data = user.data;
let newUser = {
name: data.name,
body: data.body,
@ -175,16 +195,32 @@ const migrateUserTable = async () => {
settingsDealsAutoApprove: data.settings?.settings_deals_auto_approve,
allowAutomaticDataStorage: data.settings?.allow_automatic_data_storage,
allowEncryptedDataStorage: data.settings?.allow_encrypted_data_storage,
onboarding: { ...data.onboarding, hidePrivacyAlert: data.status?.hidePrivacyAlert },
onboarding:
data.onboarding || data.status?.hidePrivacyAlert
? { ...data.onboarding, hidePrivacyAlert: data.status?.hidePrivacyAlert }
: null,
twitterUsername: data.twitter?.username,
twitterVerified: data.twitter?.verified,
};
const response = await DB.from("users").where("id", user.id).update(newUser);
if (defaultPhotos.includes(data.photo)) {
// console.log(`replaced default photo ${data.photo} with null`);
newUser.photo = null;
}
if (defaultBody.includes(data.body)) {
// console.log(`replaced default body with null`);
newUser.body = null;
}
// console.log({ data });
console.log({ id: user.id, newUser });
const response = await DB.from("users").where("id", user.id).update(newUser).returning("*");
// console.log({ response });
}
};
const migrateSlateTable = async () => {
const slates = await DB.select("id", "data").from("slates");
for (let slate of slates) {
let data = slate.data;
let newSlate = {
@ -192,6 +228,14 @@ const migrateSlateTable = async () => {
body: data.body,
preview: data.preview,
};
if (defaultBody.includes(data.body)) {
// console.log(`replaced default body with null`);
newSlate.body = null;
}
// console.log({ data });
// console.log({ newSlate });
const response = await DB.from("slates").where("id", slate.id).update(newSlate);
}
};
@ -213,7 +257,6 @@ const migrateFileTable = async () => {
blurhash: data.blurhash,
source: data.source,
author: data.author,
data: data.unity ? { unity: data.unity } : null,
linkName: data.link?.name,
linkBody: data.link?.body,
linkAuthor: data.link?.author,
@ -225,34 +268,42 @@ const migrateFileTable = async () => {
linkIFrameAllowed: data.link?.iFrameAllowed,
};
if (data.unity) {
newFile.data = { unity: data.unity };
}
// if (newFile.data) {
// console.log({ unity: newFile.data.unity });
// }
let coverImage = data.coverImage;
if (coverImage) {
console.log({ coverImage });
// console.log({ coverImage });
let newCoverImage = {
size: coverImage.data?.size,
type: coverImage.data?.type,
blurhash: coverImage.data?.blurhash,
url: coverImage.data?.url,
...coverImage.data,
...coverImage,
data: null,
};
console.log({ newCoverImage });
delete newCoverImage.data;
newFile.coverImage = newCoverImage;
// console.log({ newCoverImage });
}
let tags = await DB.select("slates.id", "slates.slatename")
let tags = await DB.select("slates.id", "slates.slatename", "slates.name")
.from("slates")
.join("slate_files", "slate_files.slateId", "=", "slates.id")
.where("slate_files.fileId", file.id);
if (tags?.length) {
newFile.tags = tags;
newFile.tags = JSON.stringify(tags);
}
console.log({ newFile });
const response = await DB.from("files").where("id", file.id).update(newFile);
}
};
/* Delete the old (and convert newData to data) */
/* Delete the old data */
const deleteUserData = async () => {
const deleteOldData = async () => {
await DB.schema.table("users", function (table) {
table.dropColumn("data");
});
@ -265,18 +316,22 @@ const deleteUserData = async () => {
};
const runScript = async () => {
await printUsersTable();
await printSlatesTable();
await printFilesTable();
// await printUsersTable();
// await printSlatesTable();
// await printFilesTable();
// await addUserColumns();
// await addSlateColumns();
// await addFileColumns();
// await editColumns();
// await migrateUserTable();
// await migrateSlateTable();
// await migrateFileTable();
console.log("SCRIPT FINISHED");
await migrateFileTable();
// await deleteOldData();
console.log("SCRIPT FINISHED. HIT CTRL + C TO END");
};
runScript();

View File

@ -34,7 +34,7 @@ const createUsersTable = db.schema.createTable("users", function (table) {
table.string("password").nullable();
table.string("salt").nullable();
table.string("name").nullable();
table.string("body").nullable();
table.string("body", 2000).nullable();
table.string("photo").nullable();
table.string("twitterId").unique().nullable();
table.string("twitterUsername").nullable();
@ -55,7 +55,7 @@ const createSlatesTable = db.schema.createTable("slates", function (table) {
table.timestamp("createdAt").notNullable().defaultTo(db.raw("now()"));
table.timestamp("updatedAt").notNullable().defaultTo(db.raw("now()"));
table.string("slatename").notNullable();
table.string("body").nullable();
table.string("body", 2000).nullable();
table.string("name").nullable();
table.string("preview").nullable();
table.boolean("isPublic").notNullable().defaultTo(false);
@ -83,18 +83,18 @@ const createFilesTable = db.schema.createTable("files", function (table) {
table.string("type").notNullable().defaultTo("link");
table.string("blurhash").nullable();
table.string("source").nullable();
table.string("body").nullable();
table.string("body", 2000).nullable();
table.string("author").nullable();
table.jsonb("coverImage").nullable();
table.jsonb("data").nullable();
table.string("linkName").nullable();
table.string("linkBody").nullable();
table.string("linkBody", 2000).nullable();
table.string("linkAuthor").nullable();
table.string("linkSource").nullable();
table.string("linkDomain").nullable();
table.string("linkImage").nullable();
table.string("linkFavicon").nullable();
table.string("linkHtml").nullable();
table.text("linkHtml").nullable();
table.boolean("linkIFrameAllowed").nullable().defaultTo(false);
table.jsonb("tags").nullable();
table.integer("downloadCount").notNullable().defaultTo(0);