Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 12-character game link support #2299

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
<data
android:host="lichess.org" android:scheme="https"
android:pathPattern="/......../white" />
<data
android:host="lichess.org" android:scheme="https"
android:pathPattern="/............" />
</intent-filter>

</activity>
Expand Down
6 changes: 5 additions & 1 deletion apple-app-site-association
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
"/": "/????????"
},
{
"/": "/????????/black"
"/": "/????????/white"
},
{
"/": "/????????/black"
},
{
"/": "/????????????"
},
{
"/": "/analysis"
},
Expand Down Expand Up @@ -76,6 +79,7 @@
"/????????",
"/????????/black",
"/????????/white",
"/????????????",
"/analysis",
"/analysis/*",
"/editor",
Expand Down
2 changes: 2 additions & 0 deletions deepLinkTest.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ <h1>Prod</h1>
<li><a href="https://lichess.org/2360tJXP/black#50">https://lichess.org/2360tJXP/black#50</a></li>
<li><a href="https://lichess.org/2360tJXP/black#50">https://lichess.org/2360tJXP/black#500</a></li>
<li><a href="https://lichess.org/2360tJXP/black#asdf">https://lichess.org/2360tJXP/black#asdf</a></li>
<li><a href="https://lichess.org/2360tJXP000Z#50">https://lichess.org/2360tJXP000Z#50</a></li>
<li><a href="https://lichess.org/2360tJXP000z#50">https://lichess.org/2360tJXP000z#50</a></li>
<li><a href="https://lichess.org/nalysis">https://lichess.org/analysis</a></li>
<li><a href="https://lichess.org/analysis/rn1qkbnr/pppb1ppp/4p3/3p4/3PP3/2N2N2/PPP2PPP/R1BQKB1R_b_KQkq_-_0_1">https://lichess.org/analysis/rn1qkbnr/pppb1ppp/4p3/3p4/3PP3/2N2N2/PPP2PPP/R1BQKB1R_b_KQkq_-_0_1</a></li>
<li><a href="https://lichess.org/editor">https://lichess.org/editor</a></li>
Expand Down
24 changes: 11 additions & 13 deletions src/deepLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import router from './router'
import session, { Session } from './session'
import signupModal from './ui/signupModal'
import { handleXhrError } from './utils'
import { extractGameReference } from './utils/gameId'
import { buildQueryString } from './utils/querystring'

const fenParams = ':r1/:r2/:r3/:r4/:r5/:r6/:r7/:r8'
Expand All @@ -15,28 +16,27 @@ function fenFromParams(params: any): string {

export default {
init() {
App.addListener('appUrlOpen', ({ url }) => {
void App.addListener('appUrlOpen', ({ url }) => {
setTimeout(() => {
const urlObject = new URL(url)
const path = urlObject.pathname
const matched = links.run(path)
if (!matched) {
// it can be a game or challenge but we want to do an exact regex match
const found = gamePattern.exec(path)
if (found) {
const color = found[2]
const plyMatch = plyHashPattern.exec(urlObject.hash)

const gameIdParts = extractGameReference(urlObject)
if (gameIdParts !== null) {
const {color, gameId, ply} = gameIdParts
const queryParams = {} as Record<string, string>
if (color) {
queryParams['color'] = color.substring(1)

if (color !== undefined) {
queryParams['color'] = color
}
if (plyMatch) {
queryParams['ply'] = plyMatch[1]
if (ply !== undefined) {
queryParams['ply'] = ply
}

const queryString = buildQueryString(queryParams)
router.set(`/game/${found[1]}?${queryString}`)
router.set(`/game/${gameId}?${queryString}`)
} else {
console.warn('Could not handle deep link', path)
}
Expand All @@ -47,8 +47,6 @@ export default {
}

const links = new Rlite()
const gamePattern = /^\/(\w{8})(\/black|\/white)?$/
const plyHashPattern = /^#(\d+)$/

links.add('analysis', () => router.set('/analyse'))
links.add(`analysis/${fenParams}`, ({ params }) => {
Expand Down
22 changes: 22 additions & 0 deletions src/utils/__tests__/gameId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as gameId from '../gameId'

describe.each([
{url: 'https://lichess.org/DABBAD00yyya', id: 'DABBAD00', color: 'black', ply: undefined},
{url: 'https://lichess.org/DABBAD00yyy0', id: 'DABBAD00', color: 'white', ply: undefined},
{url: 'https://lichess.org/DABBAD00yyyB#123', id: 'DABBAD00', color: 'white', ply: '123'},
{url: 'https://lichess.org/DABBAD00', id: 'DABBAD00', color: undefined, ply: undefined},
{url: 'https://lichess.org/DABBAD00/black', id: 'DABBAD00', color: 'black', ply: undefined},
{url: 'https://lichess.org/DABBAD00/white', id: 'DABBAD00', color: 'white', ply: undefined},
{url: 'https://lichess.org/DABBAD00#123', id: 'DABBAD00', color: undefined, ply: '123'},
{url: 'https://lichess.org/DABBAD00/black#123', id: 'DABBAD00', color: 'black', ply: '123'},
])('extractGameIdParts', ({url, id, color, ply}) => {
it(`extracts correctly from ${url}`, () => {
const urlObject = new URL(url)
const gamePointer = gameId.extractGameReference(urlObject)
expect(gamePointer).toEqual<gameId.GameReference>({
gameId: id,
color: color,
ply: ply,
})
})
})
37 changes: 37 additions & 0 deletions src/utils/gameId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const blackSuffixPattern = /[5-9a-z]/
const gamePattern = /^\/(\w{8})(\/black|\/white)?$/
const fullGamePattern = /^\/(\w{8})(\w{3}[0-9a-zA-Z])$/
const plyHashPattern = /^#(\d+)$/

export type GameReference = {
color: string | undefined,
gameId: string,
ply: string | undefined,
}

export function extractGameReference(urlObject: URL): GameReference | null {
const path = urlObject.pathname
const gameFound = gamePattern.exec(path)
const ply = plyHashPattern.exec(urlObject.hash)?.[1]

if (gameFound) {
return {
gameId: gameFound[1],
color: gameFound[2]?.substring(1),
ply: ply,
}
}

const fullGameFound = fullGamePattern.exec(path)
if (fullGameFound) {
const gameId = fullGameFound[1]
const color = blackSuffixPattern.test(fullGameFound[2].charAt(3)) ? 'black' : 'white'
return {
gameId: gameId,
color: color,
ply: ply,
}
}

return null
}