first commit
This commit is contained in:
12
services/chat/.gitignore
vendored
Normal file
12
services/chat/.gitignore
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
**.swp
|
||||
|
||||
public/build/
|
||||
|
||||
node_modules/
|
||||
|
||||
plato/
|
||||
|
||||
**/*.map
|
||||
|
||||
# managed by dev-environment$ bin/update_build_scripts
|
||||
.npmrc
|
3
services/chat/.mocharc.json
Normal file
3
services/chat/.mocharc.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"require": "test/setup.js"
|
||||
}
|
1
services/chat/.nvmrc
Normal file
1
services/chat/.nvmrc
Normal file
@@ -0,0 +1 @@
|
||||
20.18.2
|
27
services/chat/Dockerfile
Normal file
27
services/chat/Dockerfile
Normal file
@@ -0,0 +1,27 @@
|
||||
# This file was auto-generated, do not edit it directly.
|
||||
# Instead run bin/update_build_scripts from
|
||||
# https://github.com/overleaf/internal/
|
||||
|
||||
FROM node:20.18.2 AS base
|
||||
|
||||
WORKDIR /overleaf/services/chat
|
||||
|
||||
# Google Cloud Storage needs a writable $HOME/.config for resumable uploads
|
||||
# (see https://googleapis.dev/nodejs/storage/latest/File.html#createWriteStream)
|
||||
RUN mkdir /home/node/.config && chown node:node /home/node/.config
|
||||
|
||||
FROM base AS app
|
||||
|
||||
COPY package.json package-lock.json /overleaf/
|
||||
COPY services/chat/package.json /overleaf/services/chat/
|
||||
COPY libraries/ /overleaf/libraries/
|
||||
COPY patches/ /overleaf/patches/
|
||||
|
||||
RUN cd /overleaf && npm ci --quiet
|
||||
|
||||
COPY services/chat/ /overleaf/services/chat/
|
||||
|
||||
FROM app
|
||||
USER node
|
||||
|
||||
CMD ["node", "--expose-gc", "app.js"]
|
662
services/chat/LICENSE
Normal file
662
services/chat/LICENSE
Normal file
@@ -0,0 +1,662 @@
|
||||
|
||||
GNU AFFERO GENERAL PUBLIC LICENSE
|
||||
Version 3, 19 November 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU Affero General Public License is a free, copyleft license for
|
||||
software and other kinds of works, specifically designed to ensure
|
||||
cooperation with the community in the case of network server software.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
our General Public Licenses are intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
Developers that use our General Public Licenses protect your rights
|
||||
with two steps: (1) assert copyright on the software, and (2) offer
|
||||
you this License which gives you legal permission to copy, distribute
|
||||
and/or modify the software.
|
||||
|
||||
A secondary benefit of defending all users' freedom is that
|
||||
improvements made in alternate versions of the program, if they
|
||||
receive widespread use, become available for other developers to
|
||||
incorporate. Many developers of free software are heartened and
|
||||
encouraged by the resulting cooperation. However, in the case of
|
||||
software used on network servers, this result may fail to come about.
|
||||
The GNU General Public License permits making a modified version and
|
||||
letting the public access it on a server without ever releasing its
|
||||
source code to the public.
|
||||
|
||||
The GNU Affero General Public License is designed specifically to
|
||||
ensure that, in such cases, the modified source code becomes available
|
||||
to the community. It requires the operator of a network server to
|
||||
provide the source code of the modified version running there to the
|
||||
users of that server. Therefore, public use of a modified version, on
|
||||
a publicly accessible server, gives the public access to the source
|
||||
code of the modified version.
|
||||
|
||||
An older license, called the Affero General Public License and
|
||||
published by Affero, was designed to accomplish similar goals. This is
|
||||
a different license, not a version of the Affero GPL, but Affero has
|
||||
released a new version of the Affero GPL which permits relicensing under
|
||||
this license.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU Affero General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Remote Network Interaction; Use with the GNU General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, if you modify the
|
||||
Program, your modified version must prominently offer all users
|
||||
interacting with it remotely through a computer network (if your version
|
||||
supports such interaction) an opportunity to receive the Corresponding
|
||||
Source of your version by providing access to the Corresponding Source
|
||||
from a network server at no charge, through some standard or customary
|
||||
means of facilitating copying of software. This Corresponding Source
|
||||
shall include the Corresponding Source for any work covered by version 3
|
||||
of the GNU General Public License that is incorporated pursuant to the
|
||||
following paragraph.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the work with which it is combined will remain governed by version
|
||||
3 of the GNU General Public License.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU Affero General Public License from time to time. Such new versions
|
||||
will be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU Affero General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU Affero General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU Affero General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If your software can interact with users remotely through a computer
|
||||
network, you should also make sure that it provides a way for users to
|
||||
get its source. For example, if your program is a web application, its
|
||||
interface could display a "Source" link that leads users to an archive
|
||||
of the code. There are many ways you could offer source, and different
|
||||
solutions will be better for different programs; see section 13 for the
|
||||
specific requirements.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU AGPL, see
|
||||
<http://www.gnu.org/licenses/>.
|
156
services/chat/Makefile
Normal file
156
services/chat/Makefile
Normal file
@@ -0,0 +1,156 @@
|
||||
# This file was auto-generated, do not edit it directly.
|
||||
# Instead run bin/update_build_scripts from
|
||||
# https://github.com/overleaf/internal/
|
||||
|
||||
BUILD_NUMBER ?= local
|
||||
BRANCH_NAME ?= $(shell git rev-parse --abbrev-ref HEAD)
|
||||
PROJECT_NAME = chat
|
||||
BUILD_DIR_NAME = $(shell pwd | xargs basename | tr -cd '[a-zA-Z0-9_.\-]')
|
||||
|
||||
DOCKER_COMPOSE_FLAGS ?= -f docker-compose.yml
|
||||
DOCKER_COMPOSE := BUILD_NUMBER=$(BUILD_NUMBER) \
|
||||
BRANCH_NAME=$(BRANCH_NAME) \
|
||||
PROJECT_NAME=$(PROJECT_NAME) \
|
||||
MOCHA_GREP=${MOCHA_GREP} \
|
||||
docker compose ${DOCKER_COMPOSE_FLAGS}
|
||||
|
||||
COMPOSE_PROJECT_NAME_TEST_ACCEPTANCE ?= test_acceptance_$(BUILD_DIR_NAME)
|
||||
DOCKER_COMPOSE_TEST_ACCEPTANCE = \
|
||||
COMPOSE_PROJECT_NAME=$(COMPOSE_PROJECT_NAME_TEST_ACCEPTANCE) $(DOCKER_COMPOSE)
|
||||
|
||||
COMPOSE_PROJECT_NAME_TEST_UNIT ?= test_unit_$(BUILD_DIR_NAME)
|
||||
DOCKER_COMPOSE_TEST_UNIT = \
|
||||
COMPOSE_PROJECT_NAME=$(COMPOSE_PROJECT_NAME_TEST_UNIT) $(DOCKER_COMPOSE)
|
||||
|
||||
clean:
|
||||
-docker rmi ci/$(PROJECT_NAME):$(BRANCH_NAME)-$(BUILD_NUMBER)
|
||||
-docker rmi us-east1-docker.pkg.dev/overleaf-ops/ol-docker/$(PROJECT_NAME):$(BRANCH_NAME)-$(BUILD_NUMBER)
|
||||
-$(DOCKER_COMPOSE_TEST_UNIT) down --rmi local
|
||||
-$(DOCKER_COMPOSE_TEST_ACCEPTANCE) down --rmi local
|
||||
|
||||
HERE=$(shell pwd)
|
||||
MONOREPO=$(shell cd ../../ && pwd)
|
||||
# Run the linting commands in the scope of the monorepo.
|
||||
# Eslint and prettier (plus some configs) are on the root.
|
||||
RUN_LINTING = docker run --rm -v $(MONOREPO):$(MONOREPO) -w $(HERE) node:20.18.2 npm run --silent
|
||||
|
||||
RUN_LINTING_CI = docker run --rm --volume $(MONOREPO)/.editorconfig:/overleaf/.editorconfig --volume $(MONOREPO)/.eslintignore:/overleaf/.eslintignore --volume $(MONOREPO)/.eslintrc:/overleaf/.eslintrc --volume $(MONOREPO)/.prettierignore:/overleaf/.prettierignore --volume $(MONOREPO)/.prettierrc:/overleaf/.prettierrc --volume $(MONOREPO)/tsconfig.backend.json:/overleaf/tsconfig.backend.json ci/$(PROJECT_NAME):$(BRANCH_NAME)-$(BUILD_NUMBER) npm run --silent
|
||||
|
||||
# Same but from the top of the monorepo
|
||||
RUN_LINTING_MONOREPO = docker run --rm -v $(MONOREPO):$(MONOREPO) -w $(MONOREPO) node:20.18.2 npm run --silent
|
||||
|
||||
SHELLCHECK_OPTS = \
|
||||
--shell=bash \
|
||||
--external-sources
|
||||
SHELLCHECK_COLOR := $(if $(CI),--color=never,--color)
|
||||
SHELLCHECK_FILES := { git ls-files "*.sh" -z; git grep -Plz "\A\#\!.*bash"; } | sort -zu
|
||||
|
||||
shellcheck:
|
||||
@$(SHELLCHECK_FILES) | xargs -0 -r docker run --rm -v $(HERE):/mnt -w /mnt \
|
||||
koalaman/shellcheck:stable $(SHELLCHECK_OPTS) $(SHELLCHECK_COLOR)
|
||||
|
||||
shellcheck_fix:
|
||||
@$(SHELLCHECK_FILES) | while IFS= read -r -d '' file; do \
|
||||
diff=$$(docker run --rm -v $(HERE):/mnt -w /mnt koalaman/shellcheck:stable $(SHELLCHECK_OPTS) --format=diff "$$file" 2>/dev/null); \
|
||||
if [ -n "$$diff" ] && ! echo "$$diff" | patch -p1 >/dev/null 2>&1; then echo "\033[31m$$file\033[0m"; \
|
||||
elif [ -n "$$diff" ]; then echo "$$file"; \
|
||||
else echo "\033[2m$$file\033[0m"; fi \
|
||||
done
|
||||
|
||||
format:
|
||||
$(RUN_LINTING) format
|
||||
|
||||
format_ci:
|
||||
$(RUN_LINTING_CI) format
|
||||
|
||||
format_fix:
|
||||
$(RUN_LINTING) format:fix
|
||||
|
||||
lint:
|
||||
$(RUN_LINTING) lint
|
||||
|
||||
lint_ci:
|
||||
$(RUN_LINTING_CI) lint
|
||||
|
||||
lint_fix:
|
||||
$(RUN_LINTING) lint:fix
|
||||
|
||||
typecheck:
|
||||
$(RUN_LINTING) types:check
|
||||
|
||||
typecheck_ci:
|
||||
$(RUN_LINTING_CI) types:check
|
||||
|
||||
test: format lint typecheck shellcheck test_unit test_acceptance
|
||||
|
||||
test_unit:
|
||||
ifneq (,$(wildcard test/unit))
|
||||
$(DOCKER_COMPOSE_TEST_UNIT) run --rm test_unit
|
||||
$(MAKE) test_unit_clean
|
||||
endif
|
||||
|
||||
test_clean: test_unit_clean
|
||||
test_unit_clean:
|
||||
ifneq (,$(wildcard test/unit))
|
||||
$(DOCKER_COMPOSE_TEST_UNIT) down -v -t 0
|
||||
endif
|
||||
|
||||
test_acceptance: test_acceptance_clean test_acceptance_pre_run test_acceptance_run
|
||||
$(MAKE) test_acceptance_clean
|
||||
|
||||
test_acceptance_debug: test_acceptance_clean test_acceptance_pre_run test_acceptance_run_debug
|
||||
$(MAKE) test_acceptance_clean
|
||||
|
||||
test_acceptance_run:
|
||||
ifneq (,$(wildcard test/acceptance))
|
||||
$(DOCKER_COMPOSE_TEST_ACCEPTANCE) run --rm test_acceptance
|
||||
endif
|
||||
|
||||
test_acceptance_run_debug:
|
||||
ifneq (,$(wildcard test/acceptance))
|
||||
$(DOCKER_COMPOSE_TEST_ACCEPTANCE) run -p 127.0.0.9:19999:19999 --rm test_acceptance npm run test:acceptance -- --inspect=0.0.0.0:19999 --inspect-brk
|
||||
endif
|
||||
|
||||
test_clean: test_acceptance_clean
|
||||
test_acceptance_clean:
|
||||
$(DOCKER_COMPOSE_TEST_ACCEPTANCE) down -v -t 0
|
||||
|
||||
test_acceptance_pre_run:
|
||||
ifneq (,$(wildcard test/acceptance/js/scripts/pre-run))
|
||||
$(DOCKER_COMPOSE_TEST_ACCEPTANCE) run --rm test_acceptance test/acceptance/js/scripts/pre-run
|
||||
endif
|
||||
|
||||
benchmarks:
|
||||
$(DOCKER_COMPOSE_TEST_ACCEPTANCE) run --rm test_acceptance npm run benchmarks
|
||||
|
||||
build:
|
||||
docker build \
|
||||
--pull \
|
||||
--build-arg BUILDKIT_INLINE_CACHE=1 \
|
||||
--tag ci/$(PROJECT_NAME):$(BRANCH_NAME)-$(BUILD_NUMBER) \
|
||||
--tag us-east1-docker.pkg.dev/overleaf-ops/ol-docker/$(PROJECT_NAME):$(BRANCH_NAME)-$(BUILD_NUMBER) \
|
||||
--tag us-east1-docker.pkg.dev/overleaf-ops/ol-docker/$(PROJECT_NAME):$(BRANCH_NAME) \
|
||||
--cache-from us-east1-docker.pkg.dev/overleaf-ops/ol-docker/$(PROJECT_NAME):$(BRANCH_NAME) \
|
||||
--cache-from us-east1-docker.pkg.dev/overleaf-ops/ol-docker/$(PROJECT_NAME):main \
|
||||
--file Dockerfile \
|
||||
../..
|
||||
|
||||
tar:
|
||||
$(DOCKER_COMPOSE) up tar
|
||||
|
||||
publish:
|
||||
|
||||
docker push $(DOCKER_REPO)/$(PROJECT_NAME):$(BRANCH_NAME)-$(BUILD_NUMBER)
|
||||
|
||||
|
||||
.PHONY: clean \
|
||||
format format_fix \
|
||||
lint lint_fix \
|
||||
build_types typecheck \
|
||||
lint_ci format_ci typecheck_ci \
|
||||
shellcheck shellcheck_fix \
|
||||
test test_clean test_unit test_unit_clean \
|
||||
test_acceptance test_acceptance_debug test_acceptance_pre_run \
|
||||
test_acceptance_run test_acceptance_run_debug test_acceptance_clean \
|
||||
benchmarks \
|
||||
build tar publish \
|
11
services/chat/README.md
Normal file
11
services/chat/README.md
Normal file
@@ -0,0 +1,11 @@
|
||||
overleaf/chat
|
||||
===============
|
||||
|
||||
The backend API that powers the chat service in Overleaf
|
||||
|
||||
License
|
||||
-------
|
||||
|
||||
The code in this repository is released under the GNU AFFERO GENERAL PUBLIC LICENSE, version 3. A copy can be found in the `LICENSE` file.
|
||||
|
||||
Copyright (c) Overleaf, 2014-2019.
|
26
services/chat/app.js
Normal file
26
services/chat/app.js
Normal file
@@ -0,0 +1,26 @@
|
||||
// Metrics must be initialized before importing anything else
|
||||
import '@overleaf/metrics/initialize.js'
|
||||
|
||||
import logger from '@overleaf/logger'
|
||||
import settings from '@overleaf/settings'
|
||||
import { mongoClient } from './app/js/mongodb.js'
|
||||
import { createServer } from './app/js/server.js'
|
||||
|
||||
const port = settings.internal.chat.port
|
||||
const host = settings.internal.chat.host
|
||||
mongoClient
|
||||
.connect()
|
||||
.then(async () => {
|
||||
const { server } = await createServer()
|
||||
server.listen(port, host, function (err) {
|
||||
if (err) {
|
||||
logger.fatal({ err }, `Cannot bind to ${host}:${port}. Exiting.`)
|
||||
process.exit(1)
|
||||
}
|
||||
logger.debug(`Chat starting up, listening on ${host}:${port}`)
|
||||
})
|
||||
})
|
||||
.catch(err => {
|
||||
logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
|
||||
process.exit(1)
|
||||
})
|
60
services/chat/app/js/Features/Messages/MessageFormatter.js
Normal file
60
services/chat/app/js/Features/Messages/MessageFormatter.js
Normal file
@@ -0,0 +1,60 @@
|
||||
export function formatMessageForClientSide(message) {
|
||||
if (message._id) {
|
||||
message.id = message._id.toString()
|
||||
delete message._id
|
||||
}
|
||||
const formattedMessage = {
|
||||
id: message.id,
|
||||
content: message.content,
|
||||
timestamp: message.timestamp,
|
||||
user_id: message.user_id,
|
||||
}
|
||||
if (message.edited_at) {
|
||||
formattedMessage.edited_at = message.edited_at
|
||||
}
|
||||
return formattedMessage
|
||||
}
|
||||
|
||||
export function formatMessagesForClientSide(messages) {
|
||||
return messages.map(message => formatMessageForClientSide(message))
|
||||
}
|
||||
|
||||
export function groupMessagesByThreads(rooms, messages) {
|
||||
let room, thread
|
||||
const roomsById = {}
|
||||
for (room of rooms) {
|
||||
roomsById[room._id.toString()] = room
|
||||
}
|
||||
|
||||
const threads = {}
|
||||
const getThread = function (room) {
|
||||
const threadId = room.thread_id.toString()
|
||||
if (threads[threadId]) {
|
||||
return threads[threadId]
|
||||
} else {
|
||||
const thread = { messages: [] }
|
||||
if (room.resolved) {
|
||||
thread.resolved = true
|
||||
thread.resolved_at = room.resolved.ts
|
||||
thread.resolved_by_user_id = room.resolved.user_id
|
||||
}
|
||||
threads[threadId] = thread
|
||||
return thread
|
||||
}
|
||||
}
|
||||
|
||||
for (const message of messages) {
|
||||
room = roomsById[message.room_id.toString()]
|
||||
if (room) {
|
||||
thread = getThread(room)
|
||||
thread.messages.push(formatMessageForClientSide(message))
|
||||
}
|
||||
}
|
||||
|
||||
for (const threadId in threads) {
|
||||
thread = threads[threadId]
|
||||
thread.messages.sort((a, b) => a.timestamp - b.timestamp)
|
||||
}
|
||||
|
||||
return threads
|
||||
}
|
313
services/chat/app/js/Features/Messages/MessageHttpController.js
Normal file
313
services/chat/app/js/Features/Messages/MessageHttpController.js
Normal file
@@ -0,0 +1,313 @@
|
||||
import logger from '@overleaf/logger'
|
||||
import * as MessageManager from './MessageManager.js'
|
||||
import * as MessageFormatter from './MessageFormatter.js'
|
||||
import * as ThreadManager from '../Threads/ThreadManager.js'
|
||||
import { ObjectId } from '../../mongodb.js'
|
||||
|
||||
const DEFAULT_MESSAGE_LIMIT = 50
|
||||
const MAX_MESSAGE_LENGTH = 10 * 1024 // 10kb, about 1,500 words
|
||||
|
||||
function readContext(context, req) {
|
||||
req.body = context.requestBody
|
||||
req.params = context.params.path
|
||||
req.query = context.params.query
|
||||
if (typeof req.params.projectId !== 'undefined') {
|
||||
if (!ObjectId.isValid(req.params.projectId)) {
|
||||
context.res.status(400).setBody('Invalid projectId')
|
||||
}
|
||||
}
|
||||
if (typeof req.params.threadId !== 'undefined') {
|
||||
if (!ObjectId.isValid(req.params.threadId)) {
|
||||
context.res.status(400).setBody('Invalid threadId')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param context
|
||||
* @param {(req: unknown, res: unknown) => Promise<unknown>} ControllerMethod
|
||||
* @returns {Promise<*>}
|
||||
*/
|
||||
export async function callMessageHttpController(context, ControllerMethod) {
|
||||
const req = {}
|
||||
readContext(context, req)
|
||||
if (context.res.statusCode !== 400) {
|
||||
return await ControllerMethod(req, context.res)
|
||||
} else {
|
||||
return context.res.body
|
||||
}
|
||||
}
|
||||
|
||||
export async function getGlobalMessages(context) {
|
||||
return await callMessageHttpController(context, _getGlobalMessages)
|
||||
}
|
||||
|
||||
export async function sendGlobalMessage(context) {
|
||||
return await callMessageHttpController(context, _sendGlobalMessage)
|
||||
}
|
||||
|
||||
export async function sendMessage(context) {
|
||||
return await callMessageHttpController(context, _sendThreadMessage)
|
||||
}
|
||||
|
||||
export async function getThreads(context) {
|
||||
return await callMessageHttpController(context, _getAllThreads)
|
||||
}
|
||||
|
||||
export async function resolveThread(context) {
|
||||
return await callMessageHttpController(context, _resolveThread)
|
||||
}
|
||||
|
||||
export async function reopenThread(context) {
|
||||
return await callMessageHttpController(context, _reopenThread)
|
||||
}
|
||||
|
||||
export async function deleteThread(context) {
|
||||
return await callMessageHttpController(context, _deleteThread)
|
||||
}
|
||||
|
||||
export async function editMessage(context) {
|
||||
return await callMessageHttpController(context, _editMessage)
|
||||
}
|
||||
|
||||
export async function deleteMessage(context) {
|
||||
return await callMessageHttpController(context, _deleteMessage)
|
||||
}
|
||||
|
||||
export async function deleteUserMessage(context) {
|
||||
return await callMessageHttpController(context, _deleteUserMessage)
|
||||
}
|
||||
|
||||
export async function getResolvedThreadIds(context) {
|
||||
return await callMessageHttpController(context, _getResolvedThreadIds)
|
||||
}
|
||||
|
||||
export async function destroyProject(context) {
|
||||
return await callMessageHttpController(context, _destroyProject)
|
||||
}
|
||||
|
||||
export async function duplicateCommentThreads(context) {
|
||||
return await callMessageHttpController(context, _duplicateCommentThreads)
|
||||
}
|
||||
|
||||
export async function generateThreadData(context) {
|
||||
return await callMessageHttpController(context, _generateThreadData)
|
||||
}
|
||||
|
||||
export async function getStatus(context) {
|
||||
const message = 'chat is alive'
|
||||
context.res.status(200).setBody(message)
|
||||
return message
|
||||
}
|
||||
|
||||
const _getGlobalMessages = async (req, res) => {
|
||||
await _getMessages(ThreadManager.GLOBAL_THREAD, req, res)
|
||||
}
|
||||
|
||||
async function _sendGlobalMessage(req, res) {
|
||||
const { user_id: userId, content } = req.body
|
||||
const { projectId } = req.params
|
||||
return await _sendMessage(
|
||||
userId,
|
||||
projectId,
|
||||
content,
|
||||
ThreadManager.GLOBAL_THREAD,
|
||||
res
|
||||
)
|
||||
}
|
||||
|
||||
async function _sendThreadMessage(req, res) {
|
||||
const { user_id: userId, content } = req.body
|
||||
const { projectId, threadId } = req.params
|
||||
return await _sendMessage(userId, projectId, content, threadId, res)
|
||||
}
|
||||
|
||||
const _getAllThreads = async (req, res) => {
|
||||
const { projectId } = req.params
|
||||
logger.debug({ projectId }, 'getting all threads')
|
||||
const rooms = await ThreadManager.findAllThreadRooms(projectId)
|
||||
const roomIds = rooms.map(r => r._id)
|
||||
const messages = await MessageManager.findAllMessagesInRooms(roomIds)
|
||||
const threads = MessageFormatter.groupMessagesByThreads(rooms, messages)
|
||||
res.json(threads)
|
||||
}
|
||||
|
||||
const _generateThreadData = async (req, res) => {
|
||||
const { projectId } = req.params
|
||||
const { threads } = req.body
|
||||
logger.debug({ projectId }, 'getting all threads')
|
||||
const rooms = await ThreadManager.findThreadsById(projectId, threads)
|
||||
const roomIds = rooms.map(r => r._id)
|
||||
const messages = await MessageManager.findAllMessagesInRooms(roomIds)
|
||||
logger.debug({ rooms, messages }, 'looked up messages in the rooms')
|
||||
const threadData = MessageFormatter.groupMessagesByThreads(rooms, messages)
|
||||
res.json(threadData)
|
||||
}
|
||||
|
||||
const _resolveThread = async (req, res) => {
|
||||
const { projectId, threadId } = req.params
|
||||
const { user_id: userId } = req.body
|
||||
logger.debug({ userId, projectId, threadId }, 'marking thread as resolved')
|
||||
await ThreadManager.resolveThread(projectId, threadId, userId)
|
||||
res.status(204)
|
||||
}
|
||||
|
||||
const _reopenThread = async (req, res) => {
|
||||
const { projectId, threadId } = req.params
|
||||
logger.debug({ projectId, threadId }, 'reopening thread')
|
||||
await ThreadManager.reopenThread(projectId, threadId)
|
||||
res.status(204)
|
||||
}
|
||||
|
||||
const _deleteThread = async (req, res) => {
|
||||
const { projectId, threadId } = req.params
|
||||
logger.debug({ projectId, threadId }, 'deleting thread')
|
||||
const roomId = await ThreadManager.deleteThread(projectId, threadId)
|
||||
await MessageManager.deleteAllMessagesInRoom(roomId)
|
||||
res.status(204)
|
||||
}
|
||||
|
||||
const _editMessage = async (req, res) => {
|
||||
const { content, userId } = req.body
|
||||
const { projectId, threadId, messageId } = req.params
|
||||
logger.debug({ projectId, threadId, messageId, content }, 'editing message')
|
||||
const room = await ThreadManager.findOrCreateThread(projectId, threadId)
|
||||
const found = await MessageManager.updateMessage(
|
||||
room._id,
|
||||
messageId,
|
||||
userId,
|
||||
content,
|
||||
Date.now()
|
||||
)
|
||||
if (!found) {
|
||||
res.status(404)
|
||||
return
|
||||
}
|
||||
res.status(204)
|
||||
}
|
||||
|
||||
const _deleteMessage = async (req, res) => {
|
||||
const { projectId, threadId, messageId } = req.params
|
||||
logger.debug({ projectId, threadId, messageId }, 'deleting message')
|
||||
const room = await ThreadManager.findOrCreateThread(projectId, threadId)
|
||||
await MessageManager.deleteMessage(room._id, messageId)
|
||||
res.status(204)
|
||||
}
|
||||
|
||||
const _deleteUserMessage = async (req, res) => {
|
||||
const { projectId, threadId, userId, messageId } = req.params
|
||||
const room = await ThreadManager.findOrCreateThread(projectId, threadId)
|
||||
await MessageManager.deleteUserMessage(userId, room._id, messageId)
|
||||
res.status(204)
|
||||
}
|
||||
|
||||
const _getResolvedThreadIds = async (req, res) => {
|
||||
const { projectId } = req.params
|
||||
const resolvedThreadIds = await ThreadManager.getResolvedThreadIds(projectId)
|
||||
res.json({ resolvedThreadIds })
|
||||
}
|
||||
|
||||
const _destroyProject = async (req, res) => {
|
||||
const { projectId } = req.params
|
||||
logger.debug({ projectId }, 'destroying project')
|
||||
const rooms = await ThreadManager.findAllThreadRoomsAndGlobalThread(projectId)
|
||||
const roomIds = rooms.map(r => r._id)
|
||||
logger.debug({ projectId, roomIds }, 'deleting all messages in rooms')
|
||||
await MessageManager.deleteAllMessagesInRooms(roomIds)
|
||||
logger.debug({ projectId }, 'deleting all threads in project')
|
||||
await ThreadManager.deleteAllThreadsInProject(projectId)
|
||||
res.status(204)
|
||||
}
|
||||
|
||||
async function _sendMessage(userId, projectId, content, clientThreadId, res) {
|
||||
if (!ObjectId.isValid(userId)) {
|
||||
const message = 'Invalid userId'
|
||||
res.status(400).setBody(message)
|
||||
return message
|
||||
}
|
||||
if (!content) {
|
||||
const message = 'No content provided'
|
||||
res.status(400).setBody(message)
|
||||
return message
|
||||
}
|
||||
if (content.length > MAX_MESSAGE_LENGTH) {
|
||||
const message = `Content too long (> ${MAX_MESSAGE_LENGTH} bytes)`
|
||||
res.status(400).setBody(message)
|
||||
return message
|
||||
}
|
||||
logger.debug(
|
||||
{ clientThreadId, projectId, userId, content },
|
||||
'new message received'
|
||||
)
|
||||
const thread = await ThreadManager.findOrCreateThread(
|
||||
projectId,
|
||||
clientThreadId
|
||||
)
|
||||
let message = await MessageManager.createMessage(
|
||||
thread._id,
|
||||
userId,
|
||||
content,
|
||||
Date.now()
|
||||
)
|
||||
message = MessageFormatter.formatMessageForClientSide(message)
|
||||
message.room_id = projectId
|
||||
res.status(201).setBody(message)
|
||||
}
|
||||
|
||||
async function _getMessages(clientThreadId, req, res) {
|
||||
let before, limit
|
||||
const { projectId } = req.params
|
||||
if (req.query.before) {
|
||||
before = parseInt(req.query.before, 10)
|
||||
} else {
|
||||
before = null
|
||||
}
|
||||
if (req.query.limit) {
|
||||
limit = parseInt(req.query.limit, 10)
|
||||
} else {
|
||||
limit = DEFAULT_MESSAGE_LIMIT
|
||||
}
|
||||
logger.debug(
|
||||
{ limit, before, projectId, clientThreadId },
|
||||
'get message request received'
|
||||
)
|
||||
const thread = await ThreadManager.findOrCreateThread(
|
||||
projectId,
|
||||
clientThreadId
|
||||
)
|
||||
const threadObjectId = thread._id
|
||||
logger.debug(
|
||||
{ limit, before, projectId, clientThreadId, threadObjectId },
|
||||
'found or created thread'
|
||||
)
|
||||
let messages = await MessageManager.getMessages(threadObjectId, limit, before)
|
||||
messages = MessageFormatter.formatMessagesForClientSide(messages)
|
||||
logger.debug({ projectId, messages }, 'got messages')
|
||||
res.status(200).setBody(messages)
|
||||
}
|
||||
|
||||
async function _duplicateCommentThreads(req, res) {
|
||||
const { projectId } = req.params
|
||||
const { threads } = req.body
|
||||
const result = {}
|
||||
for (const id of threads) {
|
||||
logger.debug({ projectId, thread: id }, 'duplicating thread')
|
||||
try {
|
||||
const { oldRoom, newRoom } = await ThreadManager.duplicateThread(
|
||||
projectId,
|
||||
id
|
||||
)
|
||||
await MessageManager.duplicateRoomToOtherRoom(oldRoom._id, newRoom._id)
|
||||
result[id] = { duplicateId: newRoom.thread_id }
|
||||
} catch (error) {
|
||||
if (error instanceof ThreadManager.MissingThreadError) {
|
||||
// Expected error when the comment has been deleted prior to duplication
|
||||
result[id] = { error: 'not found' }
|
||||
} else {
|
||||
logger.err({ error }, 'error duplicating thread')
|
||||
result[id] = { error: 'unknown' }
|
||||
}
|
||||
}
|
||||
}
|
||||
res.json({ newThreads: result })
|
||||
}
|
112
services/chat/app/js/Features/Messages/MessageManager.js
Normal file
112
services/chat/app/js/Features/Messages/MessageManager.js
Normal file
@@ -0,0 +1,112 @@
|
||||
import { db, ObjectId } from '../../mongodb.js'
|
||||
|
||||
export async function createMessage(roomId, userId, content, timestamp) {
|
||||
let newMessageOpts = {
|
||||
content,
|
||||
room_id: roomId,
|
||||
user_id: userId,
|
||||
timestamp,
|
||||
}
|
||||
newMessageOpts = _ensureIdsAreObjectIds(newMessageOpts)
|
||||
const confirmation = await db.messages.insertOne(newMessageOpts)
|
||||
newMessageOpts._id = confirmation.insertedId
|
||||
return newMessageOpts
|
||||
}
|
||||
|
||||
export async function getMessages(roomId, limit, before) {
|
||||
let query = { room_id: roomId }
|
||||
if (before) {
|
||||
query.timestamp = { $lt: before }
|
||||
}
|
||||
query = _ensureIdsAreObjectIds(query)
|
||||
return await db.messages
|
||||
.find(query)
|
||||
.sort({ timestamp: -1 })
|
||||
.limit(limit)
|
||||
.toArray()
|
||||
}
|
||||
|
||||
export async function findAllMessagesInRooms(roomIds) {
|
||||
return await db.messages
|
||||
.find({
|
||||
room_id: { $in: roomIds },
|
||||
})
|
||||
.toArray()
|
||||
}
|
||||
|
||||
export async function deleteAllMessagesInRoom(roomId) {
|
||||
await db.messages.deleteMany({
|
||||
room_id: roomId,
|
||||
})
|
||||
}
|
||||
|
||||
export async function deleteAllMessagesInRooms(roomIds) {
|
||||
await db.messages.deleteMany({
|
||||
room_id: { $in: roomIds },
|
||||
})
|
||||
}
|
||||
|
||||
export async function updateMessage(
|
||||
roomId,
|
||||
messageId,
|
||||
userId,
|
||||
content,
|
||||
timestamp
|
||||
) {
|
||||
const query = _ensureIdsAreObjectIds({
|
||||
_id: messageId,
|
||||
room_id: roomId,
|
||||
})
|
||||
if (userId) {
|
||||
query.user_id = new ObjectId(userId)
|
||||
}
|
||||
const res = await db.messages.updateOne(query, {
|
||||
$set: {
|
||||
content,
|
||||
edited_at: timestamp,
|
||||
},
|
||||
})
|
||||
return res.modifiedCount === 1
|
||||
}
|
||||
|
||||
export async function deleteMessage(roomId, messageId) {
|
||||
const query = _ensureIdsAreObjectIds({
|
||||
_id: messageId,
|
||||
room_id: roomId,
|
||||
})
|
||||
await db.messages.deleteOne(query)
|
||||
}
|
||||
|
||||
export async function deleteUserMessage(userId, roomId, messageId) {
|
||||
await db.messages.deleteOne({
|
||||
_id: new ObjectId(messageId),
|
||||
user_id: new ObjectId(userId),
|
||||
room_id: new ObjectId(roomId),
|
||||
})
|
||||
}
|
||||
|
||||
function _ensureIdsAreObjectIds(query) {
|
||||
if (query.user_id && !(query.user_id instanceof ObjectId)) {
|
||||
query.user_id = new ObjectId(query.user_id)
|
||||
}
|
||||
if (query.room_id && !(query.room_id instanceof ObjectId)) {
|
||||
query.room_id = new ObjectId(query.room_id)
|
||||
}
|
||||
if (query._id && !(query._id instanceof ObjectId)) {
|
||||
query._id = new ObjectId(query._id)
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
export async function duplicateRoomToOtherRoom(sourceRoomId, targetRoomId) {
|
||||
const sourceMessages = await findAllMessagesInRooms([sourceRoomId])
|
||||
const targetMessages = sourceMessages.map(comment => {
|
||||
return _ensureIdsAreObjectIds({
|
||||
room_id: targetRoomId,
|
||||
content: comment.content,
|
||||
timestamp: comment.timestamp,
|
||||
user_id: comment.user_id,
|
||||
})
|
||||
})
|
||||
await db.messages.insertMany(targetMessages)
|
||||
}
|
157
services/chat/app/js/Features/Threads/ThreadManager.js
Normal file
157
services/chat/app/js/Features/Threads/ThreadManager.js
Normal file
@@ -0,0 +1,157 @@
|
||||
import { db, ObjectId } from '../../mongodb.js'
|
||||
|
||||
export class MissingThreadError extends Error {}
|
||||
|
||||
export const GLOBAL_THREAD = 'GLOBAL'
|
||||
|
||||
export async function findOrCreateThread(projectId, threadId) {
|
||||
let query, update
|
||||
projectId = new ObjectId(projectId.toString())
|
||||
if (threadId !== GLOBAL_THREAD) {
|
||||
threadId = new ObjectId(threadId.toString())
|
||||
}
|
||||
|
||||
if (threadId === GLOBAL_THREAD) {
|
||||
query = {
|
||||
project_id: projectId,
|
||||
thread_id: { $exists: false },
|
||||
}
|
||||
update = {
|
||||
project_id: projectId,
|
||||
}
|
||||
} else {
|
||||
query = {
|
||||
project_id: projectId,
|
||||
thread_id: threadId,
|
||||
}
|
||||
update = {
|
||||
project_id: projectId,
|
||||
thread_id: threadId,
|
||||
}
|
||||
}
|
||||
|
||||
const result = await db.rooms.findOneAndUpdate(
|
||||
query,
|
||||
{ $set: update },
|
||||
{ upsert: true, returnDocument: 'after' }
|
||||
)
|
||||
return result
|
||||
}
|
||||
|
||||
export async function findAllThreadRooms(projectId) {
|
||||
return await db.rooms
|
||||
.find(
|
||||
{
|
||||
project_id: new ObjectId(projectId.toString()),
|
||||
thread_id: { $exists: true },
|
||||
},
|
||||
{
|
||||
thread_id: 1,
|
||||
resolved: 1,
|
||||
}
|
||||
)
|
||||
.toArray()
|
||||
}
|
||||
|
||||
export async function findAllThreadRoomsAndGlobalThread(projectId) {
|
||||
return await db.rooms
|
||||
.find(
|
||||
{
|
||||
project_id: new ObjectId(projectId.toString()),
|
||||
},
|
||||
{
|
||||
thread_id: 1,
|
||||
resolved: 1,
|
||||
}
|
||||
)
|
||||
.toArray()
|
||||
}
|
||||
|
||||
export async function resolveThread(projectId, threadId, userId) {
|
||||
await db.rooms.updateOne(
|
||||
{
|
||||
project_id: new ObjectId(projectId.toString()),
|
||||
thread_id: new ObjectId(threadId.toString()),
|
||||
},
|
||||
{
|
||||
$set: {
|
||||
resolved: {
|
||||
user_id: userId,
|
||||
ts: new Date(),
|
||||
},
|
||||
},
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
export async function reopenThread(projectId, threadId) {
|
||||
await db.rooms.updateOne(
|
||||
{
|
||||
project_id: new ObjectId(projectId.toString()),
|
||||
thread_id: new ObjectId(threadId.toString()),
|
||||
},
|
||||
{
|
||||
$unset: {
|
||||
resolved: true,
|
||||
},
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
export async function deleteThread(projectId, threadId) {
|
||||
const room = await findOrCreateThread(projectId, threadId)
|
||||
await db.rooms.deleteOne({
|
||||
_id: room._id,
|
||||
})
|
||||
return room._id
|
||||
}
|
||||
|
||||
export async function deleteAllThreadsInProject(projectId) {
|
||||
await db.rooms.deleteMany({
|
||||
project_id: new ObjectId(projectId.toString()),
|
||||
})
|
||||
}
|
||||
|
||||
export async function getResolvedThreadIds(projectId) {
|
||||
const resolvedThreadIds = await db.rooms
|
||||
.find(
|
||||
{
|
||||
project_id: new ObjectId(projectId),
|
||||
thread_id: { $exists: true },
|
||||
resolved: { $exists: true },
|
||||
},
|
||||
{ projection: { thread_id: 1 } }
|
||||
)
|
||||
.map(record => record.thread_id.toString())
|
||||
.toArray()
|
||||
return resolvedThreadIds
|
||||
}
|
||||
|
||||
export async function duplicateThread(projectId, threadId) {
|
||||
const room = await db.rooms.findOne({
|
||||
project_id: new ObjectId(projectId),
|
||||
thread_id: new ObjectId(threadId),
|
||||
})
|
||||
if (!room) {
|
||||
throw new MissingThreadError('Trying to duplicate a non-existent thread')
|
||||
}
|
||||
const newRoom = {
|
||||
project_id: room.project_id,
|
||||
thread_id: new ObjectId(),
|
||||
}
|
||||
if (room.resolved) {
|
||||
newRoom.resolved = room.resolved
|
||||
}
|
||||
const confirmation = await db.rooms.insertOne(newRoom)
|
||||
newRoom._id = confirmation.insertedId
|
||||
return { oldRoom: room, newRoom }
|
||||
}
|
||||
|
||||
export async function findThreadsById(projectId, threadIds) {
|
||||
return await db.rooms
|
||||
.find({
|
||||
project_id: new ObjectId(projectId),
|
||||
thread_id: { $in: threadIds.map(id => new ObjectId(id)) },
|
||||
})
|
||||
.toArray()
|
||||
}
|
18
services/chat/app/js/mongodb.js
Normal file
18
services/chat/app/js/mongodb.js
Normal file
@@ -0,0 +1,18 @@
|
||||
import Metrics from '@overleaf/metrics'
|
||||
import Settings from '@overleaf/settings'
|
||||
import { MongoClient } from 'mongodb'
|
||||
|
||||
export { ObjectId } from 'mongodb'
|
||||
|
||||
export const mongoClient = new MongoClient(
|
||||
Settings.mongo.url,
|
||||
Settings.mongo.options
|
||||
)
|
||||
const mongoDb = mongoClient.db()
|
||||
|
||||
export const db = {
|
||||
messages: mongoDb.collection('messages'),
|
||||
rooms: mongoDb.collection('rooms'),
|
||||
}
|
||||
|
||||
Metrics.mongodb.monitor(mongoClient)
|
51
services/chat/app/js/server.js
Normal file
51
services/chat/app/js/server.js
Normal file
@@ -0,0 +1,51 @@
|
||||
import http from 'node:http'
|
||||
import metrics from '@overleaf/metrics'
|
||||
import logger from '@overleaf/logger'
|
||||
import express from 'express'
|
||||
import exegesisExpress from 'exegesis-express'
|
||||
import path from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import * as messagesController from './Features/Messages/MessageHttpController.js'
|
||||
|
||||
const __dirname = fileURLToPath(new URL('.', import.meta.url))
|
||||
|
||||
logger.initialize('chat')
|
||||
metrics.open_sockets.monitor()
|
||||
|
||||
metrics.leaked_sockets.monitor(logger)
|
||||
|
||||
export async function createServer() {
|
||||
const app = express()
|
||||
|
||||
app.use(metrics.http.monitor(logger))
|
||||
metrics.injectMetricsRoute(app)
|
||||
|
||||
// See https://github.com/exegesis-js/exegesis/blob/master/docs/Options.md
|
||||
const options = {
|
||||
controllers: { messagesController },
|
||||
ignoreServers: true,
|
||||
allowMissingControllers: false,
|
||||
}
|
||||
|
||||
// const exegesisMiddleware = await exegesisExpress.middleware(
|
||||
const exegesisMiddleware = await exegesisExpress.middleware(
|
||||
path.resolve(__dirname, '../../chat.yaml'),
|
||||
options
|
||||
)
|
||||
|
||||
// If you have any body parsers, this should go before them.
|
||||
app.use(exegesisMiddleware)
|
||||
|
||||
// Return a 404
|
||||
app.use((req, res) => {
|
||||
res.status(404).json({ message: `Not found` })
|
||||
})
|
||||
|
||||
// Handle any unexpected errors
|
||||
app.use((err, req, res, next) => {
|
||||
res.status(500).json({ message: `Internal error: ${err.message}` })
|
||||
})
|
||||
|
||||
const server = http.createServer(app)
|
||||
return { app, server }
|
||||
}
|
10
services/chat/app/js/util/promises.js
Normal file
10
services/chat/app/js/util/promises.js
Normal file
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* Transform an async function into an Express middleware
|
||||
*
|
||||
* Any error will be passed to the error middlewares via `next()`
|
||||
*/
|
||||
export function expressify(fn) {
|
||||
return (req, res, next) => {
|
||||
fn(req, res, next).catch(next)
|
||||
}
|
||||
}
|
9
services/chat/buildscript.txt
Normal file
9
services/chat/buildscript.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
chat
|
||||
--dependencies=mongo
|
||||
--docker-repos=us-east1-docker.pkg.dev/overleaf-ops/ol-docker
|
||||
--env-add=
|
||||
--env-pass-through=
|
||||
--esmock-loader=False
|
||||
--node-version=20.18.2
|
||||
--public-repo=False
|
||||
--script-version=4.7.0
|
416
services/chat/chat.yaml
Normal file
416
services/chat/chat.yaml
Normal file
@@ -0,0 +1,416 @@
|
||||
openapi: 3.1.0
|
||||
x-stoplight:
|
||||
id: okoe8mh50pjec
|
||||
info:
|
||||
title: chat
|
||||
version: '1.0'
|
||||
servers:
|
||||
- url: 'http://chat:3010'
|
||||
x-exegesis-controller: messagesController
|
||||
paths:
|
||||
'/project/{projectId}/messages':
|
||||
parameters:
|
||||
- schema:
|
||||
type: string
|
||||
name: projectId
|
||||
in: path
|
||||
required: true
|
||||
get:
|
||||
summary: Get Global messages
|
||||
tags: []
|
||||
responses:
|
||||
'201':
|
||||
description: OK
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/Message'
|
||||
operationId: getGlobalMessages
|
||||
description: Get global messages for the project with Project ID provided
|
||||
parameters:
|
||||
- schema:
|
||||
type: string
|
||||
in: query
|
||||
name: before
|
||||
- schema:
|
||||
type: string
|
||||
in: query
|
||||
name: limit
|
||||
post:
|
||||
summary: Send Global message
|
||||
operationId: sendGlobalMessage
|
||||
responses:
|
||||
'201':
|
||||
description: OK
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Message'
|
||||
examples:
|
||||
example-1:
|
||||
value:
|
||||
user_id: string
|
||||
content: string
|
||||
description: 'UserID and Content of the message to be posted. '
|
||||
description: Send global message for the project with Project ID provided
|
||||
'/project/{projectId}/thread/{threadId}/messages':
|
||||
parameters:
|
||||
- schema:
|
||||
type: string
|
||||
name: projectId
|
||||
in: path
|
||||
required: true
|
||||
- schema:
|
||||
type: string
|
||||
name: threadId
|
||||
in: path
|
||||
required: true
|
||||
post:
|
||||
summary: Send message
|
||||
operationId: sendMessage
|
||||
responses:
|
||||
'201':
|
||||
description: Created
|
||||
description: Add a message to the thread with thread ID provided from the Project with Project ID provided.
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Message'
|
||||
description: |-
|
||||
JSON object with :
|
||||
- user_id: Id of the user
|
||||
- content: Content of the message
|
||||
'/project/{projectId}/threads':
|
||||
parameters:
|
||||
- schema:
|
||||
type: string
|
||||
name: projectId
|
||||
in: path
|
||||
required: true
|
||||
get:
|
||||
summary: Get Threads
|
||||
tags: []
|
||||
responses:
|
||||
'200':
|
||||
description: OK
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/Thread'
|
||||
examples: {}
|
||||
'404':
|
||||
description: Not Found
|
||||
operationId: getThreads
|
||||
description: Get the list of threads for the project with Project ID provided
|
||||
'/project/{projectId}/thread/{threadId}/messages/{messageId}/edit':
|
||||
parameters:
|
||||
- schema:
|
||||
type: string
|
||||
name: projectId
|
||||
in: path
|
||||
required: true
|
||||
- schema:
|
||||
type: string
|
||||
name: threadId
|
||||
in: path
|
||||
required: true
|
||||
- schema:
|
||||
type: string
|
||||
name: messageId
|
||||
in: path
|
||||
required: true
|
||||
post:
|
||||
summary: Edit message
|
||||
operationId: editMessage
|
||||
responses:
|
||||
'204':
|
||||
description: No Content
|
||||
'404':
|
||||
description: Not Found
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
content:
|
||||
type: string
|
||||
user_id:
|
||||
type: string
|
||||
readOnly: true
|
||||
required:
|
||||
- content
|
||||
examples: {}
|
||||
description: |-
|
||||
JSON object with :
|
||||
- content: Content of the message to edit
|
||||
- user_id: Id of the user (optional)
|
||||
description: |
|
||||
Update message with Message ID provided from the Thread ID and Project ID provided
|
||||
'/project/{projectId}/thread/{threadId}/messages/{messageId}':
|
||||
parameters:
|
||||
- schema:
|
||||
type: string
|
||||
name: projectId
|
||||
in: path
|
||||
required: true
|
||||
- schema:
|
||||
type: string
|
||||
name: threadId
|
||||
in: path
|
||||
required: true
|
||||
- schema:
|
||||
type: string
|
||||
name: messageId
|
||||
in: path
|
||||
required: true
|
||||
delete:
|
||||
summary: Delete message
|
||||
operationId: deleteMessage
|
||||
responses:
|
||||
'204':
|
||||
description: No Content
|
||||
description: 'Delete message with Message ID provided, from the Thread with ThreadID and ProjectID provided'
|
||||
'/project/{projectId}/thread/{threadId}/user/{userId}/messages/{messageId}':
|
||||
parameters:
|
||||
- schema:
|
||||
type: string
|
||||
name: projectId
|
||||
in: path
|
||||
required: true
|
||||
- schema:
|
||||
type: string
|
||||
name: threadId
|
||||
in: path
|
||||
required: true
|
||||
- schema:
|
||||
type: string
|
||||
name: userId
|
||||
in: path
|
||||
required: true
|
||||
- schema:
|
||||
type: string
|
||||
name: messageId
|
||||
in: path
|
||||
required: true
|
||||
delete:
|
||||
summary: Delete message written by a given user
|
||||
operationId: deleteUserMessage
|
||||
responses:
|
||||
'204':
|
||||
description: No Content
|
||||
'/project/{projectId}/thread/{threadId}/resolve':
|
||||
parameters:
|
||||
- schema:
|
||||
type: string
|
||||
name: projectId
|
||||
in: path
|
||||
required: true
|
||||
- schema:
|
||||
type: string
|
||||
name: threadId
|
||||
in: path
|
||||
required: true
|
||||
post:
|
||||
summary: Resolve Thread
|
||||
operationId: resolveThread
|
||||
responses:
|
||||
'204':
|
||||
description: No Content
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
user_id:
|
||||
type: string
|
||||
required:
|
||||
- user_id
|
||||
description: |-
|
||||
JSON object with :
|
||||
- user_id: Id of the user.
|
||||
description: Mark Thread with ThreadID and ProjectID provided owned by the user with UserID provided as resolved.
|
||||
'/project/{projectId}/thread/{threadId}/reopen':
|
||||
parameters:
|
||||
- schema:
|
||||
type: string
|
||||
name: projectId
|
||||
in: path
|
||||
required: true
|
||||
- schema:
|
||||
type: string
|
||||
name: threadId
|
||||
in: path
|
||||
required: true
|
||||
post:
|
||||
summary: Reopen Thread
|
||||
operationId: reopenThread
|
||||
responses:
|
||||
'204':
|
||||
description: No Content
|
||||
description: |-
|
||||
Reopen Thread with ThreadID and ProjectID provided.
|
||||
i.e unmark it as resolved.
|
||||
'/project/{projectId}/thread/{threadId}':
|
||||
parameters:
|
||||
- schema:
|
||||
type: string
|
||||
name: projectId
|
||||
in: path
|
||||
required: true
|
||||
- schema:
|
||||
type: string
|
||||
name: threadId
|
||||
in: path
|
||||
required: true
|
||||
delete:
|
||||
summary: Delete thread
|
||||
operationId: deleteThread
|
||||
responses:
|
||||
'204':
|
||||
description: No Content
|
||||
description: Delete thread with ThreadID and ProjectID provided
|
||||
'/project/{projectId}/resolved-thread-ids':
|
||||
parameters:
|
||||
- schema:
|
||||
type: string
|
||||
name: projectId
|
||||
in: path
|
||||
required: true
|
||||
get:
|
||||
summary: Get resolved thread ids
|
||||
operationId: getResolvedThreadIds
|
||||
responses:
|
||||
'200':
|
||||
description: Resolved thread ids
|
||||
'/project/{projectId}':
|
||||
parameters:
|
||||
- schema:
|
||||
type: string
|
||||
name: projectId
|
||||
in: path
|
||||
required: true
|
||||
delete:
|
||||
summary: Destroy project
|
||||
operationId: destroyProject
|
||||
responses:
|
||||
'204':
|
||||
description: No Content
|
||||
description: 'Delete all threads from Project with Project ID provided, and all messages in those threads.'
|
||||
/status:
|
||||
get:
|
||||
summary: Check status
|
||||
tags: []
|
||||
responses:
|
||||
'200':
|
||||
description: OK
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: string
|
||||
description: chat is alive
|
||||
operationId: getStatus
|
||||
description: Check that the Chat service is alive
|
||||
head:
|
||||
summary: Check status
|
||||
tags: []
|
||||
responses:
|
||||
'200':
|
||||
description: OK
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: string
|
||||
description: chat is alive
|
||||
operationId: getStatus
|
||||
description: Check that the Chat service is alive
|
||||
'/project/{projectId}/duplicate-comment-threads':
|
||||
parameters:
|
||||
- schema:
|
||||
type: string
|
||||
name: projectId
|
||||
in: path
|
||||
required: true
|
||||
post:
|
||||
summary: Duplicate comment threads
|
||||
operationId: duplicateCommentThreads
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
threads:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
responses:
|
||||
'200':
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
newThreads:
|
||||
type: object
|
||||
description: Mapping of old thread ids to their duplicated thread ids
|
||||
description: Duplicate a list of comment threads
|
||||
'/project/{projectId}/generate-thread-data':
|
||||
parameters:
|
||||
- schema:
|
||||
type: string
|
||||
name: projectId
|
||||
in: path
|
||||
required: true
|
||||
post:
|
||||
summary: Generate thread data to load into the frontend
|
||||
operationId: generateThreadData
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
threads:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
responses:
|
||||
'200':
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
description: Load threads and generate a json blob containing all messages in all the threads
|
||||
components:
|
||||
schemas:
|
||||
Message:
|
||||
title: Message
|
||||
x-stoplight:
|
||||
id: ue9n1vvezlutw
|
||||
type: object
|
||||
examples:
|
||||
- user_id: string
|
||||
- content: string
|
||||
properties:
|
||||
user_id:
|
||||
type: string
|
||||
content:
|
||||
type: string
|
||||
required:
|
||||
- user_id
|
||||
- content
|
||||
Thread:
|
||||
title: Thread
|
||||
x-stoplight:
|
||||
id: 0ppt3jw4h5bua
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/Message'
|
33
services/chat/config/settings.defaults.cjs
Normal file
33
services/chat/config/settings.defaults.cjs
Normal file
@@ -0,0 +1,33 @@
|
||||
const http = require('node:http')
|
||||
const https = require('node:https')
|
||||
|
||||
http.globalAgent.keepAlive = false
|
||||
https.globalAgent.keepAlive = false
|
||||
|
||||
module.exports = {
|
||||
internal: {
|
||||
chat: {
|
||||
host: process.env.LISTEN_ADDRESS || '127.0.0.1',
|
||||
port: 3010,
|
||||
},
|
||||
},
|
||||
|
||||
apis: {
|
||||
web: {
|
||||
url: `http://${process.env.WEB_HOST || '127.0.0.1'}:${
|
||||
process.env.WEB_PORT || 3000
|
||||
}`,
|
||||
user: process.env.WEB_API_USER || 'overleaf',
|
||||
pass: process.env.WEB_API_PASSWORD || 'password',
|
||||
},
|
||||
},
|
||||
|
||||
mongo: {
|
||||
url:
|
||||
process.env.MONGO_CONNECTION_STRING ||
|
||||
`mongodb://${process.env.MONGO_HOST || '127.0.0.1'}/sharelatex`,
|
||||
options: {
|
||||
monitorCommands: true,
|
||||
},
|
||||
},
|
||||
}
|
52
services/chat/docker-compose.ci.yml
Normal file
52
services/chat/docker-compose.ci.yml
Normal file
@@ -0,0 +1,52 @@
|
||||
# This file was auto-generated, do not edit it directly.
|
||||
# Instead run bin/update_build_scripts from
|
||||
# https://github.com/overleaf/internal/
|
||||
|
||||
version: "2.3"
|
||||
|
||||
services:
|
||||
test_unit:
|
||||
image: ci/$PROJECT_NAME:$BRANCH_NAME-$BUILD_NUMBER
|
||||
user: node
|
||||
command: npm run test:unit:_run
|
||||
environment:
|
||||
NODE_ENV: test
|
||||
NODE_OPTIONS: "--unhandled-rejections=strict"
|
||||
|
||||
|
||||
test_acceptance:
|
||||
build: .
|
||||
image: ci/$PROJECT_NAME:$BRANCH_NAME-$BUILD_NUMBER
|
||||
environment:
|
||||
ELASTIC_SEARCH_DSN: es:9200
|
||||
MONGO_HOST: mongo
|
||||
POSTGRES_HOST: postgres
|
||||
MOCHA_GREP: ${MOCHA_GREP}
|
||||
NODE_ENV: test
|
||||
NODE_OPTIONS: "--unhandled-rejections=strict"
|
||||
depends_on:
|
||||
mongo:
|
||||
condition: service_started
|
||||
user: node
|
||||
command: npm run test:acceptance
|
||||
|
||||
|
||||
tar:
|
||||
build: .
|
||||
image: ci/$PROJECT_NAME:$BRANCH_NAME-$BUILD_NUMBER
|
||||
volumes:
|
||||
- ./:/tmp/build/
|
||||
command: tar -czf /tmp/build/build.tar.gz --exclude=build.tar.gz --exclude-vcs .
|
||||
user: root
|
||||
mongo:
|
||||
image: mongo:6.0.13
|
||||
command: --replSet overleaf
|
||||
volumes:
|
||||
- ../../bin/shared/mongodb-init-replica-set.js:/docker-entrypoint-initdb.d/mongodb-init-replica-set.js
|
||||
environment:
|
||||
MONGO_INITDB_DATABASE: sharelatex
|
||||
extra_hosts:
|
||||
# Required when using the automatic database setup for initializing the
|
||||
# replica set. This override is not needed when running the setup after
|
||||
# starting up mongo.
|
||||
- mongo:127.0.0.1
|
56
services/chat/docker-compose.yml
Normal file
56
services/chat/docker-compose.yml
Normal file
@@ -0,0 +1,56 @@
|
||||
# This file was auto-generated, do not edit it directly.
|
||||
# Instead run bin/update_build_scripts from
|
||||
# https://github.com/overleaf/internal/
|
||||
|
||||
version: "2.3"
|
||||
|
||||
services:
|
||||
test_unit:
|
||||
image: node:20.18.2
|
||||
volumes:
|
||||
- .:/overleaf/services/chat
|
||||
- ../../node_modules:/overleaf/node_modules
|
||||
- ../../libraries:/overleaf/libraries
|
||||
working_dir: /overleaf/services/chat
|
||||
environment:
|
||||
MOCHA_GREP: ${MOCHA_GREP}
|
||||
LOG_LEVEL: ${LOG_LEVEL:-}
|
||||
NODE_ENV: test
|
||||
NODE_OPTIONS: "--unhandled-rejections=strict"
|
||||
command: npm run --silent test:unit
|
||||
user: node
|
||||
|
||||
test_acceptance:
|
||||
image: node:20.18.2
|
||||
volumes:
|
||||
- .:/overleaf/services/chat
|
||||
- ../../node_modules:/overleaf/node_modules
|
||||
- ../../libraries:/overleaf/libraries
|
||||
working_dir: /overleaf/services/chat
|
||||
environment:
|
||||
ELASTIC_SEARCH_DSN: es:9200
|
||||
MONGO_HOST: mongo
|
||||
POSTGRES_HOST: postgres
|
||||
MOCHA_GREP: ${MOCHA_GREP}
|
||||
LOG_LEVEL: ${LOG_LEVEL:-}
|
||||
NODE_ENV: test
|
||||
NODE_OPTIONS: "--unhandled-rejections=strict"
|
||||
user: node
|
||||
depends_on:
|
||||
mongo:
|
||||
condition: service_started
|
||||
command: npm run --silent test:acceptance
|
||||
|
||||
mongo:
|
||||
image: mongo:6.0.13
|
||||
command: --replSet overleaf
|
||||
volumes:
|
||||
- ../../bin/shared/mongodb-init-replica-set.js:/docker-entrypoint-initdb.d/mongodb-init-replica-set.js
|
||||
environment:
|
||||
MONGO_INITDB_DATABASE: sharelatex
|
||||
extra_hosts:
|
||||
# Required when using the automatic database setup for initializing the
|
||||
# replica set. This override is not needed when running the setup after
|
||||
# starting up mongo.
|
||||
- mongo:127.0.0.1
|
||||
|
49
services/chat/package.json
Normal file
49
services/chat/package.json
Normal file
@@ -0,0 +1,49 @@
|
||||
{
|
||||
"name": "@overleaf/chat",
|
||||
"description": "The backend API that powers Overleaf chat",
|
||||
"private": true,
|
||||
"main": "app.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"start": "node app.js",
|
||||
"test:acceptance": "npm run test:acceptance:_run -- --grep=$MOCHA_GREP",
|
||||
"test:unit": "npm run test:unit:_run -- --grep=$MOCHA_GREP",
|
||||
"nodemon": "node --watch app.js",
|
||||
"test:acceptance:_run": "mocha --recursive --reporter spec --timeout 15000 --exit $@ test/acceptance/js",
|
||||
"test:unit:_run": "mocha --recursive --reporter spec $@ test/unit/js",
|
||||
"lint": "eslint --max-warnings 0 --format unix .",
|
||||
"format": "prettier --list-different $PWD/'**/*.*js'",
|
||||
"format:fix": "prettier --write $PWD/'**/*.*js'",
|
||||
"lint:fix": "eslint --fix .",
|
||||
"types:check": "tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"@overleaf/logger": "*",
|
||||
"@overleaf/metrics": "*",
|
||||
"@overleaf/settings": "*",
|
||||
"async": "^3.2.5",
|
||||
"body-parser": "^1.20.3",
|
||||
"exegesis-express": "^4.0.0",
|
||||
"express": "^4.21.2",
|
||||
"mongodb": "6.12.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"acorn": "^7.1.1",
|
||||
"ajv": "^6.12.0",
|
||||
"chai": "^4.3.6",
|
||||
"chai-as-promised": "^7.1.1",
|
||||
"mocha": "^11.1.0",
|
||||
"request": "^2.88.2",
|
||||
"sandboxed-module": "^2.0.4",
|
||||
"sinon": "^9.2.4",
|
||||
"timekeeper": "^2.2.0",
|
||||
"typescript": "^5.0.4"
|
||||
},
|
||||
"version": "1.0.0",
|
||||
"directories": {
|
||||
"test": "test"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "AGPL-3.0"
|
||||
}
|
@@ -0,0 +1,93 @@
|
||||
import { ObjectId } from '../../../app/js/mongodb.js'
|
||||
import { expect } from 'chai'
|
||||
|
||||
import * as ChatClient from './helpers/ChatClient.js'
|
||||
import * as ChatApp from './helpers/ChatApp.js'
|
||||
|
||||
const user1Id = new ObjectId().toString()
|
||||
const user2Id = new ObjectId().toString()
|
||||
|
||||
async function createCommentThread(projectId, threadId = new ObjectId()) {
|
||||
const { response: response1 } = await ChatClient.sendMessage(
|
||||
projectId,
|
||||
threadId.toString(),
|
||||
user1Id,
|
||||
'message 1'
|
||||
)
|
||||
expect(response1.statusCode).to.equal(201)
|
||||
const { response: response2 } = await ChatClient.sendMessage(
|
||||
projectId,
|
||||
threadId,
|
||||
user2Id,
|
||||
'message 2'
|
||||
)
|
||||
expect(response2.statusCode).to.equal(201)
|
||||
return threadId.toString()
|
||||
}
|
||||
|
||||
describe('Cloning comment threads', async function () {
|
||||
const projectId = new ObjectId().toString()
|
||||
|
||||
before(async function () {
|
||||
await ChatApp.ensureRunning()
|
||||
this.thread1Id = await createCommentThread(projectId)
|
||||
this.thread2Id = await createCommentThread(projectId)
|
||||
this.thread3Id = await createCommentThread(projectId)
|
||||
})
|
||||
|
||||
describe('with non-orphaned threads', async function () {
|
||||
before(async function () {
|
||||
const {
|
||||
response: { body: result, statusCode },
|
||||
} = await ChatClient.duplicateCommentThreads(projectId, [this.thread3Id])
|
||||
this.result = result
|
||||
expect(statusCode).to.equal(200)
|
||||
expect(this.result).to.have.property('newThreads')
|
||||
this.newThreadId = this.result.newThreads[this.thread3Id].duplicateId
|
||||
})
|
||||
|
||||
it('should duplicate threads', function () {
|
||||
expect(this.result.newThreads).to.have.property(this.thread3Id)
|
||||
expect(this.result.newThreads[this.thread3Id]).to.have.property(
|
||||
'duplicateId'
|
||||
)
|
||||
expect(this.result.newThreads[this.thread3Id].duplicateId).to.not.equal(
|
||||
this.thread3Id
|
||||
)
|
||||
})
|
||||
|
||||
it('should not duplicate other threads threads', function () {
|
||||
expect(this.result.newThreads).to.not.have.property(this.thread1Id)
|
||||
expect(this.result.newThreads).to.not.have.property(this.thread2Id)
|
||||
})
|
||||
|
||||
it('should duplicate the messages in the thread', async function () {
|
||||
const {
|
||||
response: { body: threads },
|
||||
} = await ChatClient.getThreads(projectId)
|
||||
function ignoreId(comment) {
|
||||
return {
|
||||
...comment,
|
||||
id: undefined,
|
||||
}
|
||||
}
|
||||
expect(threads[this.thread3Id].messages.map(ignoreId)).to.deep.equal(
|
||||
threads[this.newThreadId].messages.map(ignoreId)
|
||||
)
|
||||
})
|
||||
|
||||
it('should have two separate unlinked threads', async function () {
|
||||
await ChatClient.sendMessage(
|
||||
projectId,
|
||||
this.newThreadId,
|
||||
user1Id,
|
||||
'third message'
|
||||
)
|
||||
const {
|
||||
response: { body: threads },
|
||||
} = await ChatClient.getThreads(projectId)
|
||||
expect(threads[this.thread3Id].messages.length).to.equal(2)
|
||||
expect(threads[this.newThreadId].messages.length).to.equal(3)
|
||||
})
|
||||
})
|
||||
})
|
47
services/chat/test/acceptance/js/DeletingAMessageTests.js
Normal file
47
services/chat/test/acceptance/js/DeletingAMessageTests.js
Normal file
@@ -0,0 +1,47 @@
|
||||
import { ObjectId } from '../../../app/js/mongodb.js'
|
||||
import { expect } from 'chai'
|
||||
|
||||
import * as ChatClient from './helpers/ChatClient.js'
|
||||
import * as ChatApp from './helpers/ChatApp.js'
|
||||
|
||||
describe('Deleting a message', async function () {
|
||||
const projectId = new ObjectId().toString()
|
||||
const userId = new ObjectId().toString()
|
||||
const threadId = new ObjectId().toString()
|
||||
|
||||
before(async function () {
|
||||
await ChatApp.ensureRunning()
|
||||
})
|
||||
|
||||
describe('in a thread', async function () {
|
||||
before(async function () {
|
||||
const { response } = await ChatClient.sendMessage(
|
||||
projectId,
|
||||
threadId,
|
||||
userId,
|
||||
'first message'
|
||||
)
|
||||
expect(response.statusCode).to.equal(201)
|
||||
const { response: response2, body: message } =
|
||||
await ChatClient.sendMessage(
|
||||
projectId,
|
||||
threadId,
|
||||
userId,
|
||||
'deleted message'
|
||||
)
|
||||
expect(response2.statusCode).to.equal(201)
|
||||
const { response: response3 } = await ChatClient.deleteMessage(
|
||||
projectId,
|
||||
threadId,
|
||||
message.id
|
||||
)
|
||||
expect(response3.statusCode).to.equal(204)
|
||||
})
|
||||
|
||||
it('should then remove the message from the threads', async function () {
|
||||
const { response, body: threads } = await ChatClient.getThreads(projectId)
|
||||
expect(response.statusCode).to.equal(200)
|
||||
expect(threads[threadId].messages.length).to.equal(1)
|
||||
})
|
||||
})
|
||||
})
|
38
services/chat/test/acceptance/js/DeletingAThreadTests.js
Normal file
38
services/chat/test/acceptance/js/DeletingAThreadTests.js
Normal file
@@ -0,0 +1,38 @@
|
||||
import { ObjectId } from '../../../app/js/mongodb.js'
|
||||
import { expect } from 'chai'
|
||||
|
||||
import * as ChatClient from './helpers/ChatClient.js'
|
||||
import * as ChatApp from './helpers/ChatApp.js'
|
||||
|
||||
describe('Deleting a thread', async function () {
|
||||
const projectId = new ObjectId().toString()
|
||||
const userId = new ObjectId().toString()
|
||||
before(async function () {
|
||||
await ChatApp.ensureRunning()
|
||||
})
|
||||
|
||||
describe('with a thread that is deleted', async function () {
|
||||
const threadId = new ObjectId().toString()
|
||||
const content = 'deleted thread message'
|
||||
before(async function () {
|
||||
const { response } = await ChatClient.sendMessage(
|
||||
projectId,
|
||||
threadId,
|
||||
userId,
|
||||
content
|
||||
)
|
||||
expect(response.statusCode).to.equal(201)
|
||||
const { response: response2 } = await ChatClient.deleteThread(
|
||||
projectId,
|
||||
threadId
|
||||
)
|
||||
expect(response2.statusCode).to.equal(204)
|
||||
})
|
||||
|
||||
it('should then not list the thread for the project', async function () {
|
||||
const { response, body: threads } = await ChatClient.getThreads(projectId)
|
||||
expect(response.statusCode).to.equal(200)
|
||||
expect(Object.keys(threads).length).to.equal(0)
|
||||
})
|
||||
})
|
||||
})
|
66
services/chat/test/acceptance/js/DestroyingAProjectTests.js
Normal file
66
services/chat/test/acceptance/js/DestroyingAProjectTests.js
Normal file
@@ -0,0 +1,66 @@
|
||||
import { ObjectId } from '../../../app/js/mongodb.js'
|
||||
import { expect } from 'chai'
|
||||
|
||||
import * as ChatClient from './helpers/ChatClient.js'
|
||||
import * as ChatApp from './helpers/ChatApp.js'
|
||||
|
||||
const db = ChatApp.db
|
||||
|
||||
async function getMessage(messageId) {
|
||||
return await db.messages.findOne({
|
||||
_id: new ObjectId(messageId),
|
||||
})
|
||||
}
|
||||
|
||||
describe('Destroying a project', async function () {
|
||||
const projectId = new ObjectId().toString()
|
||||
const userId = new ObjectId().toString()
|
||||
before(async function () {
|
||||
await ChatApp.ensureRunning()
|
||||
})
|
||||
|
||||
describe('with a project that has threads and messages', async function () {
|
||||
const threadId = new ObjectId().toString()
|
||||
before(async function () {
|
||||
const { response } = await ChatClient.sendMessage(
|
||||
projectId,
|
||||
threadId,
|
||||
userId,
|
||||
'destroyed thread message'
|
||||
)
|
||||
expect(response.statusCode).to.equal(201)
|
||||
this.threadMessageId = response.body.id
|
||||
const { response: response2 } = await ChatClient.sendGlobalMessage(
|
||||
projectId,
|
||||
userId,
|
||||
'destroyed global message'
|
||||
)
|
||||
expect(response2.statusCode).to.equal(201)
|
||||
this.globalThreadMessageId = response2.body.id
|
||||
|
||||
const threadRooms = await db.rooms
|
||||
.find({ project_id: new ObjectId(projectId) })
|
||||
.toArray()
|
||||
expect(threadRooms.length).to.equal(2)
|
||||
const threadMessage = await getMessage(this.threadMessageId)
|
||||
expect(threadMessage).to.exist
|
||||
const globalThreadMessage = await getMessage(this.globalThreadMessageId)
|
||||
expect(globalThreadMessage).to.exist
|
||||
|
||||
const { response: responseDestroy } =
|
||||
await ChatClient.destroyProject(projectId)
|
||||
expect(responseDestroy.statusCode).to.equal(204)
|
||||
})
|
||||
|
||||
it('should remove the messages and threads from the database', async function () {
|
||||
const threadRooms = await db.rooms
|
||||
.find({ project_id: new ObjectId(projectId) })
|
||||
.toArray()
|
||||
expect(threadRooms.length).to.equal(0)
|
||||
const threadMessage = await getMessage(this.threadMessageId)
|
||||
expect(threadMessage).to.be.null
|
||||
const globalThreadMessage = await getMessage(this.globalThreadMessageId)
|
||||
expect(globalThreadMessage).to.be.null
|
||||
})
|
||||
})
|
||||
})
|
96
services/chat/test/acceptance/js/EditingAMessageTests.js
Normal file
96
services/chat/test/acceptance/js/EditingAMessageTests.js
Normal file
@@ -0,0 +1,96 @@
|
||||
import { ObjectId } from '../../../app/js/mongodb.js'
|
||||
import { expect } from 'chai'
|
||||
|
||||
import * as ChatClient from './helpers/ChatClient.js'
|
||||
import * as ChatApp from './helpers/ChatApp.js'
|
||||
|
||||
describe('Editing a message', async function () {
|
||||
let projectId, userId, threadId
|
||||
before(async function () {
|
||||
await ChatApp.ensureRunning()
|
||||
})
|
||||
|
||||
describe('in a thread', async function () {
|
||||
const content = 'thread message'
|
||||
const newContent = 'updated thread message'
|
||||
let messageId
|
||||
beforeEach(async function () {
|
||||
projectId = new ObjectId().toString()
|
||||
userId = new ObjectId().toString()
|
||||
threadId = new ObjectId().toString()
|
||||
|
||||
const { response, body: message } = await ChatClient.sendMessage(
|
||||
projectId,
|
||||
threadId,
|
||||
userId,
|
||||
content
|
||||
)
|
||||
expect(response.statusCode).to.equal(201)
|
||||
expect(message.id).to.exist
|
||||
expect(message.content).to.equal(content)
|
||||
messageId = message.id
|
||||
})
|
||||
|
||||
describe('without user', function () {
|
||||
beforeEach(async function () {
|
||||
const { response } = await ChatClient.editMessage(
|
||||
projectId,
|
||||
threadId,
|
||||
messageId,
|
||||
newContent
|
||||
)
|
||||
expect(response.statusCode).to.equal(204)
|
||||
})
|
||||
|
||||
it('should then list the updated message in the threads', async function () {
|
||||
const { response, body: threads } =
|
||||
await ChatClient.getThreads(projectId)
|
||||
expect(response.statusCode).to.equal(200)
|
||||
expect(threads[threadId].messages.length).to.equal(1)
|
||||
expect(threads[threadId].messages[0].content).to.equal(newContent)
|
||||
})
|
||||
})
|
||||
|
||||
describe('with the same user', function () {
|
||||
beforeEach(async function () {
|
||||
const { response } = await ChatClient.editMessageWithUser(
|
||||
projectId,
|
||||
threadId,
|
||||
messageId,
|
||||
userId,
|
||||
newContent
|
||||
)
|
||||
expect(response.statusCode).to.equal(204)
|
||||
})
|
||||
|
||||
it('should then list the updated message in the threads', async function () {
|
||||
const { response, body: threads } =
|
||||
await ChatClient.getThreads(projectId)
|
||||
expect(response.statusCode).to.equal(200)
|
||||
expect(threads[threadId].messages.length).to.equal(1)
|
||||
expect(threads[threadId].messages[0].content).to.equal(newContent)
|
||||
})
|
||||
})
|
||||
|
||||
describe('with another user', function () {
|
||||
beforeEach(async function () {
|
||||
const { response } = await ChatClient.editMessageWithUser(
|
||||
projectId,
|
||||
threadId,
|
||||
messageId,
|
||||
new ObjectId(),
|
||||
newContent
|
||||
)
|
||||
expect(response.statusCode).to.equal(404)
|
||||
})
|
||||
|
||||
it('should then list the old message in the threads', async function () {
|
||||
const { response, body: threads } =
|
||||
await ChatClient.getThreads(projectId)
|
||||
expect(response.statusCode).to.equal(200)
|
||||
expect(threads[threadId].messages.length).to.equal(1)
|
||||
expect(threads[threadId].messages[0].content).to.equal(content)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
164
services/chat/test/acceptance/js/GettingMessagesTests.js
Normal file
164
services/chat/test/acceptance/js/GettingMessagesTests.js
Normal file
@@ -0,0 +1,164 @@
|
||||
import { ObjectId } from '../../../app/js/mongodb.js'
|
||||
import { expect } from 'chai'
|
||||
|
||||
import * as ChatClient from './helpers/ChatClient.js'
|
||||
import * as ChatApp from './helpers/ChatApp.js'
|
||||
|
||||
async function getCount() {
|
||||
return await ChatClient.getMetric(line => {
|
||||
return (
|
||||
line.includes('timer_http_request_count') &&
|
||||
line.includes('path="project_{projectId}_messages"') &&
|
||||
line.includes('method="POST"')
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
describe('Getting messages', async function () {
|
||||
const userId1 = new ObjectId().toString()
|
||||
const userId2 = new ObjectId().toString()
|
||||
const content1 = 'foo bar'
|
||||
const content2 = 'hello world'
|
||||
before(async function () {
|
||||
await ChatApp.ensureRunning()
|
||||
})
|
||||
|
||||
describe('globally', async function () {
|
||||
const projectId = new ObjectId().toString()
|
||||
before(async function () {
|
||||
const previousCount = await getCount()
|
||||
const { response } = await ChatClient.sendGlobalMessage(
|
||||
projectId,
|
||||
userId1,
|
||||
content1
|
||||
)
|
||||
expect(response.statusCode).to.equal(201)
|
||||
const { response: response2 } = await ChatClient.sendGlobalMessage(
|
||||
projectId,
|
||||
userId2,
|
||||
content2
|
||||
)
|
||||
expect(response2.statusCode).to.equal(201)
|
||||
const { response: response3, body } = await ChatClient.checkStatus()
|
||||
expect(response3.statusCode).to.equal(200)
|
||||
expect(body).to.equal('chat is alive')
|
||||
expect(await getCount()).to.equal(previousCount + 2)
|
||||
})
|
||||
|
||||
it('should contain the messages and populated users when getting the messages', async function () {
|
||||
const { response, body: messages } =
|
||||
await ChatClient.getGlobalMessages(projectId)
|
||||
expect(response.statusCode).to.equal(200)
|
||||
expect(messages.length).to.equal(2)
|
||||
messages.reverse()
|
||||
expect(messages[0].content).to.equal(content1)
|
||||
expect(messages[0].user_id).to.equal(userId1)
|
||||
expect(messages[1].content).to.equal(content2)
|
||||
expect(messages[1].user_id).to.equal(userId2)
|
||||
})
|
||||
})
|
||||
|
||||
describe('from all the threads', async function () {
|
||||
const projectId = new ObjectId().toString()
|
||||
const threadId1 = new ObjectId().toString()
|
||||
const threadId2 = new ObjectId().toString()
|
||||
|
||||
before(async function () {
|
||||
const { response } = await ChatClient.sendMessage(
|
||||
projectId,
|
||||
threadId1,
|
||||
userId1,
|
||||
'one'
|
||||
)
|
||||
expect(response.statusCode).to.equal(201)
|
||||
const { response: response2 } = await ChatClient.sendMessage(
|
||||
projectId,
|
||||
threadId2,
|
||||
userId2,
|
||||
'two'
|
||||
)
|
||||
expect(response2.statusCode).to.equal(201)
|
||||
const { response: response3 } = await ChatClient.sendMessage(
|
||||
projectId,
|
||||
threadId1,
|
||||
userId1,
|
||||
'three'
|
||||
)
|
||||
expect(response3.statusCode).to.equal(201)
|
||||
const { response: response4 } = await ChatClient.sendMessage(
|
||||
projectId,
|
||||
threadId2,
|
||||
userId2,
|
||||
'four'
|
||||
)
|
||||
expect(response4.statusCode).to.equal(201)
|
||||
})
|
||||
|
||||
it('should contain a dictionary of threads with messages with populated users', async function () {
|
||||
const { response, body: threads } = await ChatClient.getThreads(projectId)
|
||||
expect(response.statusCode).to.equal(200)
|
||||
expect(Object.keys(threads).length).to.equal(2)
|
||||
const thread1 = threads[threadId1]
|
||||
expect(thread1.messages.length).to.equal(2)
|
||||
const thread2 = threads[threadId2]
|
||||
expect(thread2.messages.length).to.equal(2)
|
||||
|
||||
expect(thread1.messages[0].content).to.equal('one')
|
||||
expect(thread1.messages[0].user_id).to.equal(userId1)
|
||||
expect(thread1.messages[1].content).to.equal('three')
|
||||
expect(thread1.messages[1].user_id).to.equal(userId1)
|
||||
|
||||
expect(thread2.messages[0].content).to.equal('two')
|
||||
expect(thread2.messages[0].user_id).to.equal(userId2)
|
||||
expect(thread2.messages[1].content).to.equal('four')
|
||||
expect(thread2.messages[1].user_id).to.equal(userId2)
|
||||
})
|
||||
})
|
||||
|
||||
describe('from a list of threads', function () {
|
||||
const projectId = new ObjectId().toString()
|
||||
const threadId1 = new ObjectId().toString()
|
||||
const threadId2 = new ObjectId().toString()
|
||||
const threadId3 = new ObjectId().toString()
|
||||
|
||||
before(async function () {
|
||||
const { response } = await ChatClient.sendMessage(
|
||||
projectId,
|
||||
threadId1,
|
||||
userId1,
|
||||
'one'
|
||||
)
|
||||
expect(response.statusCode).to.equal(201)
|
||||
const { response: response2 } = await ChatClient.sendMessage(
|
||||
projectId,
|
||||
threadId2,
|
||||
userId2,
|
||||
'two'
|
||||
)
|
||||
expect(response2.statusCode).to.equal(201)
|
||||
const { response: response3 } = await ChatClient.sendMessage(
|
||||
projectId,
|
||||
threadId1,
|
||||
userId1,
|
||||
'three'
|
||||
)
|
||||
expect(response3.statusCode).to.equal(201)
|
||||
})
|
||||
|
||||
it('should contain a dictionary of threads with messages with populated users', async function () {
|
||||
const { response, body: threads } = await ChatClient.generateThreadData(
|
||||
projectId,
|
||||
[threadId1, threadId3]
|
||||
)
|
||||
expect(response.statusCode).to.equal(200)
|
||||
expect(Object.keys(threads).length).to.equal(1)
|
||||
const thread1 = threads[threadId1]
|
||||
expect(thread1.messages.length).to.equal(2)
|
||||
|
||||
expect(thread1.messages[0].content).to.equal('one')
|
||||
expect(thread1.messages[0].user_id).to.equal(userId1)
|
||||
expect(thread1.messages[1].content).to.equal('three')
|
||||
expect(thread1.messages[1].user_id).to.equal(userId1)
|
||||
})
|
||||
})
|
||||
})
|
114
services/chat/test/acceptance/js/ResolvingAThreadTests.js
Normal file
114
services/chat/test/acceptance/js/ResolvingAThreadTests.js
Normal file
@@ -0,0 +1,114 @@
|
||||
import { ObjectId } from '../../../app/js/mongodb.js'
|
||||
import { expect } from 'chai'
|
||||
|
||||
import * as ChatClient from './helpers/ChatClient.js'
|
||||
import * as ChatApp from './helpers/ChatApp.js'
|
||||
|
||||
describe('Resolving a thread', async function () {
|
||||
const projectId = new ObjectId().toString()
|
||||
const userId = new ObjectId().toString()
|
||||
before(async function () {
|
||||
await ChatApp.ensureRunning()
|
||||
})
|
||||
|
||||
describe('with a resolved thread', async function () {
|
||||
const threadId = new ObjectId().toString()
|
||||
const content = 'resolved message'
|
||||
before(async function () {
|
||||
const { response } = await ChatClient.sendMessage(
|
||||
projectId,
|
||||
threadId,
|
||||
userId,
|
||||
content
|
||||
)
|
||||
expect(response.statusCode).to.equal(201)
|
||||
const { response: response2 } = await ChatClient.resolveThread(
|
||||
projectId,
|
||||
threadId,
|
||||
userId
|
||||
)
|
||||
expect(response2.statusCode).to.equal(204)
|
||||
})
|
||||
|
||||
it('should then list the thread as resolved', async function () {
|
||||
const { response, body: threads } = await ChatClient.getThreads(projectId)
|
||||
expect(response.statusCode).to.equal(200)
|
||||
expect(threads[threadId].resolved).to.equal(true)
|
||||
expect(threads[threadId].resolved_by_user_id).to.equal(userId)
|
||||
const resolvedAt = new Date(threads[threadId].resolved_at)
|
||||
expect(new Date() - resolvedAt).to.be.below(1000)
|
||||
})
|
||||
|
||||
it('should list the thread id in the resolved thread ids endpoint', async function () {
|
||||
const { response, body } =
|
||||
await ChatClient.getResolvedThreadIds(projectId)
|
||||
expect(response.statusCode).to.equal(200)
|
||||
expect(body.resolvedThreadIds).to.include(threadId)
|
||||
})
|
||||
})
|
||||
|
||||
describe('when a thread is not resolved', async function () {
|
||||
const threadId = new ObjectId().toString()
|
||||
const content = 'open message'
|
||||
before(async function () {
|
||||
const { response } = await ChatClient.sendMessage(
|
||||
projectId,
|
||||
threadId,
|
||||
userId,
|
||||
content
|
||||
)
|
||||
expect(response.statusCode).to.equal(201)
|
||||
})
|
||||
|
||||
it('should not list the thread as resolved', async function () {
|
||||
const { response, body: threads } = await ChatClient.getThreads(projectId)
|
||||
expect(response.statusCode).to.equal(200)
|
||||
expect(threads[threadId].resolved).to.be.undefined
|
||||
})
|
||||
|
||||
it('should not list the thread in the resolved thread ids endpoint', async function () {
|
||||
const { response, body } =
|
||||
await ChatClient.getResolvedThreadIds(projectId)
|
||||
expect(response.statusCode).to.equal(200)
|
||||
expect(body.resolvedThreadIds).not.to.include(threadId)
|
||||
})
|
||||
})
|
||||
|
||||
describe('when a thread is resolved then reopened', async function () {
|
||||
const threadId = new ObjectId().toString()
|
||||
const content = 'resolved message'
|
||||
before(async function () {
|
||||
const { response } = await ChatClient.sendMessage(
|
||||
projectId,
|
||||
threadId,
|
||||
userId,
|
||||
content
|
||||
)
|
||||
expect(response.statusCode).to.equal(201)
|
||||
const { response: response2 } = await ChatClient.resolveThread(
|
||||
projectId,
|
||||
threadId,
|
||||
userId
|
||||
)
|
||||
expect(response2.statusCode).to.equal(204)
|
||||
const { response: response3 } = await ChatClient.reopenThread(
|
||||
projectId,
|
||||
threadId
|
||||
)
|
||||
expect(response3.statusCode).to.equal(204)
|
||||
})
|
||||
|
||||
it('should not list the thread as resolved', async function () {
|
||||
const { response, body: threads } = await ChatClient.getThreads(projectId)
|
||||
expect(response.statusCode).to.equal(200)
|
||||
expect(threads[threadId].resolved).to.be.undefined
|
||||
})
|
||||
|
||||
it('should not list the thread in the resolved thread ids endpoint', async function () {
|
||||
const { response, body } =
|
||||
await ChatClient.getResolvedThreadIds(projectId)
|
||||
expect(response.statusCode).to.equal(200)
|
||||
expect(body.resolvedThreadIds).not.to.include(threadId)
|
||||
})
|
||||
})
|
||||
})
|
143
services/chat/test/acceptance/js/SendingAMessageTests.js
Normal file
143
services/chat/test/acceptance/js/SendingAMessageTests.js
Normal file
@@ -0,0 +1,143 @@
|
||||
import { ObjectId } from '../../../app/js/mongodb.js'
|
||||
import { expect } from 'chai'
|
||||
|
||||
import * as ChatClient from './helpers/ChatClient.js'
|
||||
import * as ChatApp from './helpers/ChatApp.js'
|
||||
|
||||
describe('Sending a message', async function () {
|
||||
before(async function () {
|
||||
await ChatApp.ensureRunning()
|
||||
})
|
||||
|
||||
describe('globally', async function () {
|
||||
const projectId = new ObjectId().toString()
|
||||
const userId = new ObjectId().toString()
|
||||
const content = 'global message'
|
||||
before(async function () {
|
||||
const { response, body } = await ChatClient.sendGlobalMessage(
|
||||
projectId,
|
||||
userId,
|
||||
content
|
||||
)
|
||||
expect(response.statusCode).to.equal(201)
|
||||
expect(body.content).to.equal(content)
|
||||
expect(body.user_id).to.equal(userId)
|
||||
expect(body.room_id).to.equal(projectId)
|
||||
})
|
||||
|
||||
it('should then list the message in the project messages', async function () {
|
||||
const { response, body: messages } =
|
||||
await ChatClient.getGlobalMessages(projectId)
|
||||
expect(response.statusCode).to.equal(200)
|
||||
expect(messages.length).to.equal(1)
|
||||
expect(messages[0].content).to.equal(content)
|
||||
})
|
||||
})
|
||||
|
||||
describe('to a thread', async function () {
|
||||
const projectId = new ObjectId().toString()
|
||||
const userId = new ObjectId().toString()
|
||||
const threadId = new ObjectId().toString()
|
||||
const content = 'thread message'
|
||||
before(async function () {
|
||||
const { response, body } = await ChatClient.sendMessage(
|
||||
projectId,
|
||||
threadId,
|
||||
userId,
|
||||
content
|
||||
)
|
||||
expect(response.statusCode).to.equal(201)
|
||||
expect(body.content).to.equal(content)
|
||||
expect(body.user_id).to.equal(userId)
|
||||
expect(body.room_id).to.equal(projectId)
|
||||
})
|
||||
|
||||
it('should then list the message in the threads', async function () {
|
||||
const { response, body: threads } = await ChatClient.getThreads(projectId)
|
||||
expect(response.statusCode).to.equal(200)
|
||||
expect(threads[threadId].messages.length).to.equal(1)
|
||||
expect(threads[threadId].messages[0].content).to.equal(content)
|
||||
})
|
||||
|
||||
it('should not appear in the global messages', async function () {
|
||||
const { response, body: messages } =
|
||||
await ChatClient.getGlobalMessages(projectId)
|
||||
expect(response.statusCode).to.equal(200)
|
||||
expect(messages.length).to.equal(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('failure cases', async function () {
|
||||
const projectId = new ObjectId().toString()
|
||||
const userId = new ObjectId().toString()
|
||||
const threadId = new ObjectId().toString()
|
||||
|
||||
describe('with a malformed userId', async function () {
|
||||
it('should return a graceful error', async function () {
|
||||
const { response, body } = await ChatClient.sendMessage(
|
||||
projectId,
|
||||
threadId,
|
||||
'malformed-user',
|
||||
'content'
|
||||
)
|
||||
expect(response.statusCode).to.equal(400)
|
||||
expect(body).to.equal('Invalid userId')
|
||||
})
|
||||
})
|
||||
|
||||
describe('with a malformed projectId', async function () {
|
||||
it('should return a graceful error', async function () {
|
||||
const { response, body } = await ChatClient.sendMessage(
|
||||
'malformed-project',
|
||||
threadId,
|
||||
userId,
|
||||
'content'
|
||||
)
|
||||
expect(response.statusCode).to.equal(400)
|
||||
expect(body).to.equal('Invalid projectId')
|
||||
})
|
||||
})
|
||||
|
||||
describe('with a malformed threadId', async function () {
|
||||
it('should return a graceful error', async function () {
|
||||
const { response, body } = await ChatClient.sendMessage(
|
||||
projectId,
|
||||
'malformed-thread-id',
|
||||
userId,
|
||||
'content'
|
||||
)
|
||||
expect(response.statusCode).to.equal(400)
|
||||
expect(body).to.equal('Invalid threadId')
|
||||
})
|
||||
})
|
||||
|
||||
describe('with no content', async function () {
|
||||
it('should return a graceful error', async function () {
|
||||
const { response, body } = await ChatClient.sendMessage(
|
||||
projectId,
|
||||
threadId,
|
||||
userId,
|
||||
null
|
||||
)
|
||||
expect(response.statusCode).to.equal(400)
|
||||
// Exegesis is responding with validation errors. I can´t find a way to choose the validation error yet.
|
||||
// expect(body).to.equal('No content provided')
|
||||
expect(body.message).to.equal('Validation errors')
|
||||
})
|
||||
})
|
||||
|
||||
describe('with very long content', async function () {
|
||||
it('should return a graceful error', async function () {
|
||||
const content = '-'.repeat(10 * 1024 + 1)
|
||||
const { response, body } = await ChatClient.sendMessage(
|
||||
projectId,
|
||||
threadId,
|
||||
userId,
|
||||
content
|
||||
)
|
||||
expect(response.statusCode).to.equal(400)
|
||||
expect(body).to.equal('Content too long (> 10240 bytes)')
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
15
services/chat/test/acceptance/js/helpers/ChatApp.js
Normal file
15
services/chat/test/acceptance/js/helpers/ChatApp.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import { createServer } from '../../../../app/js/server.js'
|
||||
import { promisify } from 'node:util'
|
||||
|
||||
export { db } from '../../../../app/js/mongodb.js'
|
||||
|
||||
let serverPromise = null
|
||||
|
||||
export async function ensureRunning() {
|
||||
if (!serverPromise) {
|
||||
const { app } = await createServer()
|
||||
const startServer = promisify(app.listen.bind(app))
|
||||
serverPromise = startServer(3010, '127.0.0.1')
|
||||
}
|
||||
return serverPromise
|
||||
}
|
166
services/chat/test/acceptance/js/helpers/ChatClient.js
Normal file
166
services/chat/test/acceptance/js/helpers/ChatClient.js
Normal file
@@ -0,0 +1,166 @@
|
||||
import Request from 'request'
|
||||
|
||||
const request = Request.defaults({
|
||||
baseUrl: 'http://127.0.0.1:3010',
|
||||
})
|
||||
|
||||
async function asyncRequest(options) {
|
||||
return await new Promise((resolve, reject) => {
|
||||
request(options, (err, response, body) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
} else {
|
||||
resolve({ response, body })
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export async function sendGlobalMessage(projectId, userId, content) {
|
||||
return await asyncRequest({
|
||||
method: 'post',
|
||||
url: `/project/${projectId}/messages`,
|
||||
json: {
|
||||
user_id: userId,
|
||||
content,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export async function getGlobalMessages(projectId) {
|
||||
return await asyncRequest({
|
||||
method: 'get',
|
||||
url: `/project/${projectId}/messages`,
|
||||
json: true,
|
||||
})
|
||||
}
|
||||
|
||||
export async function sendMessage(projectId, threadId, userId, content) {
|
||||
return await asyncRequest({
|
||||
method: 'post',
|
||||
url: `/project/${projectId}/thread/${threadId}/messages`,
|
||||
json: {
|
||||
user_id: userId,
|
||||
content,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export async function getThreads(projectId) {
|
||||
return await asyncRequest({
|
||||
method: 'get',
|
||||
url: `/project/${projectId}/threads`,
|
||||
json: true,
|
||||
})
|
||||
}
|
||||
|
||||
export async function resolveThread(projectId, threadId, userId) {
|
||||
return await asyncRequest({
|
||||
method: 'post',
|
||||
url: `/project/${projectId}/thread/${threadId}/resolve`,
|
||||
json: {
|
||||
user_id: userId,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export async function getResolvedThreadIds(projectId) {
|
||||
return await asyncRequest({
|
||||
method: 'get',
|
||||
url: `/project/${projectId}/resolved-thread-ids`,
|
||||
json: true,
|
||||
})
|
||||
}
|
||||
|
||||
export async function editMessage(projectId, threadId, messageId, content) {
|
||||
return await asyncRequest({
|
||||
method: 'post',
|
||||
url: `/project/${projectId}/thread/${threadId}/messages/${messageId}/edit`,
|
||||
json: {
|
||||
content,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export async function editMessageWithUser(
|
||||
projectId,
|
||||
threadId,
|
||||
messageId,
|
||||
userId,
|
||||
content
|
||||
) {
|
||||
return await asyncRequest({
|
||||
method: 'post',
|
||||
url: `/project/${projectId}/thread/${threadId}/messages/${messageId}/edit`,
|
||||
json: {
|
||||
content,
|
||||
userId,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export async function checkStatus() {
|
||||
return await asyncRequest({
|
||||
method: 'get',
|
||||
url: `/status`,
|
||||
json: true,
|
||||
})
|
||||
}
|
||||
|
||||
export async function getMetric(matcher) {
|
||||
const { body } = await asyncRequest({
|
||||
method: 'get',
|
||||
url: `/metrics`,
|
||||
})
|
||||
const found = body.split('\n').find(matcher)
|
||||
if (!found) return 0
|
||||
return parseInt(found.split(' ')[1], 0)
|
||||
}
|
||||
|
||||
export async function reopenThread(projectId, threadId) {
|
||||
return await asyncRequest({
|
||||
method: 'post',
|
||||
url: `/project/${projectId}/thread/${threadId}/reopen`,
|
||||
})
|
||||
}
|
||||
|
||||
export async function deleteThread(projectId, threadId) {
|
||||
return await asyncRequest({
|
||||
method: 'delete',
|
||||
url: `/project/${projectId}/thread/${threadId}`,
|
||||
})
|
||||
}
|
||||
|
||||
export async function deleteMessage(projectId, threadId, messageId) {
|
||||
return await asyncRequest({
|
||||
method: 'delete',
|
||||
url: `/project/${projectId}/thread/${threadId}/messages/${messageId}`,
|
||||
})
|
||||
}
|
||||
|
||||
export async function destroyProject(projectId) {
|
||||
return await asyncRequest({
|
||||
method: 'delete',
|
||||
url: `/project/${projectId}`,
|
||||
})
|
||||
}
|
||||
|
||||
export async function duplicateCommentThreads(projectId, threads) {
|
||||
return await asyncRequest({
|
||||
method: 'post',
|
||||
url: `/project/${projectId}/duplicate-comment-threads`,
|
||||
json: {
|
||||
threads,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export async function generateThreadData(projectId, threads) {
|
||||
return await asyncRequest({
|
||||
method: 'post',
|
||||
url: `/project/${projectId}/generate-thread-data`,
|
||||
json: {
|
||||
threads,
|
||||
},
|
||||
})
|
||||
}
|
9
services/chat/test/setup.js
Normal file
9
services/chat/test/setup.js
Normal file
@@ -0,0 +1,9 @@
|
||||
import chai from 'chai'
|
||||
import chaiAsPromised from 'chai-as-promised'
|
||||
import { ObjectId } from 'mongodb'
|
||||
|
||||
// ensure every ObjectId has the id string as a property for correct comparisons
|
||||
ObjectId.cacheHexString = true
|
||||
|
||||
chai.should()
|
||||
chai.use(chaiAsPromised)
|
12
services/chat/tsconfig.json
Normal file
12
services/chat/tsconfig.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"extends": "../../tsconfig.backend.json",
|
||||
"include": [
|
||||
"app.js",
|
||||
"app/js/**/*",
|
||||
"benchmarks/**/*",
|
||||
"config/**/*",
|
||||
"scripts/**/*",
|
||||
"test/**/*",
|
||||
"types"
|
||||
]
|
||||
}
|
Reference in New Issue
Block a user