Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Chatterbox
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mirrored_repos
element-hq
Chatterbox
Commits
81a850fc
Commit
81a850fc
authored
3 years ago
by
Midhun Suresh
Browse files
Options
Downloads
Patches
Plain Diff
Refactor Chatterbox class from main.ts
parent
017ceee9
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/Chatterbox.ts
+48
-0
48 additions, 0 deletions
src/Chatterbox.ts
src/main.ts
+9
-39
9 additions, 39 deletions
src/main.ts
src/random.ts
+10
-0
10 additions, 0 deletions
src/random.ts
with
67 additions
and
39 deletions
src/Chatterbox.ts
0 → 100644
+
48
−
0
View file @
81a850fc
import
{
IChatterboxConfig
}
from
"
./types/IChatterboxConfig
"
;
import
{
Hydrogen
}
from
"
./Hydrogen
"
;
import
{
generateRandomString
}
from
"
./random
"
;
export
class
Chatterbox
{
private
_config
:
IChatterboxConfig
;
private
_hydrogen
:
Hydrogen
;
constructor
(
config
:
IChatterboxConfig
,
root
:
HTMLDivElement
)
{
this
.
_config
=
config
;
this
.
_hydrogen
=
new
Hydrogen
(
this
.
_homeserver
,
root
);
}
async
start
():
Promise
<
void
>
{
console
.
log
(
"
Checking if session already exists
"
);
const
sessionAlreadyExists
=
await
this
.
_hydrogen
.
attemptStartWithExistingSession
();
if
(
sessionAlreadyExists
)
{
console
.
log
(
"
Starting hydrogen with existing session
"
);
}
else
{
console
.
log
(
"
Session does not exist!
"
);
await
this
.
_registerAndLogin
();
}
console
.
log
(
"
Attempting to mount Timeline
"
);
await
this
.
_hydrogen
.
mountTimeline
(
this
.
_roomToJoin
);
console
.
log
(
"
Mounted Timeline
"
);
}
private
async
_registerAndLogin
():
Promise
<
void
>
{
const
username
=
generateRandomString
(
7
);
const
password
=
generateRandomString
(
10
);
console
.
log
(
`Attempting to register with username =
${
username
}
and password =
${
password
}
`
);
await
this
.
_hydrogen
.
register
(
username
,
password
,
"
Chatterbox
"
);
console
.
log
(
"
Registration done
"
);
console
.
log
(
"
Attempting to login with same credentials
"
);
await
this
.
_hydrogen
.
login
(
username
,
password
);
console
.
log
(
"
Login successful
"
);
}
private
get
_homeserver
():
string
{
return
this
.
_config
.
homeserver
;
}
private
get
_roomToJoin
():
string
{
return
this
.
_config
.
auto_join_room
;
}
}
This diff is collapsed.
Click to expand it.
src/main.ts
+
9
−
39
View file @
81a850fc
import
{
Hydrogen
}
from
"
./
Hydrogen
"
;
import
{
Chatterbox
}
from
"
./
Chatterbox
"
;
import
type
{
IChatterboxConfig
}
from
"
./types/IChatterboxConfig
"
;
async
function
fetchConfig
():
Promise
<
IChatterboxConfig
>
{
const
root
=
document
.
querySelector
(
"
#chatterbox
"
)
as
HTMLDivElement
;
if
(
!
root
)
{
throw
new
Error
(
"
No element with id as 'chatterbox' found!
"
);
}
const
rootDivId
=
"
#chatterbox
"
;
async
function
fetchConfig
(
root
:
HTMLDivElement
):
Promise
<
IChatterboxConfig
>
{
const
configLink
=
root
?.
dataset
.
configLink
;
if
(
!
configLink
)
{
throw
new
Error
(
"
Root element does not have config specified
"
);
}
const
config
:
IChatterboxConfig
=
await
(
await
fetch
(
configLink
)).
json
();
return
config
;
}
async
function
main
()
{
const
root
=
document
.
querySelector
(
"
#chatterbox
"
)
as
HTMLDivElement
;
const
{
homeserver
,
auto_join_room
}
=
await
fetchConfig
();
const
hydrogen
=
new
Hydrogen
(
homeserver
,
root
);
console
.
log
(
"
Checking if session already exists
"
);
const
sessionAlreadyExists
=
await
hydrogen
.
attemptStartWithExistingSession
();
if
(
sessionAlreadyExists
)
{
console
.
log
(
"
Starting Hydrogen with existing session
"
);
}
else
{
console
.
log
(
"
Session does not exist!
"
);
const
username
=
generateRandomString
(
7
);
const
password
=
generateRandomString
(
10
);
console
.
log
(
`Attempting to register with username =
${
username
}
and password =
${
password
}
`
);
await
hydrogen
.
register
(
username
,
password
,
"
Chatterbox
"
);
console
.
log
(
"
Registration done
"
);
console
.
log
(
"
Attempting to login with same credentials
"
);
await
hydrogen
.
login
(
username
,
password
);
console
.
log
(
"
Login successful
"
);
}
console
.
log
(
"
Attempting to mount Timeline
"
);
await
hydrogen
.
mountTimeline
(
auto_join_room
);
console
.
log
(
"
Mounted Timeline
"
);
}
function
generateRandomString
(
length
:
number
):
string
{
let
result
=
""
;
const
characters
=
"
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_
"
;
var
charactersLength
=
characters
.
length
;
for
(
let
i
=
0
;
i
<
length
;
i
++
)
{
result
+=
characters
.
charAt
(
Math
.
floor
(
Math
.
random
()
*
charactersLength
));
const
root
=
document
.
querySelector
(
rootDivId
)
as
HTMLDivElement
;
if
(
!
root
)
{
throw
new
Error
(
"
No element with id as 'chatterbox' found!
"
);
}
return
result
;
const
config
=
await
fetchConfig
(
root
);
const
chatterbox
=
new
Chatterbox
(
config
,
root
);
chatterbox
.
start
();
}
main
();
This diff is collapsed.
Click to expand it.
src/random.ts
0 → 100644
+
10
−
0
View file @
81a850fc
// todo: do we need something better than this?
export
function
generateRandomString
(
length
:
number
):
string
{
let
result
=
""
;
const
characters
=
"
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_
"
;
var
charactersLength
=
characters
.
length
;
for
(
let
i
=
0
;
i
<
length
;
i
++
)
{
result
+=
characters
.
charAt
(
Math
.
floor
(
Math
.
random
()
*
charactersLength
));
}
return
result
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment