-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jack Works <zjwpeter@gmail.com>
- Loading branch information
1 parent
b92f7d5
commit 5f22449
Showing
5 changed files
with
91 additions
and
35 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* Not implementing this now. | ||
*/ | ||
export default undefined |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
type Identifiers = 'person' | 'group' | 'post' | ||
const fromString = Symbol() | ||
function noSlash(str: string) { | ||
if (str.split('/')[1]) throw new TypeError('Cannot contain / in a part of identifier') | ||
} | ||
export abstract class Identifier { | ||
abstract toString(): string | ||
static fromString(id: string): Identifier | null { | ||
const [type, ...rest] = id.split(':') as [Identifiers, string] | ||
switch (type) { | ||
case 'person': | ||
return PersonIdentifier[fromString](rest.join('')) | ||
case 'group': | ||
return GroupIdentifier[fromString](rest.join('')) | ||
case 'post': | ||
return PostIdentifier[fromString](rest.join('')) | ||
default: | ||
return null | ||
} | ||
} | ||
} | ||
export class PersonIdentifier extends Identifier { | ||
/** | ||
* @param network - Network belongs to | ||
* @param userId - User ID | ||
*/ | ||
constructor(public network: string, public userId: string) { | ||
super() | ||
noSlash(network) | ||
noSlash(userId) | ||
} | ||
toString() { | ||
return `person:${this.network}/${this.userId}` | ||
} | ||
static [fromString](str: string) { | ||
const [network, userId] = str.split('/') | ||
if (!network || !userId) return null | ||
return new PersonIdentifier(network, userId) | ||
} | ||
} | ||
export class GroupIdentifier extends Identifier { | ||
constructor(public network: string, public groupId: string, public virtual = false) { | ||
super() | ||
noSlash(network) | ||
noSlash(groupId) | ||
} | ||
toString() { | ||
return `group:${this.network}/${this.groupId}/${this.virtual ? 'virtual' : 'real'}` | ||
} | ||
static [fromString](str: string) { | ||
const [network, groupId, virtual] = str.split('/') | ||
if (!network || !groupId || !virtual) return null | ||
return new GroupIdentifier(network, groupId, virtual === 'virtual') | ||
} | ||
} | ||
export class PostIdentifier extends Identifier { | ||
/** | ||
* If identifier is a PostIdentifier, that means this post is binded with other post in some kind | ||
* e.g. a comment. | ||
*/ | ||
constructor(public identifier: Identifier, public postId: string) { | ||
super() | ||
noSlash(postId) | ||
} | ||
toString() { | ||
return `post:${this.postId}/${this.identifier.toString()}` | ||
} | ||
static [fromString](str: string) { | ||
const [postId, ...identifier] = str.split('/') | ||
const id = Identifier.fromString(identifier.join('/')) | ||
if (!id || !postId) return null | ||
return new PostIdentifier(id, postId) | ||
} | ||
} |