Skip to content

Commit

Permalink
[nostr] Fix community source link (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
grunch authored May 13, 2024
1 parent 6ee4368 commit a56f94b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
12 changes: 7 additions & 5 deletions bot/modules/community/scenes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ const { Scenes } = require('telegraf');
const { logger } = require('../../../logger');
const { Community, User, PendingPayment } = require('../../../models');
const { isPendingPayment } = require('../../../ln');
const { isGroupAdmin, itemsFromMessage } = require('../../../util');
const {
isGroupAdmin,
itemsFromMessage,
removeAtSymbol,
} = require('../../../util');
const messages = require('../../messages');
const { isValidInvoice } = require('../../validations');
const {
Expand Down Expand Up @@ -351,8 +355,7 @@ const createCommunitySteps = {
const usernames = itemsFromMessage(text);
if (usernames.length > 0 && usernames.length < 10) {
for (let i = 0; i < usernames.length; i++) {
const username =
usernames[i][0] == '@' ? usernames[i].slice(1) : usernames[i];
const username = removeAtSymbol(usernames[i]);
const user = await User.findOne({ username });
if (user) {
solvers.push({
Expand Down Expand Up @@ -699,8 +702,7 @@ exports.updateSolversCommunityWizard = new Scenes.WizardScene(

if (usernames.length > 0 && usernames.length < 10) {
for (let i = 0; i < usernames.length; i++) {
const username =
usernames[i][0] == '@' ? usernames[i].slice(1) : usernames[i];
const username = removeAtSymbol(usernames[i]);
const user = await User.findOne({ username });
if (user) {
solvers.push({
Expand Down
3 changes: 2 additions & 1 deletion bot/modules/dispute/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const messages = require('./messages');
const globalMessages = require('../../messages');
const { logger } = require('../../../logger');
const OrderEvents = require('../../modules/events/orders');
const { removeAtSymbol } = require('../../../util');

const dispute = async ctx => {
try {
Expand Down Expand Up @@ -98,7 +99,7 @@ const deleteDispute = async ctx => {
if (!order) {
return await globalMessages.notActiveOrderMessage(ctx);
}
username = username[0] == '@' ? username.slice(1) : username;
username = removeAtSymbol(username);
const user = await User.findOne({ username });
if (!user) {
return await globalMessages.notFoundUserMessage(ctx);
Expand Down
10 changes: 4 additions & 6 deletions bot/modules/nostr/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { finalizeEvent, verifyEvent } = require('nostr-tools/pure');
const Config = require('./config');

const { Community } = require('../../../models');
const { toKebabCase } = require('../../../util');
const { toKebabCase, removeAtSymbol } = require('../../../util');

/// All events broadcasted are Parameterized Replaceable Events,
/// the event kind must be between 30000 and 39999
Expand All @@ -16,10 +16,7 @@ const orderToTags = async order => {
if (order.fiat_amount === undefined) {
fiat_amount = `${order.min_amount}-${order.max_amount}`;
}
const channel =
process.env.CHANNEL[0] === '@'
? process.env.CHANNEL.slice(1)
: process.env.CHANNEL;
const channel = removeAtSymbol(process.env.CHANNEL);
let source = `https://t.me/${channel}/${order.tg_channel_message1}`;
const tags = [];
tags.push(['d', order.id]);
Expand All @@ -32,7 +29,8 @@ const orderToTags = async order => {
tags.push(['premium', order.price_margin.toString()]);
if (order.community_id) {
const community = await Community.findById(order.community_id);
source = `https://t.me/${community.group}/${order.tg_channel_message1}`;
const group = removeAtSymbol(community.group);
source = `https://t.me/${group}/${order.tg_channel_message1}`;
tags.push(['community_id', order.community_id]);
}
tags.push(['source', source]);
Expand Down
7 changes: 3 additions & 4 deletions bot/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const {
Dispute,
Config,
} = require('../models');
const { getCurrenciesWithPrice, deleteOrderFromChannel } = require('../util');
const { getCurrenciesWithPrice, deleteOrderFromChannel, removeAtSymbol } = require('../util');
const {
commandArgsMiddleware,
stageMiddleware,
Expand Down Expand Up @@ -65,7 +65,6 @@ const {
nodeInfo,
} = require('../jobs');
const { logger } = require('../logger');

export interface MainContext extends Context {
match: Array<string> | null;
i18n: I18nContext;
Expand Down Expand Up @@ -591,7 +590,7 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont

if (!username) return;

username = username[0] == '@' ? username.slice(1) : username;
username = removeAtSymbol(username);
const user = await User.findOne({
$or: [{ username }, { tg_id: username }],
});
Expand Down Expand Up @@ -634,7 +633,7 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont

if (!username) return;

username = username[0] == '@' ? username.slice(1) : username;
username = removeAtSymbol(username);
const user = await User.findOne({
$or: [{ username }, { tg_id: username }],
});
Expand Down
4 changes: 4 additions & 0 deletions util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,3 +488,7 @@ exports.getStars = (rate, totalReviews) => {

return `${roundedRating} ${stars} (${totalReviews})`;
};

exports.removeAtSymbol = text => {
return text[0] === '@' ? text.slice(1) : text;
};

0 comments on commit a56f94b

Please sign in to comment.