Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for new skin logo in space settings #498

Merged
merged 13 commits into from
Feb 8, 2025
Merged
81 changes: 70 additions & 11 deletions src/helpers/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,95 @@ import snapshot from '@snapshot-labs/snapshot.js';
import db from './mysql';
import { DEFAULT_NETWORK_ID, jsonParse, NETWORK_ID_WHITELIST } from './utils';

export async function addOrUpdateSpace(space: string, settings: any) {
if (!settings?.name) return false;
if (settings.domain) {
settings.domain = settings.domain?.replace(/(^\w+:|^)\/\//, '').replace(/\/$/, '');
const DEFAULT_SKIN = 'light';

function normalizeSettings(settings: any) {
const _settings = snapshot.utils.clone(settings);

if (_settings.domain) {
_settings.domain = _settings.domain?.replace(/(^\w+:|^)\/\//, '').replace(/\/$/, '');
}
if (settings.delegationPortal) {
settings.delegationPortal = {
if (_settings.delegationPortal) {
_settings.delegationPortal = {
delegationNetwork: '1',
...settings.delegationPortal
..._settings.delegationPortal
};
}

delete _settings.skinParams;

return _settings;
}

export async function addOrUpdateSpace(id: string, settings: any) {
if (!settings?.name) return false;

const normalizedSettings = normalizeSettings(settings);

const ts = (Date.now() / 1e3).toFixed();
const query =
'INSERT INTO spaces SET ? ON DUPLICATE KEY UPDATE updated = ?, settings = ?, name = ?, hibernated = 0, domain = ?';

await db.queryAsync(query, [
{
id: space,
id,
name: settings.name,
created: ts,
updated: ts,
settings: JSON.stringify(settings),
settings: JSON.stringify(normalizedSettings),
domain: settings.domain || null
},
ts,
JSON.stringify(settings),
JSON.stringify(normalizedSettings),
settings.name,
settings.domain || null
normalizedSettings.domain || null
wa0x6e marked this conversation as resolved.
Show resolved Hide resolved
]);

await addOrUpdateSkin(id, settings.skinParams);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it is good to call addOrUpdateSkin inside addOrUpdateSpace, maybe we should just call it after addOrUpdateSpace everywhere

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addOrUpdateSkin is dependent of the addOrUpdateSpace, and will never be called without it. Why would making an extra call everywhere better ?

}

export async function addOrUpdateSkin(id: string, skinParams: Record<string, string>) {
if (!skinParams) return false;

const COLORS = [
'bg_color',
'link_color',
'text_color',
'content_color',
'border_color',
'heading_color',
'primary_color',
'header_color'
];

const _params = snapshot.utils.clone(skinParams);
COLORS.forEach(color => {
if (_params[color]) {
_params[color] = _params[color].replace('#', '');
wa0x6e marked this conversation as resolved.
Show resolved Hide resolved
wa0x6e marked this conversation as resolved.
Show resolved Hide resolved
}
});
const existingSkin = (
await db.queryAsync('SELECT theme, logo FROM skins WHERE id = ? LIMIT 1', [id])
)[0];

await db.queryAsync(
`INSERT INTO skins
SET ?
ON DUPLICATE KEY UPDATE
${COLORS.map(color => `${color} = ?`).join(',')},
theme = ?,
logo = ?
`,
[
{
id,
..._params
},
...COLORS.map(color => _params[color]),
_params.theme || existingSkin?.theme || DEFAULT_SKIN,
_params.logo || existingSkin?.logo || null
]
);
wa0x6e marked this conversation as resolved.
Show resolved Hide resolved
}

export async function getProposal(space, id) {
Expand Down
95 changes: 94 additions & 1 deletion test/integration/helpers/actions.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { addOrUpdateSpace, getSpace, sxSpaceExists } from '../../../src/helpers/actions';
import {
addOrUpdateSkin,
addOrUpdateSpace,
getSpace,
sxSpaceExists
} from '../../../src/helpers/actions';
import db, { sequencerDB } from '../../../src/helpers/mysql';
import { DEFAULT_NETWORK_ID } from '../../../src/helpers/utils';
import { spacesSqlFixtures } from '../../fixtures/space';
Expand Down Expand Up @@ -166,4 +171,92 @@ describe('helpers/actions', () => {
});
});
});

describe('addOrUpdateSkin', () => {
afterAll(async () => {
await db.queryAsync('DELETE FROM snapshot_sequencer_test.skins');
});

it('adds a new skin when it does not exist', async () => {
const testId = 'test-new-skin-ids';
const skinParams = {
bg_color: '#000000',
link_color: '#ffffff',
text_color: '#000000',
content_color: '#ffffff',
border_color: '#ffffff',
heading_color: '#ffffff',
primary_color: '#ffffff'
};
await addOrUpdateSkin(testId, skinParams);
const skin = (await db.queryAsync('SELECT * FROM skins WHERE id = ?', [testId]))[0];
expect(skin).toEqual({
id: testId,
bg_color: '000000',
link_color: 'ffffff',
text_color: '000000',
content_color: 'ffffff',
border_color: 'ffffff',
heading_color: 'ffffff',
primary_color: 'ffffff',
header_color: null,
theme: 'light',
logo: null
});
});

it('updates an existing skin', async () => {
const testId = 'test-update-skin-id';
const skinParams = {
bg_color: '#FFFF00',
link_color: '#FFFFFF'
};
await db.queryAsync(
'INSERT INTO skins (id, bg_color, link_color, text_color, content_color, border_color, heading_color, primary_color, header_color, theme) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
[
testId,
'000000',
'000000',
'000000',
'000000',
'000000',
'000000',
'000000',
'000000',
'light'
]
);
await addOrUpdateSkin(testId, skinParams);
const skin = (await db.queryAsync('SELECT * FROM skins WHERE id = ?', [testId]))[0];
expect(skin).toEqual({
id: testId,
bg_color: 'FFFF00',
link_color: 'FFFFFF',
text_color: null,
content_color: null,
border_color: null,
heading_color: null,
primary_color: null,
header_color: null,
theme: 'light',
logo: null
});

await addOrUpdateSkin(testId, { theme: 'dark' });
const skin2 = (await db.queryAsync('SELECT * FROM skins WHERE id = ?', [testId]))[0];
expect(skin2).toEqual({
id: testId,
bg_color: null,
link_color: null,
text_color: null,
content_color: null,
border_color: null,
heading_color: null,
primary_color: null,
header_color: null,
theme: 'dark',
logo: null
});
});
});
});
3 changes: 2 additions & 1 deletion test/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,10 @@ CREATE TABLE skins (
primary_color VARCHAR(6) DEFAULT NULL,
header_color VARCHAR(6) DEFAULT NULL,
theme VARCHAR(5) NOT NULL DEFAULT 'light',
logo VARCHAR(256) DEFAULT NULL,
PRIMARY KEY (id)
);

CREATE TABLE options (
name VARCHAR(100) NOT NULL,
value VARCHAR(100) NOT NULL,
Expand Down