generated from beefchimi/template-common
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ [Discord] Broaden support for URL variations
- Loading branch information
Showing
12 changed files
with
434 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,26 @@ | ||
import {profileReplacement} from '../capture'; | ||
import {discordPreferredUrls} from '../capture'; | ||
import type {SocialiteNetwork} from '../types'; | ||
|
||
// Discord is difficult to solve given Socialite's current design. | ||
// There are essentially 3 different urls to support: | ||
// 1. User profiles (discordapp.com/users/*) | ||
// 2. Server/channel urls (discord.com/channels/{serverid}/{channelid}) | ||
// 3. Official vanity urls (discord.gg/*) | ||
// Since we are not yet validating against a Top-level domain (.gg), | ||
// any `discord` url validates as true and captures the `path`. | ||
// This degrades the confidence provided by `users` or `channels`. | ||
|
||
// TODO: Solve this problem by improving `preferredUrl` and parsing criteria. | ||
// https://github.com/beefchimi/socialite/issues/35 | ||
export const discord: SocialiteNetwork = { | ||
id: 'discord', | ||
preferredUrl: `https://discordapp.com/users/${profileReplacement.user}`, | ||
preferredUrl: discordPreferredUrls.users, | ||
matcher: { | ||
domain: /discord/, | ||
user: /^(?:\/users\/)([^/]+)/, | ||
user: /^\/(?:users\/|channels\/)?([^/]+)/, | ||
// TODO: If we want to support capturing EVERYTHING after | ||
// the first `/` (necessary for capturing the `channelid`), | ||
// then we would need to use the following: | ||
// user: /^\/(?:users\/|channels\/)?(.+)/, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,114 @@ | ||
import {Socialite} from '../../socialite'; | ||
import type {SocialiteProfile} from '../../types'; | ||
import {allSocialiteNetworks, mockGenericUser} from '../../tests/fixtures'; | ||
import { | ||
allSocialiteNetworks, | ||
discordValidUrls, | ||
mockGenericUser, | ||
} from '../../tests/fixtures'; | ||
import {discord} from '../discord'; | ||
|
||
describe('Social networks > discord', () => { | ||
const mockSocialite = new Socialite(allSocialiteNetworks); | ||
const mockCommonUrl = `https://www.discordapp.com/users/${mockGenericUser}`; | ||
|
||
it('returns expected `id` and `user` from common url', () => { | ||
const {id, user} = mockSocialite.parseProfile( | ||
mockCommonUrl, | ||
) as SocialiteProfile; | ||
// TODO: We want to resolve these in the future | ||
// https://github.com/beefchimi/socialite/issues/35 | ||
describe('bogus', () => { | ||
it('mistakenly returns the first path match for any `discord` url', () => { | ||
const mockPageSlug = 'about-us-page'; | ||
const mockBogusUrl = `https://www.discord.com/${mockPageSlug}`; | ||
|
||
expect(id).toBe(discord.id); | ||
expect(user).toBe(mockGenericUser); | ||
const {id, user} = mockSocialite.parseProfile( | ||
mockBogusUrl, | ||
) as SocialiteProfile; | ||
|
||
expect(id).toBe(discord.id); | ||
expect(user).toBe(mockPageSlug); | ||
}); | ||
}); | ||
|
||
describe('users', () => { | ||
const mockUsersUrl = `https://www.discordapp.com/users/${mockGenericUser}`; | ||
|
||
it('returns `id` and `user`', () => { | ||
const {id, user} = mockSocialite.parseProfile( | ||
mockUsersUrl, | ||
) as SocialiteProfile; | ||
|
||
expect(id).toBe(discord.id); | ||
expect(user).toBe(mockGenericUser); | ||
}); | ||
|
||
it('omits any trailing path after the first `user` match', () => { | ||
const mockUsersTrailingUrl = `${mockUsersUrl}/trail-123`; | ||
|
||
const {id, user} = mockSocialite.parseProfile( | ||
mockUsersTrailingUrl, | ||
) as SocialiteProfile; | ||
|
||
expect(id).toBe(discord.id); | ||
expect(user).toBe(mockGenericUser); | ||
}); | ||
}); | ||
|
||
it('returns expected `id` and `user` from url with trailing path', () => { | ||
const mockUncommonUrl = `${mockCommonUrl}/trail-123`; | ||
const {id, user} = mockSocialite.parseProfile( | ||
mockUncommonUrl, | ||
) as SocialiteProfile; | ||
describe('channels', () => { | ||
const mockChannelsUrl = `https://www.discord.com/channels/${mockGenericUser}`; | ||
|
||
it('returns `id` and `user`', () => { | ||
const {id, user} = mockSocialite.parseProfile( | ||
mockChannelsUrl, | ||
) as SocialiteProfile; | ||
|
||
expect(id).toBe(discord.id); | ||
expect(user).toBe(mockGenericUser); | ||
}); | ||
|
||
it('omits any trailing path after the first `user` match', () => { | ||
const mockChannelsTrailingUrl = `${mockChannelsUrl}/trail-123`; | ||
|
||
const {id, user} = mockSocialite.parseProfile( | ||
mockChannelsTrailingUrl, | ||
) as SocialiteProfile; | ||
|
||
expect(id).toBe(discord.id); | ||
expect(user).toBe(mockGenericUser); | ||
}); | ||
}); | ||
|
||
describe('vanity', () => { | ||
const mockVanityUrl = `https://discord.gg/${mockGenericUser}`; | ||
|
||
it('returns `id` and `user`', () => { | ||
const {id, user} = mockSocialite.parseProfile( | ||
mockVanityUrl, | ||
) as SocialiteProfile; | ||
|
||
expect(id).toBe(discord.id); | ||
expect(user).toBe(mockGenericUser); | ||
}); | ||
|
||
it('omits any trailing path after the first `user` match', () => { | ||
const mockVanityTrailingUrl = `${mockVanityUrl}/trail-123`; | ||
|
||
const {id, user} = mockSocialite.parseProfile( | ||
mockVanityTrailingUrl, | ||
) as SocialiteProfile; | ||
|
||
expect(id).toBe(discord.id); | ||
expect(user).toBe(mockGenericUser); | ||
expect(id).toBe(discord.id); | ||
expect(user).toBe(mockGenericUser); | ||
}); | ||
}); | ||
|
||
it('returns `id` with no `user` when provided an unrecognized leading path', () => { | ||
const mockUnsupportedUrl = `https://discordapp.com/foo/${mockGenericUser}`; | ||
const match = mockSocialite.parseProfile( | ||
mockUnsupportedUrl, | ||
) as SocialiteProfile; | ||
describe('all variations', () => { | ||
it('returns `id` and `user`', () => { | ||
discordValidUrls.forEach(({originalUrl, preferredUrl, user}) => { | ||
const match = mockSocialite.parseProfile( | ||
originalUrl, | ||
) as SocialiteProfile; | ||
|
||
expect(match.id).toBe(discord.id); | ||
expect(match.user).toBeUndefined(); | ||
expect(match.id).toBe(discord.id); | ||
expect(match.preferredUrl).toBe(preferredUrl); | ||
expect(match.user).toBe(user); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.