Skip to content

Commit

Permalink
Improve Discord bot config
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpolman committed Jun 11, 2024
1 parent 5a25b21 commit b568c84
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 211 deletions.
4 changes: 2 additions & 2 deletions apps/api/scripts/script.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import db from '@thxnetwork/api/util/database';
import main from './src/veLiquidity';
// import main from './src/veLiquidity';
// import main from './src/veTransfer';
// import main from './src/veRewards';
// import main from './src/time';
Expand All @@ -8,7 +8,7 @@ import main from './src/veLiquidity';
// import main from './src/vethx';
// import main from './src/safe';
// import main from './src/ipfs';
// import main from './src/invoices';
import main from './src/invoices';
// import main from './src/demo';
// import main from './src/preview';
// import main from './src/metamask';
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/app/controllers/pools/guilds/patch.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import * as CreateController from './post.controller';
const validation = [param('guildId').optional().isMongoId(), ...CreateController.validation];

const controller = async (req: Request, res: Response) => {
const { secret, adminRoleId, channelId } = req.body;
const { secret, adminRoleId, channelId, notifications } = req.body;
const guild = await DiscordGuild.findByIdAndUpdate(
req.params.guildId,
{ secret, channelId, adminRoleId, poolId: req.params.id },
{ secret, channelId, adminRoleId, poolId: req.params.id, notifications },
{ new: true },
);
const result = await DiscordDataProxy.getGuild({ ...guild.toJSON(), isConnected: true });
Expand Down
13 changes: 9 additions & 4 deletions apps/api/src/app/controllers/pools/guilds/post.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@ import { body, param } from 'express-validator';

const validation = [
param('id').isMongoId(),
body('settings.channelId').optional().isString(),
body('settings.adminRoleId').optional().isString(),
body('notifications.questCreated.isDisabled').optional().isBoolean(),
body('notifications.questCreated.message').optional().isString(),
body('notifications.questCreated.channelId').optional().isString(),
body('notifications.questEntryCreated.isDisabled').optional().isBoolean(),
body('notifications.questEntryCreated.message').optional().isString(),
body('notifications.questEntryCreated.channelId').optional().isString(),
];

const controller = async (req: Request, res: Response) => {
const { guildId, name, adminRoleId, channelId } = req.body;
const { guildId, name, adminRoleId, notifications } = req.body;
const guild = await DiscordGuild.create({
poolId: req.params.id,
sub: req.auth.sub,
guildId,
name,
channelId,
adminRoleId,
poolId: req.params.id,
notifications,
});
const result = await DiscordDataProxy.getGuild({ ...guild.toJSON(), isConnected: true });

Expand Down
6 changes: 0 additions & 6 deletions apps/api/src/app/controllers/pools/patch.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ const validation = [
body('settings.isArchived').optional().isBoolean(),
body('settings.isPublished').optional().isBoolean(),
body('settings.isWeeklyDigestEnabled').optional().isBoolean(),
body('settings.isTwitterSyncEnabled').optional().isBoolean(),
body('settings.defaults.conditionalRewards.title').optional().isString(),
body('settings.defaults.conditionalRewards.description').optional().isString(),
body('settings.defaults.conditionalRewards.amount').optional().isInt(),
body('settings.defaults.conditionalRewards.hashtag').optional().isString(),
body('settings.defaults.conditionalRewards.isPublished').optional().isBoolean(),
body('settings.authenticationMethods').optional().isArray(),
];

Expand Down
61 changes: 4 additions & 57 deletions apps/api/src/app/controllers/widget/js/get.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { API_URL, AUTH_URL, DASHBOARD_URL, NODE_ENV, WIDGET_URL } from '@thxnetwork/api/config/secrets';
import BrandService from '@thxnetwork/api/services/BrandService';
import PoolService from '@thxnetwork/api/services/PoolService';
import { QuestInvite } from '@thxnetwork/api/models/QuestInvite';
import { Widget } from '@thxnetwork/api/models/Widget';
import { NotFoundError } from '@thxnetwork/api/util/errors';
import { Request, Response } from 'express';
Expand All @@ -15,25 +14,9 @@ const validation = [
];

const controller = async (req: Request, res: Response) => {
// #swagger.tags = ['Widget']
const referralRewards = await QuestInvite.find({
poolId: req.params.id,
});
const refs = JSON.stringify(
referralRewards
.filter((r) => r.successUrl)
.map((r) => {
return {
uuid: r.uuid,
successUrl: r.successUrl,
};
}),
);

const pool = await PoolService.getById(req.params.id);
if (!pool) throw new NotFoundError('Pool not found.');

const expired = pool.settings.endDate ? pool.settings.endDate.getTime() <= Date.now() : false;
const brand = await BrandService.get(pool._id);
const widget = await Widget.findOne({ poolId: req.params.id });
const referrerHeader = req.header('Referrer');
Expand Down Expand Up @@ -137,7 +120,6 @@ if (typeof window.THXWidget !== 'undefined') {
}
onLoad() {
this.referrals = JSON.parse(this.settings.refs).filter((r) => r.successUrl);
this.iframe = this.createIframe();
if (this.settings.isPublished) {
Expand All @@ -147,21 +129,10 @@ if (typeof window.THXWidget !== 'undefined') {
this.container = this.createContainer(this.iframe, this.launcher, this.message);
}
this.parseURL();
window.matchMedia('(max-width: 990px)').addListener(this.onMatchMedia.bind(this));
window.onmessage = this.onMessage.bind(this);
}
parseURL() {
const url = new URL(window.location.href)
this.ref = url.searchParams.get('ref');
if (!this.ref) return;
this.successUrls = this.referrals.map((r) => r.successUrl);
if (!this.successUrls.length) return;
}
get isSmallMedia() {
const getWidth = () => window.innerWidth;
return getWidth() < this.MD_BREAKPOINT;
Expand All @@ -170,7 +141,7 @@ if (typeof window.THXWidget !== 'undefined') {
createURL() {
const parentUrl = new URL(window.location.href)
const path = parentUrl.searchParams.get('thx_widget_path');
const { widgetUrl, poolId, chainId, theme, expired, logoUrl, backgroundUrl, title } = this.settings;
const { widgetUrl, poolId, chainId, theme, logoUrl, backgroundUrl, title } = this.settings;
const url = new URL(widgetUrl);
url.pathname = this.widgetPath = '/c/' + poolId + (path || '/quests');
Expand All @@ -180,7 +151,7 @@ if (typeof window.THXWidget !== 'undefined') {
}
createIframe() {
const { widgetUrl, poolId, chainId, theme, align, expired, containerSelector } = this.settings;
const { widgetUrl, poolId, chainId, theme, align, containerSelector } = this.settings;
const iframe = document.createElement('iframe');
const styles = this.isSmallMedia ? this.defaultStyles['sm'] : this.defaultStyles['md'];
const url = this.createURL();
Expand Down Expand Up @@ -427,15 +398,7 @@ if (typeof window.THXWidget !== 'undefined') {
}
return container;
}
storeRef(ref) {
if (!ref) return;
window.localStorage.setItem('thx:widget:' + this.settings.poolId + ':ref', ref);
this.iframe.contentWindow.postMessage({ message: 'thx.config.ref', ref }, this.settings.widgetUrl);
this.timer = window.setInterval(this.onURLDetectionCallback.bind(this), 500);
}
onMessage(event) {
if (event.origin !== this.settings.widgetUrl) return;
const { message, amount, isAuthenticated, url } = event.data;
Expand Down Expand Up @@ -489,7 +452,6 @@ if (typeof window.THXWidget !== 'undefined') {
const widgetPath = parentUrl.searchParams.get('thx_widget_path');
this.open(widgetPath);
this.storeRef(this.ref);
if (this.settings.identity) {
this.setIdentity(this.settings.identity)
Expand All @@ -509,20 +471,6 @@ if (typeof window.THXWidget !== 'undefined') {
if (shouldShow) this.message.remove();
}
onURLDetectionCallback() {
for (const ref of this.referrals) {
if (!(this.successUrls.filter((url) => url.includes(window.location.origin + window.location.pathname))).length) continue;
this.iframe.contentWindow.postMessage({ message: 'thx.referral.claim.create', uuid: ref.uuid, }, this.settings.widgetUrl);
const index = this.referrals.findIndex((r) => ref.uuid);
this.referrals.splice(index, 1);
}
if (!this.referrals.length) {
window.clearInterval(this.timer);
}
}
onMatchMedia(x) {
if (x.matches) {
Expand All @@ -534,6 +482,7 @@ if (typeof window.THXWidget !== 'undefined') {
}
}
}
window.THXWidget = new THXWidget({
apiUrl: '${API_URL}',
isPublished: window.location.origin.includes("${DASHBOARD_URL}") || ${widget.isPublished},
Expand All @@ -548,8 +497,6 @@ if (typeof window.THXWidget !== 'undefined') {
message: '${widget.message || ''}',
align: '${widget.align || 'right'}',
theme: '${widget.theme}',
refs: ${JSON.stringify(refs)},
expired: '${expired}',
identity: '${req.query.identity || ''}',
containerSelector: '${req.query.containerSelector || ''}'
});
Expand Down
99 changes: 0 additions & 99 deletions apps/api/src/app/jobs/createTwitterQuests.ts

This file was deleted.

23 changes: 23 additions & 0 deletions apps/api/src/app/migrations/20240611104442-discord-guilds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = {
async up(db, client) {
for (const guild of await db.collection('discordguild').find({}).toArray()) {
await db.collection('discordguild').updateOne(
{ _id: guild._id },
{
$set: {
notifications: {
questCreate: { isEnabled: true, message: '', channelId: guild.channelId },
questEntryCreate: { isEnabled: true, message: '', channelId: guild.channelId },
},
},
},
);
}
},

async down(db, client) {
// TODO write the statements to rollback your migration (if possible)
// Example:
// await db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: false}});
},
};
14 changes: 13 additions & 1 deletion apps/api/src/app/models/DiscordGuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,22 @@ export const DiscordGuild = mongoose.model<DiscordGuildDocument>(
sub: String,
poolId: String,
guildId: String,
channelId: String,
// channelId: String,
adminRoleId: String,
name: String,
secret: String,
notifications: {
questCreate: {
isEnabled: Boolean,
message: String,
channelId: String,
},
questEntryCreate: {
isEnabled: Boolean,
message: String,
channelId: String,
},
},
},
{
timestamps: true,
Expand Down
17 changes: 1 addition & 16 deletions apps/api/src/app/models/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,10 @@ const schema = new mongoose.Schema(
title: String,
slug: String,
description: String,
startDate: Date,
endDate: Date,
galachainPrivateKey: String,
isArchived: Boolean,
isPublished: { type: Boolean, default: false },
isWeeklyDigestEnabled: { type: Boolean, default: true },
isTwitterSyncEnabled: { type: Boolean, default: false },
discordWebhookUrl: String,
galachainPrivateKey: String,
defaults: {
discordMessage: String,
conditionalRewards: {
title: String,
description: String,
amount: Number,
hashtag: String,
isPublished: { type: Boolean, default: false },
locks: { type: [{ questId: String, variant: Number }], default: [] },
},
},
authenticationMethods: [Number],
},
},
Expand Down
Loading

0 comments on commit b568c84

Please sign in to comment.