Skip to content

Commit

Permalink
feat: add new skins table
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Jan 10, 2025
1 parent b69bd54 commit ef0aa8a
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 13 deletions.
47 changes: 39 additions & 8 deletions src/graphql/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ const ARG_LIMITS = {
}
};

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

export function checkLimits(args: any = {}, type) {
const { where = {} } = args;
const typeLimits = { ...ARG_LIMITS.default, ...(ARG_LIMITS[type] || {}) };
Expand All @@ -61,14 +72,24 @@ export function checkLimits(args: any = {}, type) {
return true;
}

function formatSkinSettings(result) {
return SKIN_SETTINGS.reduce((acc, color) => {
if (!result[color]) return acc;

acc[color] = `#${result[color]}`;
return acc;
}, {});
}

export function formatSpace({
id,
settings,
domain,
verified,
turbo,
flagged,
hibernated
hibernated,
skinSettings
}) {
const spaceMetadata = spacesMetadata[id] || {};
const space = { ...jsonParse(settings, {}), ...spaceMetadata.counts };
Expand Down Expand Up @@ -115,6 +136,7 @@ export function formatSpace({
space.validation = space.validation || { name: 'any', params: {} };
space.treasuries = space.treasuries || [];
space.labels = space.labels || [];
space.skinSettings = skinSettings;

space.verified = verified ?? null;
space.flagged = flagged ?? null;
Expand Down Expand Up @@ -283,15 +305,20 @@ export async function fetchSpaces(args) {
}

const query = `
SELECT s.* FROM spaces s
SELECT s.*, skins.*, s.id AS id FROM spaces s
LEFT JOIN skins ON s.id = skins.id
WHERE s.deleted = 0 ${queryStr}
GROUP BY s.id
ORDER BY s.${orderBy} ${orderDirection} LIMIT ?, ?
`;
params.push(skip, first);

const spaces = await db.queryAsync(query, params);
return spaces.map(space => Object.assign(space, formatSpace(space)));
return spaces.map(space =>
Object.assign(
space,
formatSpace({ skinSettings: formatSkinSettings(space), ...space })
)
);
}

function checkRelatedSpacesNesting(requestedFields): void {
Expand Down Expand Up @@ -410,7 +437,8 @@ export function formatProposal(proposal) {
verified: proposal.spaceVerified,
turbo: proposal.spaceTurbo,
flagged: proposal.spaceFlagged,
hibernated: proposal.spaceHibernated
hibernated: proposal.spaceHibernated,
skinSettings: formatSkinSettings(proposal)
});
const networkPrefix = network === 'testnet' ? 's-tn' : 's';
proposal.link = `https://${domain}/#/${networkPrefix}:${proposal.space.id}/proposal/${proposal.id}`;
Expand All @@ -435,7 +463,8 @@ export function formatVote(vote) {
verified: vote.spaceVerified,
turbo: vote.spaceTurbo,
flagged: vote.spaceFlagged,
hibernated: vote.spaceHibernated
hibernated: vote.spaceHibernated,
skinSettings: formatSkinSettings(vote)
});
return vote;
}
Expand All @@ -448,7 +477,8 @@ export function formatFollow(follow) {
verified: follow.spaceVerified,
turbo: follow.spaceTurbo,
flagged: follow.spaceFlagged,
hibernated: follow.spaceHibernated
hibernated: follow.spaceHibernated,
skinSettings: formatSkinSettings(follow)
});
return follow;
}
Expand All @@ -461,7 +491,8 @@ export function formatSubscription(subscription) {
verified: subscription.spaceVerified,
turbo: subscription.spaceTurbo,
flagged: subscription.spaceFlagged,
hibernated: subscription.spaceHibernated
hibernated: subscription.spaceHibernated,
skinSettings: formatSkinSettings(subscription)
});
return subscription;
}
Expand Down
6 changes: 5 additions & 1 deletion src/graphql/operations/follows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export default async function (parent, args) {
if (!['ASC', 'DESC'].includes(orderDirection)) orderDirection = 'DESC';

const query = `
SELECT f.*,
SELECT
f.*,
skins.*,
f.id AS id,
spaces.settings,
spaces.domain as spaceDomain,
spaces.flagged as spaceFlagged,
Expand All @@ -37,6 +40,7 @@ export default async function (parent, args) {
spaces.hibernated as spaceHibernated
FROM follows f
LEFT JOIN spaces ON spaces.id = f.space
LEFT JOIN skins ON spaces.id = skins.id
WHERE 1=1 ${queryStr}
ORDER BY ${orderBy} ${orderDirection} LIMIT ?, ?
`;
Expand Down
6 changes: 5 additions & 1 deletion src/graphql/operations/proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { formatProposal } from '../helpers';

export default async function (parent, { id }) {
const query = `
SELECT p.*,
SELECT
p.*,
skins.*,
p.id AS id,
spaces.settings,
spaces.domain as spaceDomain,
spaces.flagged as spaceFlagged,
Expand All @@ -14,6 +17,7 @@ export default async function (parent, { id }) {
spaces.hibernated as spaceHibernated
FROM proposals p
INNER JOIN spaces ON spaces.id = p.space
LEFT JOIN skins ON spaces.id = skins.id
WHERE p.id = ? AND spaces.settings IS NOT NULL
LIMIT 1
`;
Expand Down
6 changes: 5 additions & 1 deletion src/graphql/operations/proposals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ export default async function (parent, args) {
if (!['ASC', 'DESC'].includes(orderDirection)) orderDirection = 'DESC';

const query = `
SELECT p.*,
SELECT
p.*,
skins.*,
p.id AS id,
spaces.settings,
spaces.domain as spaceDomain,
spaces.flagged as spaceFlagged,
Expand All @@ -94,6 +97,7 @@ export default async function (parent, args) {
spaces.hibernated as spaceHibernated
FROM proposals p
INNER JOIN spaces ON spaces.id = p.space
LEFT JOIN skins ON spaces.id = skins.id
WHERE spaces.settings IS NOT NULL ${queryStr} ${searchSql}
ORDER BY ${orderBy} ${orderDirection}, p.id ASC LIMIT ?, ?
`;
Expand Down
6 changes: 5 additions & 1 deletion src/graphql/operations/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export default async function (parent, args) {
let subscriptions: any[] = [];

const query = `
SELECT s.*,
SELECT
s.*,
skins.*,
s.id AS id,
spaces.settings,
spaces.domain as spaceDomain,
spaces.flagged as spaceFlagged,
Expand All @@ -38,6 +41,7 @@ export default async function (parent, args) {
spaces.hibernated as spaceHibernated
FROM subscriptions s
INNER JOIN spaces ON spaces.id = s.space
LEFT JOIN skins ON spaces.id = skins.id
WHERE spaces.settings IS NOT NULL ${queryStr}
ORDER BY ${orderBy} ${orderDirection} LIMIT ?, ?
`;
Expand Down
6 changes: 5 additions & 1 deletion src/graphql/operations/votes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ async function query(parent, args, context?, info?) {
if (requestedFields.proposal && votes.length > 0) {
const proposalIds = votes.map(vote => vote.proposal);
const query = `
SELECT p.*,
SELECT
p.*,
skins.*,
p.id AS id,
spaces.settings,
spaces.domain as spaceDomain,
spaces.flagged as spaceFlagged,
Expand All @@ -96,6 +99,7 @@ async function query(parent, args, context?, info?) {
spaces.hibernated as spaceHibernated
FROM proposals p
INNER JOIN spaces ON spaces.id = p.space
LEFT JOIN skins ON spaces.id = skins.id
WHERE spaces.settings IS NOT NULL AND p.id IN (?)
`;
try {
Expand Down
12 changes: 12 additions & 0 deletions src/graphql/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ type Space {
network: String
symbol: String
skin: String
skinSettings: SkinSettings
domain: String
strategies: [Strategy]
admins: [String]
Expand Down Expand Up @@ -651,3 +652,14 @@ type Option {
name: String
value: String
}

type SkinSettings {
bg_color: String
link_color: String
text_color: String
content_color: String
border_color: String
heading_color: String
header_color: String
primary_color: String
}
12 changes: 12 additions & 0 deletions src/helpers/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,15 @@ CREATE TABLE options (
value VARCHAR(100) NOT NULL,
PRIMARY KEY (name)
);

CREATE TABLE skins (
id VARCHAR(100) NOT NULL,
bg_color VARCHAR(6) DEFAULT NULL,
link_color VARCHAR(6) DEFAULT NULL,
text_color VARCHAR(6) DEFAULT NULL,
border_color VARCHAR(6) DEFAULT NULL,
heading_color VARCHAR(6) DEFAULT NULL,
primary_color VARCHAR(6) DEFAULT NULL,
header_color VARCHAR(6) DEFAULT NULL,
PRIMARY KEY (id)
);

0 comments on commit ef0aa8a

Please sign in to comment.