Skip to content

Commit

Permalink
refactor: tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
beeman committed Sep 27, 2024
1 parent c45f083 commit 5bcbd67
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
12 changes: 6 additions & 6 deletions anchor/programs/pubkey-protocol/src/state/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ impl Community {

// Create a Link struct and validate method
let social_links = vec![
Link::new("x", self.x.as_deref()),
Link::new("discord", self.discord.as_deref()),
Link::new("github", self.github.as_deref()),
Link::new("website", self.website.as_deref()),
Link::new("farcaster", self.farcaster.as_deref()),
Link::new("github", self.github.as_deref()),
Link::new("telegram", self.telegram.as_deref()),
Link::new("website", self.website.as_deref()),
Link::new("x", self.x.as_deref()),
];

for link in social_links {
Expand All @@ -110,12 +110,12 @@ impl Community {
pub fn validate(&self) -> Result<()> {
if let Some(url) = self.url {
match self.link_type {
"x" => require!(is_valid_x(url), PubkeyProfileError::InvalidXURL),
"discord" => require!(is_valid_discord(url), PubkeyProfileError::InvalidDiscordURL),
"github" => require!(is_valid_github(url), PubkeyProfileError::InvalidGitHubURL),
"website" => require!(is_valid_url(url), PubkeyProfileError::InvalidWebsiteURL),
"farcaster" => require!(is_valid_farcaster(url), PubkeyProfileError::InvalidFarcasterURL),
"github" => require!(is_valid_github(url), PubkeyProfileError::InvalidGitHubURL),
"telegram" => require!(is_valid_telegram(url), PubkeyProfileError::InvalidTelegramURL),
"website" => require!(is_valid_url(url), PubkeyProfileError::InvalidWebsiteURL),
"x" => require!(is_valid_x(url), PubkeyProfileError::InvalidXURL),
_ => return Err(PubkeyProfileError::InvalidProviderID.into()),
}
}
Expand Down
19 changes: 9 additions & 10 deletions anchor/programs/pubkey-protocol/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use crate::errors::*;
use crate::id;

pub fn is_valid_username(username: &str) -> bool {
if username.len() < 3 || username.len() > 30 {
if username.len() < 3 || username.len() > 20 {
return false;
}

if !username
.chars()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_' || c == '-')
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_')
{
return false;
}
Expand Down Expand Up @@ -58,6 +58,10 @@ pub fn is_valid_url(url: &str) -> bool {
valid_scheme && valid_path && valid_domain
}

pub fn is_valid_farcaster(link: &str) -> bool {
link.starts_with("https://warpcast.com/")
}

pub fn is_valid_discord(link: &str) -> bool {
link.starts_with("https://discord.com/invite/") || link.starts_with("https://discord.gg/")
}
Expand All @@ -66,17 +70,12 @@ pub fn is_valid_github(link: &str) -> bool {
link.starts_with("https://github.com/")
}

pub fn is_valid_x(link: &str) -> bool {
link.starts_with("https://twitter.com/") || link.starts_with("https://x.com/")
}

pub fn is_valid_farcaster(link: &str) -> bool {
link.starts_with("https://warpcast.com/")
}

pub fn is_valid_telegram(link: &str) -> bool {
link.starts_with("https://t.me/") || link.starts_with("https://telegram.me/")
}
pub fn is_valid_x(link: &str) -> bool {
link.starts_with("https://twitter.com/") || link.starts_with("https://x.com/")
}

pub fn realloc_account<'a>(
account: AccountInfo<'a>,
Expand Down
14 changes: 5 additions & 9 deletions anchor/tests/pubkey-protocol.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import { getPubKeyPointerPda, getPubKeyProfilePda, PubKeyIdentityProvider } from
import { PubkeyProtocol } from '../target/types/pubkey_protocol'

function unique(str: string) {
return `${str}_${Math.random()
.toString(36)
.replace(/[^a-z]+/g, '')
.substring(0, 13)}`
return `${str}_${Math.random().toString(36).substring(2, 15)}`
}

function getAvatarUrl(username: string) {
Expand Down Expand Up @@ -253,8 +250,7 @@ describe('pubkey-protocol', () => {

describe('Community', () => {
let testCommunity: anchor.web3.PublicKey
const slug = unique('test-community')
console.log('***SLUG***', slug)
const slug = unique('acme')

async function createTestCommunity(slug: string) {
const PREFIX = 'pubkey_protocol'
Expand All @@ -265,7 +261,7 @@ describe('pubkey-protocol', () => {
program.programId,
)

const createCommunityArgs = {
const createCommunityInput = {
slug,
name: 'Test Community',
avatarUrl: getAvatarUrl(slug),
Expand All @@ -278,7 +274,7 @@ describe('pubkey-protocol', () => {
}

await program.methods
.createCommunity(createCommunityArgs)
.createCommunity(createCommunityInput)
.accountsStrict({
community,
authority: communityAuthority.publicKey,
Expand All @@ -297,7 +293,7 @@ describe('pubkey-protocol', () => {

it('Create Community', async () => {
const communityAccount = await program.account.community.fetch(testCommunity)
console.log('communityAccount', communityAccount)

expect(communityAccount.slug).toEqual(slug)
expect(communityAccount.name).toEqual('Test Community')
expect(communityAccount.avatarUrl).toEqual(getAvatarUrl(slug))
Expand Down

0 comments on commit 5bcbd67

Please sign in to comment.