Initial commit.

This commit is contained in:
hummingbard 2021-03-01 06:34:49 +03:00
commit 8e5ac94b4b
205 changed files with 36268 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
bin/*
config.toml
bigrooms.json
static/js
static/css
ui/js/node_modules

661
LICENSE Normal file
View File

@ -0,0 +1,661 @@
GNU AFFERO GENERAL PUBLIC LICENSE
Version 3, 19 November 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://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 <https://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
<https://www.gnu.org/licenses/>.

8
Makefile Normal file
View File

@ -0,0 +1,8 @@
all: clean build
build:
go build -o bin/hummingbard cmd/hummingbard/main.go
vendor: clean vendorbuild
vendorbuild:
go build -mod=vendor -o bin/hummingbard cmd/hummingbard/main.go
clean:
rm -f bin/hummingbard

65
README.md Normal file
View File

@ -0,0 +1,65 @@
# Hummingbard
Hummingbard is an experimental client for building decentralized communities on top of [Matrix](https:/matrix.org). See a live instance on [hummingbard.com](https://hummingbard.com)
### What Works
- Register local and federated users
- Federated logins with existing Matrix accounts
- Join local and federated spaces
- Follow local and federated users
- Generic post editor (markdown)
- Quick posts, with images/attachments/links/youtube/etc
- Blog posts with slug/metadata
- Replies to posts
- Sharing posts on profiles and across spaces
- User feed
- Public feed
- Create local and federated spaces
- Different space types - community, gallery
- Customize spaces and user profiles with basic info, custom CSS
- Deeply nested spaces (`/music/jazz/fusion`)
### What Doesn't Work
- Private spaces and user profiles
- Embedded chat in spaces
- Direct Messages
- Registration flows
### Dendrite
Hummingbard relies on these features that are currently only implemented in Dendrite, or expected to be implemented soon:
- Spaces ([MSC2946](https://github.com/matrix-org/matrix-doc/pull/2946))
- Threading ([MSC2836](https://github.com/matrix-org/matrix-doc/pull/2836))
There is a temporary patch in our [forked
Dendrite](https://github.com/hummingbard/dendrite) for paginating threads. This
should not be necessary once upstream Dendrite implements threads fully.
## Install
To run Hummingbard, you'll need:
- [Dendrite fork](https://github.com/hummingbard/dendrite) configured and running
- redis (for session storage)
- postgres (for various non-Matrix storage)
- [goose](https://github.com/pressly/goose) for migrations
### Steps:
1. Clone the repo
2. Copy `config-sample.toml` to `config.toml`, update with DB config etc.
3. Run `make`
4. Run migrations in `db/migrations`
5. Run `npm run build` in `/ui/js`
6. Pull a JSON dump for large matrix rooms with `curl 'https://matrix-client.matrix.org:443/_matrix/client/r0/publicRooms?limit=500' > bigrooms.json` (we avoid large rooms to help Dendrite not consume too much resources)
7. Run the binary `./bin/hummingbard`
### You may want to:
1. Put Hummingbard behind Nginx
2. Server static files via Nginx
3. Use a systemd unit if appropriate
## License
The code is currenly licensed under [AGPLv3](https://www.gnu.org/licenses/agpl-3.0.html). I may choose a more permissive license in the future.

103
cache/cache.go vendored Normal file
View File

@ -0,0 +1,103 @@
package cache
import (
"github.com/dgraph-io/ristretto"
)
type Cache struct {
Users *ristretto.Cache
PublicRooms *ristretto.Cache
LargeRooms *ristretto.Cache
Rooms *ristretto.Cache
RoomMembers *ristretto.Cache
Events *ristretto.Cache
RoomState *ristretto.Cache
Articles *ristretto.Cache
}
func NewCache() (*Cache, error) {
users, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7,
MaxCost: 1 << 30,
BufferItems: 64,
})
if err != nil {
return nil, err
}
pubRooms, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7,
MaxCost: 1 << 30,
BufferItems: 64,
})
if err != nil {
return nil, err
}
rooms, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7,
MaxCost: 1 << 30,
BufferItems: 64,
})
if err != nil {
return nil, err
}
largeRooms, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7,
MaxCost: 1 << 30,
BufferItems: 64,
})
if err != nil {
return nil, err
}
roomMembers, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7,
MaxCost: 1 << 30,
BufferItems: 64,
})
if err != nil {
return nil, err
}
events, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7,
MaxCost: 1 << 30,
BufferItems: 64,
})
if err != nil {
return nil, err
}
state, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7,
MaxCost: 1 << 30,
BufferItems: 64,
})
if err != nil {
return nil, err
}
articles, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7,
MaxCost: 1 << 30,
BufferItems: 64,
})
if err != nil {
return nil, err
}
c := &Cache{
Users: users,
PublicRooms: pubRooms,
Rooms: rooms,
LargeRooms: largeRooms,
RoomMembers: roomMembers,
Events: events,
RoomState: state,
Articles: articles,
}
return c, nil
}

676
client/auth.go Normal file
View File

@ -0,0 +1,676 @@
package client
import (
"encoding/json"
"fmt"
"hummingbard/gomatrix"
"log"
"net/http"
"strings"
"github.com/unrolled/secure"
)
type WellKnownServer struct {
ServerName string `json:"m.server"`
}
func (c *Client) Login() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
type page struct {
BasePage
UserExists bool
ServerDown bool
LoginError bool
LoginUsername string
LoginFederated bool
}
nonce := secure.CSPNonce(r.Context())
t := &page{}
s, err := GetSession(r, c)
if err != nil {
log.Println(err)
}
if s != nil {
x := s.Flashes("login-error")
if len(x) > 0 {
t.LoginError = true
}
u := s.Flashes("login-username")
if len(u) > 0 {
t.LoginUsername = u[0].(string)
}
f := s.Flashes("login-federated")
if len(f) > 0 {
t.LoginFederated = f[0].(bool)
}
s.Save(r, w)
}
query := r.URL.Query()
account := query.Get("account")
if account == "matrix" {
t.LoginFederated = true
}
t.Nonce = nonce
c.Templates.ExecuteTemplate(w, "login", t)
}
}
//Log user in
func (c *Client) ValidateLogin() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
username := r.FormValue("username")
password := r.FormValue("password")
federated := r.FormValue("federated") == "on"
if username == "" || password == "" {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
s, err := GetSession(r, c)
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
fu, us := c.IsFederated(username)
//port is only for my dev environment, this needs to go, or i'm just
//confused
serverName := c.URLScheme(c.Config.Matrix.Server) + fmt.Sprintf(`:%d`, c.Config.Matrix.Port)
//if federation user, we query homeserver at the /well-known endpoint
//for full server path
if fu {
wk, err := WellKnown(c.URLScheme(us.ServerName))
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
log.Println(wk)
serverName = c.URLScheme(wk.ServerName)
username = fmt.Sprintf(`%s:%s`, us.LocalPart, us.ServerName)
}
matrix, err := gomatrix.NewClient(serverName, "", "")
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
rl := &gomatrix.ReqLogin{
Type: "m.login.password",
User: username,
Password: password,
}
resp, err := matrix.Login(rl)
if err != nil {
log.Println(err)
s.AddFlash("Username or Password Wrong", "login-error")
s.AddFlash(username, "login-username")
s.AddFlash(federated, "login-federated")
s.Save(r, w)
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
matrix.SetCredentials(resp.UserID, resp.AccessToken)
//check if a room exists for this username with canonical room alis in
//the format #@username:server.org
un := fmt.Sprintf(`#@%s:%s`, username, c.Config.Matrix.Server)
if fu {
un = fmt.Sprintf(`#%s:%s`, us.LocalPart, us.ServerName)
}
res, err := matrix.ResolveAlias(un)
if err != nil {
log.Println(err)
}
if res != nil {
go func() {
c.OperatorJoinRoom(string(res.RoomID))
}()
}
// If user's room doesn't exist, we create it
newUser := false
if res == nil && fu {
newUser = true
go func() {
u := username
if fu {
u = us.LocalPart
}
crr, err := matrix.CreateRoom(&gomatrix.ReqCreateRoom{
Visibility: "public",
Preset: "public_chat",
RoomAliasName: fmt.Sprintf(`%s`, u),
Name: fmt.Sprintf(`%s's Timeline`, u),
CreationContent: map[string]interface{}{
"m.federate": true,
},
InitialState: []gomatrix.Event{gomatrix.Event{
Type: "m.room.history_visibility",
Content: map[string]interface{}{
"history_visibility": "world_readable",
},
}, gomatrix.Event{
Type: "m.room.guest_access",
Content: map[string]interface{}{
"guest_access": "can_join",
},
}},
})
if err != nil || crr == nil {
log.Println(err)
}
if crr != nil {
go func() {
c.OperatorJoinRoom(string(crr.RoomID))
_, err := matrix.SendStateEvent(crr.RoomID, "m.room.power_levels", "", map[string]interface{}{
"ban": 50,
"events": map[string]interface{}{
"m.room.name": 100,
"m.room.power_levels": 100,
},
"events_default": 0,
"invite": 50,
"kick": 50,
"notifications": map[string]interface{}{
"room": 20,
},
"redact": 50,
"state_default": 50,
"users": map[string]interface{}{
resp.UserID: 100,
c.DefaultUser.UserID: 100,
},
"users_default": 0,
})
if err != nil {
log.Println(err)
}
}()
c.UpdateUserRoomID(r, crr.RoomID)
}
}()
}
pub := fmt.Sprintf(`#public:%s`, c.Config.Client.Domain)
_, err = matrix.JoinRoom(pub, "", nil)
if err != nil {
log.Println(err)
}
rms, err := c.GetUserJoinedRooms(matrix)
if err != nil {
c.Error(w, r)
return
}
profile, err := matrix.GetProfile(resp.UserID)
if err != nil {
log.Println(err)
}
// store user session
token := RandomString(64)
u := User{
AccessToken: token,
MatrixAccessToken: resp.AccessToken,
DeviceID: resp.DeviceID,
HomeServer: resp.HomeServer,
UserID: resp.UserID,
JoinedRooms: rms,
WellKnown: serverName,
Federated: fu,
}
if profile != nil {
if profile.Displayname != nil && len(*profile.Displayname) > 0 {
u.DisplayName = *profile.Displayname
}
if profile.AvatarURL != nil && len(*profile.AvatarURL) > 0 {
u.AvatarURL = StripMXCPrefix(*profile.AvatarURL)
}
}
if res != nil && res.RoomID != "" {
u.RoomID = string(res.RoomID)
}
serialized, err := json.Marshal(u)
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
err = c.Store.Set(token, resp.UserID, 0).Err()
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
err = c.Store.Set(resp.UserID, serialized, 0).Err()
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
s.Values["access_token"] = token
s.Values["device_id"] = resp.DeviceID
s.AddFlash("User logged in", "login-success")
if newUser {
s.AddFlash("Signed Up", "signed-up")
}
s.Save(r, w)
//redirect to index
http.Redirect(w, r, "/", http.StatusSeeOther)
}
}
//signup page
func (c *Client) Signup() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
//us := LoggedInUser(r)
type page struct {
BasePage
UserExists bool
ServerDown bool
SignupError bool
Interactive bool
HomeServer string
}
nonce := secure.CSPNonce(r.Context())
t := &page{}
s, err := GetSession(r, c)
if err != nil {
log.Println(err)
}
if s != nil {
x := s.Flashes("user-exists")
if len(x) > 0 {
t.UserExists = true
s.Save(r, w)
}
y := s.Flashes("server-down")
if len(y) > 0 {
t.ServerDown = true
s.Save(r, w)
}
i := s.Flashes("interactive")
if len(i) > 0 {
t.Interactive = true
t.HomeServer = i[0].(string)
s.Save(r, w)
}
}
t.Nonce = nonce
c.Templates.ExecuteTemplate(w, "signup", t)
}
}
// Copied from Dendrite clientapi/routing/register.go
const (
minPasswordLength = 8 // http://matrix.org/docs/spec/client_server/r0.2.0.html#password-based
maxPasswordLength = 512 // https://github.com/matrix-org/synapse/blob/v0.20.0/synapse/rest/client/v2_alpha/register.py#L161
maxUsernameLength = 254 // http://matrix.org/speculator/spec/HEAD/intro.html#user-identifiers TODO account for domain
sessionIDLength = 24
)
//sign user up
func (c *Client) ValidateSignup() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
username := r.FormValue("username")
password := r.FormValue("password")
repeat := r.FormValue("repeat")
if j := RejectUsername(username); j {
http.Redirect(w, r, "/signup", http.StatusSeeOther)
return
}
if username == "" || password == "" ||
len(username) < 3 ||
len(username) > maxUsernameLength ||
len(password) < minPasswordLength ||
len(password) > maxPasswordLength {
http.Redirect(w, r, "/signup", http.StatusSeeOther)
return
}
if password != repeat {
http.Redirect(w, r, "/signup", http.StatusSeeOther)
return
}
type Auth struct {
Type string
Session string
}
s, err := GetSession(r, c)
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
serverName := c.URLScheme(c.Config.Matrix.Server) + fmt.Sprintf(`:%d`, c.Config.Matrix.Port)
hs := GetHomeServerPart(username)
if strings.Contains(username, ":") {
_, us := c.IsFederated(username)
wk, err := WellKnown(c.URLScheme(us.ServerName))
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
serverName = c.URLScheme(wk.ServerName)
//get rid of the @ prefix
username = us.LocalPart[1:]
}
matrix, err := gomatrix.NewClient(serverName, "", "")
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
//check if username s available
av, err := matrix.RegisterAvailable(&gomatrix.ReqRegisterAvailable{
Username: username,
})
if err != nil {
log.Println(err)
s.AddFlash("Server Down", "server-down")
s.Save(r, w)
http.Redirect(w, r, "/signup", http.StatusSeeOther)
return
}
if av == nil || !av.Available {
s.AddFlash("User Exists", "user-exists")
s.Save(r, w)
http.Redirect(w, r, "/signup", http.StatusSeeOther)
return
}
//actually register the user
resp, inter, err := matrix.Register(&gomatrix.ReqRegister{
Username: username,
Password: password,
Auth: Auth{
Type: "m.login.dummy",
},
})
if err != nil || (resp == nil && inter == nil) {
s.AddFlash("Server Down", "server-down")
s.Save(r, w)
http.Redirect(w, r, "/signup", http.StatusSeeOther)
return
}
if inter != nil {
log.Printf("%+v\n", inter)
s.AddFlash(hs, "interactive")
s.Save(r, w)
http.Redirect(w, r, "/signup", http.StatusSeeOther)
return
}
// create the user's timeline room
matrix.SetCredentials(resp.UserID, resp.AccessToken)
//let them join #public
go func() {
pub := fmt.Sprintf(`#public:%s`, c.Config.Client.Domain)
_, err := matrix.JoinRoom(pub, "", nil)
if err != nil {
log.Println(err)
}
}()
crr, err := matrix.CreateRoom(&gomatrix.ReqCreateRoom{
Visibility: "public",
Preset: "public_chat",
RoomAliasName: fmt.Sprintf(`@%s`, username),
Name: fmt.Sprintf(`%s's Profile`, username),
Topic: fmt.Sprintf(`This is @%s's hummingbard profile.`, username),
CreationContent: map[string]interface{}{
"m.federate": true,
},
InitialState: []gomatrix.Event{gomatrix.Event{
Type: "m.room.history_visibility",
Content: map[string]interface{}{
"history_visibility": "world_readable",
},
}, gomatrix.Event{
Type: "m.room.guest_access",
Content: map[string]interface{}{
"guest_access": "can_join",
},
}, gomatrix.Event{
Type: "com.hummingbard.room",
Content: map[string]interface{}{
"type": "profile",
},
}, gomatrix.Event{
Type: "m.room.power_levels",
Content: map[string]interface{}{
"ban": 50,
"events": map[string]interface{}{
"m.room.name": 100,
"m.room.power_levels": 100,
},
"events_default": 0,
"invite": 50,
"kick": 50,
"notifications": map[string]interface{}{
"room": 20,
},
"redact": 50,
"state_default": 50,
"users": map[string]interface{}{
resp.UserID: 100,
c.DefaultUser.UserID: 100,
},
"users_default": 0,
},
}},
})
if err != nil || crr == nil {
log.Println(err)
c.Error(w, r)
return
}
go func() {
c.OperatorJoinRoom(string(crr.RoomID))
/*
text, html := InitialMessage()
npe := gomatrix.CreatePostEvent{
RoomID: crr.RoomID,
Text: text,
FormattedText: html,
}
_, err = matrix.CreatePost(&npe)
if err != nil {
log.Println(err)
}
*/
}()
rms, err := c.GetUserJoinedRooms(matrix)
if err != nil {
c.Error(w, r)
return
}
//store session
token := RandomString(64)
u := User{
AccessToken: token,
MatrixAccessToken: resp.AccessToken,
DeviceID: resp.DeviceID,
HomeServer: resp.HomeServer,
UserID: resp.UserID,
RefreshToken: resp.RefreshToken,
JoinedRooms: rms,
RoomID: crr.RoomID,
}
serialized, err := json.Marshal(u)
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
err = c.Store.Set(token, resp.UserID, 0).Err()
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
err = c.Store.Set(resp.UserID, serialized, 0).Err()
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
go func() {
err = c.RefreshRoomsCache()
if err != nil {
log.Println(err)
}
}()
s.Values["access_token"] = token
s.Values["device_id"] = resp.DeviceID
s.AddFlash("Signed Up", "signed-up")
s.Save(r, w)
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
}
//log user out, kill session in redis
func (c *Client) Logout() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
s, err := GetSession(r, c)
if err != nil {
log.Println(err)
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
token, ok := s.Values["access_token"].(string)
if ok {
userid, err := c.Store.Get(token).Result()
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
_, err = c.Store.Del(userid).Result()
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
_, err = c.Store.Del(token).Result()
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
s.Values["access_token"] = ""
s.Options.MaxAge = -1
err = s.Save(r, w)
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
}
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
}

153
client/build.go Normal file
View File

@ -0,0 +1,153 @@
package client
import (
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"gopkg.in/fsnotify.v1"
)
func (c *Client) Build() {
BuildCSSFiles()
if c.Config.Mode == "development" {
go CSSWatcher()
}
}
func BuildCSSFiles() {
css := "static/css"
if _, err := os.Stat(css); os.IsNotExist(err) {
os.MkdirAll(css, os.ModePerm)
} else {
os.RemoveAll(css)
os.MkdirAll(css, os.ModePerm)
}
root := "ui/css"
files, err := ioutil.ReadDir(root)
if err != nil {
panic(err)
}
for _, file := range files {
x := strings.Split(file.Name(), ".")
x = append(x, "")
copy(x[2:], x[1:])
x[1] = RandomString(20)
y := strings.Join(x, ".")
from, err := os.Open(root + "/" + file.Name())
if err != nil {
log.Fatal(err)
}
defer from.Close()
to, err := os.OpenFile(css+"/"+y, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
log.Fatal(err)
}
defer to.Close()
_, err = io.Copy(to, from)
if err != nil {
log.Fatal(err)
}
name := filepath.Join(css, y)
cmd := exec.Command("uglifycss", "--output", name, name)
err = cmd.Run()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
}
}
func rebuildCSS(name string) {
css := "static/css/"
root := "ui/css/"
// Get the filename without extension
fn := strings.Split(name, ".")[0]
// Remove all matching files from last build
files, err := ioutil.ReadDir(css)
if err != nil {
panic(err)
}
for _, file := range files {
// find matching files
x := strings.Split(file.Name(), ".")
if fn == x[0] {
os.Remove(css + file.Name())
}
}
x := strings.Split(name, ".")
x = append(x, "")
copy(x[2:], x[1:])
x[1] = RandomString(20)
y := strings.Join(x, ".")
from, err := os.Open(root + "/" + name)
if err != nil {
log.Fatal(err)
}
defer from.Close()
to, err := os.OpenFile(css+"/"+y, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
log.Fatal(err)
}
defer to.Close()
_, err = io.Copy(to, from)
if err != nil {
log.Fatal(err)
}
}
func CSSWatcher() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("modified CSS file:", event.Name)
x := strings.Split(event.Name, "/")
rebuildCSS(x[len(x)-1])
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()
err = watcher.Add("ui/css/")
if err != nil {
log.Fatal(err)
}
<-done
}

265
client/cache.go Normal file
View File

@ -0,0 +1,265 @@
package client
import (
"encoding/json"
"errors"
"fmt"
"hummingbard/gomatrix"
"log"
"sort"
"strings"
"time"
"github.com/tidwall/gjson"
)
type JoinedPublicRoom struct {
RoomID string `json:"room_id"`
Events interface{} `json:"events"`
}
func (c *Client) GetAllPublicRooms() ([]*JoinedPublicRoom, error) {
fil, err := c.Matrix.CreateFilter([]byte(`
{
"room": {
"timeline": {
"limit": 0,
"types": ["com.hummingbard.post"]
}
}
}
`))
sre, err := c.Matrix.SyncRequest(0, "", fil.FilterID, true, "offline")
if err != nil {
log.Println(err)
return nil, err
}
rms := []*JoinedPublicRoom{}
for roomID, room := range sre.Rooms.Join {
st, _ := json.Marshal(room.State.Events)
roomType := gjson.Parse(string(st)).Get(`#(type="com.hummingbard.room")`).Get("content.room_type")
if roomType.String() == "page" || roomType.String() == "post" {
continue
}
alias := gjson.Parse(string(st)).Get(`#(type="m.room.canonical_alias")`).Get("content.alias")
if len(alias.String()) > 0 &&
!strings.Contains(alias.String(), "@") &&
!strings.Contains(alias.String(), "#thread") &&
!strings.Contains(alias.String(), "#public") {
rms = append(rms, &JoinedPublicRoom{RoomID: roomID, Events: room.State.Events})
}
}
return rms, nil
}
type PublicRoom struct {
RoomID string `json:"room_id"`
RoomPath string `json:"room_path"`
RoomAlias string `json:"room_alias"`
Avatar string `json:"avatar"`
}
func (c *Client) GetPublicRooms() ([]*PublicRoom, error) {
fil, err := c.Matrix.CreateFilter([]byte(`
{
"room": {
"timeline": {
"limit": 0,
"types": ["com.hummingbard.post"]
}
}
}
`))
sre, err := c.Matrix.SyncRequest(0, "", fil.FilterID, true, "offline")
if err != nil {
log.Println(err)
return nil, err
}
rms := []*PublicRoom{}
for roomID, room := range sre.Rooms.Join {
st, _ := json.Marshal(room.State.Events)
roomType := gjson.Parse(string(st)).Get(`#(type="com.hummingbard.room")`).Get("content.room_type")
if roomType.String() == "page" || roomType.String() == "post" {
continue
}
alias := gjson.Parse(string(st)).Get(`#(type="m.room.canonical_alias")`).Get("content.alias")
if len(alias.String()) > 0 &&
!strings.Contains(alias.String(), "@") &&
!strings.Contains(alias.String(), "#thread") &&
!strings.Contains(alias.String(), "#public") {
room := &PublicRoom{
RoomID: roomID,
RoomAlias: alias.String(),
}
sp := strings.Split(alias.String(), ":")
rp := sp[0][1:]
p := strings.Split(rp, "_")
al := strings.Join(p, "/")
if strings.Contains(alias.String(), fmt.Sprintf(`:%s`, c.Config.Client.Domain)) {
room.RoomPath = al
} else {
room.RoomPath = fmt.Sprintf(`%s:%s`, al, sp[1])
}
avatar := gjson.Parse(string(st)).Get(`#(type="m.room.avatar")`).Get("content.url")
if avatar.String() != "" {
room.Avatar = c.BuildAvatar(avatar.String())
}
rms = append(rms, room)
}
}
return rms, nil
}
func (c *Client) GetPublicRoomsFromCache() ([]*PublicRoom, error) {
rooms, ok := c.Cache.PublicRooms.Get("public")
if !ok {
return nil, errors.New("couldn't find public room in cache")
}
if v, ok := rooms.([]*PublicRoom); ok {
if len(v) > 0 {
return v, nil
}
}
return nil, nil
}
func (c *Client) RefreshCache() {
err := c.RefreshRoomsCache()
if err != nil {
log.Println(err)
}
}
func (c *Client) RefreshRoomsCache() error {
items, err := c.GetPublicRooms()
if err != nil {
log.Println(err)
return err
}
sort.Slice(items, func(i, j int) bool { return items[i].RoomAlias < items[j].RoomAlias })
c.Cache.PublicRooms.Set("public", items, 1)
return nil
}
func (c *Client) GetRoomAliasFromCache(roomID string) (string, error) {
room, ok := c.Cache.Rooms.Get(roomID)
if !ok {
return "", errors.New("couldn't find room in cache")
}
return room.(gomatrix.PublicRoom).CanonicalAlias, nil
}
func (c *Client) ProcessJoinedRooms(rooms []string) ([]JoinedRoom, error) {
joinedRooms := []JoinedRoom{}
for _, x := range rooms {
r := JoinedRoom{
RoomID: x,
}
room, ok := c.Cache.Rooms.Get(x)
if ok {
r.RoomAlias = room.(gomatrix.PublicRoom).CanonicalAlias
}
joinedRooms = append(joinedRooms, r)
}
return joinedRooms, nil
}
func (c *Client) RefreshRoomEvents(roomID string) (bool, error) {
token := c.DefaultUser.AccessToken
userid := c.DefaultUser.UserID
serverName := c.URLScheme(c.Config.Matrix.Server) + fmt.Sprintf(`:%d`, c.Config.Matrix.Port)
cli, err := gomatrix.NewClient(serverName, userid, token)
if err != nil {
log.Println(err)
return false, nil
}
state, err := cli.RoomState(roomID)
if err != nil {
log.Println(err)
return false, err
}
cli.Prefix = "/_matrix/client/"
rc := c.RoomCreateEventFromState(state)
opts := map[string]interface{}{
"event_id": rc,
"room_id": roomID,
"depth_first": false,
"recent_first": true,
"include_parent": false,
"include_children": true,
"direction": "down",
"limit": 14,
"max_depth": 0,
"max_breadth": 0,
"last_event": "0",
}
relationships, err := cli.GetRelationships(opts)
if err != nil {
log.Println(err)
}
cli.Prefix = "/_matrix/client/r0"
cachedEvents := CachedRoomEvents{
Events: relationships.Events,
Time: time.Now(),
}
c.Cache.Events.Set(roomID, cachedEvents, 1)
return true, nil
}
func (c *Client) RefreshPublicEvents(roomID string) (bool, error) {
serverName := c.URLScheme(c.Config.Matrix.Server) + fmt.Sprintf(`:%d`, c.Config.Matrix.Port)
cli, err := gomatrix.NewClient(serverName, c.DefaultUser.UserID, c.DefaultUser.AccessToken)
if err != nil {
log.Println(err)
return false, err
}
var pev PublicEvents
msg, err := cli.Messages(roomID, "", "", 'b', 23, "")
if err != nil {
log.Println(err)
}
pev = PublicEvents{
Events: msg.Chunk,
LastEvent: msg.End,
}
c.Cache.Events.Set(roomID, pev, 1)
return true, nil
}

391
client/client.go Normal file
View File

@ -0,0 +1,391 @@
package client
import (
"context"
"encoding/json"
"fmt"
"hummingbard/config"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"hummingbard/gomatrix"
"github.com/go-chi/chi"
"github.com/go-redis/redis"
"github.com/gorilla/sessions"
"github.com/robfig/cron/v3"
cache "hummingbard/cache"
)
func init() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
type Client struct {
Config *config.Config
Router *chi.Mux
HTTP *http.Server
Templates *Template
Sessions *sessions.CookieStore
Store *redis.Client
Matrix *gomatrix.Client
DefaultUser User
AnonymousUser User
Cache *cache.Cache
DB *DB
Cron *cron.Cron
}
func (c *Client) Activate() {
log.Println("started server")
idleConnsClosed := make(chan struct{})
go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
signal.Notify(sigint, syscall.SIGTERM)
<-sigint
if err := c.HTTP.Shutdown(context.Background()); err != nil {
log.Printf("HTTP server Shutdown: %v", err)
log.Printf("Shutdown by user")
}
close(idleConnsClosed)
}()
if err := c.HTTP.ListenAndServe(); err != http.ErrServerClosed {
log.Printf("HTTP server ListenAndServe: %v", err)
}
<-idleConnsClosed
}
func Start() {
db, err := NewDB()
if err != nil {
panic(err)
}
conf, err := config.Read()
if err != nil {
panic(err)
}
tmpl, err := NewTemplate()
if err != nil {
panic(err)
}
router := chi.NewRouter()
redis := redis.NewClient(&redis.Options{
Addr: conf.Redis.Address,
Password: conf.Redis.Password,
DB: conf.Redis.DB,
})
server := fmt.Sprintf(`http://%s:%d`, conf.Matrix.Server, conf.Matrix.Port)
matrix, err := gomatrix.NewClient(server, "", "")
if err != nil {
panic(err)
}
//log into the default matrix account
defUser := User{}
username := conf.Client.Domain
password := conf.Matrix.Password
resp, err := matrix.Login(&gomatrix.ReqLogin{
Type: "m.login.password",
User: username,
Password: password,
})
if resp != nil {
defUser.UserID = resp.UserID
defUser.AccessToken = resp.AccessToken
matrix.SetCredentials(resp.UserID, resp.AccessToken)
}
//default account doesn't exist yet, let's create it
if err != nil {
log.Println(err)
av, err := matrix.RegisterAvailable(&gomatrix.ReqRegisterAvailable{
Username: username,
})
if err != nil {
log.Println(err)
}
log.Println(av)
if av == nil || !av.Available {
panic(err)
}
type Auth struct {
Type string
Session string
}
resp, _, err := matrix.Register(&gomatrix.ReqRegister{
Username: username,
Password: password,
Auth: Auth{
Type: "m.login.dummy",
},
})
log.Println(resp)
if err != nil || resp == nil {
panic(err)
}
defUser.UserID = resp.UserID
defUser.AccessToken = resp.AccessToken
matrix.SetCredentials(resp.UserID, resp.AccessToken)
}
anonUser := User{}
//create @anonymous account if it doesn't exist yet
av, err := matrix.RegisterAvailable(&gomatrix.ReqRegisterAvailable{
Username: "anonymous",
})
if err != nil {
log.Println(err)
}
log.Println(av)
if av == nil || !av.Available {
ma, err := gomatrix.NewClient(server, "", "")
if err != nil {
panic(err)
}
resp, err := ma.Login(&gomatrix.ReqLogin{
Type: "m.login.password",
User: "anonymous",
Password: conf.Matrix.AnonymousPassword,
})
if resp != nil {
anonUser.UserID = resp.UserID
anonUser.AccessToken = resp.AccessToken
}
} else {
type Auth struct {
Type string
Session string
}
rep, _, err := matrix.Register(&gomatrix.ReqRegister{
Username: "anonymous",
Password: conf.Matrix.AnonymousPassword,
Auth: Auth{
Type: "m.login.dummy",
},
})
log.Println(rep)
if err != nil || rep == nil {
log.Println(err)
}
anonUser.UserID = rep.UserID
anonUser.AccessToken = rep.AccessToken
}
//create @anonymous user's profile
acr, err := matrix.CreateRoom(&gomatrix.ReqCreateRoom{
Visibility: "public",
Preset: "public_chat",
RoomAliasName: fmt.Sprintf(`@%s`, "anonymous"),
Name: fmt.Sprintf(`@%s's Timeline`, "anonymous"),
Topic: fmt.Sprintf(`This is @%s's hummingbard profile page. Follow them to post on their timeline.`, "anonymous"),
CreationContent: map[string]interface{}{
"m.federate": true,
},
InitialState: []gomatrix.Event{gomatrix.Event{
Type: "m.room.history_visibility",
Content: map[string]interface{}{
"history_visibility": "world_readable",
},
}, gomatrix.Event{
Type: "m.room.guest_access",
Content: map[string]interface{}{
"guest_access": "can_join",
},
}, gomatrix.Event{
Type: "com.hummingbard.room",
Content: map[string]interface{}{
"type": "profile",
},
}, gomatrix.Event{
Type: "m.room.power_levels",
Content: map[string]interface{}{
"ban": 50,
"events": map[string]interface{}{
"m.room.name": 100,
"m.room.power_levels": 100,
},
"events_default": 0,
"invite": 50,
"kick": 50,
"notifications": map[string]interface{}{
"room": 20,
},
"redact": 50,
"state_default": 50,
"users": map[string]interface{}{
anonUser.UserID: 100,
defUser.UserID: 100,
},
"users_default": 0,
},
}},
})
if err != nil || acr == nil {
log.Println(err)
}
pub := fmt.Sprintf(`#@anonymous:%s`, conf.Client.Domain)
_, err = matrix.JoinRoom(pub, "", nil)
if err != nil {
log.Println(err)
}
//does default #public room exist?
//create #public room
un := fmt.Sprintf(`#public:%s`, conf.Matrix.Server)
res, err := matrix.ResolveAlias(un)
if err != nil || res == nil {
log.Println(err)
//no, let's create it
crr, err := matrix.CreateRoom(&gomatrix.ReqCreateRoom{
Preset: "public_chat",
Visibility: "public",
RoomAliasName: "public",
Name: "Public",
CreationContent: map[string]interface{}{
"m.federate": true,
},
InitialState: []gomatrix.Event{gomatrix.Event{
Type: "m.room.history_visibility",
Content: map[string]interface{}{
"history_visibility": "world_readable",
},
}, gomatrix.Event{
Type: "m.room.guest_access",
Content: map[string]interface{}{
"guest_access": "can_join",
},
}},
})
if err != nil || crr == nil {
log.Println(err)
}
pub := fmt.Sprintf(`#public:%s`, conf.Client.Domain)
jr, err := matrix.JoinRoom(pub, "", nil)
if err != nil {
log.Println(err)
}
log.Println("join?", jr)
nm, err := gomatrix.NewClient(server, anonUser.UserID, anonUser.AccessToken)
if err != nil {
log.Println(err)
}
_, err = nm.JoinRoom(pub, "", nil)
if err != nil {
log.Println(err)
}
apub := fmt.Sprintf(`#@anonymous:%s`, conf.Client.Domain)
_, err = nm.JoinRoom(apub, "", nil)
if err != nil {
log.Println(err)
}
}
sess := NewSession(conf.Client.SecureCookie)
sess.Options.Domain = fmt.Sprintf(`.%s`, conf.Client.Domain)
cache, err := cache.NewCache()
if err != nil {
panic(err)
}
cron := cron.New()
c := &Client{
DB: db,
Config: conf,
Matrix: matrix,
HTTP: &http.Server{
ReadTimeout: 21 * time.Second,
WriteTimeout: 60 * time.Second,
IdleTimeout: 120 * time.Second,
Addr: conf.Client.Port,
Handler: router,
},
Router: router,
Templates: tmpl,
Sessions: sess,
Store: redis,
Cache: cache,
DefaultUser: defUser,
AnonymousUser: anonUser,
Cron: cron,
}
c.Middleware()
c.Routes()
c.Build()
//let's cache bigroom items from json dump
bigrooms, err := os.Open("bigrooms.json")
if err != nil {
panic(err)
}
defer bigrooms.Close()
b, err := ioutil.ReadAll(bigrooms)
if err != nil {
panic(err)
}
var rpub *gomatrix.RespPublicRooms
json.Unmarshal(b, &rpub)
for i, _ := range rpub.Chunk {
if rpub.Chunk[i].CanonicalAlias != "" {
c.Cache.LargeRooms.Set(rpub.Chunk[i].CanonicalAlias[1:], true, 1)
}
}
go c.RefreshRoomsCache()
go c.Cron.AddFunc("*/15 * * * *", c.RefreshCache)
go c.Cron.Start()
c.Activate()
}

396
client/create.go Normal file
View File

@ -0,0 +1,396 @@
package client
import (
"context"
"encoding/json"
"fmt"
"hummingbard/gomatrix"
"log"
"net/http"
"strings"
"time"
"github.com/unrolled/secure"
)
func (c *Client) CreateRoom() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
us := LoggedInUser(r)
type page struct {
BasePage
UserExists bool
}
nonce := secure.CSPNonce(r.Context())
t := &page{}
t.Nonce = nonce
t.LoggedInUser = us
c.Templates.ExecuteTemplate(w, "create", t)
}
}
func (c *Client) UsernameAvailable() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
user, err := c.GetTokenUser(token)
if err != nil || user == nil {
log.Println(err)
w.WriteHeader(http.StatusUnauthorized)
return
}
type payload struct {
Username string `json:"username"`
SubSpace bool `json:"sub_space"`
ParentRoomID string `json:"parent_room_id"`
}
var pay payload
if r.Body == nil {
log.Println(err)
http.Error(w, "Please send a request body", 400)
return
}
err = json.NewDecoder(r.Body).Decode(&pay)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
log.Println("recieved payload ", pay)
type Response struct {
Available bool `json:"available"`
}
ff := Response{Available: false}
matrix, err := c.TempMatrixClient(user.UserID, user.MatrixAccessToken)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
username := strings.ToLower(pay.Username)
if pay.SubSpace {
state, err := matrix.RoomState(pay.ParentRoomID)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
alias := c.CanonicalAliasFromState(state)
alias = alias[1:]
s := strings.Split(alias, ":")
localPart := s[0]
username = fmt.Sprintf(`%s_%s`, localPart, strings.ToLower(pay.Username))
}
canon := fmt.Sprintf(`#%s:%s`, username, c.Config.Client.Domain)
if user.Federated {
canon = fmt.Sprintf(`#%s:%s`, username, user.HomeServer)
}
av, err := matrix.ResolveAlias(canon)
if av == nil {
ff.Available = true
}
js, err := json.Marshal(ff)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
}
func (c *Client) ValidateRoomCreation() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
user, err := c.GetTokenUser(token)
if err != nil || user == nil {
log.Println(err)
w.WriteHeader(http.StatusUnauthorized)
return
}
type payload struct {
Username string `json:"username"`
Title string `json:"title"`
About string `json:"about"`
Type string `json:"type"`
Private bool `json:"private"`
NSFW bool `json:"nsfw"`
SubSpace bool `json:"sub_space"`
ParentRoomID string `json:"parent_room_id"`
Page bool `json:"page"`
}
var pay payload
if r.Body == nil {
log.Println(err)
http.Error(w, "Please send a request body", 400)
return
}
err = json.NewDecoder(r.Body).Decode(&pay)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
log.Println("recieved payload ", pay)
type NewRoom struct {
RoomID string `json:"room_id"`
Alias string `json:"alias"`
CanonicalAlias string `json:"canonical_alias"`
Path string `json:"path"`
}
type Response struct {
Created bool `json:"created"`
Room NewRoom `json:"room,omitempty"`
}
ff := Response{
Created: false,
}
matrix, err := c.TempMatrixClient(user.UserID, user.MatrixAccessToken)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
canon := fmt.Sprintf(`#%s:%s`, strings.ToLower(pay.Username), c.Config.Client.Domain)
var state []*gomatrix.Event
alias := ""
if pay.SubSpace {
state, err = matrix.RoomState(pay.ParentRoomID)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
alias = c.CanonicalAliasFromState(state)
s := strings.Split(alias, ":")
localPart := s[0]
username := fmt.Sprintf(`%s_%s`, localPart, strings.ToLower(pay.Username))
canon = fmt.Sprintf(`#%s:%s`, username, c.Config.Client.Domain)
}
_, err = c.Matrix.ResolveAlias(canon)
if err != nil {
//create the room
pl := gomatrix.Event{
Type: "m.room.power_levels",
Content: map[string]interface{}{
"ban": 50,
"events": map[string]interface{}{
"m.room.name": 100,
"m.room.power_levels": 100,
},
"events_default": 0,
"invite": 50,
"kick": 50,
"notifications": map[string]interface{}{
"room": 20,
},
"redact": 50,
"state_default": 50,
"users": map[string]interface{}{
user.UserID: 100,
c.DefaultUser.UserID: 100,
},
"users_default": 0,
},
}
initState := []gomatrix.Event{
gomatrix.Event{
Type: "m.room.history_visibility",
Content: map[string]interface{}{
"history_visibility": "world_readable",
},
}, gomatrix.Event{
Type: "m.room.guest_access",
Content: map[string]interface{}{
"guest_access": "can_join",
},
}, gomatrix.Event{
Type: "com.hummingbard.room",
Content: map[string]interface{}{
"room_type": pay.Type,
},
}, gomatrix.Event{
Type: "m.room.type",
Content: map[string]interface{}{
"type": "m.space",
},
},
pl,
}
username := strings.ToLower(pay.Username)
path := ""
if pay.SubSpace {
alias = alias[1:]
s := strings.Split(alias, ":")
localPart := s[0]
username = fmt.Sprintf(`%s_%s`, localPart, strings.ToLower(pay.Username))
x := strings.Split(localPart, "_")
j := strings.Join(x, "/")
path = fmt.Sprintf(`%s/%s`, j, strings.ToLower(pay.Username))
content := map[string]interface{}{
"canonical_alias": fmt.Sprintf(`#%s:%s`, username, user.HomeServer),
"local_part": username,
"stripped": strings.ToLower(pay.Username),
"room_path": path,
"via": []string{c.Config.Client.Domain},
}
if pay.Page {
content["page"] = true
}
initState = append(initState, gomatrix.Event{
Type: fmt.Sprintf(`%s.parent`, c.Config.Spaces.Prefix),
StateKey: &pay.ParentRoomID,
Content: content,
})
}
if pay.NSFW {
initState = append(initState, gomatrix.Event{
Type: "com.hummingbard.room.nsfw",
Content: map[string]interface{}{
"nsfw": true,
},
})
}
req := &gomatrix.ReqCreateRoom{
Preset: "public_chat",
Visibility: "public",
RoomAliasName: strings.ToLower(pay.Username),
Name: pay.Title,
Topic: pay.About,
CreationContent: map[string]interface{}{
"m.federate": true,
},
InitialState: initState,
}
if pay.SubSpace {
req.RoomAliasName = username
}
if pay.Private {
req.Preset = "private_chat"
req.Visibility = "private"
}
crr, err := matrix.CreateRoom(req)
if err != nil || crr == nil {
log.Println(err)
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
ff.Created = true
canon := fmt.Sprintf(`#%s:%s`, pay.Username, user.HomeServer)
ff.Room = NewRoom{
RoomID: crr.RoomID,
Alias: strings.ToLower(pay.Username),
CanonicalAlias: canon,
Path: path,
}
err = c.RefreshRoomsCache()
if err != nil {
log.Println(err)
}
err = c.UpdateJoinedRooms(matrix, r)
if err != nil {
log.Println(err)
}
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 3*time.Second)
if !pay.Page {
alias := fmt.Sprintf(`#%s:%s`, strings.ToLower(username), user.HomeServer)
path := username
if strings.Contains(path, "_") {
s := strings.Split(path, "_")
path = strings.Join(s, "/")
}
c.Cache.Rooms.Set(crr.RoomID, alias, 1)
}
if crr != nil {
c.OperatorJoinRoom(crr.RoomID)
if pay.Type != "gallery" && pay.Type == "page" {
text, html := InitialMessage()
npe := gomatrix.CreatePostEvent{
RoomID: crr.RoomID,
Text: text,
FormattedText: html,
}
_, err := matrix.CreatePost(&npe)
if err != nil {
log.Println(err)
log.Println(err)
}
}
}
}
js, err := json.Marshal(ff)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
}

174
client/db.go Normal file
View File

@ -0,0 +1,174 @@
package client
import (
"context"
"database/sql"
"fmt"
"hummingbard/config"
"log"
"regexp"
"strings"
"time"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
)
type DB struct {
*sqlx.DB
}
// NewDB returns a new database instace
func NewDB() (*DB, error) {
c, err := config.Read()
if err != nil {
panic(err)
}
conn := fmt.Sprintf("host=%s port=%s user=%s "+
"password=%s dbname=%s sslmode=%s",
c.DB.Host, c.DB.Port, c.DB.User, c.DB.Password, c.DB.Name, c.DB.SSL)
db, err := sqlx.Open("postgres", conn)
if err != nil {
return nil, err
}
if err = db.Ping(); err != nil {
return nil, err
}
db.SetMaxOpenConns(25)
db.SetMaxIdleConns(25)
db.SetConnMaxLifetime(5 * time.Minute)
store := &DB{db}
return store, nil
}
func Slugify(title string) string {
sp := strings.Split(title, " ")
jp := strings.Join(sp, "-")
lp := strings.ToLower(jp)
reg := regexp.MustCompile("[^a-zA-Z0-9-]+")
slug := reg.ReplaceAllString(lp, "")
return slug
}
func (c *Client) DoesSlugExist(ctx context.Context, roomPath, slug string) (bool, error) {
var exists bool
err := c.DB.QueryRow("select exists(select 1 from slug_to_event where room_path=$1 and slug=$2)", roomPath, slug).Scan(&exists)
if err != nil || err == sql.ErrNoRows {
log.Println(err)
return true, err
}
return exists, nil
}
func (c *Client) GetSlugEventID(ctx context.Context, roomPath, slug string) (string, error) {
var event string
err := c.DB.QueryRow("select event_id from slug_to_event where room_path=$1 and slug=$2", roomPath, slug).Scan(&event)
if err != nil || err == sql.ErrNoRows {
log.Println(err)
return "", err
}
return event, nil
}
func (c *Client) UpdateEventSlug(ctx context.Context, roomPath, slug, event string) (bool, error) {
_, err := c.DB.Exec(`INSERT INTO slug_to_event(room_path, slug, event_id) VALUES($1, $2, $3)`, roomPath, slug, event)
log.Println(err)
if err != nil {
return false, err
}
return true, nil
}
func (c *Client) AddRoom(ctx context.Context, userID, roomID, roomAlias, path string) error {
_, err := c.DB.Exec(`INSERT INTO rooms(user_id, room_id, room_alias, room_path) VALUES($1, $2, $3, $4)`, userID, roomID, roomAlias, path)
log.Println(err)
if err != nil {
return err
}
return nil
}
func (c *Client) GetUserRooms(ctx context.Context, userID string) ([]*OwnedRoom, error) {
query := `
select
rooms.room_id,
rooms.room_alias
FROM rooms
WHERE user_id=$1
LIMIt 53
`
sargs := []interface{}{userID}
rows, err := c.DB.Queryx(query, sargs...)
if err != nil || err == sql.ErrNoRows {
log.Println(err)
return nil, err
}
rooms := []*OwnedRoom{}
for rows.Next() {
results := make(map[string]interface{})
err = rows.MapScan(results)
room := OwnedRoom{}
id, ok := results["room_id"].(string)
if ok {
room.RoomID = id
}
alias, ok := results["room_alias"].(string)
if ok {
room.RoomAlias = alias
}
rooms = append(rooms, &room)
}
return rooms, nil
}
func (c *Client) GetAllRooms(ctx context.Context) (map[string]string, error) {
query := `
select
rooms.room_id,
rooms.room_alias
FROM rooms
`
rows, err := c.DB.Queryx(query)
if err != nil || err == sql.ErrNoRows {
log.Println(err)
return nil, err
}
rooms := map[string]string{}
for rows.Next() {
results := make(map[string]interface{})
err = rows.MapScan(results)
id, ok := results["room_id"].(string)
alias, ok := results["room_alias"].(string)
if ok {
rooms[id] = alias
}
}
return rooms, nil
}

63
client/dispatch.go Normal file
View File

@ -0,0 +1,63 @@
package client
import (
"errors"
"net/http"
"strings"
"github.com/go-chi/chi"
)
func (c *Client) Dispatch() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
//This handler should do all sorts of path/routing stuff but for now we
//hand off to timeline handler
c.Timeline(w, r)
}
}
type PathItemsSkeleton struct {
Path string `json:"path"`
Items []string `json:"items"`
Length int `json:"length"`
}
func (c *Client) PathItems(r *http.Request) (*PathItemsSkeleton, error) {
path := chi.URLParam(r, "*")
pathItems := strings.Split(path, "/")
if len(pathItems) == 0 {
return nil, errors.New("Empty Path")
}
pi := &PathItemsSkeleton{
Path: path,
Items: pathItems,
Length: len(pathItems),
}
if len(pathItems) > 1 {
x := []string{}
for _, item := range pathItems {
if strings.Contains(item, "$") {
x = append(x, item)
} else {
x = append(x, strings.ToLower(item))
}
}
pi.Path = strings.Join(x, "/")
}
return pi, nil
}
func (c *PathItemsSkeleton) ItemByPosition(i int) (string, error) {
if len(c.Items) == 0 {
return "", errors.New("Empty Path.")
}
return c.Items[i], nil
}

305
client/events.go Normal file
View File

@ -0,0 +1,305 @@
package client
import (
"fmt"
"html/template"
"hummingbard/gomatrix"
"log"
"sort"
"strings"
"time"
)
func (c *Client) SortRelationships(events []gomatrix.Event, rootID string) []*gomatrix.Event {
items := []*gomatrix.Event{}
for i, _ := range events {
log.Println("type is", events[i].Type)
if events[i].ID == rootID {
items = append(items, &events[i])
}
if mr, ok := events[i].Content["m.relationship"].(map[string]interface{}); ok {
if mr["event_id"] == rootID {
items = append(items, &events[i])
}
}
}
var findReplies func(id string) []*gomatrix.Event
findReplies = func(id string) []*gomatrix.Event {
replies := []*gomatrix.Event{}
for i, _ := range events {
if mr, ok := events[i].Content["m.relationship"].(map[string]interface{}); ok {
if mr["event_id"] == id {
x := events[i]
x.Replies = findReplies(events[i].ID)
replies = append(replies, &x)
}
}
}
return replies
}
for _, event := range items {
if event.ID == rootID {
continue
}
event.Replies = findReplies(event.ID)
}
return items
}
func (c *Client) SortReplies(events []gomatrix.Event, rootID string, s string) []*gomatrix.Event {
items := []*gomatrix.Event{}
for i, _ := range events {
if mr, ok := events[i].Content["m.relationship"].(map[string]interface{}); ok {
if mr["event_id"] == rootID {
items = append(items, &events[i])
}
}
}
var findReplies func(id string) ([]*gomatrix.Event, int)
findReplies = func(id string) ([]*gomatrix.Event, int) {
replies := []*gomatrix.Event{}
for i, _ := range events {
if mr, ok := events[i].Content["m.relationship"].(map[string]interface{}); ok {
if mr["event_id"] == id {
x := events[i]
reps, count := findReplies(events[i].ID)
x.Replies = reps
x.TotalReplies = len(reps) + count
replies = append(replies, &x)
}
}
}
c := 0
for _, x := range replies {
c += x.TotalReplies
}
sort.Slice(replies, func(i, j int) bool { return replies[j].Time.After(replies[i].Time) })
return replies, c
}
for _, event := range items {
if event.ID == rootID {
continue
}
reps, count := findReplies(event.ID)
event.Replies = reps
event.TotalReplies = len(reps) + count
}
switch s {
case "replies":
sort.Slice(items, func(i, j int) bool { return items[i].TotalReplies > items[j].TotalReplies })
case "recent":
sort.Slice(items, func(i, j int) bool { return items[i].Time.After(items[j].Time) })
default:
sort.Slice(items, func(i, j int) bool { return items[j].Time.After(items[i].Time) })
}
return items
}
func (c *Client) ProcessMessages(resp []gomatrix.Event, state []*gomatrix.Event, user *User) []gomatrix.Event {
members := []*gomatrix.Event{}
for _, m := range state {
if m.Type == "m.room.member" {
members = append(members, m)
}
}
events := []gomatrix.Event{}
for i, _ := range resp {
if resp[i].Content["m.relationship"] == nil {
continue
}
c.ProcessEvent(&resp[i], user)
_, sok := resp[i].Content["shared_post"].(map[string]interface{})
for m, _ := range members {
if resp[i].Sender == members[m].Sender {
dn, ok := members[m].Content["displayname"].(string)
if ok {
resp[i].Author.DisplayName = dn
}
au, ok := members[m].Content["avatar_url"].(string)
if ok {
resp[i].Author.AvatarURL = c.BuildAvatar(au)
}
}
if sok && resp[i].SharedPost.Sender == members[m].Sender {
dn, ok := members[m].Content["displayname"].(string)
if ok {
resp[i].SharedPost.Author.DisplayName = dn
}
au, ok := members[m].Content["avatar_url"].(string)
if ok {
resp[i].SharedPost.Author.AvatarURL = c.BuildAvatar(au)
}
}
}
if resp[i].Type == "com.hummingbard.post" {
events = append(events, resp[i])
}
}
sort.Slice(events, func(i, j int) bool { return events[i].Time.After(events[j].Time) })
resp = events
return resp
}
func (c *Client) ProcessEvent(x *gomatrix.Event, user *User) {
red, ok := x.Unsigned["redacted_by"].(string)
if ok && len(red) > 0 {
x.Redacted = true
return
}
y := time.Unix(x.Timestamp/1000, x.Timestamp/10000*1000)
x.Time = y
ft := y.Format("Mon Jan 2 15:04:05 MST 2006")
x.Date = ft
x.When = FormatTime(y)
//convert content body markdown to HTML
body, ok := x.Content["formatted_body"].(string)
if ok {
x.Content["bodyHTML"] = template.HTML(body)
} else {
body, ok := x.Content["body"].(string)
if ok {
x.Content["bodyHTML"] = template.HTML(body)
}
}
//format room_path
roomPath, ok := x.Content["room_path"].(string)
if ok {
if strings.Contains(roomPath, "_") &&
strings.Contains(roomPath, ":") {
sp := strings.Split(roomPath, ":")
xx := strings.Split(sp[0], "_")
rem := xx[1:]
rest := strings.Join(rem, "/")
path := fmt.Sprintf(`%s:%s/%s`, xx[0], sp[1], rest)
x.Content["room_path"] = path
}
x.Content["bodyHTML"] = template.HTML(body)
}
if strings.Contains(x.Sender, c.Config.Client.Domain) {
s := strings.Split(x.Sender, ":")
x.Author.FormattedID = s[0]
} else {
x.Author.FormattedID = x.Sender
}
if ch, ok := x.Unsigned["children"].(map[string]interface{}); ok {
x.TotalReplies = int(ch["m.reference"].(float64)) - 1
}
shp, ok := x.Content["shared_post"].(map[string]interface{})
if ok {
sp := &gomatrix.Event{
Sender: shp["sender"].(string),
ID: shp["event_id"].(string),
RoomID: shp["room_id"].(string),
Type: shp["type"].(string),
Content: shp["content"].(map[string]interface{}),
}
content, ok := shp["content"].(map[string]interface{})
if ok {
body, ok := content["formatted_body"]
if ok {
sp.Content["bodyHTML"] = template.HTML(body.(string))
} else {
body, ok := content["body"].(string)
if ok {
sp.Content["bodyHTML"] = template.HTML(body)
}
}
article, ok := content["com.hummingbard.article"]
if ok && article != nil {
sp.IsArticle = true
}
}
ts, ok := shp["origin_server_ts"].(float64)
if ok {
tst := int64(ts)
sp.Timestamp = tst
y := time.Unix(tst/1000, tst/10000*1000)
ft := y.Format("Mon Jan 2 15:04:05 MST 2006")
sp.Date = ft
sp.When = FormatTime(y)
}
x.SharedPost = sp
}
if user != nil && len(user.UserID) > 0 {
if user.UserID == x.Sender {
x.Owner = true
}
}
body, ok = x.Content["body"].(string)
if ok {
x.Content["body_length"] = len(body)
}
x.ShortID = fmt.Sprintf(`ev%s`, RandomString(9))
t := false
s := false
article, ok := x.Content["com.hummingbard.article"].(map[string]interface{})
if ok {
if v, ok := article["title"].(string); ok && len(v) > 0 {
t = true
}
if v, ok := article["slug"].(string); ok && len(v) > 0 {
s = true
}
if v, ok := article["featured_image"].(map[string]interface{}); ok {
if r, ok := v["mxc"].(string); ok && len(r) > 0 {
v["mxc"] = c.BuildImage(r)
}
}
if t && s {
x.IsArticle = true
}
}
}

600
client/index.go Normal file
View File

@ -0,0 +1,600 @@
package client
import (
"encoding/json"
"fmt"
"html/template"
"hummingbard/gomatrix"
"log"
"net/http"
"sort"
"strconv"
"strings"
"github.com/unrolled/secure"
)
type PublicEvents struct {
Events []gomatrix.Event
LastEvent string
}
func (c *Client) PublicFeed() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
us := LoggedInUser(r)
serverName := c.URLScheme(c.Config.Matrix.Server) + fmt.Sprintf(`:%d`, c.Config.Matrix.Port)
cli, err := gomatrix.NewClient(serverName, c.DefaultUser.UserID, c.DefaultUser.AccessToken)
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
room := fmt.Sprintf(`#public:%s`, c.Config.Client.Domain)
ra, err := cli.ResolveAlias(room)
if err != nil {
log.Println(err)
c.NotFound(w, r)
return
}
roomID := string(ra.RoomID)
state, err := cli.RoomState(roomID)
if err != nil {
log.Println(err)
}
t := TimelinePage{
Room: Room{
Path: "public",
ID: string(ra.RoomID),
},
RoomState: state,
IsUserProfile: false,
}
var pev PublicEvents
cachedEvents, ok := c.Cache.Events.Get(roomID)
if ok {
if x, ok := cachedEvents.(PublicEvents); ok {
pev = x
log.Println(len(pev.Events))
}
} else {
msg, err := cli.Messages(roomID, "", "", 'b', 23, "")
if err != nil {
log.Println(err)
}
pev = PublicEvents{
Events: msg.Chunk,
LastEvent: msg.End,
}
c.Cache.Events.Set(roomID, pev, 1)
}
t.Posts = c.ProcessMessages(pev.Events, state, us)
t.LastEvent = pev.LastEvent
nonce := secure.CSPNonce(r.Context())
t.Nonce = nonce
t.LoggedInUser = us
ip, err := json.Marshal(t.Posts)
if err != nil {
c.Error(w, r)
return
}
t.InitialPosts = string(ip)
t.Room.Type = "feed"
t.Room.Alias = "public"
if c.Config.Mode == "development" {
t.HomeServerURL = c.URLScheme(c.Config.Matrix.Server) + fmt.Sprintf(`:%d`, c.Config.Matrix.Port)
} else {
t.HomeServerURL = fmt.Sprintf(`https://%s`, c.Config.Matrix.Server)
}
c.Templates.ExecuteTemplate(w, "public", t)
}
}
func (c *Client) Index() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
us := LoggedInUser(r)
if us != nil {
c.IndexUser(w, r)
return
}
serverName := c.URLScheme(c.Config.Matrix.Server) + fmt.Sprintf(`:%d`, c.Config.Matrix.Port)
cli, err := gomatrix.NewClient(serverName, c.DefaultUser.UserID, c.DefaultUser.AccessToken)
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
room := fmt.Sprintf(`#public:%s`, c.Config.Client.Domain)
ra, err := cli.ResolveAlias(room)
if err != nil {
log.Println(err)
c.NotFound(w, r)
return
}
roomID := string(ra.RoomID)
state, err := cli.RoomState(roomID)
if err != nil {
log.Println(err)
}
t := TimelinePage{
Room: Room{
Path: "public",
ID: string(ra.RoomID),
},
RoomState: state,
IsUserProfile: false,
}
var pev PublicEvents
cachedEvents, ok := c.Cache.Events.Get(roomID)
if ok {
if x, ok := cachedEvents.(PublicEvents); ok {
pev = x
}
} else {
msg, err := cli.Messages(roomID, "", "", 'b', 23, "")
if err != nil {
log.Println(err)
}
pev = PublicEvents{
Events: msg.Chunk,
LastEvent: msg.End,
}
c.Cache.Events.Set(roomID, pev, 1)
}
t.Posts = c.ProcessMessages(pev.Events, state, us)
t.LastEvent = pev.LastEvent
rooms, err := c.GetPublicRoomsFromCache()
if err != nil {
log.Println(err)
}
if rooms != nil && len(rooms) > 0 {
r := []*PublicRoom{}
for _, x := range rooms {
/*
if i == 53 {
break
}
*/
if !strings.Contains(x.RoomPath, "/") {
r = append(r, x)
}
}
t.PublicRooms = r
}
//let's add them manually for now
/*
rooms := []string{
"art",
"design",
"environment",
"eargasm:matrix.org",
"hummingbard",
"hummingbard/bugs",
"hummingbard/feature-requests",
"music",
"music/classical",
"music/jazz",
"music/jazz/fusion",
"music/metal",
"programming",
"rhythm:matrix.org",
"news",
"technology",
"videos",
}
*/
nonce := secure.CSPNonce(r.Context())
t.Nonce = nonce
ip, err := json.Marshal(t.Posts)
if err != nil {
c.Error(w, r)
return
}
t.InitialPosts = string(ip)
t.Room.Type = "feed"
t.Room.Alias = "public"
if c.Config.Mode == "development" {
t.HomeServerURL = c.URLScheme(c.Config.Matrix.Server) + fmt.Sprintf(`:%d`, c.Config.Matrix.Port)
} else {
t.HomeServerURL = fmt.Sprintf(`https://%s`, c.Config.Matrix.Server)
}
c.Templates.ExecuteTemplate(w, "index", t)
}
}
func (c *Client) IndexUser(w http.ResponseWriter, r *http.Request) {
user := LoggedInUser(r)
s, err := GetSession(r, c)
if err != nil {
log.Println(err)
}
if s != nil {
x := s.Flashes("signed-up")
if len(x) > 0 {
s.Save(r, w)
c.WelcomePage(w, r)
return
}
}
serverName := c.URLScheme(c.Config.Matrix.Server) + fmt.Sprintf(`:%d`, c.Config.Matrix.Port)
matrix, err := gomatrix.NewClient(serverName, c.DefaultUser.UserID, c.DefaultUser.AccessToken)
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
posts := []gomatrix.Event{}
feedItems := []*IndexFeed{}
limit := 14
l := len(user.JoinedRooms)
if len(user.JoinedRooms) > 0 {
switch {
case l > 14:
limit = 2
case l > 9 && l < 14:
limit = 3
case l > 5 && l <= 9:
limit = 3
case l > 2 && l <= 4:
limit = 6
case l == 2:
limit = 8
}
for _, room := range user.JoinedRooms {
matrix.Prefix = "/_matrix/client/r0"
state, err := matrix.RoomState(room.RoomID)
if err != nil {
log.Println(err)
continue
}
matrix.Prefix = "/_matrix/client/"
rc := c.RoomCreateEventFromState(state)
opts := map[string]interface{}{
"event_id": rc,
"room_id": room.RoomID,
"depth_first": false,
"recent_first": false,
"include_parent": false,
"include_children": true,
"direction": "down",
"limit": limit,
"max_depth": 0,
"max_breadth": 0,
"last_event": "0",
}
relationships, err := matrix.GetRelationships(opts)
if err != nil {
log.Println(err)
continue
}
if relationships != nil && len(relationships.Events) > 0 {
processed := c.ProcessMessages(relationships.Events, state, user)
posts = append(posts, processed...)
lastEvent := relationships.Events[len(relationships.Events)-1].Timestamp
feedItems = append(feedItems, &IndexFeed{
RoomID: room.RoomID,
LastEvent: lastEvent,
})
}
}
sort.Slice(posts, func(i, j int) bool { return posts[j].Time.Before(posts[i].Time) })
}
type page struct {
Room Room
BasePage
LoggedInUser *User
State interface{}
ProfileLink template.URL
Posts interface{}
HomeServerURL string
FeedItems interface{}
InitialPosts interface{}
Depth int
}
nonce := secure.CSPNonce(r.Context())
t := &page{
Room: Room{
Path: "user-index",
},
Posts: posts,
FeedItems: feedItems,
}
if user.Federated {
t.ProfileLink = template.URL(user.UserID)
} else {
sp := strings.Split(user.UserID, ":")
t.ProfileLink = template.URL(sp[0])
}
if c.Config.Mode == "development" {
t.HomeServerURL = c.URLScheme(c.Config.Matrix.Server) + fmt.Sprintf(`:%d`, c.Config.Matrix.Port)
} else {
t.HomeServerURL = fmt.Sprintf(`https://%s`, c.Config.Matrix.Server)
}
t.Nonce = nonce
ip, err := json.Marshal(t.Posts)
if err != nil {
c.Error(w, r)
return
}
t.InitialPosts = string(ip)
t.LoggedInUser = user
c.Templates.ExecuteTemplate(w, "index-user", t)
}
func (c *Client) WelcomePage(w http.ResponseWriter, r *http.Request) {
user := LoggedInUser(r)
type page struct {
BasePage
LoggedInUser *User
HomeServerURL string
Rooms interface{}
}
nonce := secure.CSPNonce(r.Context())
t := &page{}
rooms, err := c.GetPublicRoomsFromCache()
if err != nil {
log.Println(err)
}
if rooms != nil && len(rooms) > 0 {
r := []*PublicRoom{}
for i, x := range rooms {
if i == 53 {
break
}
if !(strings.Contains(x.RoomPath, "/") &&
strings.Contains(x.RoomPath, ":")) {
r = append(r, x)
}
}
t.Rooms = r
}
t.Nonce = nonce
t.LoggedInUser = user
if c.Config.Mode == "development" {
t.HomeServerURL = c.URLScheme(c.Config.Matrix.Server) + fmt.Sprintf(`:%d`, c.Config.Matrix.Port)
} else {
t.HomeServerURL = fmt.Sprintf(`https://%s`, c.Config.Matrix.Server)
}
c.Templates.ExecuteTemplate(w, "welcome", t)
}
func (c *Client) GetFeedEvents() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
user, err := c.GetTokenUser(token)
if err != nil || user == nil {
log.Println(err)
}
type payload struct {
Feed []struct {
RoomID string `json:"room_id"`
LastEvent int64 `json:"last_event"`
} `json:"feed"`
}
var pay payload
if r.Body == nil {
log.Println(err)
http.Error(w, "Please send a request body", 400)
return
}
err = json.NewDecoder(r.Body).Decode(&pay)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
log.Println("recieved payload ", pay)
serverName := c.URLScheme(c.Config.Matrix.Server) + fmt.Sprintf(`:%d`, c.Config.Matrix.Port)
matrix, err := gomatrix.NewClient(serverName, c.DefaultUser.UserID, c.DefaultUser.AccessToken)
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
posts := []gomatrix.Event{}
feedItems := []*IndexFeed{}
limit := 14
l := len(pay.Feed)
if len(pay.Feed) > 0 {
switch {
case l > 14:
limit = 1
case l > 9 && l < 14:
limit = 3
case l > 5 && l <= 9:
limit = 3
case l > 2 && l <= 4:
limit = 6
case l == 2:
limit = 8
}
for _, room := range pay.Feed {
matrix.Prefix = "/_matrix/client/r0"
state, err := matrix.RoomState(room.RoomID)
if err != nil {
log.Println(err)
continue
}
matrix.Prefix = "/_matrix/client/"
rc := c.RoomCreateEventFromState(state)
lastEvent := room.LastEvent
le := strconv.FormatInt(lastEvent, 10)
opts := map[string]interface{}{
"event_id": rc,
"room_id": room.RoomID,
"depth_first": false,
"recent_first": false,
"include_parent": false,
"include_children": true,
"direction": "down",
"limit": limit,
"max_depth": 0,
"max_breadth": 0,
"last_event": le,
}
relationships, err := matrix.GetRelationships(opts)
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
if relationships != nil && len(relationships.Events) > 0 {
processed := c.ProcessMessages(relationships.Events, state, user)
posts = append(posts, processed...)
lastEvent := relationships.Events[len(relationships.Events)-1].Timestamp
feedItems = append(feedItems, &IndexFeed{
RoomID: room.RoomID,
LastEvent: lastEvent,
})
}
}
}
sort.Slice(posts, func(i, j int) bool { return posts[j].Time.Before(posts[i].Time) })
type Response struct {
Posts interface{} `json:"posts"`
FeedItems interface{} `json:"feed_items"`
}
ff := Response{
Posts: posts,
FeedItems: feedItems,
}
js, err := json.Marshal(ff)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
}
type IndexFeed struct {
RoomID string `json:"room_id"`
LastEvent int64 `json:"last_event"`
}
func (c *Client) ProcessUserFeed(join *gomatrix.RespSync, user *User) ([]gomatrix.Event, []*IndexFeed, error) {
publicRoom := ""
for _, p := range user.JoinedRooms {
if strings.Contains(p.RoomAlias, "#public:") {
publicRoom = p.RoomID
}
}
events := []gomatrix.Event{}
feedItems := []*IndexFeed{}
for roomID, room := range join.Rooms.Join {
for i, _ := range user.JoinedRooms {
if user.JoinedRooms[i].RoomID == roomID && user.JoinedRooms[i].RoomAlias != "" &&
user.JoinedRooms[i].RoomID != publicRoom {
for _, y := range room.Timeline.Events {
c.ProcessEvent(&y, user)
events = append(events, y)
}
/*
feedItems = append(feedItems, &IndexFeed{
RoomID: roomID,
LastEvent: room.Timeline.PrevBatch,
})
*/
}
}
}
sort.Slice(events, func(i, j int) bool { return events[i].Time.After(events[j].Time) })
return events, feedItems, nil
}

221
client/link.go Normal file
View File

@ -0,0 +1,221 @@
package client
import (
"context"
"encoding/json"
"log"
"net/http"
"net/url"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/gocolly/colly"
"github.com/gocolly/colly/extensions"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
)
func (c *Client) LinkMetadata() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
user, err := c.GetTokenUser(token)
if err != nil || user == nil {
log.Println(err)
w.WriteHeader(http.StatusUnauthorized)
return
}
type payload struct {
Href string `json:"href"`
}
var pay payload
if r.Body == nil {
log.Println(err)
http.Error(w, "Please send a request body", 400)
return
}
err = json.NewDecoder(r.Body).Decode(&pay)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
log.Println("recieved payload ", pay)
type Response struct {
Title string `json:"title,omitempty"`
Author string `json:"author,omitempty"`
Description string `json:"description,omitempty"`
Image string `json:"image,omitempty"`
IsYoutube bool `json:"is_youtube,omitempty"`
YoutubeID string `json:"youtube_id,omitempty"`
}
ff := Response{}
up, err := url.Parse(pay.Href)
if err != nil || up == nil || up.Host == "" {
http.Error(w, err.Error(), 400)
return
}
isYoutube := up.Host == "www.youtube.com" || up.Host == "youtube.com"
isShortYoutube := up.Host == "youtu.be"
var title, description, image, author string
if isYoutube || isShortYoutube {
m, err := url.ParseQuery(up.RawQuery)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
id := ""
if isYoutube {
if len(m) > 0 {
id = m["v"][0]
}
} else if isShortYoutube {
if isShortYoutube {
id = up.Path[1:]
}
}
key := c.Config.YoutubeKey
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 7*time.Second)
service, err := youtube.NewService(ctx, option.WithAPIKey(key))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
videos := service.Videos.List([]string{"id", "snippet"})
videos = videos.Id(id)
response, err := videos.Do()
if err != nil {
log.Println(err)
}
if response != nil {
items := response.Items
if items != nil && len(items) >= 1 {
title = items[0].Snippet.Title
description = items[0].Snippet.Description
}
if len(description) > 500 {
description = description[:500]
}
ff.IsYoutube = true
ff.YoutubeID = id
}
} else {
md := c.Scrape(pay.Href, up.Host)
title = md.Title
description = md.Description
author = md.Author
image = md.Image
}
ff.Title = title
ff.Author = author
ff.Description = description
ff.Image = image
js, err := json.Marshal(ff)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
}
type LinkMetaData struct {
Title string
Description string
Image string
Author string
}
func (c *Client) Scrape(link string, domain string) LinkMetaData {
co := colly.NewCollector()
extensions.RandomUserAgent(co)
extensions.Referer(co)
htmldata := ""
lmd := LinkMetaData{}
co.OnResponse(func(r *colly.Response) {
htmldata = string(r.Body)
})
co.OnHTML("head", func(e *colly.HTMLElement) {
// Extract meta tags from the document
metaTags := e.DOM.ParentsUntil("~").Find("meta")
metaTags.Each(func(_ int, s *goquery.Selection) {
// Search for og:type meta tags
name, _ := s.Attr("name")
prop, _ := s.Attr("property")
if strings.EqualFold(name, "description") {
description, _ := s.Attr("content")
lmd.Description = description
}
if strings.EqualFold(name, "author") {
author, _ := s.Attr("content")
lmd.Author = author
}
if strings.EqualFold(prop, "og:image") {
image, _ := s.Attr("content")
lmd.Image = image
}
if lmd.Image == "" && strings.EqualFold(prop, "twitter:image:src") {
image, _ := s.Attr("content")
lmd.Image = image
}
})
})
co.OnHTML("head title", func(e *colly.HTMLElement) {
lmd.Title = e.Text
})
co.OnRequest(func(r *colly.Request) {
})
co.Visit(link)
return lmd
}

38
client/matrix.go Normal file
View File

@ -0,0 +1,38 @@
package client
import (
"fmt"
"hummingbard/gomatrix"
"log"
)
func (c *Client) TempMatrixClient(userID, accessToken string) (*gomatrix.Client, error) {
fu, us := c.IsFederated(userID)
//port is only for my dev environment, this needs to go
serverName := c.URLScheme(c.Config.Matrix.Server) + fmt.Sprintf(`:%d`, c.Config.Matrix.Port)
//if federation user, we query homeserver at the /well-known endpoint
//for full server path
if fu {
wk, err := WellKnown(c.URLScheme(us.ServerName))
if err != nil {
log.Println(err)
return nil, err
}
serverName = c.URLScheme(wk.ServerName)
}
matrix, err := gomatrix.NewClient(serverName, "", "")
if accessToken != "" {
matrix.SetCredentials(userID, accessToken)
}
if err != nil {
log.Println(err)
return nil, err
}
return matrix, nil
}

126
client/middleware.go Normal file
View File

@ -0,0 +1,126 @@
package client
import (
"context"
"encoding/json"
"log"
"net/http"
)
func (c *Client) Middleware() {
if c.Config.Mode == "development" {
c.Router.Use(c.reloadtemplates)
}
}
func LoggedInUser(r *http.Request) *User {
us, ok := r.Context().Value("user").(*User)
if !ok {
return nil
}
return us
}
//Checks for logged in user on routes that use it
func (c *Client) GetLoggedInUser(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
s, err := GetSession(r, c)
if err != nil {
log.Println(err)
h.ServeHTTP(w, r)
return
}
token, ok := s.Values["access_token"].(string)
if ok {
userid, err := c.Store.Get(token).Result()
if err != nil {
log.Println(err)
h.ServeHTTP(w, r)
return
}
user, err := c.Store.Get(userid).Result()
if err != nil {
log.Println(err)
h.ServeHTTP(w, r)
return
}
var us User
err = json.Unmarshal([]byte(user), &us)
if err != nil {
log.Println(err)
h.ServeHTTP(w, r)
return
}
if len(us.AvatarURL) > 0 {
us.AvatarURL = c.BuildAvatar(us.AvatarURL)
}
ctx := context.WithValue(r.Context(), "user", &us)
h.ServeHTTP(w, r.WithContext(ctx))
return
}
h.ServeHTTP(w, r)
})
}
//makes sure this route is autehnticated
func (c *Client) RequireAuthentication(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("are we authenticated?")
s, err := GetSession(r, c)
if err != nil {
log.Println(err)
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
log.Println("lol")
token, ok := s.Values["access_token"].(string)
if !ok {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
userid, err := c.Store.Get(token).Result()
if err != nil {
log.Println(err)
h.ServeHTTP(w, r)
return
}
user, err := c.Store.Get(userid).Result()
if err != nil {
log.Println(err)
h.ServeHTTP(w, r)
return
}
var us User
err = json.Unmarshal([]byte(user), &us)
if err != nil || us.UserID == "" {
log.Println(err)
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
h.ServeHTTP(w, r)
})
}
func (c *Client) reloadtemplates(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c.ReloadTemplates()
h.ServeHTTP(w, r)
})
}

815
client/post.go Normal file
View File

@ -0,0 +1,815 @@
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"hummingbard/gomatrix"
"log"
"net/http"
"path/filepath"
"strings"
"time"
)
func (c *Client) CreateNewPost() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
user, err := c.GetTokenUser(token)
if err != nil || user == nil {
log.Println(err)
w.WriteHeader(http.StatusUnauthorized)
return
}
type payload struct {
RoomID string `json:"room_id"`
RoomAlias string `json:"room_alias"`
Post struct {
Content struct {
Text string `json:"text"`
HTML string `json:"html"`
} `json:"content"`
Links []struct {
Href string `json:"href"`
Metadata struct {
Title string `json:"title"`
Author string `json:"author"`
Description string `json:"description"`
Image string `json:"image"`
IsYoutube bool `json:"is_youtube"`
YoutubeID string `json:"youtube_id"`
} `json:"metadata"`
} `json:"links"`
Images []struct {
Caption string `json:"caption"`
Description string `json:"description"`
Filename string `json:"filename"`
Size uint `json:"size"`
Mimetype string `json:"mimetype"`
MXC string `json:"mxc"`
Width uint `json:"width"`
Height uint `json:"height"`
} `json:"images"`
Attachments []struct {
Filename string `json:"filename"`
Size uint `json:"size"`
Mimetype string `json:"mimetype"`
MXC string `json:"mxc"`
} `json:"attachments"`
Article struct {
Enabled bool `json:"enabled"`
Title string `json:"title"`
Subtitle string `json:"subtitle"`
Description string `json:"description"`
CanonicalLink string `json:"canonical_link"`
FeaturedImage *struct {
Caption string `json:"caption"`
MXC string `json:"mxc"`
Width uint `json:"width"`
Height uint `json:"height"`
} `json:"featured_image"`
} `json:"article"`
} `json:"post"`
Page bool `json:"page"`
Reply bool `json:"reply"`
EventID string `json:"event_id"`
NSFW bool `json:"nsfw"`
Anonymous bool `json:"anonymous"`
SharedPostID string `json:"shared_post_id"`
Share bool `json:"share"`
SharedPostRoomID string `json:"shared_post_room_id"`
ShareReply bool `json:"share_reply"`
ReplyPermalink string `json:"reply_permalink"`
}
var pay payload
if r.Body == nil {
http.Error(w, "Please send a request body", 400)
return
}
err = json.NewDecoder(r.Body).Decode(&pay)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
log.Println("recieved payload ", pay)
type Response struct {
Post interface{} `json:"post"`
}
fu, us := c.FederationUser(user.UserID)
//port is only for my dev environment, this needs to go
serverName := c.URLScheme(c.Config.Matrix.Server) + fmt.Sprintf(`:%d`, c.Config.Matrix.Port)
//if federation user, we query homeserver at the /well-known endpoint
//for full server path
if fu {
wk, err := WellKnown(c.URLScheme(us.ServerName))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
serverName = c.URLScheme(wk.ServerName)
}
if pay.Anonymous {
serverName = c.URLScheme(c.Config.Matrix.Server) + fmt.Sprintf(`:%d`, c.Config.Matrix.Port)
}
userid := user.UserID
accessToken := user.MatrixAccessToken
if pay.Anonymous {
userid = c.AnonymousUser.UserID
accessToken = c.AnonymousUser.AccessToken
}
matrix, err := gomatrix.NewClient(serverName, userid, accessToken)
roomCreateEventID := ""
if !pay.Reply {
state, err := matrix.RoomState(pay.RoomID)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
rc := c.RoomCreateEventFromState(state)
if len(rc) > 0 {
roomCreateEventID = rc
}
}
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
post := &gomatrix.Event{}
if pay.Share && len(pay.SharedPostID) > 0 {
ev, err := c.Matrix.RoomEvent(pay.SharedPostRoomID, pay.SharedPostID)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
post = ev
}
_, err = matrix.JoinRoom(pay.RoomAlias, "", nil)
if err != nil {
log.Println(err)
}
threadRoomAlias := fmt.Sprintf(`thread%s`, RandomNumber(32))
reAlias := fmt.Sprintf(`#%s:%s`, threadRoomAlias, user.HomeServer)
roomID := ""
text, err := SanitizeHTML(pay.Post.Content.Text)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
html, err := ToStrictHTML(pay.Post.Content.Text)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
if pay.Post.Article.Enabled {
html, err = ToHTML(pay.Post.Content.Text)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
}
//create post in new thread rroom
npe := gomatrix.CreatePostEvent{
RoomID: roomID,
RoomAlias: pay.RoomAlias,
Unsanitized: pay.Post.Content.Text,
ThreadInRoomID: pay.RoomID,
NSFW: pay.NSFW,
Anonymous: pay.Anonymous,
}
if !pay.Post.Article.Enabled {
npe.Text = text
npe.FormattedText = string(html)
}
if pay.Post.Article.Enabled {
b := []byte(html)
l := int64(len(b))
r := bytes.NewReader(b)
upl, err := matrix.UploadToContentRepo(r, "text/html", l)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
if upl != nil {
npe.ArticleContent = upl.ContentURI
}
}
slug := ""
if pay.Post.Article.Enabled {
npe.IsArticle = true
npe.Title = pay.Post.Article.Title
npe.Subtitle = pay.Post.Article.Subtitle
npe.CanonicalLink = pay.Post.Article.CanonicalLink
npe.Description = pay.Post.Article.Description
if pay.Post.Article.FeaturedImage != nil &&
len(pay.Post.Article.FeaturedImage.MXC) > 0 {
f := (float32(pay.Post.Article.FeaturedImage.Height) / float32(pay.Post.Article.FeaturedImage.Width)) * 100
asp := uint(f)
y := gomatrix.Image{
Caption: pay.Post.Article.FeaturedImage.Caption,
Width: pay.Post.Article.FeaturedImage.Width,
Height: pay.Post.Article.FeaturedImage.Height,
AspectRatio: asp,
MXC: StripMXCPrefix(pay.Post.Article.FeaturedImage.MXC),
}
npe.FeaturedImage = &y
}
slug = Slugify(pay.Post.Article.Title)
roomPath := c.RoomPathFromAlias(pay.RoomAlias)
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 7*time.Second)
exists, err := c.DoesSlugExist(ctx, roomPath, slug)
if err != nil {
log.Println(err)
}
if exists {
slug = fmt.Sprintf(`%s-%s`, slug, RandomNumber(8))
}
npe.Slug = slug
go func() {
roomPath := c.RoomPathFromAlias(pay.RoomAlias)
fullPath := filepath.Join(roomPath, slug)
c.Cache.Articles.Set(fullPath, html, 1)
}()
}
if post != nil && pay.Share && len(pay.SharedPostID) > 0 {
c.ProcessEvent(post, user)
npe.Share = true
npe.SharedPost = post
if pay.ShareReply && len(pay.ReplyPermalink) > 0 {
npe.ShareReply = true
npe.ReplyPermalink = pay.ReplyPermalink
post.Content["share_reply"] = true
post.Content["reply_permalink"] = pay.ReplyPermalink
}
}
{
rp := pay.RoomAlias
if pay.RoomAlias[0] == '#' {
rp = pay.RoomAlias[1:]
}
sp := strings.Split(rp, ":")
if sp[1] != c.Config.Client.Domain {
npe.Federated = true
}
}
if len(pay.Post.Links) > 0 {
links := []gomatrix.Link{}
for _, x := range pay.Post.Links {
y := gomatrix.Link{
Href: x.Href,
Title: x.Metadata.Title,
Description: x.Metadata.Description,
IsYoutube: x.Metadata.IsYoutube,
YoutubeID: x.Metadata.YoutubeID,
}
links = append(links, y)
}
npe.Links = links
}
if len(pay.Post.Images) > 0 {
images := []gomatrix.Image{}
for _, x := range pay.Post.Images {
f := (float32(x.Height) / float32(x.Width)) * 100
asp := uint(f)
y := gomatrix.Image{
Caption: x.Caption,
Description: x.Description,
Filename: x.Filename,
Size: x.Size,
Mimetype: x.Mimetype,
Width: x.Width,
Height: x.Height,
AspectRatio: asp,
MXC: StripMXCPrefix(x.MXC),
}
images = append(images, y)
}
npe.Images = images
}
if len(pay.Post.Attachments) > 0 {
attachments := []gomatrix.Attachment{}
for _, x := range pay.Post.Attachments {
y := gomatrix.Attachment{
Filename: x.Filename,
Size: x.Size,
Mimetype: x.Mimetype,
MXC: StripMXCPrefix(x.MXC),
}
attachments = append(attachments, y)
}
npe.Attachments = attachments
}
npe.RoomID = pay.RoomID
npe.Reply = pay.Reply
if pay.Reply && pay.EventID != "" {
npe.EventID = pay.EventID
}
if !pay.Reply {
npe.EventID = roomCreateEventID
npe.ThreadRoomID = roomID
npe.ThreadRoomAlias = reAlias
npe.ThreadInRoomID = ""
}
cre, err := matrix.CreatePost(&npe)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
if slug != "" {
go func() {
roomPath := c.RoomPathFromAlias(pay.RoomAlias)
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 7*time.Second)
_, err = c.UpdateEventSlug(ctx, roomPath, slug, cre.EventID)
if err != nil {
log.Println(err)
}
}()
}
//duplicate it in #public room if it isn't a thread reply
if !pay.Reply && !pay.Page {
go func() {
npe.EventID = cre.EventID
un := fmt.Sprintf(`#public:%s`, c.Config.Client.Domain)
res, err := matrix.ResolveAlias(un)
if err != nil || res == nil {
log.Println(err)
}
if res != nil {
npe.ThreadRoomID = ""
npe.ThreadInRoomID = ""
npe.Reply = false
npe.RoomID = string(res.RoomID)
if !strings.Contains(pay.RoomAlias, `#test`) {
_, err = matrix.CreatePost(&npe)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
}
go func() {
_, err = c.RefreshPublicEvents(string(res.RoomID))
if err != nil {
log.Println(err)
}
}()
}
}()
}
event, err := matrix.RoomEvent(pay.RoomID, cre.EventID)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
c.ProcessEvent(event, user)
ff := Response{
Post: event,
}
if fu {
go func() {
time.Sleep(7 * time.Second)
_, err = c.RefreshRoomEvents(pay.RoomID)
if err != nil {
log.Println(err)
}
}()
} else {
_, err = c.RefreshRoomEvents(pay.RoomID)
if err != nil {
log.Println(err)
}
}
js, err := json.Marshal(ff)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
}
func (c *Client) EditPost() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
user, err := c.GetTokenUser(token)
if err != nil || user == nil {
log.Println(err)
w.WriteHeader(http.StatusUnauthorized)
return
}
type payload struct {
RoomID string `json:"room_id"`
EventID string `json:"event_id"`
Content string `json:"content"`
}
var pay payload
if r.Body == nil {
http.Error(w, "Please send a request body", 400)
return
}
err = json.NewDecoder(r.Body).Decode(&pay)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
log.Println("recieved payload ", pay)
fu, us := c.FederationUser(user.UserID)
//port is only for my dev environment, this needs to go
serverName := c.URLScheme(c.Config.Matrix.Server) + fmt.Sprintf(`:%d`, c.Config.Matrix.Port)
//if federation user, we query homeserver at the /well-known endpoint
//for full server path
if fu {
wk, err := WellKnown(c.URLScheme(us.ServerName))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
serverName = c.URLScheme(wk.ServerName)
}
matrix, err := gomatrix.NewClient(serverName, user.UserID, user.MatrixAccessToken)
_, err = matrix.RoomEvent(pay.RoomID, pay.EventID)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
text, err := SanitizeHTML(pay.Content)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
html, err := ToStrictHTML(pay.Content)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
npe := gomatrix.CreatePostEvent{
RoomID: pay.RoomID,
Unsanitized: pay.Content,
Text: text,
FormattedText: string(html),
Edit: true,
EditEventID: pay.EventID,
}
_, err = matrix.CreatePost(&npe)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
type Response struct {
Updated bool `json:"updated"`
}
ff := Response{
Updated: false,
}
js, err := json.Marshal(ff)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
}
func (c *Client) ReactToPost() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
user, err := c.GetTokenUser(token)
if err != nil || user == nil {
log.Println(err)
w.WriteHeader(http.StatusUnauthorized)
return
}
type payload struct {
RoomID string `json:"room_id"`
EventID string `json:"event_id"`
Key string `json:"key"`
}
var pay payload
if r.Body == nil {
http.Error(w, "Please send a request body", 400)
return
}
err = json.NewDecoder(r.Body).Decode(&pay)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
log.Println("recieved payload ", pay)
type Response struct {
Reacted bool `json:"reacted"`
}
fu, us := c.FederationUser(user.UserID)
//port is only for my dev environment, this needs to go
serverName := c.URLScheme(c.Config.Matrix.Server) + fmt.Sprintf(`:%d`, c.Config.Matrix.Port)
//if federation user, we query homeserver at the /well-known endpoint
//for full server path
if fu {
wk, err := WellKnown(c.URLScheme(us.ServerName))
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
serverName = c.URLScheme(wk.ServerName)
}
matrix, err := gomatrix.NewClient(serverName, user.UserID, user.MatrixAccessToken)
_, err = matrix.SendStateEvent(pay.RoomID, "m.reaction", "", map[string]interface{}{
"room_id": pay.RoomID,
"m.relates_to": map[string]string{
"event_id": pay.EventID,
"key": pay.Key,
"rel_type": "m.annotation",
},
})
if err != nil {
log.Println(err)
}
ff := Response{
Reacted: true,
}
js, err := json.Marshal(ff)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
}
func (c *Client) RedactPost() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
user, err := c.GetTokenUser(token)
if err != nil || user == nil {
log.Println(err)
w.WriteHeader(http.StatusUnauthorized)
return
}
type payload struct {
RoomID string `json:"room_id"`
EventID string `json:"event_id"`
Reason string `json:"reason"`
}
var pay payload
if r.Body == nil {
http.Error(w, "Please send a request body", 400)
return
}
err = json.NewDecoder(r.Body).Decode(&pay)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
log.Println("recieved payload ", pay)
type Response struct {
Redacted bool `json:"redacted"`
}
fu, us := c.FederationUser(user.UserID)
//port is only for my dev environment, this needs to go
serverName := c.URLScheme(c.Config.Matrix.Server) + fmt.Sprintf(`:%d`, c.Config.Matrix.Port)
//if federation user, we query homeserver at the /well-known endpoint
//for full server path
if fu {
wk, err := WellKnown(c.URLScheme(us.ServerName))
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
serverName = c.URLScheme(wk.ServerName)
}
matrix, err := gomatrix.NewClient(serverName, user.UserID, user.MatrixAccessToken)
_, err = matrix.RedactEvent(pay.RoomID, pay.EventID, &gomatrix.ReqRedact{
Reason: pay.Reason,
})
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
ff := Response{
Redacted: true,
}
go func() {
_, err = c.RefreshRoomEvents(pay.RoomID)
if err != nil {
log.Println(err)
}
}()
js, err := json.Marshal(ff)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
}
func (c *Client) ReportPost() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
user, err := c.GetTokenUser(token)
if err != nil || user == nil {
log.Println(err)
w.WriteHeader(http.StatusUnauthorized)
return
}
type payload struct {
RoomID string `json:"room_id"`
EventID string `json:"event_id"`
Reason string `json:"reason"`
Score int `json:"score"`
}
var pay payload
if r.Body == nil {
http.Error(w, "Please send a request body", 400)
return
}
err = json.NewDecoder(r.Body).Decode(&pay)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
log.Println("recieved payload ", pay)
type Response struct {
Reported bool `json:"reported"`
}
fu, us := c.FederationUser(user.UserID)
//port is only for my dev environment, this needs to go
serverName := c.URLScheme(c.Config.Matrix.Server) + fmt.Sprintf(`:%d`, c.Config.Matrix.Port)
//if federation user, we query homeserver at the /well-known endpoint
//for full server path
if fu {
wk, err := WellKnown(c.URLScheme(us.ServerName))
if err != nil {
log.Println(err)
c.Error(w, r)
return
}
serverName = c.URLScheme(wk.ServerName)
}
matrix, err := gomatrix.NewClient(serverName, user.UserID, user.MatrixAccessToken)
_, err = matrix.ReportEvent(pay.RoomID, pay.EventID, &gomatrix.ReqReport{
Reason: pay.Reason,
Score: pay.Score,
})
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
ff := Response{
Reported: true,
}
js, err := json.Marshal(ff)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
}

520
client/room.go Normal file
View File

@ -0,0 +1,520 @@
package client
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
)
func (c *Client) JoinRoom() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
user, err := c.GetTokenUser(token)
if err != nil || user == nil {
log.Println(err)
w.WriteHeader(http.StatusUnauthorized)
return
}
type payload struct {
Id string `json:"id"`
Alias string `json:"alias"`
}
var pay payload
if r.Body == nil {
log.Println(err)
http.Error(w, "Please send a request body", 400)
return
}
err = json.NewDecoder(r.Body).Decode(&pay)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
log.Println("recieved payload ", pay)
type Response struct {
Joined bool `json:"joined"`
}
matrix, err := c.TempMatrixClient(user.UserID, user.MatrixAccessToken)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
ff := Response{
Joined: false,
}
jr, err := matrix.JoinRoom(pay.Alias, "", nil)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
} else if jr != nil {
ff.Joined = true
}
nr := JoinedRoom{
RoomID: pay.Id,
RoomAlias: pay.Alias,
}
err = c.AddJoinedRoom(nr, r)
if err != nil {
log.Println(err)
}
js, err := json.Marshal(ff)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
}
func (c *Client) LeaveRoom() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
user, err := c.GetTokenUser(token)
if err != nil || user == nil {
log.Println(err)
w.WriteHeader(http.StatusUnauthorized)
return
}
type payload struct {
Id string `json:"id"`
Alias string `json:"alias"`
}
var pay payload
if r.Body == nil {
log.Println(err)
http.Error(w, "Please send a request body", 400)
return
}
err = json.NewDecoder(r.Body).Decode(&pay)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
log.Println("recieved payload ", pay)
type Response struct {
Left bool `json:"left"`
}
matrix, err := c.TempMatrixClient(user.UserID, user.MatrixAccessToken)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
ff := Response{
Left: false,
}
jr, err := matrix.LeaveRoom(pay.Id)
if err != nil {
log.Println(err)
} else if jr != nil {
ff.Left = true
}
rooms, err := matrix.JoinedRooms()
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
err = c.RefreshJoinedRooms(r, rooms.JoinedRooms)
if err != nil {
log.Println(err)
}
js, err := json.Marshal(ff)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
}
func (c *Client) GetRoomState() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
user, err := c.GetTokenUser(token)
if err != nil || user == nil {
log.Println(err)
w.WriteHeader(http.StatusUnauthorized)
return
}
type payload struct {
RoomID string `json:"room_id"`
RoomAlias string `json:"room_alias"`
}
var pay payload
if r.Body == nil {
log.Println(err)
http.Error(w, "Please send a request body", 400)
return
}
err = json.NewDecoder(r.Body).Decode(&pay)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
type Response struct {
State interface{} `json:"state"`
}
matrix, err := c.TempMatrixClient(user.UserID, user.MatrixAccessToken)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
ff := Response{}
state, err := matrix.RoomState(pay.RoomID)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
ff.State = state
js, err := json.Marshal(ff)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
}
func (c *Client) GetRoomMembers() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
user, err := c.GetTokenUser(token)
if err != nil || user == nil {
log.Println(err)
w.WriteHeader(http.StatusUnauthorized)
return
}
type payload struct {
RoomID string `json:"room_id"`
}
var pay payload
if r.Body == nil {
log.Println(err)
http.Error(w, "Please send a request body", 400)
return
}
err = json.NewDecoder(r.Body).Decode(&pay)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
type member struct {
DisplayName *string `json:"display_name"`
AvatarURL *string `json:"avatar_url"`
}
type Response struct {
Members map[string]member `json:"members"`
}
matrix, err := c.TempMatrixClient(user.UserID, user.MatrixAccessToken)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
ff := Response{Members: map[string]member{}}
members, err := matrix.JoinedMembers(pay.RoomID)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
for r, x := range members.Joined {
if strings.Contains(r, `@anonymous`) ||
strings.Contains(r, fmt.Sprintf(`@%s`, c.Config.Client.Domain)) {
continue
}
ff.Members[r] = x
}
js, err := json.Marshal(ff)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
}
func (c *Client) UpdateRoomInfo() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
user, err := c.GetTokenUser(token)
if err != nil || user == nil {
log.Println(err)
w.WriteHeader(http.StatusUnauthorized)
return
}
type payload struct {
RoomID string `json:"room_id"`
Profile bool `json:"profile"`
Info struct {
Title string `json:"title"`
About string `json:"about"`
Avatar string `json:"avatar"`
} `json:"info"`
Appearance struct {
Header string `json:"header"`
CSS string `json:"css"`
} `json:"appearance"`
}
var pay payload
if r.Body == nil {
log.Println(err)
http.Error(w, "Please send a request body", 400)
return
}
err = json.NewDecoder(r.Body).Decode(&pay)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
log.Println("recieved payload ", pay)
type Response struct {
Updated bool `json:"updated"`
}
matrix, err := c.TempMatrixClient(user.UserID, user.MatrixAccessToken)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
ff := Response{}
owner := false
if pay.Profile && user.RoomID == pay.RoomID {
owner = true
}
if len(pay.Info.Title) > 0 {
_, err = matrix.SendStateEvent(pay.RoomID, "m.room.name", "", map[string]interface{}{
"name": pay.Info.Title,
})
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
if owner {
err = matrix.SetDisplayName(pay.Info.Title)
if err != nil {
log.Println(err)
log.Println(err)
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
c.UpdateDisplayName(r, pay.Info.Title)
}
}
if len(pay.Info.About) > 0 {
_, err = matrix.SendStateEvent(pay.RoomID, "m.room.topic", "", map[string]interface{}{
"topic": pay.Info.About,
})
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
}
_, err = matrix.SendStateEvent(pay.RoomID, "com.hummingbard.room.style", "", map[string]interface{}{
"css": pay.Appearance.CSS,
})
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
if len(pay.Appearance.Header) > 0 {
_, err = matrix.SendStateEvent(pay.RoomID, "com.hummingbard.room.header", "", map[string]interface{}{
"url": pay.Appearance.Header,
})
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
}
if len(pay.Info.Avatar) > 0 {
_, err = matrix.SendStateEvent(pay.RoomID, "m.room.avatar", "", map[string]interface{}{
"url": pay.Info.Avatar,
})
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
if owner {
err = matrix.SetAvatarURL(pay.Info.Avatar)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
c.UpdateAvatar(r, pay.Info.Avatar)
}
}
ff.Updated = true
js, err := json.Marshal(ff)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
}
func (c *Client) GetRoomInfo() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
user, err := c.GetTokenUser(token)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusUnauthorized)
return
}
type payload struct {
RoomID string `json:"room_id"`
}
var pay payload
if r.Body == nil {
log.Println(err)
http.Error(w, "Please send a request body", 400)
return
}
err = json.NewDecoder(r.Body).Decode(&pay)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
log.Println("recieved payload ", pay)
type Response struct {
State interface{} `json:"state"`
}
userID := c.DefaultUser.UserID
accessToken := c.DefaultUser.AccessToken
if user != nil && len(user.UserID) > 0 {
userID = user.UserID
token = user.MatrixAccessToken
}
matrix, err := c.TempMatrixClient(userID, accessToken)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
ff := Response{}
state, err := matrix.RoomState(pay.RoomID)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}
ff.State = state
js, err := json.Marshal(ff)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
}

316
client/routes.go Normal file
View File

@ -0,0 +1,316 @@
package client
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"runtime/debug"
"strings"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
"github.com/go-chi/hostrouter"
"github.com/lpar/gzipped"
dendriteUserApi "github.com/matrix-org/dendrite/userapi/api"
"github.com/unrolled/secure"
)
func (c *Client) Routes() {
compressor := middleware.NewCompressor(5, "text/html", "text/css")
compressor.SetEncoder("nop", func(w io.Writer, _ int) io.Writer {
return w
})
//c.Router.Use(middleware.ThrottleBacklog(10, 50, time.Second*10))
c.Router.Use(middleware.RequestID)
c.Router.Use(middleware.RealIP)
c.Router.Use(middleware.Logger)
c.Router.Use(c.Recoverer)
c.Router.Use(middleware.StripSlashes)
c.Router.Use(compressor.Handler)
c.CORS()
c.ServeStaticFiles()
hr := hostrouter.New()
ad := fmt.Sprintf(`%s%s`, c.Config.Client.Domain, c.Config.Client.Port)
if c.Config.Mode == "production" {
ad = c.Config.Client.Domain
}
hr.Map(ad, routes(c))
//local dev please ignore
hr.Map("192.168.1.4:7666", routes(c))
c.Router.Mount("/", hr)
}
func routes(c *Client) chi.Router {
sop := secure.Options{
ContentSecurityPolicy: "script-src 'self' 'unsafe-eval' 'unsafe-inline' $NONCE",
IsDevelopment: false,
AllowedHosts: []string{
fmt.Sprintf(`%s%s`, c.Config.Client.Domain, c.Config.Client.Port),
fmt.Sprintf(`%s`, c.Config.Matrix.Server),
"192.168.1.4:7666",
},
}
if c.Config.Mode == "production" {
sop.AllowedHosts = []string{c.Config.Client.Domain}
}
secureMiddleware := secure.New(sop)
r := chi.NewRouter()
r.Use(c.GetLoggedInUser)
r.Route("/login", func(r chi.Router) {
r.Use(secureMiddleware.Handler)
r.Get("/", c.Login())
r.Post("/", c.ValidateLogin())
})
r.Route("/logout", func(r chi.Router) {
r.Use(secureMiddleware.Handler)
r.Use(c.RequireAuthentication)
r.Get("/", c.Logout())
})
r.Route("/signup", func(r chi.Router) {
r.Use(secureMiddleware.Handler)
r.Get("/", c.Signup())
r.Post("/", c.ValidateSignup())
})
r.Route("/messages", func(r chi.Router) {
r.Post("/fetch", c.GetMoreMessages())
})
r.Route("/feed", func(r chi.Router) {
r.Post("/fetch", c.GetFeedEvents())
})
r.Route("/public", func(r chi.Router) {
r.Use(secureMiddleware.Handler)
r.Get("/", c.PublicFeed())
})
r.Route("/room", func(r chi.Router) {
r.Post("/join", c.JoinRoom())
r.Post("/leave", c.LeaveRoom())
r.Post("/state", c.GetRoomState())
r.Post("/info", c.GetRoomInfo())
r.Post("/info/update", c.UpdateRoomInfo())
r.Post("/members", c.GetRoomMembers())
})
r.Route("/link", func(r chi.Router) {
r.Post("/metadata", c.LinkMetadata())
})
r.Route("/username", func(r chi.Router) {
r.Get("/", c.NotFound)
r.Post("/available", c.UsernameAvailable())
})
r.Route("/create", func(r chi.Router) {
r.Use(secureMiddleware.Handler)
r.Use(c.RequireAuthentication)
r.Get("/", c.CreateRoom())
r.Post("/", c.ValidateRoomCreation())
})
r.Route("/post", func(r chi.Router) {
r.Post("/create", c.CreateNewPost())
r.Post("/edit", c.EditPost())
r.Post("/redact", c.RedactPost())
r.Post("/react", c.ReactToPost())
r.Post("/report", c.ReportPost())
r.Post("/replies/fetch", c.FetchReplies())
})
r.Route("/", func(r chi.Router) {
r.Use(secureMiddleware.Handler)
r.Get("/favicon.ico", c.NotFound)
r.Get("/about", c.StaticPage())
r.Get("/", c.Index())
r.Get("/*", c.Dispatch())
})
compressor := middleware.NewCompressor(5, "text/html", "text/css")
compressor.SetEncoder("nop", func(w io.Writer, _ int) io.Writer {
return w
})
r.NotFound(c.NotFound)
return r
}
type UserSession struct {
CreatedAt time.Time
User struct {
Account *dendriteUserApi.Account
Device *dendriteUserApi.Device
}
}
func (c *Client) NotFound(w http.ResponseWriter, r *http.Request) {
us := LoggedInUser(r)
type NotFoundPage struct {
LoggedInUser interface{}
Nonce string
}
nonce := secure.CSPNonce(r.Context())
pg := NotFoundPage{
LoggedInUser: us,
Nonce: nonce,
}
c.Templates.ExecuteTemplate(w, "not-found", pg)
}
func (c *Client) ServeStaticFiles() {
path := "/static"
if strings.ContainsAny(path, "{}*") {
panic("FileServer does not permit URL parameters.")
}
workDir, _ := os.Getwd()
filesDir := filepath.Join(workDir, "static")
fs := http.StripPrefix(path, gzipped.FileServer(FileSystem{http.Dir(filesDir)}))
if path != "/" && path[len(path)-1] != '/' {
c.Router.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
path += "/"
}
path += "*"
c.Router.Get(path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "max-age=31536000")
fs.ServeHTTP(w, r)
}))
}
type FileSystem struct {
fs http.FileSystem
}
func (nfs FileSystem) Open(path string) (http.File, error) {
f, err := nfs.fs.Open(path)
if err != nil {
return nil, err
}
s, err := f.Stat()
if s.IsDir() {
index := strings.TrimSuffix(path, "/") + "/index.html"
if _, err := nfs.fs.Open(index); err != nil {
return nil, err
}
}
return f, nil
}
func (c *Client) CORS() {
cors := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"X-PINGOTHER", "Accept", "Authorization", "Image", "Attachment", "File-Type", "Content-Type", "X-CSRF-Token", "Access-Control-Allow-Origin"},
ExposedHeaders: []string{"Link"},
AllowCredentials: true,
MaxAge: 300,
})
c.Router.Use(cors.Handler)
}
func (c *Client) Recoverer(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rvr := recover(); rvr != nil {
logEntry := middleware.GetLogEntry(r)
if logEntry != nil {
logEntry.Panic(rvr, debug.Stack())
} else {
fmt.Fprintf(os.Stderr, "Panic: %+v\n", rvr)
debug.PrintStack()
}
c.Error(w, r)
return
}
}()
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func (c *Client) Error(w http.ResponseWriter, r *http.Request) {
us := LoggedInUser(r)
type errorPage struct {
LoggedInUser interface{}
Nonce string
}
nonce := secure.CSPNonce(r.Context())
pg := errorPage{
LoggedInUser: us,
Nonce: nonce,
}
c.Templates.ExecuteTemplate(w, "error", pg)
}
func (c *Client) RoomTooLarge(w http.ResponseWriter, r *http.Request) {
us := LoggedInUser(r)
type errorPage struct {
LoggedInUser interface{}
Nonce string
}
nonce := secure.CSPNonce(r.Context())
pg := errorPage{
LoggedInUser: us,
Nonce: nonce,
}
c.Templates.ExecuteTemplate(w, "room-too-large", pg)
}
func (c *Client) StaticPage() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
us := LoggedInUser(r)
url := strings.TrimLeft(r.URL.Path, "/")
type page struct {
LoggedInUser interface{}
Nonce string
}
nonce := secure.CSPNonce(r.Context())
pg := page{
LoggedInUser: us,
Nonce: nonce,
}
c.Templates.ExecuteTemplate(w, url, pg)
}
}

459
client/sessions.go Normal file
View File

@ -0,0 +1,459 @@
package client
import (
"encoding/json"
"hummingbard/gomatrix"
"log"
"net/http"
"strings"
"github.com/gorilla/sessions"
"github.com/tidwall/gjson"
)
type JoinedRoom struct {
RoomID string `json:"room_id"`
RoomAlias string `json:"room_alias"`
}
type OwnedRoom struct {
RoomID string `json:"room_id"`
RoomAlias string `json:"room_alias"`
}
type User struct {
DisplayName string `json:"display_name"`
AvatarURL string `json:"avatar_url"`
AccessToken string `json:"access_token"`
MatrixAccessToken string `json:"matrix_access_token"`
DeviceID string `json:"device_id"`
HomeServer string `json:"home_server"`
UserID string `json:"user_id"`
RefreshToken string `json:"refresh_token"`
RoomID string `json:"room_id"`
JoinedRooms []JoinedRoom `json:"joined_rooms"`
OwnedRooms []JoinedRoom `json:"owned_rooms"`
WellKnown string `json:"well_known"`
Federated bool `json:"federated"`
}
func NewSession(sec string) *sessions.CookieStore {
s := sessions.NewCookieStore([]byte(sec))
s.Options = &sessions.Options{
Path: "/",
MaxAge: 864000 * 150,
HttpOnly: false,
}
return s
}
func GetSession(r *http.Request, c *Client) (*sessions.Session, error) {
s, err := c.Sessions.Get(r, c.Config.Client.CookieName)
if err != nil {
log.Println(err)
return nil, err
}
return s, nil
}
func (c *Client) GetTokenUser(token string) (*User, error) {
userid, err := c.Store.Get(token).Result()
if err != nil {
log.Println(err)
return nil, err
}
user, err := c.Store.Get(userid).Result()
if err != nil {
log.Println(err)
return nil, err
}
var us User
err = json.Unmarshal([]byte(user), &us)
if err != nil {
log.Println(err)
return nil, err
}
return &us, nil
}
func (c *Client) AddJoinedRoom(j JoinedRoom, r *http.Request) error {
s, err := c.Sessions.Get(r, c.Config.Client.CookieName)
if err != nil {
log.Println(err)
return err
}
token, ok := s.Values["access_token"].(string)
if ok {
userid, err := c.Store.Get(token).Result()
if err != nil {
log.Println(err)
return err
}
user, err := c.Store.Get(userid).Result()
if err != nil {
log.Println(err)
return err
}
var us User
err = json.Unmarshal([]byte(user), &us)
if err != nil {
log.Println(err)
return err
}
us.JoinedRooms = append(us.JoinedRooms, j)
serialized, err := json.Marshal(us)
if err != nil {
log.Println(err)
return err
}
err = c.Store.Set(userid, serialized, 0).Err()
if err != nil {
log.Println(err)
return err
}
}
return nil
}
func (c *Client) UpdateJoinedRooms(matrix *gomatrix.Client, r *http.Request) error {
rms, err := c.GetUserJoinedRooms(matrix)
if err != nil {
log.Println(err)
return err
}
s, err := c.Sessions.Get(r, c.Config.Client.CookieName)
if err != nil {
log.Println(err)
return err
}
token, ok := s.Values["access_token"].(string)
if ok {
userid, err := c.Store.Get(token).Result()
if err != nil {
log.Println(err)
return err
}
user, err := c.Store.Get(userid).Result()
if err != nil {
log.Println(err)
return err
}
var us User
err = json.Unmarshal([]byte(user), &us)
if err != nil {
log.Println(err)
return err
}
us.JoinedRooms = rms
serialized, err := json.Marshal(us)
if err != nil {
log.Println(err)
return err
}
err = c.Store.Set(userid, serialized, 0).Err()
if err != nil {
log.Println(err)
return err
}
}
return nil
}
func (c *Client) RefreshJoinedRooms(r *http.Request, rooms []string) error {
s, err := c.Sessions.Get(r, c.Config.Client.CookieName)
if err != nil {
log.Println(err)
return err
}
token, ok := s.Values["access_token"].(string)
if ok {
userid, err := c.Store.Get(token).Result()
if err != nil {
log.Println(err)
return err
}
user, err := c.Store.Get(userid).Result()
if err != nil {
log.Println(err)
return err
}
var us User
err = json.Unmarshal([]byte(user), &us)
if err != nil {
log.Println(err)
return err
}
rms := []JoinedRoom{}
for i, _ := range rooms {
x := JoinedRoom{
RoomID: rooms[i],
}
room, ok := c.Cache.Rooms.Get(rooms[i])
if ok {
x.RoomAlias = room.(gomatrix.PublicRoom).CanonicalAlias
}
rms = append(rms, x)
}
us.JoinedRooms = rms
serialized, err := json.Marshal(us)
if err != nil {
log.Println(err)
return err
}
err = c.Store.Set(userid, serialized, 0).Err()
if err != nil {
log.Println(err)
return err
}
}
return nil
}
func (c *Client) UpdateUserRoomID(r *http.Request, roomID string) error {
s, err := c.Sessions.Get(r, c.Config.Client.CookieName)
if err != nil {
log.Println(err)
return err
}
token, ok := s.Values["access_token"].(string)
if ok {
userid, err := c.Store.Get(token).Result()
if err != nil {
log.Println(err)
return err
}
user, err := c.Store.Get(userid).Result()
if err != nil {
log.Println(err)
return err
}
var us User
err = json.Unmarshal([]byte(user), &us)
if err != nil {
log.Println(err)
return err
}
us.RoomID = roomID
serialized, err := json.Marshal(us)
if err != nil {
log.Println(err)
return err
}
err = c.Store.Set(userid, serialized, 0).Err()
if err != nil {
log.Println(err)
return err
}
}
return nil
}
func (c *Client) IsRoomMember(r *http.Request, roomID string) (bool, error) {
s, err := c.Sessions.Get(r, c.Config.Client.CookieName)
if err != nil {
log.Println(err)
return false, err
}
token, ok := s.Values["access_token"].(string)
if ok {
userid, err := c.Store.Get(token).Result()
if err != nil {
log.Println(err)
return false, err
}
user, err := c.Store.Get(userid).Result()
if err != nil {
log.Println(err)
return false, err
}
var us User
err = json.Unmarshal([]byte(user), &us)
if err != nil {
log.Println(err)
return false, err
}
for i, _ := range us.JoinedRooms {
for x, _ := range us.JoinedRooms {
if us.JoinedRooms[i] == us.JoinedRooms[x] {
return true, nil
}
}
}
}
return false, nil
}
func (c *Client) UpdateDisplayName(r *http.Request, displayName string) error {
s, err := c.Sessions.Get(r, c.Config.Client.CookieName)
if err != nil {
log.Println(err)
return err
}
token, ok := s.Values["access_token"].(string)
if ok {
userid, err := c.Store.Get(token).Result()
if err != nil {
log.Println(err)
return err
}
user, err := c.Store.Get(userid).Result()
if err != nil {
log.Println(err)
return err
}
var us User
err = json.Unmarshal([]byte(user), &us)
if err != nil {
log.Println(err)
return err
}
us.DisplayName = displayName
serialized, err := json.Marshal(us)
if err != nil {
log.Println(err)
return err
}
err = c.Store.Set(userid, serialized, 0).Err()
if err != nil {
log.Println(err)
return err
}
}
return nil
}
func (c *Client) UpdateAvatar(r *http.Request, avatar string) error {
s, err := c.Sessions.Get(r, c.Config.Client.CookieName)
if err != nil {
log.Println(err)
return err
}
token, ok := s.Values["access_token"].(string)
if ok {
userid, err := c.Store.Get(token).Result()
if err != nil {
log.Println(err)
return err
}
user, err := c.Store.Get(userid).Result()
if err != nil {
log.Println(err)
return err
}
var us User
err = json.Unmarshal([]byte(user), &us)
if err != nil {
log.Println(err)
return err
}
us.AvatarURL = avatar
serialized, err := json.Marshal(us)
if err != nil {
log.Println(err)
return err
}
err = c.Store.Set(userid, serialized, 0).Err()
if err != nil {
log.Println(err)
return err
}
}
return nil
}
func (c *Client) GetUserJoinedRooms(matrix *gomatrix.Client) ([]JoinedRoom, error) {
fil, err := matrix.CreateFilter([]byte(`
{
"room": {
"timeline": {
"limit": 0,
"types": ["com.hummingbard.post"]
}
}
}
`))
sre, err := matrix.SyncRequest(0, "", fil.FilterID, true, "offline")
if err != nil {
log.Println(err)
return nil, err
}
rms := []JoinedRoom{}
for roomID, room := range sre.Rooms.Join {
st, _ := json.Marshal(room.State.Events)
roomType := gjson.Parse(string(st)).Get(`#(type="com.hummingbard.room")`).Get("content.room_type")
if roomType.String() == "page" || roomType.String() == "post" {
continue
}
alias := gjson.Parse(string(st)).Get(`#(type="m.room.canonical_alias")`).Get("content.alias")
if len(alias.String()) > 0 &&
!strings.Contains(alias.String(), "#@") &&
!strings.Contains(alias.String(), "#thread") &&
!strings.Contains(alias.String(), "#public") {
rms = append(rms, JoinedRoom{RoomID: roomID, RoomAlias: alias.String()})
}
}
return rms, nil
}

168
client/state.go Normal file
View File

@ -0,0 +1,168 @@
package client
import (
"encoding/json"
"html/template"
"hummingbard/gomatrix"
"log"
"strings"
"time"
"github.com/tidwall/gjson"
)
type ProcessStateRequest struct {
State []*gomatrix.Event
User *User
Page *TimelinePage
}
func (c *Client) ProcessState(req *ProcessStateRequest) {
st, _ := json.Marshal(req.State)
de := gjson.Parse(string(st)).Get(`#(type="com.hummingbard.room.info")`).Get("content.description")
if de.String() != "" {
html, err := UnsafeHTML(de.String())
if err != nil {
log.Println(err)
} else {
req.Page.Room.Description = html
}
}
alias := gjson.Parse(string(st)).Get(`#(type="m.room.canonical_alias")`).Get("content.alias")
if alias.String() != "" {
req.Page.Room.Alias = alias.String()
}
owner := gjson.Parse(string(st)).Get(`#(type="m.room.create")`).Get("content.creator")
if owner.String() != "" {
ow := owner.String()
if strings.Contains(ow, c.Config.Client.Domain) {
s := strings.Split(ow, ":")
ow = s[0]
}
req.Page.Room.Owner = User{
UserID: ow,
}
if req.User != nil && req.User.UserID == owner.String() {
req.Page.IsOwner = true
}
}
name := gjson.Parse(string(st)).Get(`#(type="m.room.name")`).Get("content.name")
if name.String() != "" {
req.Page.Room.Name = name.String()
}
avatar := gjson.Parse(string(st)).Get(`#(type="m.room.avatar")`).Get("content.url")
if avatar.String() != "" {
avt := c.BuildImage(avatar.String())
req.Page.Room.Avatar = avt
}
header := gjson.Parse(string(st)).Get(`#(type="com.hummingbard.room.header")`).Get("content.url")
if header.String() != "" {
avt := c.BuildImage(header.String())
req.Page.Room.Header = avt
}
topic := gjson.Parse(string(st)).Get(`#(type="m.room.topic")`).Get("content.topic")
if topic.String() != "" {
html, err := ToStrictHTML(topic.String())
if err != nil {
log.Println(err)
req.Page.Room.Topic = template.HTML(topic.String())
} else {
req.Page.Room.Topic = html
}
}
roomType := gjson.Parse(string(st)).Get(`#(type="com.hummingbard.room")`).Get("content.room_type")
if roomType.String() != "" {
req.Page.Room.Type = roomType.String()
} else {
req.Page.Room.Archaic = true
}
cre := gjson.Parse(string(st)).Get(`#(type="m.room.create")`).Get("origin_server_ts")
ct := cre.Int()
y := time.Unix(ct/1000, ct/10000*1000)
ft := y.Format("Jan 2, 2006")
req.Page.Room.CreatedAt = ft
powerlevels := gjson.Parse(string(st)).Get(`#(type="m.room.power_levels")`).Get("content.users")
powerlevels.ForEach(func(key, value gjson.Result) bool {
if req.User != nil && req.User.UserID != "" {
if key.String() == req.User.UserID && value.Int() == 100 {
req.Page.IsAdmin = true
}
}
return true // keep iterating
})
style := gjson.Parse(string(st)).Get(`#(type="com.hummingbard.room.style")`).Get("content.css")
if style.String() != "" {
css := style.String()
req.Page.Room.CSS = template.CSS(css)
}
members := gjson.Parse(string(st)).Get(`#(type="m.room.member")#`)
mem := 0
members.ForEach(func(key, value gjson.Result) bool {
mem += 1
return true // keep iterating
})
req.Page.Room.Members = mem - 2
req.Page.Room.State = req.State
}
func (c *Client) IsPage(state []*gomatrix.Event) bool {
st, _ := json.Marshal(state)
roomType := gjson.Parse(string(st)).Get(`#(type="com.hummingbard.room")`).Get("content.room_type")
if roomType.String() == "page" {
return true
}
return false
}
func (c *Client) IsRoomArchaic(state []*gomatrix.Event) bool {
st, _ := json.Marshal(state)
roomType := gjson.Parse(string(st)).Get(`#(type="com.hummingbard.room")`).Get("content.room_type")
if len(roomType.String()) > 0 {
return false
}
return true
}
func (c *Client) CanonicalAliasFromState(state []*gomatrix.Event) string {
st, _ := json.Marshal(state)
alias := gjson.Parse(string(st)).Get(`#(type="m.room.canonical_alias")`).Get("content.alias")
if alias.String() != "" {
return alias.String()
}
return ""
}
func (c *Client) RoomCreateEventFromState(state []*gomatrix.Event) string {
st, _ := json.Marshal(state)
evid := gjson.Parse(string(st)).Get(`#(type="m.room.create")`).Get("event_id")
if evid.String() != "" {
return evid.String()
}
return ""
}

271
client/templates.go Normal file
View File

@ -0,0 +1,271 @@
package client
import (
"errors"
"fmt"
"html/template"
"hummingbard/config"
"io"
"io/ioutil"
"log"
"math"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"time"
)
type BasePage struct {
Name string `json:"name"`
LoggedInUser interface{} `json:"logged_in_user"`
Nonce string `json:"nonce"`
}
type Template struct {
*template.Template
}
var fMap = template.FuncMap{
"InsertJS": insertJS,
"InsertCSS": insertCSS,
"FormatTime": formatTime,
"Map": mapp,
"FileSize": FileSize,
"ToString": ToString,
"IsLastItem": IsLastItem,
"StripMXCPrefix": StripMXCPrefix,
"AspectRatio": AspectRatio,
"IsUserProfile": isUserProfile,
"RandomString": randomString,
"Title": title,
"Sum": sum,
"Concat": concat,
"Trunc": truncate,
"HasColon": hasColon,
}
func hasColon(s string) bool {
return strings.Contains(s, ":")
}
func truncate(s string, i int) string {
runes := []rune(s)
if len(runes) > i {
return string(runes[:i])
}
return s
}
func concat(values ...string) string {
return strings.Trim(strings.Join(values, ""), "")
}
func sum(i, g int) int {
return i + g
}
func title(s string) string {
return strings.Title(s)
}
func randomString(i int) string {
return RandomString(i)
}
func isUserProfile(s string) bool {
conf, err := config.Read()
if err != nil {
log.Println(err)
}
return strings.Contains(s, "@") && strings.Contains(s, conf.Client.Domain)
}
func AspectRatio(x, y string) string {
height, err := strconv.Atoi(x)
if err != nil {
log.Println(err)
}
width, err := strconv.Atoi(y)
if err != nil {
log.Println(err)
}
return fmt.Sprintf(`%d %d`, height, width)
}
func mapp(values ...interface{}) (map[string]interface{}, error) {
if len(values)%2 != 0 {
return nil, errors.New("invalid dict call")
}
dict := make(map[string]interface{}, len(values)/2)
for i := 0; i < len(values); i += 2 {
key, ok := values[i].(string)
if !ok {
return nil, errors.New("dict keys must be strings")
}
dict[key] = values[i+1]
}
return dict, nil
}
func formatTime(t int64) string {
ut := time.Unix(t, 0)
log.Println(ut)
log.Println(ut)
log.Println(ut)
return fmt.Sprintf(`%s`, ut)
}
func insertJS(name string) template.HTML {
root := "static/js"
files, err := ioutil.ReadDir(root)
if err != nil {
}
var scr string
for _, file := range files {
x := strings.Split(file.Name(), ".")
if name == x[0] && x[len(x)-1] == "js" {
scr = fmt.Sprintf("/static/js/%s", file.Name())
return template.HTML(scr)
}
}
scr = fmt.Sprintf("/static/js/%s.js", "missing")
return template.HTML(scr)
}
func insertCSS(name string) template.HTML {
root := "static/css"
files, err := ioutil.ReadDir(root)
if err != nil {
panic(err)
}
var scr string
for _, file := range files {
x := strings.Split(file.Name(), ".")
if name == x[0] && x[len(x)-1] == "css" {
scr = fmt.Sprintf("/static/css/%s", file.Name())
return template.HTML(scr)
}
}
return template.HTML("")
}
func NewTemplate() (*Template, error) {
tmpl, err := findAndParseTemplates([]interface{}{"templates"}, fMap)
if err != nil {
panic(err)
}
return tmpl, err
}
func (c *Client) ReloadTemplates() {
tmpl, err := findAndParseTemplates([]interface{}{"templates"}, fMap)
if err != nil {
log.Printf("parsing: %s", err)
}
c.Templates = tmpl
}
func (t *Template) execute(wr io.Writer, name string, data interface{}) error {
pdat := reflect.TypeOf(data)
newdat := reflect.New(pdat)
t.ExecuteTemplate(wr, name, newdat)
return nil
}
func findAndParseTemplates(rootDir interface{}, funcMap template.FuncMap) (*Template, error) {
root := template.New("")
tempo := &Template{root}
var err error
for _, x := range rootDir.([]interface{}) {
cleanRoot := filepath.Clean(x.(string))
pfx := len(cleanRoot) + 1
err = filepath.Walk(cleanRoot, func(path string, info os.FileInfo, e1 error) error {
if !info.IsDir() && strings.HasSuffix(path, ".html") {
if e1 != nil {
return e1
}
b, e2 := ioutil.ReadFile(path)
if e2 != nil {
return e2
}
name := path[pfx:]
t := tempo.New(name).Funcs(funcMap)
t, e2 = t.Parse(string(b))
if e2 != nil {
return e2
}
}
return nil
})
}
return tempo, err
}
func IsLastItem(index, length int) bool {
return index == length-1
}
func Round(val float64, roundOn float64, places int) (newVal float64) {
var round float64
pow := math.Pow(10, float64(places))
digit := pow * val
_, div := math.Modf(digit)
if div >= roundOn {
round = math.Ceil(digit)
} else {
round = math.Floor(digit)
}
newVal = round / pow
return
}
func FileSize(t float64) string {
suffixes := []string{"Bytes", "KB", "MB", "GB"}
base := math.Log(float64(t)) / math.Log(1024)
getSize := Round(math.Pow(1024, base-math.Floor(base)), .5, 2)
getSuffix := suffixes[int(math.Floor(base))]
return strconv.FormatFloat(getSize, 'f', -1, 64) + " " + string(getSuffix)
}
func ToString(value interface{}) string {
switch v := value.(type) {
case string:
return v
case int:
return strconv.Itoa(v)
case uint:
return fmt.Sprint(v)
case float64:
return fmt.Sprint(v)
default:
return ""
}
}

1180
client/timeline.go Normal file

File diff suppressed because it is too large Load Diff

432
client/util.go Normal file
View File

@ -0,0 +1,432 @@
package client
import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"log"
"math"
"math/rand"
"net/http"
"regexp"
"strings"
"time"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday/v2"
)
func WellKnown(s string) (*WellKnownServer, error) {
resp, err := http.Get(fmt.Sprintf(`%s/.well-known/matrix/server`, s))
if err != nil {
return nil, err
}
defer resp.Body.Close()
bodyBytes, _ := ioutil.ReadAll(resp.Body)
var res WellKnownServer
err = json.Unmarshal(bodyBytes, &res)
if err != nil {
return nil, err
}
return &res, nil
}
type UserID struct {
LocalPart string
ServerName string
}
//Determind whether username belongs to this homesever or an external one
//external usernames should be in full @username:homeserver.org form
//local usernames should be @username only for better UI
func (c *Client) FederationUser(username string) (bool, *UserID) {
//validUsernameRegex := regexp.MustCompile(`^@[0-9a-z_\-=./]+:[0-9a-z_\-=./]+\.[a-z]{2,}$`)
validUsernameRegex := regexp.MustCompile(`^@.+?:.+$`)
if validUsernameRegex.MatchString(username) {
username = username[1:]
parts := strings.Split(username, ":")
if parts[1] == c.Config.Matrix.Server {
return false, nil
}
return true, &UserID{
LocalPart: parts[0],
ServerName: parts[1],
}
}
return false, nil
}
func FederationRoom(username string) (bool, *UserID) {
//validUsernameRegex := regexp.MustCompile(`^@[0-9a-z_\-=./]+:[0-9a-z_\-=./]+\.[a-z]{2,}$`)
validUsernameRegex := regexp.MustCompile(`^.+?:.+$`)
if validUsernameRegex.MatchString(username) {
parts := strings.Split(username, ":")
log.Println(parts)
return true, &UserID{
LocalPart: parts[0],
ServerName: parts[1],
}
}
return false, nil
}
func GetHomeServerPart(s string) string {
if strings.Contains(s, ":") {
sp := strings.Split(s, ":")
return sp[len(sp)-1]
}
return s
}
func (c *Client) IsFederated(username string) (bool, *UserID) {
//federated user paths should have the same format as email, like so: username@homeserver.com
//obviously a very loose regexp
//validUsernameRegex := regexp.MustCompile(`^.+?@.+$`)
validUsernameRegex := regexp.MustCompile(`^@.+?:.+$`)
if validUsernameRegex.MatchString(username) {
//lets's split the localpart and server_name
parts := strings.Split(username, ":")
//if severname is the same as out homeserver, return
if parts[1] == c.Config.Client.Domain ||
parts[1] == c.Config.Matrix.Server {
return false, nil
}
//return federated path
return true, &UserID{
LocalPart: parts[0],
ServerName: parts[1],
}
}
return false, nil
}
func FileID(fileID string) string {
fi := strings.Replace(fileID, "mxc://", "", 1)
sp := strings.Split(fi, "/")
return sp[1]
}
func (c *Client) URLScheme(url string) string {
if c.Config.Matrix.Server != url {
return fmt.Sprintf(`https://%s`, url)
}
return fmt.Sprintf(`http://%s`, url)
}
func UnsafeHTML(x string) (template.HTML, error) {
unsafe := blackfriday.Run([]byte(x))
return template.HTML(unsafe), nil
}
func ToHTML(x string) (template.HTML, error) {
unsafe := blackfriday.Run([]byte(x))
p := bluemonday.UGCPolicy()
safe := p.Sanitize(string(unsafe))
return template.HTML(safe), nil
}
func ToStrictHTML(x string) (template.HTML, error) {
unsafe := blackfriday.Run([]byte(x))
p := bluemonday.NewPolicy()
p.AllowStandardURLs()
p.RequireParseableURLs(true)
p.AllowRelativeURLs(true)
p.AllowStandardAttributes()
p.AllowImages()
p.AllowURLSchemes("mailto", "https")
p.AllowAttrs("href").OnElements("a")
p.AllowElements("blockquote")
p.AllowElements("p")
p.AllowElements("b", "strong")
p.AllowElements("i", "em")
p.AllowAttrs("class").OnElements("span")
p.AllowElements("br")
p.AllowElements("hr")
p.AllowElements("ul")
p.AllowElements("ol")
p.AllowElements("li")
p.AllowElements("br")
p.AllowAttrs("id").OnElements("li")
p.AllowAttrs("class").OnElements("li")
p.AllowElements("sub")
p.AllowElements("sup")
p.AllowElements("s")
p.AllowElements("del")
p.AllowElements("pre")
p.AllowElements("code")
safe := p.Sanitize(string(unsafe))
return template.HTML(safe), nil
}
func SanitizeHTML(x string) (string, error) {
p := bluemonday.StrictPolicy()
p.AllowElements("br")
safe := p.Sanitize(x)
return safe, nil
}
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
const NumberBytes = "0123456789"
const (
letterIdxBits = 6
letterIdxMask = 1<<letterIdxBits - 1
letterIdxMax = 63 / letterIdxBits
)
func RandomString(n int) string {
src := rand.NewSource(time.Now().UnixNano())
b := make([]byte, n)
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}
func RandomNumber(n int) string {
b := make([]byte, n)
src := rand.NewSource(time.Now().UnixNano())
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(NumberBytes) {
b[i] = NumberBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}
func StripMXCPrefix(s string) string {
s = strings.Replace(s, "mxc://", "", -1)
return s
}
func (c *Client) RoomPathFromAlias(alias string) string {
federated := false
sp := strings.Split(alias, ":")
if sp[1] != c.Config.Client.Domain {
federated = true
}
path := ""
rp := alias[1:]
if !federated {
sp := strings.Split(rp, ":")
s := strings.Split(sp[0], "_")
if len(sp) > 1 {
p := strings.Join(s, "/")
path = p
} else {
path = s[0]
}
} else {
path = rp
}
return path
}
func FormatTime(t time.Time) string {
thisYear := "Jan _2"
pastYears := "Jan _2, 2006"
//max := 24 * time.Hour
now := time.Now()
difference := now.Sub(t)
// If it's within last 12 hours
if difference < time.Minute {
return "Just Now"
}
if difference < time.Hour {
difference = difference.Round(time.Minute)
x := math.Trunc(difference.Minutes())
return fmt.Sprintf(`%.fm`, x)
}
if difference <= time.Hour*23 {
difference = difference.Round(time.Hour)
x := math.Trunc(difference.Hours())
return fmt.Sprintf(`%.fh`, x)
}
if t.Year() == now.Year() {
x := t.Format(thisYear)
return x
}
return t.Format(pastYears)
}
func InitialMessage() (string, string) {
plain_text := `If I could write the beauty of your eyes,
And in fresh numbers number all your graces,
The age to come would say This poet lies;
Such heavenly touches neer touchd earthly faces.`
html := `If I could write the beauty of your eyes,<br>
And in fresh numbers number all your graces,<br>
The age to come would say <em>This poet lies</em>;<br>
Such heavenly touches neer touchd earthly faces.<br>`
return plain_text, html
}
func (c *Client) BuildDownloadLink(mxc string) string {
avurl := StripMXCPrefix(mxc)
if len(avurl) == 0 {
return ""
}
serverName := c.URLScheme(c.Config.Matrix.Server) + fmt.Sprintf(`:%d`, c.Config.Matrix.Port)
avurl = fmt.Sprintf(`%s/_matrix/media/r0/download/%s`, serverName, avurl)
if c.Config.Mode == "production" {
serverName = c.Config.Matrix.Server
avurl = fmt.Sprintf(`https://%s/_matrix/media/r0/download/%s`, serverName, StripMXCPrefix(mxc))
}
return avurl
}
func (c *Client) BuildAvatar(mxc string) string {
avurl := StripMXCPrefix(mxc)
if len(avurl) == 0 {
return ""
}
serverName := c.URLScheme(c.Config.Matrix.Server) + fmt.Sprintf(`:%d`, c.Config.Matrix.Port)
avurl = fmt.Sprintf(`%s/_matrix/media/r0/thumbnail/%s?width=32&height=32&method=crop`, serverName, avurl)
if c.Config.Mode == "production" {
serverName = c.Config.Matrix.Server
avurl = fmt.Sprintf(`https://%s/_matrix/media/r0/thumbnail/%s?width=32&height=32&method=crop`, serverName, StripMXCPrefix(mxc))
}
return avurl
}
func (c *Client) BuildProfileAvatar(mxc string) string {
avurl := StripMXCPrefix(mxc)
if len(avurl) == 0 {
return ""
}
serverName := c.URLScheme(c.Config.Matrix.Server) + fmt.Sprintf(`:%d`, c.Config.Matrix.Port)
avurl = fmt.Sprintf(`%s/_matrix/media/r0/thumbnail/%s?width=100&height=100&method=crop`, serverName, avurl)
if c.Config.Mode == "production" {
serverName = c.Config.Matrix.Server
avurl = fmt.Sprintf(`https://%s/_matrix/media/r0/thumbnail/%s?width=32&height=32&method=crop`, serverName, StripMXCPrefix(mxc))
}
return avurl
}
func (c *Client) BuildImage(mxc string) string {
avurl := StripMXCPrefix(mxc)
if len(avurl) == 0 {
return ""
}
serverName := c.URLScheme(c.Config.Matrix.Server) + fmt.Sprintf(`:%d`, c.Config.Matrix.Port)
avurl = fmt.Sprintf(`%s/_matrix/media/r0/thumbnail/%s?width=800&height=600&method=scale`, serverName, avurl)
if c.Config.Mode == "production" {
serverName = c.Config.Matrix.Server
avurl = fmt.Sprintf(`https://%s/_matrix/media/r0/thumbnail/%s?width=800&height=600&method=scale`, serverName, StripMXCPrefix(mxc))
}
return avurl
}
func RejectUsername(username string) bool {
usernames := []string{
"admin",
"matrix",
}
exists := false
for _, x := range usernames {
if x == username {
return true
}
}
return exists
}
func GetLocalPart(s string) string {
s = s[1:]
x := strings.Split(s, ":")
return x[0]
}
func (c *Client) GetLocalPartPath(s string) string {
s = s[1:]
x := strings.Split(s, ":")
g := strings.Split(x[0], "_")
if !strings.Contains(x[1], c.Config.Client.Domain) {
g[0] = fmt.Sprintf(`%s:%s`, g[0], x[1])
}
return strings.Join(g, "/")
}

22
cmd/hummingbard/main.go Normal file
View File

@ -0,0 +1,22 @@
package main
import (
"hummingbard/client"
"log"
"os"
"os/signal"
)
func main() {
sc := make(chan os.Signal, 1)
signal.Notify(sc, os.Interrupt)
go func() {
<-sc
log.Println("Shutting down server")
os.Exit(1)
}()
client.Start()
}

31
config-sample.toml Normal file
View File

@ -0,0 +1,31 @@
name = "hummingbard"
mode = "development"
youtube_key ="your-youtube-api-key"
[client]
domain = "hummingbard.com"
port = 8999
cookie_name = "hummingbard"
secure_cookie = "something_random"
[matrix]
server = "localhost.com"
port = 8008
password = "CaR6hxVWKBzYn"
anonymous_password = "blahblah"
[db]
user = "hummingbard"
password = "hummingbard"
name = "hummingbard"
host = "localhost"
port = "5432"
ssl = "require"
[redis]
address = "localhost:6379"
password = "redis"
db = 1
[spaces]
prefix = "org.matrix.msc1772.space"

74
config/config.go Normal file
View File

@ -0,0 +1,74 @@
package config
import (
"io/ioutil"
"os"
"github.com/BurntSushi/toml"
)
type Client struct {
Domain string `toml:"domain"`
Port string `toml:"port"`
CookieName string `toml:"cookie_name"`
SecureCookie string `toml:"secure_cookie"`
}
type Matrix struct {
Server string `toml:"server"`
Port int `toml:"port"`
Password string `toml:"password"`
AnonymousPassword string `toml:"anonymous_password"`
}
type DB struct {
User string `toml:"user"`
Password string `toml:"password"`
Name string `toml:"name"`
Host string `toml:"host"`
Port string `toml:"port"`
SSL string `toml:"ssl"`
}
type Redis struct {
Address string `toml:"address"`
Password string `toml:"password"`
DB int `toml:"db"`
}
type Spaces struct {
Prefix string `toml:"prefix"`
}
type Config struct {
Name string `toml:"name"`
Mode string `toml:"mode"`
Client Client `toml:"client"`
Matrix Matrix `toml:"matrix"`
DB DB `toml:"db"`
Redis Redis `toml:"redis"`
YoutubeKey string `toml:"youtube_key"`
Spaces Spaces `toml:"spaces"`
}
var conf Config
// Read reads the config file and returns the Values struct
func Read() (*Config, error) {
file, err := os.Open("config.toml")
if err != nil {
panic(err)
}
defer file.Close()
b, err := ioutil.ReadAll(file)
if err != nil {
panic(err)
}
if _, err := toml.Decode(string(b), &conf); err != nil {
panic(err)
}
return &conf, err
}

View File

@ -0,0 +1,20 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS slug_to_event(
id BIGSERIAL PRIMARY KEY,
room_path text NOT NULL,
slug text NOT NULL,
event_id text UNIQUE NOT NULL,
UNIQUE(room_path, slug, event_id),
created_at timestamp WITH time zone DEFAULT now(),
updated_at timestamp WITH time zone,
deleted boolean NOT NULL default false,
deleted_at timestamp WITH time zone
);
CREATE INDEX slug_to_event_idx on slug_to_event(slug);
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS slug_to_event;
-- +goose StatementEnd

View File

@ -0,0 +1,21 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS rooms(
id BIGSERIAL PRIMARY KEY,
user_id text NOT NULL,
room_id text NOT NULL,
room_alias text NOT NULL,
room_path text NOT NULL,
UNIQUE(user_id, room_id),
created_at timestamp WITH time zone DEFAULT now(),
updated_at timestamp WITH time zone,
deleted boolean NOT NULL default false,
deleted_at timestamp WITH time zone
);
CREATE INDEX rooms_idx on rooms(user_id);
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS rooms;
-- +goose StatementEnd

44
go.mod Normal file
View File

@ -0,0 +1,44 @@
module hummingbard
go 1.15
require (
cloud.google.com/go v0.75.0 // indirect
github.com/BurntSushi/toml v0.3.1
github.com/PuerkitoBio/goquery v1.5.1
github.com/antchfx/htmlquery v1.2.3 // indirect
github.com/antchfx/xmlquery v1.3.3 // indirect
github.com/dgraph-io/ristretto v0.0.2
github.com/go-chi/chi v1.5.1
github.com/go-chi/cors v1.1.1
github.com/go-chi/hostrouter v0.0.0-20201102173854-85defb39fbf4
github.com/go-redis/redis v6.15.9+incompatible
github.com/gobwas/glob v0.2.3 // indirect
github.com/gocolly/colly v1.2.0
github.com/golang/gddo v0.0.0-20210115222349-20d68f94ee1f // indirect
github.com/golang/protobuf v1.4.3
github.com/gorilla/sessions v1.2.1
github.com/jmoiron/sqlx v1.3.1
github.com/kennygrant/sanitize v1.2.4 // indirect
github.com/lib/pq v1.8.0
github.com/lpar/gzipped v1.1.0
github.com/matrix-org/dendrite v0.3.4
github.com/matrix-org/gomatrix v0.0.0-20200827122206-7dd5e2a05bcd
github.com/microcosm-cc/bluemonday v1.0.1
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/russross/blackfriday v1.5.2
github.com/russross/blackfriday/v2 v2.0.1
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca // indirect
github.com/temoto/robotstxt v1.1.1 // indirect
github.com/tidwall/gjson v1.6.7
github.com/unrolled/secure v1.0.8
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
golang.org/x/oauth2 v0.0.0-20210113205817-d3ed898aa8a3 // indirect
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect
golang.org/x/text v0.3.5 // indirect
google.golang.org/api v0.36.0
google.golang.org/genproto v0.0.0-20210122163508-8081c04a3579 // indirect
google.golang.org/grpc v1.35.0 // indirect
gopkg.in/fsnotify.v1 v1.4.7
maunium.net/go/mautrix v0.8.0
)

1231
go.sum Normal file

File diff suppressed because it is too large Load Diff

24
gomatrix/.gitignore vendored Normal file
View File

@ -0,0 +1,24 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.test
*.prof

21
gomatrix/.golangci.yml Normal file
View File

@ -0,0 +1,21 @@
run:
timeout: 5m
linters:
enable:
- vet
- vetshadow
- typecheck
- deadcode
- gocyclo
- golint
- varcheck
- structcheck
- maligned
- ineffassign
- misspell
- unparam
- goimports
- goconst
- unconvert
- errcheck
- interfacer

7
gomatrix/.travis.yml Normal file
View File

@ -0,0 +1,7 @@
language: go
go:
- 1.13.10
install:
- go get github.com/golangci/golangci-lint/cmd/golangci-lint@v1.24.0
- go build
script: ./hooks/pre-commit

201
gomatrix/LICENSE Normal file
View File

@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright {yyyy} {name of copyright owner}
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

6
gomatrix/README.md Normal file
View File

@ -0,0 +1,6 @@
# gomatrix
[![GoDoc](https://godoc.org/github.com/matrix-org/gomatrix?status.svg)](https://godoc.org/github.com/matrix-org/gomatrix)
A Golang Matrix client.
**THIS IS UNDER ACTIVE DEVELOPMENT: BREAKING CHANGES ARE FREQUENT.**

1045
gomatrix/client.go Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,119 @@
package gomatrix
import (
"fmt"
"net/http"
)
func Example_sync() {
cli, _ := NewClient("https://matrix.org", "@example:matrix.org", "MDAefhiuwehfuiwe")
cli.Store.SaveFilterID("@example:matrix.org", "2") // Optional: if you know it already
cli.Store.SaveNextBatch("@example:matrix.org", "111_222_333_444") // Optional: if you know it already
syncer := cli.Syncer.(*DefaultSyncer)
syncer.OnEventType("m.room.message", func(ev *Event) {
fmt.Println("Message: ", ev)
})
// Blocking version
if err := cli.Sync(); err != nil {
fmt.Println("Sync() returned ", err)
}
// Non-blocking version
go func() {
for {
if err := cli.Sync(); err != nil {
fmt.Println("Sync() returned ", err)
}
// Optional: Wait a period of time before trying to sync again.
}
}()
}
func Example_customInterfaces() {
// Custom interfaces must be set prior to calling functions on the client.
cli, _ := NewClient("https://matrix.org", "@example:matrix.org", "MDAefhiuwehfuiwe")
// anything which implements the Storer interface
customStore := NewInMemoryStore()
cli.Store = customStore
// anything which implements the Syncer interface
customSyncer := NewDefaultSyncer("@example:matrix.org", customStore)
cli.Syncer = customSyncer
// any http.Client
cli.Client = http.DefaultClient
// Once you call a function, you can't safely change the interfaces.
_, _ = cli.SendText("!foo:bar", "Down the rabbit hole")
}
func ExampleClient_BuildURLWithQuery() {
cli, _ := NewClient("https://matrix.org", "@example:matrix.org", "abcdef123456")
out := cli.BuildURLWithQuery([]string{"sync"}, map[string]string{
"filter_id": "5",
})
fmt.Println(out)
// Output: https://matrix.org/_matrix/client/r0/sync?access_token=abcdef123456&filter_id=5
}
func ExampleClient_BuildURL() {
userID := "@example:matrix.org"
cli, _ := NewClient("https://matrix.org", userID, "abcdef123456")
out := cli.BuildURL("user", userID, "filter")
fmt.Println(out)
// Output: https://matrix.org/_matrix/client/r0/user/@example:matrix.org/filter?access_token=abcdef123456
}
func ExampleClient_BuildBaseURL() {
userID := "@example:matrix.org"
cli, _ := NewClient("https://matrix.org", userID, "abcdef123456")
out := cli.BuildBaseURL("_matrix", "client", "r0", "directory", "room", "#matrix:matrix.org")
fmt.Println(out)
// Output: https://matrix.org/_matrix/client/r0/directory/room/%23matrix:matrix.org?access_token=abcdef123456
}
// Retrieve the content of a m.room.name state event.
func ExampleClient_StateEvent() {
content := struct {
Name string `json:"name"`
}{}
cli, _ := NewClient("https://matrix.org", "@example:matrix.org", "abcdef123456")
if err := cli.StateEvent("!foo:bar", "m.room.name", "", &content); err != nil {
panic(err)
}
}
// Join a room by ID.
func ExampleClient_JoinRoom_id() {
cli, _ := NewClient("http://localhost:8008", "@example:localhost", "abcdef123456")
if _, err := cli.JoinRoom("!uOILRrqxnsYgQdUzar:localhost", "", nil); err != nil {
panic(err)
}
}
// Join a room by alias.
func ExampleClient_JoinRoom_alias() {
cli, _ := NewClient("http://localhost:8008", "@example:localhost", "abcdef123456")
if resp, err := cli.JoinRoom("#test:localhost", "", nil); err != nil {
panic(err)
} else {
// Use room ID for something.
_ = resp.RoomID
}
}
// Login to a local homeserver and set the user ID and access token on success.
func ExampleClient_Login() {
cli, _ := NewClient("http://localhost:8008", "", "")
resp, err := cli.Login(&ReqLogin{
Type: "m.login.password",
User: "alice",
Password: "wonderland",
})
if err != nil {
panic(err)
}
cli.SetCredentials(resp.UserID, resp.AccessToken)
}

154
gomatrix/client_test.go Normal file
View File

@ -0,0 +1,154 @@
package gomatrix
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"testing"
)
func TestClient_LeaveRoom(t *testing.T) {
cli := mockClient(func(req *http.Request) (*http.Response, error) {
if req.Method == "POST" && req.URL.Path == "/_matrix/client/r0/rooms/!foo:bar/leave" {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(`{}`)),
}, nil
}
return nil, fmt.Errorf("unhandled URL: %s", req.URL.Path)
})
if _, err := cli.LeaveRoom("!foo:bar"); err != nil {
t.Fatalf("LeaveRoom: error, got %s", err.Error())
}
}
func TestClient_GetAvatarUrl(t *testing.T) {
cli := mockClient(func(req *http.Request) (*http.Response, error) {
if req.Method == "GET" && req.URL.Path == "/_matrix/client/r0/profile/@user:test.gomatrix.org/avatar_url" {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(`{"avatar_url":"mxc://matrix.org/iJaUjkshgdfsdkjfn"}`)),
}, nil
}
return nil, fmt.Errorf("unhandled URL: %s", req.URL.Path)
})
if response, err := cli.GetAvatarURL(); err != nil {
t.Fatalf("GetAvatarURL: Got error: %s", err.Error())
} else if response == "" {
t.Fatal("GetAvatarURL: Got empty response")
} else if response != "mxc://matrix.org/iJaUjkshgdfsdkjfn" {
t.Fatalf("Unexpected response URL: %s", response)
}
}
func TestClient_SetAvatarUrl(t *testing.T) {
cli := mockClient(func(req *http.Request) (*http.Response, error) {
if req.Method == "PUT" && req.URL.Path == "/_matrix/client/r0/profile/@user:test.gomatrix.org/avatar_url" {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(`{}`)),
}, nil
}
return nil, fmt.Errorf("unhandled URL: %s", req.URL.Path)
})
if err := cli.SetAvatarURL("https://foo.com/bar.png"); err != nil {
t.Fatalf("GetAvatarURL: Got error: %s", err.Error())
}
}
func TestClient_StateEvent(t *testing.T) {
cli := mockClient(func(req *http.Request) (*http.Response, error) {
if req.Method == "GET" && req.URL.Path == "/_matrix/client/r0/rooms/!foo:bar/state/m.room.name" {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(`{"name":"Room Name Goes Here"}`)),
}, nil
}
return nil, fmt.Errorf("unhandled URL: %s", req.URL.Path)
})
content := struct {
Name string `json:"name"`
}{}
if err := cli.StateEvent("!foo:bar", "m.room.name", "", &content); err != nil {
t.Fatalf("StateEvent: error, got %s", err.Error())
}
if content.Name != "Room Name Goes Here" {
t.Fatalf("StateEvent: got %s, want %s", content.Name, "Room Name Goes Here")
}
}
func TestClient_PublicRooms(t *testing.T) {
cli := mockClient(func(req *http.Request) (*http.Response, error) {
if req.Method == "GET" && req.URL.Path == "/_matrix/client/r0/publicRooms" {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(`{
"chunk": [
{
"aliases": [
"#murrays:cheese.bar"
],
"avatar_url": "mxc://bleeker.street/CHEDDARandBRIE",
"guest_can_join": false,
"name": "CHEESE",
"num_joined_members": 37,
"room_id": "!ol19s:bleecker.street",
"topic": "Tasty tasty cheese",
"world_readable": true
}
],
"next_batch": "p190q",
"prev_batch": "p1902",
"total_room_count_estimate": 115
}`)),
}, nil
}
return nil, fmt.Errorf("unhandled URL: %s", req.URL.Path)
})
publicRooms, err := cli.PublicRooms(0, "", "")
if err != nil {
t.Fatalf("PublicRooms: error, got %s", err.Error())
}
if publicRooms.TotalRoomCountEstimate != 115 {
t.Fatalf("PublicRooms: got %d, want %d", publicRooms.TotalRoomCountEstimate, 115)
}
if len(publicRooms.Chunk) != 1 {
t.Fatalf("PublicRooms: got %d, want %d", len(publicRooms.Chunk), 1)
}
if publicRooms.Chunk[0].Name != "CHEESE" {
t.Fatalf("PublicRooms: got %s, want %s", publicRooms.Chunk[0].Name, "CHEESE")
}
if publicRooms.Chunk[0].NumJoinedMembers != 37 {
t.Fatalf("PublicRooms: got %d, want %d", publicRooms.Chunk[0].NumJoinedMembers, 37)
}
}
func mockClient(fn func(*http.Request) (*http.Response, error)) *Client {
mrt := MockRoundTripper{
RT: fn,
}
cli, _ := NewClient("https://test.gomatrix.org", "@user:test.gomatrix.org", "abcdef")
cli.Client = &http.Client{
Transport: mrt,
}
return cli
}
type MockRoundTripper struct {
RT func(*http.Request) (*http.Response, error)
}
func (t MockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
return t.RT(req)
}

241
gomatrix/events.go Normal file
View File

@ -0,0 +1,241 @@
package gomatrix
import (
"html"
"regexp"
"time"
)
// Event represents a single Matrix event.
type Event struct {
StateKey *string `json:"state_key,omitempty"` // The state key for the event. Only present on State Events.
Sender string `json:"sender"` // The user ID of the sender of the event
Type string `json:"type"` // The event type
Timestamp int64 `json:"origin_server_ts"` // The unix timestamp when this message was sent by the origin server
Time time.Time `json:"time"`
Date string `json:"date"`
When string `json:"when"`
ID string `json:"event_id"` // The unique ID of this event
RoomID string `json:"room_id"` // The room the event was sent to. May be nil (e.g. for presence)
Redacts string `json:"redacts,omitempty"` // The event ID that was redacted if a m.room.redaction event
Unsigned map[string]interface{} `json:"unsigned"` // The unsigned portions of the event, such as age and prev_content
Content map[string]interface{} `json:"content"` // The JSON content of the event.
PrevContent map[string]interface{} `json:"prev_content,omitempty"` // The JSON prev_content of the event.
Author Author `json:"author,omitempty"`
SharedPost *Event `json:"shared_post,omitempty"`
Redacted bool `json:"redacted"`
Owner bool `json:"owner"`
ShortID string `json:"short_id"`
IsArticle bool `json:"is_article"`
Replies []*Event `json:"replies"`
TotalReplies int `json:"total_replies"`
}
type Author struct {
DisplayName string `json:"display_name,omitempty"`
AvatarURL string `json:"avatar_url,omitempty"`
FormattedID string `json:"formatted_id,omitempty"`
}
// Body returns the value of the "body" key in the event content if it is
// present and is a string.
func (event *Event) Body() (body string, ok bool) {
value, exists := event.Content["body"]
if !exists {
return
}
body, ok = value.(string)
return
}
// MessageType returns the value of the "msgtype" key in the event content if
// it is present and is a string.
func (event *Event) MessageType() (msgtype string, ok bool) {
value, exists := event.Content["msgtype"]
if !exists {
return
}
msgtype, ok = value.(string)
return
}
// This is the standard Hummngbard post event.
type Post struct {
MsgType string `json:"msgtype"`
Body string `json:"body"`
FormattedBody string `json:"formatted_body"`
Format string `json:"format"`
Links []Link `json:"links,omitempty"`
Attachments []Attachment `json:"attachments,omitempty"`
Images []Image `json:"images,omitempty"`
ThreadRoomID *string `json:"thread_room_id,omitempty"`
ThreadRoomAlias *string `json:"thread_room_alias"`
ThreadInRoomID *string `json:"thread_in_room_id,omitempty"`
EventID *string `json:"event_id,omitempty"`
Root *bool `json:"root,omitempty"`
RoomAlias *string `json:"room_alias,omitempty"`
RoomPath string `json:"room_path,omitempty"`
MRelationship map[string]string `json:"m.relationship,omitempty"`
MRelatesTo map[string]string `json:"m_relates_to"`
MNewContent map[string]string `json:"m_new_content"`
NSFW bool `json:"nsfw,omitempty"`
Anonymous bool `json:"anonymous,omitempty"`
SharedPost interface{} `json:"shared_post,omitempty"`
Article *Article `json:"com.hummingbard.article,omitempty"`
ShareReply *bool `json:"share_reply"`
ReplyPermalink *string `json:"reply_permalink"`
}
type Article struct {
Title string `json:"title,omitempty"`
Slug string `json:"slug,omitempty"`
Subtitle string `json:"subtitle,omitempty"`
Description string `json:"description,omitempty"`
CanonicalLink string `json:"canonical_link,omitempty"`
FeaturedImage *Image `json:"featured_image,omitempty"`
ContentURI string `json:"content_uri"`
}
type Link struct {
Href string `json:"href"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Author string `json:"author,omitempty"`
Image string `json:"image,omitempty"`
IsYoutube bool `json:"is_youtube"`
YoutubeID string `json:"youtube_id"`
}
type Attachment struct {
Filename string `json:"filename"`
Mimetype string `json:"mimetype"`
Size uint `json:"size"`
MXC string `json:"mxc"`
}
type Image struct {
Caption string `json:"caption,omitempty"`
Description string `json:"description,omitempty"`
Filename string `json:"filename,omitempty"`
Height uint `json:"height,omitempty"`
Width uint `json:"width,omitempty"`
AspectRatio uint `json:"aspect_ratio,omitempty"`
Mimetype string `json:"mimetype,omitempty"`
Size uint `json:"size,omitempty"`
MXC string `json:"mxc,omitempty"`
}
// TextMessage is the contents of a Matrix formated message event.
type TextMessage struct {
MsgType string `json:"msgtype"`
Body string `json:"body"`
FormattedBody string `json:"formatted_body"`
Format string `json:"format"`
}
// ThumbnailInfo contains info about an thumbnail image - http://matrix.org/docs/spec/client_server/r0.2.0.html#m-image
type ThumbnailInfo struct {
Height uint `json:"h,omitempty"`
Width uint `json:"w,omitempty"`
Mimetype string `json:"mimetype,omitempty"`
Size uint `json:"size,omitempty"`
}
// ImageInfo contains info about an image - http://matrix.org/docs/spec/client_server/r0.2.0.html#m-image
type ImageInfo struct {
Height uint `json:"h,omitempty"`
Width uint `json:"w,omitempty"`
Mimetype string `json:"mimetype,omitempty"`
Size uint `json:"size,omitempty"`
ThumbnailInfo ThumbnailInfo `json:"thumbnail_info,omitempty"`
ThumbnailURL string `json:"thumbnail_url,omitempty"`
}
// VideoInfo contains info about a video - http://matrix.org/docs/spec/client_server/r0.2.0.html#m-video
type VideoInfo struct {
Mimetype string `json:"mimetype,omitempty"`
ThumbnailInfo ThumbnailInfo `json:"thumbnail_info"`
ThumbnailURL string `json:"thumbnail_url,omitempty"`
Height uint `json:"h,omitempty"`
Width uint `json:"w,omitempty"`
Duration uint `json:"duration,omitempty"`
Size uint `json:"size,omitempty"`
}
// VideoMessage is an m.video - http://matrix.org/docs/spec/client_server/r0.2.0.html#m-video
type VideoMessage struct {
MsgType string `json:"msgtype"`
Body string `json:"body"`
URL string `json:"url"`
Info VideoInfo `json:"info"`
}
// ImageMessage is an m.image event
type ImageMessage struct {
MsgType string `json:"msgtype"`
Body string `json:"body"`
URL string `json:"url"`
Info ImageInfo `json:"info"`
}
// An HTMLMessage is the contents of a Matrix HTML formated message event.
type HTMLMessage struct {
Body string `json:"body"`
MsgType string `json:"msgtype"`
Format string `json:"format"`
FormattedBody string `json:"formatted_body"`
}
// FileInfo contains info about an file - http://matrix.org/docs/spec/client_server/r0.2.0.html#m-file
type FileInfo struct {
Mimetype string `json:"mimetype,omitempty"`
Size uint `json:"size,omitempty"` //filesize in bytes
}
// FileMessage is an m.file event - http://matrix.org/docs/spec/client_server/r0.2.0.html#m-file
type FileMessage struct {
MsgType string `json:"msgtype"`
Body string `json:"body"`
URL string `json:"url"`
Filename string `json:"filename"`
Info FileInfo `json:"info,omitempty"`
ThumbnailURL string `json:"thumbnail_url,omitempty"`
ThumbnailInfo ImageInfo `json:"thumbnail_info,omitempty"`
}
// LocationMessage is an m.location event - http://matrix.org/docs/spec/client_server/r0.2.0.html#m-location
type LocationMessage struct {
MsgType string `json:"msgtype"`
Body string `json:"body"`
GeoURI string `json:"geo_uri"`
ThumbnailURL string `json:"thumbnail_url,omitempty"`
ThumbnailInfo ImageInfo `json:"thumbnail_info,omitempty"`
}
// AudioInfo contains info about an file - http://matrix.org/docs/spec/client_server/r0.2.0.html#m-audio
type AudioInfo struct {
Mimetype string `json:"mimetype,omitempty"`
Size uint `json:"size,omitempty"` //filesize in bytes
Duration uint `json:"duration,omitempty"` //audio duration in ms
}
// AudioMessage is an m.audio event - http://matrix.org/docs/spec/client_server/r0.2.0.html#m-audio
type AudioMessage struct {
MsgType string `json:"msgtype"`
Body string `json:"body"`
URL string `json:"url"`
Info AudioInfo `json:"info,omitempty"`
}
var htmlRegex = regexp.MustCompile("<[^<]+?>")
// GetHTMLMessage returns an HTMLMessage with the body set to a stripped version of the provided HTML, in addition
// to the provided HTML.
func GetHTMLMessage(msgtype, htmlText string) HTMLMessage {
return HTMLMessage{
Body: html.UnescapeString(htmlRegex.ReplaceAllLiteralString(htmlText, "")),
MsgType: msgtype,
Format: "org.matrix.custom.html",
FormattedBody: htmlText,
}
}

90
gomatrix/filter.go Normal file
View File

@ -0,0 +1,90 @@
// Copyright 2017 Jan Christian Grünhage
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gomatrix
import "errors"
//Filter is used by clients to specify how the server should filter responses to e.g. sync requests
//Specified by: https://matrix.org/docs/spec/client_server/r0.2.0.html#filtering
type Filter struct {
AccountData FilterPart `json:"account_data,omitempty"`
EventFields []string `json:"event_fields,omitempty"`
EventFormat string `json:"event_format,omitempty"`
Presence FilterPart `json:"presence,omitempty"`
Room RoomFilter `json:"room,omitempty"`
}
// RoomFilter is used to define filtering rules for room events
type RoomFilter struct {
AccountData FilterPart `json:"account_data,omitempty"`
Ephemeral FilterPart `json:"ephemeral,omitempty"`
IncludeLeave bool `json:"include_leave,omitempty"`
NotRooms []string `json:"not_rooms,omitempty"`
Rooms []string `json:"rooms,omitempty"`
State FilterPart `json:"state,omitempty"`
Timeline FilterPart `json:"timeline,omitempty"`
}
// FilterPart is used to define filtering rules for specific categories of events
type FilterPart struct {
NotRooms []string `json:"not_rooms,omitempty"`
Rooms []string `json:"rooms,omitempty"`
Limit int `json:"limit,omitempty"`
NotSenders []string `json:"not_senders,omitempty"`
NotTypes []string `json:"not_types,omitempty"`
Senders []string `json:"senders,omitempty"`
Types []string `json:"types,omitempty"`
ContainsURL *bool `json:"contains_url,omitempty"`
}
// Validate checks if the filter contains valid property values
func (filter *Filter) Validate() error {
if filter.EventFormat != "client" && filter.EventFormat != "federation" {
return errors.New("Bad event_format value. Must be one of [\"client\", \"federation\"]")
}
return nil
}
// DefaultFilter returns the default filter used by the Matrix server if no filter is provided in the request
func DefaultFilter() Filter {
return Filter{
AccountData: DefaultFilterPart(),
EventFields: nil,
EventFormat: "client",
Presence: DefaultFilterPart(),
Room: RoomFilter{
AccountData: DefaultFilterPart(),
Ephemeral: DefaultFilterPart(),
IncludeLeave: false,
NotRooms: nil,
Rooms: nil,
State: DefaultFilterPart(),
Timeline: DefaultFilterPart(),
},
}
}
// DefaultFilterPart returns the default filter part used by the Matrix server if no filter is provided in the request
func DefaultFilterPart() FilterPart {
return FilterPart{
NotRooms: nil,
Rooms: nil,
Limit: 20,
NotSenders: nil,
NotTypes: nil,
Senders: nil,
Types: nil,
}
}

5
gomatrix/hooks/install.sh Executable file
View File

@ -0,0 +1,5 @@
#! /bin/bash
DOT_GIT="$(dirname $0)/../.git"
ln -s "../../hooks/pre-commit" "$DOT_GIT/hooks/pre-commit"

19
gomatrix/hooks/pre-commit Executable file
View File

@ -0,0 +1,19 @@
#! /bin/bash
set -eu
# gofmt doesn't exit with an error code if the files don't match the expected
# format. So we have to run it and see if it outputs anything.
if gofmt -l -s . 2>&1 | read
then
echo "Error: not all code had been formatted with gofmt."
echo "Fixing the following files"
gofmt -s -w -l .
echo
echo "Please add them to the commit"
git status --short
exit 1
fi
golangci-lint run
go test -timeout 5s . ./...

120
gomatrix/id/contenturi.go Normal file
View File

@ -0,0 +1,120 @@
package id
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
)
var (
InvalidContentURI = errors.New("invalid Matrix content URI")
InputNotJSONString = errors.New("input doesn't look like a JSON string")
)
// ContentURIString is a string that's expected to be a Matrix content URI.
// It's useful for delaying the parsing of the content URI to move errors from the event content
// JSON parsing step to a later step where more appropriate errors can be produced.
type ContentURIString string
func (uriString ContentURIString) Parse() (ContentURI, error) {
return ParseContentURI(string(uriString))
}
func (uriString ContentURIString) ParseOrIgnore() ContentURI {
parsed, _ := ParseContentURI(string(uriString))
return parsed
}
// ContentURI represents a Matrix content URI.
// https://matrix.org/docs/spec/client_server/r0.6.0#matrix-content-mxc-uris
type ContentURI struct {
Homeserver string
FileID string
}
func MustParseContentURI(uri string) ContentURI {
parsed, err := ParseContentURI(uri)
if err != nil {
panic(err)
}
return parsed
}
// ParseContentURI parses a Matrix content URI.
func ParseContentURI(uri string) (parsed ContentURI, err error) {
if !strings.HasPrefix(uri, "mxc://") {
err = InvalidContentURI
} else if index := strings.IndexRune(uri[6:], '/'); index == -1 || index == len(uri)-7 {
err = InvalidContentURI
} else {
parsed.Homeserver = uri[6 : 6+index]
parsed.FileID = uri[6+index+1:]
}
return
}
var mxcBytes = []byte("mxc://")
func ParseContentURIBytes(uri []byte) (parsed ContentURI, err error) {
if !bytes.HasPrefix(uri, mxcBytes) {
err = InvalidContentURI
} else if index := bytes.IndexRune(uri[6:], '/'); index == -1 || index == len(uri)-7 {
err = InvalidContentURI
} else {
parsed.Homeserver = string(uri[6 : 6+index])
parsed.FileID = string(uri[6+index+1:])
}
return
}
func (uri *ContentURI) UnmarshalJSON(raw []byte) (err error) {
if len(raw) < 2 || raw[0] != '"' || raw[len(raw)-1] != '"' {
return InputNotJSONString
}
parsed, err := ParseContentURIBytes(raw[1 : len(raw)-1])
if err != nil {
return err
}
*uri = parsed
return nil
}
func (uri *ContentURI) MarshalJSON() ([]byte, error) {
if uri.IsEmpty() {
return []byte("null"), nil
}
return json.Marshal(uri.String())
}
func (uri *ContentURI) UnmarshalText(raw []byte) (err error) {
parsed, err := ParseContentURIBytes(raw)
if err != nil {
return err
}
*uri = parsed
return nil
}
func (uri ContentURI) MarshalText() ([]byte, error) {
if uri.IsEmpty() {
return []byte(""), nil
}
return []byte(uri.String()), nil
}
func (uri *ContentURI) String() string {
if uri.IsEmpty() {
return ""
}
return fmt.Sprintf("mxc://%s/%s", uri.Homeserver, uri.FileID)
}
func (uri *ContentURI) CUString() ContentURIString {
return ContentURIString(uri.String())
}
func (uri *ContentURI) IsEmpty() bool {
return len(uri.Homeserver) == 0 || len(uri.FileID) == 0
}

160
gomatrix/id/userid.go Normal file
View File

@ -0,0 +1,160 @@
// Copyright (c) 2020 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package id
import (
"bytes"
"encoding/hex"
"fmt"
"strings"
)
// UserID represents a Matrix user ID.
// https://matrix.org/docs/spec/appendices#user-identifiers
type UserID string
func NewUserID(localpart, homeserver string) UserID {
return UserID(fmt.Sprintf("@%s:%s", localpart, homeserver))
}
func NewEncodedUserID(localpart, homeserver string) UserID {
return NewUserID(EncodeUserLocalpart(localpart), homeserver)
}
// Parse parses the user ID into the localpart and server name.
// See http://matrix.org/docs/spec/intro.html#user-identifiers
func (userID UserID) Parse() (localpart, homeserver string, err error) {
if len(userID) == 0 || userID[0] != '@' || !strings.ContainsRune(string(userID), ':') {
err = fmt.Errorf("%s is not a valid user id", userID)
return
}
parts := strings.SplitN(string(userID), ":", 2)
localpart, homeserver = strings.TrimPrefix(parts[0], "@"), parts[1]
return
}
func (userID UserID) ParseAndDecode() (localpart, homeserver string, err error) {
localpart, homeserver, err = userID.Parse()
if err == nil {
localpart, err = DecodeUserLocalpart(localpart)
}
return
}
func (userID UserID) String() string {
return string(userID)
}
const lowerhex = "0123456789abcdef"
// encode the given byte using quoted-printable encoding (e.g "=2f")
// and writes it to the buffer
// See https://golang.org/src/mime/quotedprintable/writer.go
func encode(buf *bytes.Buffer, b byte) {
buf.WriteByte('=')
buf.WriteByte(lowerhex[b>>4])
buf.WriteByte(lowerhex[b&0x0f])
}
// escape the given alpha character and writes it to the buffer
func escape(buf *bytes.Buffer, b byte) {
buf.WriteByte('_')
if b == '_' {
buf.WriteByte('_') // another _
} else {
buf.WriteByte(b + 0x20) // ASCII shift A-Z to a-z
}
}
func shouldEncode(b byte) bool {
return b != '-' && b != '.' && b != '_' && !(b >= '0' && b <= '9') && !(b >= 'a' && b <= 'z') && !(b >= 'A' && b <= 'Z')
}
func shouldEscape(b byte) bool {
return (b >= 'A' && b <= 'Z') || b == '_'
}
func isValidByte(b byte) bool {
return isValidEscapedChar(b) || (b >= '0' && b <= '9') || b == '.' || b == '=' || b == '-'
}
func isValidEscapedChar(b byte) bool {
return b == '_' || (b >= 'a' && b <= 'z')
}
// EncodeUserLocalpart encodes the given string into Matrix-compliant user ID localpart form.
// See http://matrix.org/docs/spec/intro.html#mapping-from-other-character-sets
//
// This returns a string with only the characters "a-z0-9._=-". The uppercase range A-Z
// are encoded using leading underscores ("_"). Characters outside the aforementioned ranges
// (including literal underscores ("_") and equals ("=")) are encoded as UTF8 code points (NOT NCRs)
// and converted to lower-case hex with a leading "=". For example:
// Alph@Bet_50up => _alph=40_bet=5f50up
func EncodeUserLocalpart(str string) string {
strBytes := []byte(str)
var outputBuffer bytes.Buffer
for _, b := range strBytes {
if shouldEncode(b) {
encode(&outputBuffer, b)
} else if shouldEscape(b) {
escape(&outputBuffer, b)
} else {
outputBuffer.WriteByte(b)
}
}
return outputBuffer.String()
}
// DecodeUserLocalpart decodes the given string back into the original input string.
// Returns an error if the given string is not a valid user ID localpart encoding.
// See http://matrix.org/docs/spec/intro.html#mapping-from-other-character-sets
//
// This decodes quoted-printable bytes back into UTF8, and unescapes casing. For
// example:
// _alph=40_bet=5f50up => Alph@Bet_50up
// Returns an error if the input string contains characters outside the
// range "a-z0-9._=-", has an invalid quote-printable byte (e.g. not hex), or has
// an invalid _ escaped byte (e.g. "_5").
func DecodeUserLocalpart(str string) (string, error) {
strBytes := []byte(str)
var outputBuffer bytes.Buffer
for i := 0; i < len(strBytes); i++ {
b := strBytes[i]
if !isValidByte(b) {
return "", fmt.Errorf("Byte pos %d: Invalid byte", i)
}
if b == '_' { // next byte is a-z and should be upper-case or is another _ and should be a literal _
if i+1 >= len(strBytes) {
return "", fmt.Errorf("Byte pos %d: expected _[a-z_] encoding but ran out of string", i)
}
if !isValidEscapedChar(strBytes[i+1]) { // invalid escaping
return "", fmt.Errorf("Byte pos %d: expected _[a-z_] encoding", i)
}
if strBytes[i+1] == '_' {
outputBuffer.WriteByte('_')
} else {
outputBuffer.WriteByte(strBytes[i+1] - 0x20) // ASCII shift a-z to A-Z
}
i++ // skip next byte since we just handled it
} else if b == '=' { // next 2 bytes are hex and should be buffered ready to be read as utf8
if i+2 >= len(strBytes) {
return "", fmt.Errorf("Byte pos: %d: expected quote-printable encoding but ran out of string", i)
}
dst := make([]byte, 1)
_, err := hex.Decode(dst, strBytes[i+1:i+3])
if err != nil {
return "", err
}
outputBuffer.WriteByte(dst[0])
i += 2 // skip next 2 bytes since we just handled it
} else { // pass through
outputBuffer.WriteByte(b)
}
}
return outputBuffer.String(), nil
}

69
gomatrix/identifier.go Normal file
View File

@ -0,0 +1,69 @@
package gomatrix
// Identifier is the interface for https://matrix.org/docs/spec/client_server/r0.6.0#identifier-types
type Identifier interface {
// Returns the identifier type
// https://matrix.org/docs/spec/client_server/r0.6.0#identifier-types
Type() string
}
// UserIdentifier is the Identifier for https://matrix.org/docs/spec/client_server/r0.6.0#matrix-user-id
type UserIdentifier struct {
IDType string `json:"type"` // Set by NewUserIdentifer
User string `json:"user"`
}
// Type implements the Identifier interface
func (i UserIdentifier) Type() string {
return "m.id.user"
}
// NewUserIdentifier creates a new UserIdentifier with IDType set to "m.id.user"
func NewUserIdentifier(user string) UserIdentifier {
return UserIdentifier{
IDType: "m.id.user",
User: user,
}
}
// ThirdpartyIdentifier is the Identifier for https://matrix.org/docs/spec/client_server/r0.6.0#third-party-id
type ThirdpartyIdentifier struct {
IDType string `json:"type"` // Set by NewThirdpartyIdentifier
Medium string `json:"medium"`
Address string `json:"address"`
}
// Type implements the Identifier interface
func (i ThirdpartyIdentifier) Type() string {
return "m.id.thirdparty"
}
// NewThirdpartyIdentifier creates a new UserIdentifier with IDType set to "m.id.user"
func NewThirdpartyIdentifier(medium, address string) ThirdpartyIdentifier {
return ThirdpartyIdentifier{
IDType: "m.id.thirdparty",
Medium: medium,
Address: address,
}
}
// PhoneIdentifier is the Identifier for https://matrix.org/docs/spec/client_server/r0.6.0#phone-number
type PhoneIdentifier struct {
IDType string `json:"type"` // Set by NewPhoneIdentifier
Country string `json:"country"`
Phone string `json:"phone"`
}
// Type implements the Identifier interface
func (i PhoneIdentifier) Type() string {
return "m.id.phone"
}
// NewPhoneIdentifier creates a new UserIdentifier with IDType set to "m.id.user"
func NewPhoneIdentifier(country, phone string) PhoneIdentifier {
return PhoneIdentifier{
IDType: "m.id.phone",
Country: country,
Phone: phone,
}
}

97
gomatrix/requests.go Normal file
View File

@ -0,0 +1,97 @@
package gomatrix
// ReqRegister is the JSON request for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-register
type ReqRegister struct {
Username string `json:"username,omitempty"`
BindEmail bool `json:"bind_email,omitempty"`
Password string `json:"password,omitempty"`
DeviceID string `json:"device_id,omitempty"`
InitialDeviceDisplayName string `json:"initial_device_display_name"`
Auth interface{} `json:"auth,omitempty"`
}
type ReqRegisterAvailable struct {
Username string `json:"username,omitempty"`
}
// ReqLogin is the JSON request for http://matrix.org/docs/spec/client_server/r0.6.0.html#post-matrix-client-r0-login
type ReqLogin struct {
Type string `json:"type"`
Identifier Identifier `json:"identifier,omitempty"`
Password string `json:"password,omitempty"`
Medium string `json:"medium,omitempty"`
User string `json:"user,omitempty"`
Address string `json:"address,omitempty"`
Token string `json:"token,omitempty"`
DeviceID string `json:"device_id,omitempty"`
InitialDeviceDisplayName string `json:"initial_device_display_name,omitempty"`
}
// ReqCreateRoom is the JSON request for https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-createroom
type ReqCreateRoom struct {
Visibility string `json:"visibility,omitempty"`
RoomAliasName string `json:"room_alias_name,omitempty"`
Name string `json:"name,omitempty"`
Topic string `json:"topic,omitempty"`
Invite []string `json:"invite,omitempty"`
Invite3PID []ReqInvite3PID `json:"invite_3pid,omitempty"`
CreationContent map[string]interface{} `json:"creation_content,omitempty"`
InitialState []Event `json:"initial_state,omitempty"`
Preset string `json:"preset,omitempty"`
IsDirect bool `json:"is_direct,omitempty"`
PowerLevelContentOverride map[string]interface{} `json:"power_level_content_override,omitempty"`
}
type PowerLevelContentOverride struct {
Ban int `json:"ban"`
Events map[string]int `json:"events"`
EventsDefault int `json:"events_default"`
Invite int `json:"invite"`
Kick int `json:"kick"`
Notifications map[string]int `json:"notifications"`
Redact int `json:"redact"`
StateDefault int `json:"state_default"`
Users map[string]int `json:"users"`
UsersDefault int `json:"users_default"`
}
// ReqRedact is the JSON request for http://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-redact-eventid-txnid
type ReqRedact struct {
Reason string `json:"reason,omitempty"`
}
// ReqInvite3PID is the JSON request for https://matrix.org/docs/spec/client_server/r0.2.0.html#id57
// It is also a JSON object used in https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-createroom
type ReqInvite3PID struct {
IDServer string `json:"id_server"`
Medium string `json:"medium"`
Address string `json:"address"`
}
// ReqInviteUser is the JSON request for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-invite
type ReqInviteUser struct {
UserID string `json:"user_id"`
}
// ReqKickUser is the JSON request for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-kick
type ReqKickUser struct {
Reason string `json:"reason,omitempty"`
UserID string `json:"user_id"`
}
// ReqBanUser is the JSON request for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-ban
type ReqBanUser struct {
Reason string `json:"reason,omitempty"`
UserID string `json:"user_id"`
}
// ReqUnbanUser is the JSON request for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-unban
type ReqUnbanUser struct {
UserID string `json:"user_id"`
}
// ReqTyping is the JSON request for https://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-typing-userid
type ReqTyping struct {
Typing bool `json:"typing"`
Timeout int64 `json:"timeout"`
}

228
gomatrix/responses.go Normal file
View File

@ -0,0 +1,228 @@
package gomatrix
import "maunium.net/go/mautrix/id"
// RespError is the standard JSON error response from Homeservers. It also implements the Golang "error" interface.
// See http://matrix.org/docs/spec/client_server/r0.2.0.html#api-standards
type RespError struct {
ErrCode string `json:"errcode"`
Err string `json:"error"`
}
// Error returns the errcode and error message.
func (e RespError) Error() string {
return e.ErrCode + ": " + e.Err
}
// RespCreateFilter is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-user-userid-filter
type RespCreateFilter struct {
FilterID string `json:"filter_id"`
}
// RespVersions is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-versions
type RespVersions struct {
Versions []string `json:"versions"`
}
// RespPublicRooms is the JSON response for http://matrix.org/speculator/spec/HEAD/client_server/unstable.html#get-matrix-client-unstable-publicrooms
type RespPublicRooms struct {
TotalRoomCountEstimate int `json:"total_room_count_estimate"`
PrevBatch string `json:"prev_batch"`
NextBatch string `json:"next_batch"`
Chunk []PublicRoom `json:"chunk"`
}
// RespJoinRoom is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-join
type RespJoinRoom struct {
RoomID string `json:"room_id"`
}
// RespLeaveRoom is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-leave
type RespLeaveRoom struct{}
// RespForgetRoom is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-forget
type RespForgetRoom struct{}
// RespInviteUser is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-invite
type RespInviteUser struct{}
// RespKickUser is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-kick
type RespKickUser struct{}
// RespBanUser is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-ban
type RespBanUser struct{}
// RespUnbanUser is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-unban
type RespUnbanUser struct{}
// RespTyping is the JSON response for https://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-typing-userid
type RespTyping struct{}
// RespJoinedRooms is the JSON response for TODO-SPEC https://github.com/matrix-org/synapse/pull/1680
type RespJoinedRooms struct {
JoinedRooms []string `json:"joined_rooms"`
}
// RespJoinedMembers is the JSON response for TODO-SPEC https://github.com/matrix-org/synapse/pull/1680
type RespJoinedMembers struct {
Joined map[string]struct {
DisplayName *string `json:"display_name"`
AvatarURL *string `json:"avatar_url"`
} `json:"joined"`
}
// RespMessages is the JSON response for https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-rooms-roomid-messages
type RespMessages struct {
Start string `json:"start"`
Chunk []Event `json:"chunk"`
End string `json:"end"`
}
// RespSendEvent is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-send-eventtype-txnid
type RespSendEvent struct {
EventID string `json:"event_id"`
}
// RespMediaUpload is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-media-r0-upload
type RespMediaUpload struct {
ContentURI string `json:"content_uri"`
}
// RespUserInteractive is the JSON response for https://matrix.org/docs/spec/client_server/r0.2.0.html#user-interactive-authentication-api
type RespUserInteractive struct {
Flows []struct {
Stages []string `json:"stages"`
} `json:"flows"`
Params map[string]interface{} `json:"params"`
Session string `json:"session"`
Completed []string `json:"completed"`
ErrCode string `json:"errcode"`
Error string `json:"error"`
}
// HasSingleStageFlow returns true if there exists at least 1 Flow with a single stage of stageName.
func (r RespUserInteractive) HasSingleStageFlow(stageName string) bool {
for _, f := range r.Flows {
if len(f.Stages) == 1 && f.Stages[0] == stageName {
return true
}
}
return false
}
// RespUserDisplayName is the JSON response for https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-profile-userid-displayname
type RespUserDisplayName struct {
DisplayName string `json:"displayname"`
}
// RespUserStatus is the JSON response for https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-presence-userid-status
type RespUserStatus struct {
Presence string `json:"presence"`
StatusMsg string `json:"status_msg"`
LastActiveAgo int `json:"last_active_ago"`
CurrentlyActive bool `json:"currently_active"`
}
type RespProfile struct {
Displayname *string `json:"displayname"`
AvatarURL *string `json:"avatar_url"`
}
// RespRegister is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-register
type RespRegister struct {
AccessToken string `json:"access_token"`
DeviceID string `json:"device_id"`
HomeServer string `json:"home_server"`
RefreshToken string `json:"refresh_token"`
UserID string `json:"user_id"`
}
// RespLogin is the JSON response for http://matrix.org/docs/spec/client_server/r0.6.0.html#post-matrix-client-r0-login
type RespLogin struct {
AccessToken string `json:"access_token"`
DeviceID string `json:"device_id"`
HomeServer string `json:"home_server"`
UserID string `json:"user_id"`
WellKnown DiscoveryInformation `json:"well_known"`
}
// DiscoveryInformation is the JSON Response for https://matrix.org/docs/spec/client_server/r0.6.0#get-well-known-matrix-client and a part of the JSON Response for https://matrix.org/docs/spec/client_server/r0.6.0#post-matrix-client-r0-login
type DiscoveryInformation struct {
Homeserver struct {
BaseURL string `json:"base_url"`
} `json:"m.homeserver"`
IdentityServer struct {
BaseURL string `json:"base_url"`
} `json:"m.identitiy_server"`
}
// RespLogout is the JSON response for http://matrix.org/docs/spec/client_server/r0.6.0.html#post-matrix-client-r0-logout
type RespLogout struct{}
// RespLogoutAll is the JSON response for https://matrix.org/docs/spec/client_server/r0.6.0#post-matrix-client-r0-logout-all
type RespLogoutAll struct{}
// RespCreateRoom is the JSON response for https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-createroom
type RespCreateRoom struct {
RoomID string `json:"room_id"`
}
// RespSync is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-sync
type RespSync struct {
NextBatch string `json:"next_batch"`
AccountData struct {
Events []Event `json:"events"`
} `json:"account_data"`
Presence struct {
Events []Event `json:"events"`
} `json:"presence"`
Rooms struct {
Leave map[string]struct {
State struct {
Events []Event `json:"events"`
} `json:"state"`
Timeline struct {
Events []Event `json:"events"`
Limited bool `json:"limited"`
PrevBatch string `json:"prev_batch"`
} `json:"timeline"`
} `json:"leave"`
Join map[string]struct {
State struct {
Events []Event `json:"events"`
} `json:"state"`
Timeline struct {
Events []Event `json:"events"`
Limited bool `json:"limited"`
PrevBatch string `json:"prev_batch"`
} `json:"timeline"`
Ephemeral struct {
Events []Event `json:"events"`
} `json:"ephemeral"`
} `json:"join"`
Invite map[string]struct {
State struct {
Events []Event
} `json:"invite_state"`
} `json:"invite"`
} `json:"rooms"`
}
// RespTurnServer is the JSON response from a Turn Server
type RespTurnServer struct {
Username string `json:"username"`
Password string `json:"password"`
TTL int `json:"ttl"`
URIs []string `json:"uris"`
}
type RespAliasCreate struct{}
type RespAliasDelete struct{}
type RespAliasResolve struct {
RoomID id.RoomID `json:"room_id"`
Servers []string `json:"servers"`
}
type RespRegisterAvailable struct {
Available bool `json:"available"`
}

66
gomatrix/room.go Normal file
View File

@ -0,0 +1,66 @@
package gomatrix
// Room represents a single Matrix room.
type Room struct {
ID string
State map[string]map[string]*Event
}
// PublicRoom represents the information about a public room obtainable from the room directory
type PublicRoom struct {
CanonicalAlias string `json:"canonical_alias"`
Name string `json:"name"`
WorldReadable bool `json:"world_readable"`
Topic string `json:"topic"`
NumJoinedMembers int `json:"num_joined_members"`
AvatarURL string `json:"avatar_url"`
RoomID string `json:"room_id"`
GuestCanJoin bool `json:"guest_can_join"`
Aliases []string `json:"aliases"`
RoomType string `json:"room_type"`
NumRefs int `json:"num_refs"`
RoomPath string `json:"room_path"`
}
// UpdateState updates the room's current state with the given Event. This will clobber events based
// on the type/state_key combination.
func (room Room) UpdateState(event *Event) {
_, exists := room.State[event.Type]
if !exists {
room.State[event.Type] = make(map[string]*Event)
}
room.State[event.Type][*event.StateKey] = event
}
// GetStateEvent returns the state event for the given type/state_key combo, or nil.
func (room Room) GetStateEvent(eventType string, stateKey string) *Event {
stateEventMap := room.State[eventType]
event := stateEventMap[stateKey]
return event
}
// GetMembershipState returns the membership state of the given user ID in this room. If there is
// no entry for this member, 'leave' is returned for consistency with left users.
func (room Room) GetMembershipState(userID string) string {
state := "leave"
event := room.GetStateEvent("m.room.member", userID)
if event != nil {
membershipState, found := event.Content["membership"]
if found {
mState, isString := membershipState.(string)
if isString {
state = mState
}
}
}
return state
}
// NewRoom creates a new Room with the given ID
func NewRoom(roomID string) *Room {
// Init the State map and return a pointer to the Room
return &Room{
ID: roomID,
State: make(map[string]map[string]*Event),
}
}

65
gomatrix/store.go Normal file
View File

@ -0,0 +1,65 @@
package gomatrix
// Storer is an interface which must be satisfied to store client data.
//
// You can either write a struct which persists this data to disk, or you can use the
// provided "InMemoryStore" which just keeps data around in-memory which is lost on
// restarts.
type Storer interface {
SaveFilterID(userID, filterID string)
LoadFilterID(userID string) string
SaveNextBatch(userID, nextBatchToken string)
LoadNextBatch(userID string) string
SaveRoom(room *Room)
LoadRoom(roomID string) *Room
}
// InMemoryStore implements the Storer interface.
//
// Everything is persisted in-memory as maps. It is not safe to load/save filter IDs
// or next batch tokens on any goroutine other than the syncing goroutine: the one
// which called Client.Sync().
type InMemoryStore struct {
Filters map[string]string
NextBatch map[string]string
Rooms map[string]*Room
}
// SaveFilterID to memory.
func (s *InMemoryStore) SaveFilterID(userID, filterID string) {
s.Filters[userID] = filterID
}
// LoadFilterID from memory.
func (s *InMemoryStore) LoadFilterID(userID string) string {
return s.Filters[userID]
}
// SaveNextBatch to memory.
func (s *InMemoryStore) SaveNextBatch(userID, nextBatchToken string) {
s.NextBatch[userID] = nextBatchToken
}
// LoadNextBatch from memory.
func (s *InMemoryStore) LoadNextBatch(userID string) string {
return s.NextBatch[userID]
}
// SaveRoom to memory.
func (s *InMemoryStore) SaveRoom(room *Room) {
s.Rooms[room.ID] = room
}
// LoadRoom from memory.
func (s *InMemoryStore) LoadRoom(roomID string) *Room {
return s.Rooms[roomID]
}
// NewInMemoryStore constructs a new InMemoryStore.
func NewInMemoryStore() *InMemoryStore {
return &InMemoryStore{
Filters: make(map[string]string),
NextBatch: make(map[string]string),
Rooms: make(map[string]*Room),
}
}

168
gomatrix/sync.go Normal file
View File

@ -0,0 +1,168 @@
package gomatrix
import (
"encoding/json"
"fmt"
"runtime/debug"
"time"
)
// Syncer represents an interface that must be satisfied in order to do /sync requests on a client.
type Syncer interface {
// Process the /sync response. The since parameter is the since= value that was used to produce the response.
// This is useful for detecting the very first sync (since=""). If an error is return, Syncing will be stopped
// permanently.
ProcessResponse(resp *RespSync, since string) error
// OnFailedSync returns either the time to wait before retrying or an error to stop syncing permanently.
OnFailedSync(res *RespSync, err error) (time.Duration, error)
// GetFilterJSON for the given user ID. NOT the filter ID.
GetFilterJSON(userID string) json.RawMessage
}
// DefaultSyncer is the default syncing implementation. You can either write your own syncer, or selectively
// replace parts of this default syncer (e.g. the ProcessResponse method). The default syncer uses the observer
// pattern to notify callers about incoming events. See DefaultSyncer.OnEventType for more information.
type DefaultSyncer struct {
UserID string
Store Storer
listeners map[string][]OnEventListener // event type to listeners array
}
// OnEventListener can be used with DefaultSyncer.OnEventType to be informed of incoming events.
type OnEventListener func(*Event)
// NewDefaultSyncer returns an instantiated DefaultSyncer
func NewDefaultSyncer(userID string, store Storer) *DefaultSyncer {
return &DefaultSyncer{
UserID: userID,
Store: store,
listeners: make(map[string][]OnEventListener),
}
}
// ProcessResponse processes the /sync response in a way suitable for bots. "Suitable for bots" means a stream of
// unrepeating events. Returns a fatal error if a listener panics.
func (s *DefaultSyncer) ProcessResponse(res *RespSync, since string) (err error) {
if !s.shouldProcessResponse(res, since) {
return
}
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("ProcessResponse panicked! userID=%s since=%s panic=%s\n%s", s.UserID, since, r, debug.Stack())
}
}()
for roomID, roomData := range res.Rooms.Join {
room := s.getOrCreateRoom(roomID)
for _, event := range roomData.State.Events {
event.RoomID = roomID
room.UpdateState(&event)
s.notifyListeners(&event)
}
for _, event := range roomData.Timeline.Events {
event.RoomID = roomID
s.notifyListeners(&event)
}
for _, event := range roomData.Ephemeral.Events {
event.RoomID = roomID
s.notifyListeners(&event)
}
}
for roomID, roomData := range res.Rooms.Invite {
room := s.getOrCreateRoom(roomID)
for _, event := range roomData.State.Events {
event.RoomID = roomID
room.UpdateState(&event)
s.notifyListeners(&event)
}
}
for roomID, roomData := range res.Rooms.Leave {
room := s.getOrCreateRoom(roomID)
for _, event := range roomData.Timeline.Events {
if event.StateKey != nil {
event.RoomID = roomID
room.UpdateState(&event)
s.notifyListeners(&event)
}
}
}
return
}
// OnEventType allows callers to be notified when there are new events for the given event type.
// There are no duplicate checks.
func (s *DefaultSyncer) OnEventType(eventType string, callback OnEventListener) {
_, exists := s.listeners[eventType]
if !exists {
s.listeners[eventType] = []OnEventListener{}
}
s.listeners[eventType] = append(s.listeners[eventType], callback)
}
// shouldProcessResponse returns true if the response should be processed. May modify the response to remove
// stuff that shouldn't be processed.
func (s *DefaultSyncer) shouldProcessResponse(resp *RespSync, since string) bool {
if since == "" {
return false
}
// This is a horrible hack because /sync will return the most recent messages for a room
// as soon as you /join it. We do NOT want to process those events in that particular room
// because they may have already been processed (if you toggle the bot in/out of the room).
//
// Work around this by inspecting each room's timeline and seeing if an m.room.member event for us
// exists and is "join" and then discard processing that room entirely if so.
// TODO: We probably want to process messages from after the last join event in the timeline.
for roomID, roomData := range resp.Rooms.Join {
for i := len(roomData.Timeline.Events) - 1; i >= 0; i-- {
e := roomData.Timeline.Events[i]
if e.Type == "m.room.member" && e.StateKey != nil && *e.StateKey == s.UserID {
m := e.Content["membership"]
mship, ok := m.(string)
if !ok {
continue
}
if mship == "join" {
_, ok := resp.Rooms.Join[roomID]
if !ok {
continue
}
delete(resp.Rooms.Join, roomID) // don't re-process messages
delete(resp.Rooms.Invite, roomID) // don't re-process invites
break
}
}
}
}
return true
}
// getOrCreateRoom must only be called by the Sync() goroutine which calls ProcessResponse()
func (s *DefaultSyncer) getOrCreateRoom(roomID string) *Room {
room := s.Store.LoadRoom(roomID)
if room == nil { // create a new Room
room = NewRoom(roomID)
s.Store.SaveRoom(room)
}
return room
}
func (s *DefaultSyncer) notifyListeners(event *Event) {
listeners, exists := s.listeners[event.Type]
if !exists {
return
}
for _, fn := range listeners {
fn(event)
}
}
// OnFailedSync always returns a 10 second wait period between failed /syncs, never a fatal error.
func (s *DefaultSyncer) OnFailedSync(res *RespSync, err error) (time.Duration, error) {
return 10 * time.Second, nil
}
// GetFilterJSON returns a filter with a timeline limit of 50.
func (s *DefaultSyncer) GetFilterJSON(userID string) json.RawMessage {
return json.RawMessage(`{"room":{"timeline":{"limit":50}}}`)
}

26
gomatrix/tags.go Normal file
View File

@ -0,0 +1,26 @@
// Copyright 2019 Sumukha PK
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gomatrix
// TagContent contains the data for an m.tag message type
// https://matrix.org/docs/spec/client_server/r0.4.0.html#m-tag
type TagContent struct {
Tags map[string]TagProperties `json:"tags"`
}
// TagProperties contains the properties of a Tag
type TagProperties struct {
Order float32 `json:"order,omitempty"` // Empty values must be neglected
}

130
gomatrix/userids.go Normal file
View File

@ -0,0 +1,130 @@
package gomatrix
import (
"bytes"
"encoding/hex"
"fmt"
"strings"
)
const lowerhex = "0123456789abcdef"
// encode the given byte using quoted-printable encoding (e.g "=2f")
// and writes it to the buffer
// See https://golang.org/src/mime/quotedprintable/writer.go
func encode(buf *bytes.Buffer, b byte) {
buf.WriteByte('=')
buf.WriteByte(lowerhex[b>>4])
buf.WriteByte(lowerhex[b&0x0f])
}
// escape the given alpha character and writes it to the buffer
func escape(buf *bytes.Buffer, b byte) {
buf.WriteByte('_')
if b == '_' {
buf.WriteByte('_') // another _
} else {
buf.WriteByte(b + 0x20) // ASCII shift A-Z to a-z
}
}
func shouldEncode(b byte) bool {
return b != '-' && b != '.' && b != '_' && !(b >= '0' && b <= '9') && !(b >= 'a' && b <= 'z') && !(b >= 'A' && b <= 'Z')
}
func shouldEscape(b byte) bool {
return (b >= 'A' && b <= 'Z') || b == '_'
}
func isValidByte(b byte) bool {
return isValidEscapedChar(b) || (b >= '0' && b <= '9') || b == '.' || b == '=' || b == '-'
}
func isValidEscapedChar(b byte) bool {
return b == '_' || (b >= 'a' && b <= 'z')
}
// EncodeUserLocalpart encodes the given string into Matrix-compliant user ID localpart form.
// See http://matrix.org/docs/spec/intro.html#mapping-from-other-character-sets
//
// This returns a string with only the characters "a-z0-9._=-". The uppercase range A-Z
// are encoded using leading underscores ("_"). Characters outside the aforementioned ranges
// (including literal underscores ("_") and equals ("=")) are encoded as UTF8 code points (NOT NCRs)
// and converted to lower-case hex with a leading "=". For example:
// Alph@Bet_50up => _alph=40_bet=5f50up
func EncodeUserLocalpart(str string) string {
strBytes := []byte(str)
var outputBuffer bytes.Buffer
for _, b := range strBytes {
if shouldEncode(b) {
encode(&outputBuffer, b)
} else if shouldEscape(b) {
escape(&outputBuffer, b)
} else {
outputBuffer.WriteByte(b)
}
}
return outputBuffer.String()
}
// DecodeUserLocalpart decodes the given string back into the original input string.
// Returns an error if the given string is not a valid user ID localpart encoding.
// See http://matrix.org/docs/spec/intro.html#mapping-from-other-character-sets
//
// This decodes quoted-printable bytes back into UTF8, and unescapes casing. For
// example:
// _alph=40_bet=5f50up => Alph@Bet_50up
// Returns an error if the input string contains characters outside the
// range "a-z0-9._=-", has an invalid quote-printable byte (e.g. not hex), or has
// an invalid _ escaped byte (e.g. "_5").
func DecodeUserLocalpart(str string) (string, error) {
strBytes := []byte(str)
var outputBuffer bytes.Buffer
for i := 0; i < len(strBytes); i++ {
b := strBytes[i]
if !isValidByte(b) {
return "", fmt.Errorf("Byte pos %d: Invalid byte", i)
}
if b == '_' { // next byte is a-z and should be upper-case or is another _ and should be a literal _
if i+1 >= len(strBytes) {
return "", fmt.Errorf("Byte pos %d: expected _[a-z_] encoding but ran out of string", i)
}
if !isValidEscapedChar(strBytes[i+1]) { // invalid escaping
return "", fmt.Errorf("Byte pos %d: expected _[a-z_] encoding", i)
}
if strBytes[i+1] == '_' {
outputBuffer.WriteByte('_')
} else {
outputBuffer.WriteByte(strBytes[i+1] - 0x20) // ASCII shift a-z to A-Z
}
i++ // skip next byte since we just handled it
} else if b == '=' { // next 2 bytes are hex and should be buffered ready to be read as utf8
if i+2 >= len(strBytes) {
return "", fmt.Errorf("Byte pos: %d: expected quote-printable encoding but ran out of string", i)
}
dst := make([]byte, 1)
_, err := hex.Decode(dst, strBytes[i+1:i+3])
if err != nil {
return "", err
}
outputBuffer.WriteByte(dst[0])
i += 2 // skip next 2 bytes since we just handled it
} else { // pass through
outputBuffer.WriteByte(b)
}
}
return outputBuffer.String(), nil
}
// ExtractUserLocalpart extracts the localpart portion of a user ID.
// See http://matrix.org/docs/spec/intro.html#user-identifiers
func ExtractUserLocalpart(userID string) (string, error) {
if len(userID) == 0 || userID[0] != '@' {
return "", fmt.Errorf("%s is not a valid user id", userID)
}
return strings.TrimPrefix(
strings.SplitN(userID, ":", 2)[0], // @foo:bar:8448 => [ "@foo", "bar:8448" ]
"@", // remove "@" prefix
), nil
}

View File

@ -0,0 +1,27 @@
package gomatrix
import "fmt"
func ExampleEncodeUserLocalpart() {
localpart := EncodeUserLocalpart("Alph@Bet_50up")
fmt.Println(localpart)
// Output: _alph=40_bet__50up
}
func ExampleDecodeUserLocalpart() {
localpart, err := DecodeUserLocalpart("_alph=40_bet__50up")
if err != nil {
panic(err)
}
fmt.Println(localpart)
// Output: Alph@Bet_50up
}
func ExampleExtractUserLocalpart() {
localpart, err := ExtractUserLocalpart("@alice:matrix.org")
if err != nil {
panic(err)
}
fmt.Println(localpart)
// Output: alice
}

86
gomatrix/userids_test.go Normal file
View File

@ -0,0 +1,86 @@
package gomatrix
import (
"testing"
)
var useridtests = []struct {
Input string
Output string
}{
{"Alph@Bet_50up", "_alph=40_bet__50up"}, // The doc example
{"abcdef", "abcdef"}, // no-op
{"i_like_pie_", "i__like__pie__"}, // double underscore escaping
{"ABCDEF", "_a_b_c_d_e_f"}, // all-caps
{"!£", "=21=c2=a3"}, // punctuation and outside ascii range (U+00A3 => c2 a3)
{"___", "______"}, // literal underscores
{"hello-world.", "hello-world."}, // allowed punctuation
{"5+5=10", "5=2b5=3d10"}, // equals sign
{"東方Project", "=e6=9d=b1=e6=96=b9_project"}, // CJK mixed
{" foo bar", "=09foo=20bar"}, // whitespace (tab and space)
}
func TestEncodeUserLocalpart(t *testing.T) {
for _, u := range useridtests {
out := EncodeUserLocalpart(u.Input)
if out != u.Output {
t.Fatalf("TestEncodeUserLocalpart(%s) => Got: %s Expected: %s", u.Input, out, u.Output)
}
}
}
func TestDecodeUserLocalpart(t *testing.T) {
for _, u := range useridtests {
in, _ := DecodeUserLocalpart(u.Output)
if in != u.Input {
t.Fatalf("TestDecodeUserLocalpart(%s) => Got: %s Expected: %s", u.Output, in, u.Input)
}
}
}
var errtests = []struct {
Input string
}{
{"foo@bar"}, // invalid character @
{"foo_5bar"}, // invalid character after _
{"foo_._-bar"}, // multiple invalid characters after _
{"foo=2Hbar"}, // invalid hex after =
{"foo=2hbar"}, // invalid hex after = (lower-case)
{"foo=======2fbar"}, // multiple invalid hex after =
{"foo=2"}, // end of string after =
{"foo_"}, // end of string after _
}
func TestDecodeUserLocalpartErrors(t *testing.T) {
for _, u := range errtests {
out, err := DecodeUserLocalpart(u.Input)
if out != "" {
t.Fatalf("TestDecodeUserLocalpartErrors(%s) => Got: %s Expected: empty string", u.Input, out)
}
if err == nil {
t.Fatalf("TestDecodeUserLocalpartErrors(%s) => Got: nil error Expected: error", u.Input)
}
}
}
var localparttests = []struct {
Input string
ExpectOutput string
}{
{"@foo:bar", "foo"},
{"@foo:bar:8448", "foo"},
{"@foo.bar:baz.quuz", "foo.bar"},
}
func TestExtractUserLocalpart(t *testing.T) {
for _, u := range localparttests {
out, err := ExtractUserLocalpart(u.Input)
if err != nil {
t.Errorf("TestExtractUserLocalpart(%s) => Error: %s", u.Input, err)
continue
}
if out != u.ExpectOutput {
t.Errorf("TestExtractUserLocalpart(%s) => Got: %s, Want %s", u.Input, out, u.ExpectOutput)
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<browserconfig>
<msapplication>
<tile>
<square150x150logo src="/mstile-150x150.png"/>
<TileColor>#da532c</TileColor>
</tile>
</msapplication>
</browserconfig>

Binary file not shown.

After

Width:  |  Height:  |  Size: 750 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
static/favico/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

View File

@ -0,0 +1,27 @@
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
width="513.000000pt" height="513.000000pt" viewBox="0 0 513.000000 513.000000"
preserveAspectRatio="xMidYMid meet">
<metadata>
Created by potrace 1.11, written by Peter Selinger 2001-2013
</metadata>
<g transform="translate(0.000000,513.000000) scale(0.100000,-0.100000)"
fill="#000000" stroke="none">
<path d="M2378 5125 c-1 -1 -38 -5 -80 -9 -43 -4 -83 -8 -90 -11 -7 -2 -31 -6
-53 -9 -163 -23 -361 -77 -540 -149 -214 -85 -479 -240 -659 -385 -97 -77
-299 -279 -380 -379 -229 -281 -431 -685 -502 -1004 -2 -10 -6 -29 -10 -41 -3
-13 -7 -34 -10 -48 -2 -14 -7 -38 -11 -55 -3 -16 -7 -39 -9 -50 -1 -11 -5 -36
-8 -55 -17 -100 -23 -225 -21 -410 2 -113 6 -216 8 -230 3 -14 8 -47 11 -75
14 -119 58 -310 106 -455 87 -267 219 -514 404 -760 87 -115 299 -332 421
-431 338 -273 802 -480 1195 -534 25 -3 52 -8 60 -10 100 -26 571 -26 705 -1
11 2 40 7 65 10 64 9 83 13 195 41 55 14 110 28 121 30 31 7 199 68 269 98
219 93 454 236 650 397 16 14 93 88 170 165 181 181 324 371 435 580 24 44 46
85 50 90 15 21 102 238 129 322 52 156 93 341 117 523 3 25 7 137 8 250 3 198
-6 333 -35 490 -6 36 -13 72 -14 80 -16 89 -80 296 -131 422 -88 218 -230 462
-381 652 -90 114 -329 344 -453 437 -103 76 -263 180 -345 222 -189 98 -459
199 -620 231 -16 4 -41 9 -55 11 -77 16 -120 24 -165 29 -27 4 -66 9 -85 12
-37 6 -456 14 -462 9z"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -0,0 +1,19 @@
{
"name": "",
"short_name": "",
"icons": [
{
"src": "/android-chrome-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "/android-chrome-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
],
"theme_color": "#ffffff",
"background_color": "#ffffff",
"display": "standalone"
}

View File

@ -0,0 +1,217 @@
{{define "article"}}
<html>
<head>
{{$post := .PermalinkedPost}}
{{$article := index $post.Content "com.hummingbard.article"}}
{{$description := index $article "description"}}
<title>{{index $article "title"}} - Hummingbard</title>
{{if $description}}
{{if gt (len $description) 0 }}
<meta name="description" content="{{$description}}">
{{end}}
{{end}}
{{template "common-head" .}}
<style>
.article {
min-height: 100vh;
display: grid;
}
</style>
</head>
<body>
<div class="article">
<div class="center-content mt4">
<div class="ph3">
{{$hms := .HomeServerURL}}
{{if .IsPermalink}}
{{$post := .PermalinkedPost}}
{{$rpth := .Room.Path}}
{{$liu := .LoggedInUser}}
<div class="article-post">
<div class="flex flex-column mb5">
{{$article := index $post.Content "com.hummingbard.article"}}
<div class="lh-copy">
<h1>{{index $article "title"}}</h1>
</div>
{{$author := index $post.Author}}
<div class="lh-copy">
<div class="flex">
<div class="gr-default mr3">
<a class="gr-center" href="/{{$author.FormattedID}}" title="{{index $post.Sender}}">
{{if gt (len $author.AvatarURL) 0}}
<div class="thumbnail{{if .Nested}}-s{{end}}">
<img src="{{$author.AvatarURL}}" />
</div>
{{else}}
<svg class="gr-center" height="30" width="30">
<circle cx="15" cy="15" r="15" stroke-width="0" fill="black" />
</svg>
{{end}}
</a>
</div>
<div class="flex flex-column flex-one">
{{$us := $author.FormattedID}}
{{if $author.DisplayName}}
{{if gt (len $author.DisplayName) 0}}
{{$us = $author.DisplayName}}
{{end}}
{{end}}
<div class="small">
<a class="relative" href="/{{$author.FormattedID}}">
<span class="focusable" ><strong>{{$us}}</strong></span>
</a>
</div>
<div class="small o-90">
<span title={{index $post.Date}}>{{index $post.When}}</span>
</div>
</div>
</div>
{{$img := index $article "featured_image"}}
{{$mxc := ""}}
{{$ar := 0}}
{{if $img}}
{{$mxc = index $img "mxc"}}
{{$ar = index $img "aspect_ratio"}}
{{end}}
{{$ft := gt (len $mxc) 0}}
{{if $ft}}
<div class="fimg bg-img mt4"
style="padding-bottom:{{$ar}}%;">
<img src="{{$mxc}}" />
</div>
{{end}}
<div class="post-content mt4 lh-copy">
{{index $post.Content "bodyHTML"}}
</div>
</div>
</div>
<div class="flex">
<div class="sort-replies">
<select class="small">
<option>Oldest</option>
<option>Recent</option>
<option>Most Replies</option>
</select>
</div>
<div class="flex-one"></div>
{{if .LoggedInUser}}
{{if .IsMember}}
<div class="new-post">
<button>Reply</button>
</div>
{{end}}
{{else}}
<a href="/login">
<div class="post">
<button>Log in to Reply</button>
</div>
</a>
{{end}}
<div class="share-post"></div>
</div>
{{if and .LoggedInUser }}
<div class="create-post"></div>
{{end}}
<div class="added-posts mt4"></div>
<div class="posts">
{{if .IsReplyPermalink}}
<div class="brd-btm pa3 small flex">
<div class="">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M9.78 12.78a.75.75 0 01-1.06 0L4.47 8.53a.75.75 0 010-1.06l4.25-4.25a.75.75 0 011.06 1.06L6.06 8l3.72 3.72a.75.75 0 010 1.06z"></path></svg>
</div>
<div class="gr-center ml2">
<a href="/{{.RootEvent}}">
<span class="primary hov-un">View All Replies<span>
</a>
</div>
</div>
{{end}}
{{$rpth := .Room.Path}}
{{$liu := .LoggedInUser}}
{{$depth := .Depth}}
<div class="replies gr-default brd mb5">
<div class="lds-ring gr-center"><div></div><div></div><div></div><div></div></div>
</div>
</div>
{{end}}
{{if and .Posts (not .IsPermalink)}}
<div class="more-posts flex flex-column">
<div class="more-post-items">
</div>
<div class="load-more">
<button class="button">Load More</button>
</div>
</div>
{{end}}
<div class="timeline">
</div>
<script nonce={{.Nonce}}>
window.timeline = {
id: {{.Room.ID}},
permalink: {{.IsPermalink}},
{{if .IsPermalink}}
event_id: {{.Room.EventID}},
thread_in_room_id: {{.Room.ThreadInRoomID}},
permalinkedPost: {{.PermalinkedPost}},
replies: {{.Posts}},
root_event: {{.RootEvent}},
{{end}}
alias: {{.Room.Alias}},
profile: {{.IsUserProfile}},
initialPosts: JSON.parse({{.InitialPosts}}),
end: {{.LastEvent}},
owner: {{.IsOwner}},
admin: {{.IsAdmin}},
member: {{.IsMember}},
state: {{.Room.State}},
room_type: {{.Room.Type}},
room_path: {{.Room.Path}},
is_article: {{.IsArticle}}
}
let homeserverURL = {{.HomeServerURL}}
</script>
</div>
</div>
{{template "state" .}}
<script defer nonce={{.Nonce}} src="{{ InsertJS "timeline" }}"></script>
</div>
</body>
</html>
{{end}}

67
templates/auth/login.html Normal file
View File

@ -0,0 +1,67 @@
{{define "login"}}
<html>
<head>
<title>Login - Hummingbard</title>
{{template "common-head" .}}
</head>
<body>
<div class="root">
{{template "header" Map "Page" . "Name" "login"}}
<div class="center-content login">
<div class="gr-center w-350px brd pa3">
<form class="flex flex-column" action="/login"
ref="login"
method="POST" autocomplete="off">
<div class="flex flex-column">
{{if .LoginFederated}}
<div class="mb3 fs-09 lh-copy">
Log in with an existing <br/> matrix account.
</div>
{{end}}
<input class="" type="text" autofocus="autofocus"
value="{{.LoginUsername}}"
name="username" placeholder="username">
</div>
<div class="mt3 flex flex-column">
<input class="" type="password"
name="password" minlength="8" placeholder="password">
</div>
<script nonce={{.Nonce}}>
let loginError = false;
let loginUsername = {{.LoginUsername}}
let loginFederated = {{.LoginFederated}}
</script>
{{if .LoginError}}
<script nonce={{.Nonce}}>
loginError = true;
</script>
<div class="mt3 warn">
Username or password wrong
</div>
{{end}}
<div class="mt3 flex">
<div class="">
<button class="dark-button-small no-sel"
type="submit" >
Log In
</button>
</div>
<div class="flex-one">
</div>
<div class="gr-center">
</div>
</div>
</form>
</div>
</div>
{{template "state" .}}
<script defer nonce={{.Nonce}} src="{{ InsertJS "login" }}"></script>
</div>
</body>
</html>
{{end}}

View File

@ -0,0 +1,70 @@
{{define "signup"}}
<html>
<head>
<title>Sign Up - Hummingbard</title>
{{template "common-head" .}}
</head>
<body>
<div class="root">
{{template "header" Map "Page" . "Name" "signup"}}
<div class="center-content signup">
<div class="gr-center w-350px brd pa3">
<div class="">
<form class="flex flex-column" action="/signup"
method="POST" autocomplete="off">
<div class="flex flex-column">
<input class="" type="text" autofocus="autofocus"
minlength="3"
name="username" placeholder="username">
</div>
<div class="mt3 flex flex-column">
<input class="" type="password"
name="password" minlength="8" placeholder="password">
</div>
<div class="mt3 flex flex-column">
<input class="" type="password"
name="repeat" minlength="8" placeholder="repeat password">
</div>
{{if .UserExists}}
<div class="">
That username is already taken.
</div>
{{end}}
{{if .ServerDown}}
<div class="">
Account could not be created.
</div>
{{end}}
<script nonce={{.Nonce}}>
let signupError = false;
</script>
{{if .SignupError}}
<script nonce={{.Nonce}}>
signupError = true;
</script>
{{end}}
{{if .Interactive}}
<script nonce={{.Nonce}}>
window.interactive = true;
window.homeServer = {{.HomeServer}};
</script>
{{end}}
<div class="mt3">
<button class="dark-button-small no-sel"
type="submit" >
Sign Up
</button>
</div>
</form>
</div>
</div>
</div>
{{template "state" .}}
<script defer nonce={{.Nonce}} src="{{ InsertJS "signup" }}"></script>
</div>
</body>
</html>
{{end}}

View File

@ -0,0 +1,16 @@
{{define "common-head"}}
<meta name="viewport" content="width=device-width, initial-scale=1">
<script defer nonce={{.Nonce}} src="{{ InsertJS "default" }}"></script>
<script defer nonce={{.Nonce}} src="{{ InsertJS "nav" }}"></script>
<link rel="stylesheet" href="{{ InsertCSS "tachyons" }}" as="style">
<link rel="stylesheet" href="{{ InsertCSS "microtip" }}" as="style">
<link rel="stylesheet" href="{{ InsertCSS "index" }}" as="style">
<link rel="stylesheet" href="{{ InsertCSS "tippy" }}" as="style">
<link rel="apple-touch-icon" sizes="180x180" href="/static/favico/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/static/favico/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/static/favico/favicon-16x16.png">
<link rel="mask-icon" href="/static/favico/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="theme-color" content="#ffffff">
{{end}}

View File

@ -0,0 +1,23 @@
{{define "create"}}
<html>
<head>
<title>Hummingbard</title>
{{template "common-head" .}}
</head>
<body>
<div class="root">
{{template "header" Map "Page" . "Name" "login"}}
<div class="center-content create-room">
<div class="gr-center">
<div class="lds-ring"><div></div><div></div><div></div><div></div></div>
</div>
</div>
{{template "state" .}}
{{template "footer" .}}
<script defer nonce={{.Nonce}} src="{{ InsertJS "createRoom" }}"></script>
</div>
</body>
</html>
{{end}}

View File

@ -0,0 +1,24 @@
{{define "error"}}
<html>
<head>
<title>Error - Hummingbard</title>
{{template "common-head" .}}
</head>
<body>
<div class="root">
{{template "header" Map "Page" . "Name" "error"}}
<div class="center-content ">
<div class="gr-center flex flex-column">
<div class="mt3">
Catastrophic failure. :(
</div>
</div>
</div>
{{template "state" .}}
{{template "footer" .}}
</div>
</body>
</html>
{{end}}

View File

@ -0,0 +1,27 @@
{{define "not-found"}}
<html>
<head>
<title>Not Found - Hummingbard</title>
{{template "common-head" .}}
</head>
<body>
<div class="root">
{{template "header" Map "Page" . "Name" "error"}}
<div class="center-content ">
<div class="gr-center flex flex-column">
<div class="f1">
<strong>404</strong>
</div>
<div class="mt3 ">
Not found.
</div>
</div>
</div>
{{template "state" .}}
{{template "footer" .}}
</div>
</body>
</html>
{{end}}

View File

@ -0,0 +1,24 @@
{{define "room-too-large"}}
<html>
<head>
<title>Too Large - Hummingbard</title>
{{template "common-head" .}}
</head>
<body>
<div class="root">
{{template "header" Map "Page" . "Name" "error"}}
<div class="center-content ">
<div class="gr-center flex flex-column">
<div class="pa3 w-350px brd lh-copy tc">
Sorry, that room is too large for Hummingbard to handle right now. :(
</div>
</div>
</div>
{{template "state" .}}
{{template "footer" .}}
</div>
</body>
</html>
{{end}}

View File

@ -0,0 +1,9 @@
{{define "footer"}}
<div class="footer">
<div class="footer-content flex">
<div class="small ">
</div>
</div>
</div>
{{end}}

View File

@ -0,0 +1,179 @@
{{define "gallery-item"}}
<html>
<head>
<title>Hummingbard</title>
{{template "common-head" .}}
</head>
<body>
<div class="base {{if .LoggedInUser}}base-l{{end}}">
{{if .LoggedInUser}}
{{template "nav" Map "Page" .}}
{{end}}
<div class="root">
{{template "header" Map "Page" . "Name" "timeline"}}
<div class="content-g ">
<div class="g-con">
<div class="flex pa3 brd-btm">
<div class="mr3">
{{if gt (len .Room.Avatar) 0}}
{{$hms := .HomeServerURL}}
{{$mxc := .Room.Avatar}}
<div class="thumbnail">
<img height="30" width="30" loading="lazy" src="{{.Room.Avatar}}"/>
</div>
{{else}}
<div class="gr-center">
<svg class="gr-center" height="30" width="30">
<circle cx="15" cy="15" r="15" stroke-width="0" fill="black" />
</svg>
</div>
{{end}}
</div>
<div class="gr-center small">
{{$path := .Room.Path}}
{{$ln := len .Room.PathItems}}
{{$sin := eq (len .Room.PathItems) 1 }}
{{range $id, $val := .Room.PathItems}}
{{$last := IsLastItem $id $ln}}
{{$mtc := or (eq $val.Path $path) ($sin)}}
<span class=""><a href="/{{$val.Path}}"><span class="{{if $mtc}}bold{{end}}">{{$val.Item}}</span></a>
{{if not $last}} / {{end}}
</span>
{{end}}
</div>
<div class="flex-one"></div>
{{if and .LoggedInUser (not .IsPermalink)}}
{{if or (and .IsUserProfile .IsOwner) (and (not .IsUserProfile) (or .IsOwner .IsMember))}}
<div class="gr-center">
<div class="new-post">
<button>New Post</button>
</div>
</div>
<div class="share-post"></div>
{{end}}
{{end}}
</div>
{{if or .IsAdmin .IsOwner}}
<div class="room-settings flex">
</div>
{{end}}
{{$hms := .HomeServerURL}}
{{if .IsPermalink}}
{{$ppost := .PermalinkedPost}}
{{$rpth := .Room.Path}}
{{$liu := .LoggedInUser}}
<div class="permalinked-post">
{{template "timeline-event" Map "Event" $ppost "HomeServerURL" $hms "PermalinkedPost" true "RoomPath" $rpth "LoggedInUser" $liu "Gallery" true}}
</div>
{{if and .LoggedInUser}}
<div class="flex pa3 brd-btm" style="min-height: 60px;">
<div class="sort-replies">
<select class="small">
<option>Oldest</option>
<option {{if eq .Sort "recent"}}selected{{end}}>Recent</option>
<option {{if eq .Sort "replies"}}selected{{end}}>Most Replies</option>
</select>
</div>
<div class="flex-one"></div>
<div class="new-post">
<button>Reply</button>
</div>
<div class="share-post"></div>
</div>
{{end}}
{{if and .LoggedInUser }}
<div class="create-post"></div>
{{end}}
<div class="added-posts "></div>
<div class="posts">
{{if .IsReplyPermalink}}
<a href="/{{.RootEvent}}">
<div class="mb4 small flex">
<div class="">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.78 12.53a.75.75 0 01-1.06 0L2.47 8.28a.75.75 0 010-1.06l4.25-4.25a.75.75 0 011.06 1.06L4.81 7h7.44a.75.75 0 010 1.5H4.81l2.97 2.97a.75.75 0 010 1.06z"></path></svg>
</div>
<div class="gr-center ml2"><u>See All Replies</u></div>
</div>
</a>
{{end}}
{{if .Posts}}
<div class="replies gr-default ">
<div class="lds-ring gr-center"><div></div><div></div><div></div><div></div></div>
</div>
{{else}}
<div class="tc pv5 small bold no-replies">
No Replies
</div>
{{end}}
</div>
{{end}}
{{if and .Posts (not .IsPermalink)}}
<div class="more-posts flex flex-column">
<div class="more-post-items">
</div>
<div class="load-more">
<button class="button">Load More</button>
</div>
</div>
{{end}}
<div class="timeline">
</div>
<script nonce={{.Nonce}}>
window.timeline = {
room_id: {{.Room.ID}},
permalink: {{.IsPermalink}},
{{if .IsPermalink}}
event_id: {{.Room.EventID}},
thread_in_room_id: {{.Room.ThreadInRoomID}},
permalinkedPost: {{.PermalinkedPost}},
replies: {{.Posts}},
root_event: {{.RootEvent}},
{{end}}
alias: {{.Room.Alias}},
profile: {{.IsUserProfile}},
initialPosts: JSON.parse({{.InitialPosts}}),
end: {{.LastEvent}},
owner: {{.IsOwner}},
admin: {{.IsAdmin}},
member: {{.IsMember}},
state: {{.Room.State}},
room_type: {{.Room.Type}},
room_path: {{.Room.Path}}
}
let homeserverURL = {{.HomeServerURL}}
</script>
</div>
{{template "timeline-sidebar" .}}
</div>
{{template "state" .}}
<script defer nonce={{.Nonce}} src="{{ InsertJS "timeline" }}"></script>
</div>
</div>
</body>
</html>
{{end}}

View File

@ -0,0 +1,172 @@
{{define "gallery"}}
<html>
<head>
<title>Hummingbard</title>
{{template "common-head" .}}
</head>
<body>
<div class="base {{if .LoggedInUser}}base-l{{end}}">
{{if .LoggedInUser}}
{{template "nav" Map "Page" .}}
{{end}}
<div class="root">
{{template "header" Map "Page" . "Name" "gallery" "FullHeader" true}}
<div class="content-g ">
<div class="g-con">
<div class="g-meta pa3 flex">
<div class="mr3">
{{if gt (len .Room.Avatar) 0}}
{{$hms := .HomeServerURL}}
{{$mxc := .Room.Avatar}}
<div class="thumbnail">
<img height="30" width="30" loading="lazy" src="{{.Room.Avatar}}"/>
</div>
{{else}}
<div class="gr-center">
<svg class="gr-center" height="30" width="30">
<circle cx="15" cy="15" r="15" stroke-width="0" fill="black" />
</svg>
</div>
{{end}}
</div>
<div class="gr-center small">
{{$path := .Room.Path}}
{{$ln := len .Room.PathItems}}
{{$sin := eq (len .Room.PathItems) 1 }}
{{range $id, $val := .Room.PathItems}}
{{$last := IsLastItem $id $ln}}
{{$mtc := or (eq $val.Path $path) ($sin)}}
<span class=""><a href="/{{$val.Path}}"><span class="{{if $mtc}}bold{{end}}">{{$val.Item}}</span></a>
{{if not $last}} / {{end}}
</span>
{{end}}
</div>
<div class="flex-one"></div>
{{if or .IsAdmin .IsOwner}}
<div class="room-settings flex">
</div>
{{end}}
{{if and .IsMember (not .IsPermalink) (not .IsPage)}}
<div class="">
{{if .LoggedInUser}}
<div class="new-gallery-post">
<button>Add Image</button>
</div>
{{else}}
<div class="new-gallery-post">
<button>Log In to Add Image</button>
</div>
{{end}}
</div>
{{end}}
{{if and .LoggedInUser (not .IsPermalink) (.IsPage)}}
{{if or .IsOwner .IsMember}}
<div class="gr-center">
<div class="edit-page">
<button>Edit Page</button>
</div>
</div>
{{end}}
{{end}}
{{if and .LoggedInUser (not .IsMember)}}
<div class="join-room mt3"
data-type="{{if .IsUserProfile}}user{{else}}room{{end}}"
data-alias={{.Room.Alias}}
data-name={{.Room.Path}}
data-id="{{.Room.ID}}">
<button class="">{{if .IsUserProfile}}Follow{{else}}Join{{end}} {{.Room.Path}}</button>
</div>
{{end}}
</div>
{{$hms := .HomeServerURL}}
{{if not .IsPage}}
<div class="gallery">
<div class="gr-default h-100">
<div class="lds-ring gr-center"><div></div><div></div><div></div><div></div></div>
</div>
</div>
{{else}}
<div class="flex flex-column ma3">
<div class="">
<span class="f4 bold">{{Title .Room.Name}}</span>
</div>
<div class="mt4 lh-copy page">
{{$rid := .Room.ID}}
{{$rpth := .Room.Path}}
{{$liu := .LoggedInUser}}
{{range $idx, $post := .Posts}}
{{if and (eq $post.Type "com.hummingbard.post") (eq $idx 0) (not $post.Redacted)}}
<div class="flex flex-column">
<div class="">
{{$con := index $post.Content "bodyHTML"}}
{{$con}}
</div>
<div class="mt5">
<span class="small">Last Edited: {{$post.Date}}</span>
</div>
</div>
{{end}}
{{end}}
</div>
<div class="">
</div>
</div>
{{end}}
<script nonce={{.Nonce}}>
window.timeline = {
room_id: {{.Room.ID}},
permalink: {{.IsPermalink}},
{{if .IsPermalink}}
event_id: {{.Room.EventID}},
thread_in_room_id: {{.Room.ThreadInRoomID}},
{{end}}
alias: {{.Room.Alias}},
profile: {{.IsUserProfile}},
initialPosts: JSON.parse({{.InitialPosts}}),
end: {{.LastEvent}},
owner: {{.IsOwner}},
admin: {{.IsAdmin}},
member: {{.IsMember}},
state: {{.Room.State}},
room_type: {{.Room.Type}},
room_path: {{.Room.Path}},
children: {{.Room.Children}},
pages: {{.Room.Pages}},
}
let homeserverURL = {{.HomeServerURL}}
</script>
</div>
{{template "timeline-sidebar" .}}
</div>
{{template "state" .}}
<script defer nonce={{.Nonce}} src="{{ InsertJS "timeline" }}"></script>
</div>
</div>
</body>
</html>
{{end}}

View File

@ -0,0 +1,57 @@
{{define "header"}}
{{$hd := or (eq .Name "index")}}
<div class="header {{if not $hd}}header-b{{end}}">
<div class="header-block {{if .FullHeader}}mx-w-100{{end}}">
<div class="logo">
<a href="/">
{{template "logo"}}
</a>
</div>
<div class="spacer flex ml4">
{{if eq .Name "index"}}
<div class="gr-center small">
<a href="/about"><span class=""><u>About</u></span></a>
</div>
{{end}}
</div>
<div class="tools flex">
{{if .Page.LoggedInUser}}
<div class="gr-center small mr3">
<a href="/create"><button class="bold">Create a Space</button></a>
</div>
<div class="user-menu gr-center">
{{if gt (len .Page.LoggedInUser.AvatarURL) 0}}
<div class="thumbnail">
<img src="{{.Page.LoggedInUser.AvatarURL}}" />
</div>
{{else}}
<div class="">
<svg class="gr-center" height="30" width="30">
<circle cx="15" cy="15" r="15" stroke-width="0" fill="black" />
</svg>
</div>
{{end}}
</div>
{{else}}
<div class="gr-default">
<a class="gr-center" href="/login">
<span class="gr-center small">Log In</span>
</a>
</div>
<div class="gr-default ml3">
<a class="gr-center" href="/signup">
<button>Signup</button>
</a>
</div>
{{end}}
</div>
</div>
</div>
<div class="sttp dis-no">
<div class="gr-center pointer">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24"><path fill-rule="evenodd" d="M18.78 15.28a.75.75 0 000-1.06l-6.25-6.25a.75.75 0 00-1.06 0l-6.25 6.25a.75.75 0 101.06 1.06L12 9.56l5.72 5.72a.75.75 0 001.06 0z"></path></svg>
</div>
</div>
{{end}}

View File

@ -0,0 +1,92 @@
{{define "index-user"}}
<html>
<head>
<title>Hummingbard</title>
{{template "common-head" .}}
</head>
<body>
<div class="base {{if .LoggedInUser}}base-l{{end}}">
{{if .LoggedInUser}}
{{template "nav" Map "Page" .}}
{{end}}
<div class="root">
{{template "header" Map "Page" . "Name" "index-user"}}
<div class="center-content">
{{if .Posts}}
<div class="brd-lr flex flex-column">
<div class=" pa3 brd-btm flex" style="min-height:60px;">
<div class="gr-center">
<span class="small bold">Your feed</span>
</div>
<div class="flex-one"></div>
<div class="gr-center">
<div class="new-post">
<button>New Post</button>
</div>
</div>
</div>
<div class="share-post"></div>
{{if and .LoggedInUser }}
<div class="create-post"></div>
{{end}}
<div class="added-posts"></div>
<div class="posts ">
{{$rpth := "index-user"}}
{{$liu := .LoggedInUser}}
{{$hms := .HomeServerURL}}
{{range $idx, $post := .Posts}}
{{if and (eq $post.Type "com.hummingbard.post") (not $post.Redacted)}}
{{template "timeline-event" Map "Event" $post "HomeServerURL" $hms "RoomPath" $rpth "LoggedInUser" $liu "UserFeed" true}}
{{end}}
{{end}}
</div>
<div class="more-posts flex flex-column">
<div class="more-post-items">
</div>
<div class="load-more">
</div>
</div>
<div class="timeline">
</div>
</div>
{{else}}
<div class="gr-center flex flex-column">
<div class="">
Your feed looks a bit empty.
</div>
<div class="mt3 tc">
<button class="">Explore Spaces</button>
</div>
</div>
{{end}}
</div>
<script nonce={{.Nonce}}>
window.timeline = {
userFeed: {{true}},
feed: {{.FeedItems}},
permalink: {{false}},
room_path: {{.Room.Path}},
initialPosts: JSON.parse({{.InitialPosts}}),
}
let homeserverURL = {{.HomeServerURL}}
window.index = {
state: {{.State}},
posts: {{.Posts}}
}
</script>
{{template "state" .}}
<script defer nonce={{.Nonce}} src="{{ InsertJS "timeline" }}"></script>
</div>
</div>
</body>
</html>
{{end}}

117
templates/index/index.html Normal file
View File

@ -0,0 +1,117 @@
{{define "index"}}
<html>
<head>
<title>Hummingbard - Matrix-powered communities</title>
<meta name="description" content="Matrix-powered communities.">
{{template "common-head" .}}
<style>
canvas {
width: 100%;
height: 60vh;
}
</style>
</head>
<body>
<div class="root ind-rws">
{{template "header" Map "Page" . "Name" "index"}}
<div class="ind-meta gr-default pv5 ph3">
<div class="gr-center flex flex-column">
<div class="flex flex-column tc">
<div class="f3 bold">
Hummingbard
</div>
<div class="mt3 f4 bold">
Decentralized Communities For The Future
</div>
</div>
{{if gt (len .PublicRooms) 0}}
<div class="flex flex-column mt5">
<div class="gr-center">
<span class="small bold ncom">new communities</span>
</div>
<div class="gr-center flex flex-wrap justify-center mt4">
{{range .PublicRooms}}
<div class="mr3 mb3 small">
<a href="/{{.RoomPath}}"><span class="c-it {{if (HasColon .RoomPath)}}c-fe{{end}}">{{.RoomPath}}{{if (HasColon .RoomPath)}} 🌐{{end}}</span></a>
</div>
{{end}}
</div>
</div>
{{end}}
</div>
</div>
<div class="content-c brd-lr ">
<div class="con">
<div class="posts">
<div class="pa3 brd-btm brd-tp gr-center">
<span class="small"><strong>Public Feed</strong></span>
</div>
{{$hms := .HomeServerURL}}
{{$rpth := .Room.Path}}
{{if .Posts}}
{{range $idx, $post := .Posts}}
{{if (eq $post.Type "com.hummingbard.post")}}
{{template "timeline-event" Map "Event" $post "HomeServerURL" $hms "RoomPath" $rpth}}
{{end}}
{{end}}
{{else}}
<div class="gr-default h-100">
<div class="gr-center small bold">
No posts yet
</div>
</div>
{{end}}
</div>
</div>
<div class="more-posts flex flex-column">
<div class="more-post-items">
</div>
<div class="load-more">
<button class="button">Load More</button>
</div>
</div>
<div class="timeline">
</div>
<script nonce={{.Nonce}}>
window.timeline = {
public: true,
room_id: {{.Room.ID}},
permalink: {{.IsPermalink}},
{{if .IsPermalink}}
event_id: {{.Room.EventID}},
thread_in_room_id: {{.Room.ThreadInRoomID}},
{{end}}
alias: {{.Room.Alias}},
profile: {{.IsUserProfile}},
initialPosts: JSON.parse({{.InitialPosts}}),
end: {{.LastEvent}},
owner: {{.IsOwner}},
admin: {{.IsAdmin}},
member: {{.IsMember}},
state: {{.Room.State}},
room_type: {{.Room.Type}},
room_path: {{.Room.Path}}
}
let homeserverURL = {{.HomeServerURL}}
</script>
</div>
{{template "state" .}}
<script defer nonce={{.Nonce}} src="{{ InsertJS "index" }}"></script>
<script defer nonce={{.Nonce}} src="{{ InsertJS "timeline" }}"></script>
</div>
</body>
</html>
{{end}}

9
templates/logo/logo.html Normal file
View File

@ -0,0 +1,9 @@
{{define "logo"}}
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 34 34" width="34" height="34">
<g id="layer1">
<path id="path10" fill="#ffd42a" d="M17 33.71C7.76 33.71 0.29 26.24 0.29 17C0.29 7.76 7.76 0.29 17 0.29C26.24 0.29 33.71 7.76 33.71 17C33.71 26.24 26.24 33.71 17 33.71Z" />
<path id="path68" fill="#000" d="M17 27.55C11.17 27.55 6.45 22.83 6.45 17C6.45 11.17 11.17 6.45 17 6.45C22.83 6.45 27.55 11.17 27.55 17C27.55 22.83 22.83 27.55 17 27.55Z" />
<path id="path70" fill="#ffd42a" d="M17 21.29C14.63 21.29 12.71 19.37 12.71 17C12.71 14.63 14.63 12.71 17 12.71C19.37 12.71 21.29 14.63 21.29 17C21.29 19.37 19.37 21.29 17 21.29Z" />
</g>
</svg>
{{end}}

9
templates/nav/nav.html Normal file
View File

@ -0,0 +1,9 @@
{{ define "nav" }}
<div class="nav-de">
<div class="gr-default pointer" style="min-height:56px;">
<div class="gr-center pv3" >
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M1 2.75A.75.75 0 011.75 2h12.5a.75.75 0 110 1.5H1.75A.75.75 0 011 2.75zm0 5A.75.75 0 011.75 7h12.5a.75.75 0 110 1.5H1.75A.75.75 0 011 7.75zM1.75 12a.75.75 0 100 1.5h12.5a.75.75 0 100-1.5H1.75z"></path></svg>
</div>
</div>
</div>
{{end}}

View File

@ -0,0 +1,92 @@
{{define "public"}}
<html>
<head>
<title>Public Feed - Hummingbard</title>
<meta name="description" content="Matrix-powered communities.">
{{template "common-head" .}}
<style>
canvas {
width: 100%;
height: 60vh;
}
</style>
</head>
<body>
<div class="base {{if .LoggedInUser}}base-l{{end}}">
{{if .LoggedInUser}}
{{template "nav" Map "Page" .}}
{{end}}
<div class="root">
{{template "header" Map "Page" . "Name" "public"}}
<div class="content-c brd-lr ">
<div class="con">
<div class="posts">
<div class="pa3 brd-btm gr-center">
<span class="small"><strong>Public Feed</strong></span>
</div>
{{$hms := .HomeServerURL}}
{{$rpth := .Room.Path}}
{{$liu := .LoggedInUser}}
{{if .Posts}}
{{range $idx, $post := .Posts}}
{{if and (eq $post.Type "com.hummingbard.post") (not $post.Redacted)}}
{{template "timeline-event" Map "Event" $post "HomeServerURL" $hms "RoomPath" $rpth "LoggedInUser" $liu}}
{{end}}
{{end}}
{{else}}
<div class="gr-default h-100">
<div class="gr-center small bold">
No posts yet
</div>
</div>
{{end}}
</div>
</div>
<div class="more-posts flex flex-column">
<div class="more-post-items">
</div>
<div class="load-more">
<button class="button">Load More</button>
</div>
</div>
<div class="timeline">
</div>
<script nonce={{.Nonce}}>
window.timeline = {
public: true,
room_id: {{.Room.ID}},
permalink: {{.IsPermalink}},
{{if .IsPermalink}}
event_id: {{.Room.EventID}},
thread_in_room_id: {{.Room.ThreadInRoomID}},
{{end}}
alias: {{.Room.Alias}},
profile: {{.IsUserProfile}},
initialPosts: JSON.parse({{.InitialPosts}}),
end: {{.LastEvent}},
owner: {{.IsOwner}},
admin: {{.IsAdmin}},
member: {{.IsMember}},
state: {{.Room.State}},
room_type: {{.Room.Type}},
room_path: {{.Room.Path}}
}
let homeserverURL = {{.HomeServerURL}}
</script>
</div>
{{template "state" .}}
<script defer nonce={{.Nonce}} src="{{ InsertJS "index" }}"></script>
<script defer nonce={{.Nonce}} src="{{ InsertJS "timeline" }}"></script>
</div>
</div>
</body>
</html>
{{end}}

View File

@ -0,0 +1,25 @@
{{define "state"}}
<script nonce={{.Nonce}}>
let authenticated = false;
let identity;
{{if .LoggedInUser}}
authenticated = true
identity = {
user_id: {{.LoggedInUser.UserID}},
display_name: {{.LoggedInUser.DisplayName}},
avatar_url: {{.LoggedInUser.AvatarURL}},
matrix_access_token: {{.LoggedInUser.MatrixAccessToken}},
access_token: {{.LoggedInUser.AccessToken}},
device_id: {{.LoggedInUser.DeviceID}},
home_server: {{.LoggedInUser.HomeServer}},
well_known: {{.LoggedInUser.WellKnown}},
room_id: {{.LoggedInUser.RoomID}},
federated: {{.LoggedInUser.Federated}},
joined_rooms: {{.LoggedInUser.JoinedRooms}},
}
{{end}}
</script>
{{if .LoggedInUser}}
<script defer nonce={{.Nonce}} src="{{ InsertJS "userMenu" }}"></script>
{{end}}
{{end}}

View File

@ -0,0 +1,45 @@
{{define "about"}}
<html>
<head>
<title>About - Hummingbard</title>
{{template "common-head" .}}
</head>
<body>
<div class="root">
{{template "header" Map "Page" . "Name" "login"}}
<div class="content-c create-room">
<div class="mt5 pa3">
<div class="lh-copy pi flex flex-column brd pa3">
<div class="">
Hummingbard is a decentralized community platform built on top of <a
href="https://matrix.org"><span class="primary">Matrix</span></a>.
</div>
<div class="mt3">
<ul>
<li>
Every <i><strong>space</strong></i> on Hummingbard is a regular Matrix room.
</li>
<li>
Users can run their own instance of Hummingbard.
</li>
<li>
Users on one server can join spaces on other servers.
</li>
<li>
Users on one server can follow users on other servers.
</li>
<li>
All data is replicated to all participating servers.
</li>
</ul>
</div>
</div>
</div>
</div>
{{template "state" .}}
</div>
</body>
</html>
{{end}}

View File

@ -0,0 +1,63 @@
{{define "article-item"}}
<div class="mt3 flex flex-column{{if .Nested}} {{end}}"
id="{{.Event.ShortID}}">
{{$article := index .Event.Content "com.hummingbard.article"}}
{{$img := index $article "featured_image"}}
{{$mxc := ""}}
{{if $img}}
{{$mxc = index $img "mxc"}}
{{end}}
{{$ft := gt (len $mxc) 0}}
<a href="/{{index .Event.Content "room_path"}}/{{index $article "slug"}}">
<div class="flex flex-column lh-copy brd">
{{if $ft}}
<div class="featured-image bg-img" style="background-image:url({{$mxc}});">
</div>
{{end}}
<div class="pa3 flex flex-column {{if $ft}}pa3{{end}}">
<div class="">
<span class="f4 bold">{{index $article "title"}}</span>
</div>
{{$sub := index $article "subtitle"}}
{{if $sub}}
{{if gt (len $sub) 0}}
<div class="mt3">
{{$sub}}
</div>
{{end}}
{{end}}
{{$des := index $article "description"}}
{{if $des}}
{{if gt (len $des) 0}}
<div class="mt3">
{{$des}}
</div>
{{end}}
{{end}}
</div>
</div>
</div>
</a>
{{end}}

View File

@ -0,0 +1,18 @@
{{define "children"}}
<div class="flex flex-column {{if .Nested}}chld{{end}}">
<div class="">
{{if .Page}}
<a href="/{{.Child.Path}}"><span class="primary">{{.Child.Alias}}</span></a>
{{else}}
<a href="/{{.Child.Path}}"><span class="focusable primary hov-un" data-room-id={{.Child.RoomID}}>{{.Child.Alias}}</span></a>
{{end}}
</div>
</div>
<div class="flex flex-column {{if .Nested}}chld{{end}}">
{{if .Child.Children}}
{{range .Child.Children}}
{{template "children" Map "Child" . "Nested" true}}
{{end}}
{{end}}
</div>
{{end}}

View File

@ -0,0 +1,45 @@
{{define "youtube-item"}}
<div class="youtube mb3"
data-id={{index . "youtube_id"}}
data-title={{index . "title"}}
data-href={{index . "href"}}
data-description={{index . "description"}}>
<div class="link-item flex">
{{$id := index . "youtube_id"}}
<div class="vp-i bg-img gr-default pointer"
style="background-image: url(https://img.youtube.com/vi/{{$id}}/mqdefault.jpg);">
<div class="gr-center">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 40 40" width="40" height="40">
<defs>
<image width="40" height="40" id="img1" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAMAAAC7IEhfAAAAAXNSR0IB2cksfwAAAJBQTFRFAAAAAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBYmJidXV1CAgIenp6////5OTkYmJiAQEB7+/vAQEBY2NjdXV1AQEBY2NjAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBsx3KIgAAADB0Uk5TAAw7g77c8/0Yh93/hhmo+qda5Vmcmwe4/tbS8eD/6tT06jrU0fLT/IL5F6bkWJq3xQuJawAAATVJREFUeJyNldl6gyAQhUdFMS7RaozpYtOaWtOs7/92BWsRYTCcO+X/jswwHAFkOa5H/IDSwCee64BJ4SqisRCNkhDF0nUWK8ryVOeeChXjKkoFczYYxlXNtrqtdyZwV28lv9qEcdWT58boN3hWoo4ljGusKEXrlVX8dWktXjy/vOJkPpzH1Oe35n3/gYEZP6NVLIFN84m6Jqw10RxsUNfIAZeqIOZKW/BiHWSuB8XVA4KCmisB3wBy12Ba8iEwgsz1S7h2QBdA5vo9GvWPwIMArT9tXYx1eywbfrQ/QuuhgGQOGsfManB/TnzEc/FsvApny8t1GdOqfARe/y92tRwAN8tIuUsxtRRSdymkmGdlAm9qQpdo7ZcraEpzPZrPeIqHiRL2JxQbttqy30fX951Pju18c7+whEyqDwiKzQAAAABJRU5ErkJggg=="/>
</defs>
<style>
tspan { white-space:pre }
</style>
<use id="Background" href="#img1" x="0" y="0" />
</svg>
</div>
</div>
<div class="gr-default ph3">
<div class="gr-center flex flex-column">
<div class="fs-09 clmp-2">
<a href="{{index . "href"}}">
<span class="primary">
{{index . "title"}}
</span>
</a>
</div>
<div class="mt1 smaller o-90 clmp-2">
{{index . "description"}}
</div>
<div class="mt1 smaller o-90 clmp-2">
{{index . "href"}}
</div>
</div>
</div>
</div>
</div>
{{end}}

View File

@ -0,0 +1,411 @@
{{define "timeline-event"}}
{{$reply := or .Reply .NestedReply}}
{{$msgtype := index .Event.Content "msgtype"}}
{{$l := false}}
{{if .Depth}}{{if eq .Depth 1}}{{$l = true}}{{end}}{{end}}
{{$evid := ""}}
{{$id := index .Event.Content "event_id"}}
{{if $id}}
{{$evid = (index .Event.Content "event_id")}}
{{$evid = (Concat "/" $evid)}}
{{end}}
{{$rpth := index .Event.Content "room_path"}}
{{if and .RoomPath}}
{{if eq $rpth .RoomPath}}
{{$evid = (Concat "/" .Event.ID)}}
{{end}}
{{end}}
{{$evid = Concat (index .Event.Content "room_path") $evid}}
{{if .Nested}}
{{$evid = Concat (index .Event.Content "room_path") "/" (.Event.ID)}}
{{$rp := index .Event.Content "share_reply"}}
{{if $rp}}{{$evid = index .Event.Content "reply_permalink"}}{{end}}
{{end}}
{{$nsfw := index .Event.Content "nsfw"}}
<div class="po-co flex flex-column{{if .Nested}} brd-tp brd-lr{{end}} {{if .NestedReply}}mt3 {{if $l}}ns-1{{end}}{{end}} {{if .Focus}}foc{{end}} {{if $nsfw}}relative{{end}}"
id="{{.Event.ShortID}}">
<div class="pi flex flex-column lh-copy {{if .Focus}}foc{{end}} ">
<div class="flex">
<div class="mr3">
<a href="/{{.Event.Author.FormattedID}}" title="{{.Sender}}">
{{if gt (len .Event.Author.AvatarURL) 0}}
<div class="thumbnail{{if .Nested}}-s{{end}}">
<img src="{{.Event.Author.AvatarURL}}" />
</div>
{{else}}
{{if or .Nested $reply}}
<svg class="gr-center" height="22" width="22">
<circle cx="11" cy="11" r="11" stroke-width="0" fill="black" />
</svg>
{{else}}
<svg class="gr-center" height="30" width="30">
<circle cx="15" cy="15" r="15" stroke-width="0" fill="black" />
</svg>
{{end}}
{{end}}
</a>
</div>
<div class="flex flex-column flex-one">
{{$us := .Event.Author.FormattedID}}
{{if gt (len .Event.Author.DisplayName) 0}}
{{$us = .Event.Author.DisplayName}}
{{end}}
<div class="small">
<a class="relative" href="/{{.Event.Author.FormattedID}}">
<span class="focusable" data-room-id={{.RoomID}}><strong>{{$us}}</strong></span>
</a>
{{if and .Event.IsArticle (not .Nested)}}
posted an article
{{end}}
{{$shtp := "a post"}}
{{if .Event.SharedPost}}
{{$rp := index .Event.SharedPost.Content "share_reply"}}
{{if $rp}}{{$shtp = "a reply"}}{{end}}
{{$art := index .Event.SharedPost.IsArticle}}
{{if $art}}{{$shtp = "an article"}}{{end}}
{{$dn := .Event.SharedPost.Author.DisplayName}}
shared {{$shtp}} by <a href="/@{{$dn}}"><span class="primary">@{{.Event.SharedPost.Author.DisplayName}}</span></a>
{{end}}
{{if and (not .PermalinkedPost) (not .Reply) (not .NestedReply)}}
{{$rpth := index .Event.Content "room_path"}}
{{if $rpth}}
{{if gt (len $rpth) 0}}
{{$showpath := and (ne $rpth .RoomPath) (not (IsUserProfile $rpth))}}
{{if $showpath}}
in <a href="/{{$rpth}}"><span class="primary">{{$rpth}}</span></a>
{{end}}
{{end}}
{{end}}
{{end}}
{{if $reply}}
<span title={{.Date}}>{{.Event.When}}</span>
{{end}}
</div>
{{if not $reply}}
<div class="small o-90">
<span title={{.Date}}>{{.Event.When}}</span>
</div>
{{end}}
</div>
<div class="post-tools flex">
{{if and .LoggedInUser (not .Event.SharedPost)}}
<div class="share pointer"
data-id="{{.Event.ID}}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M8 2.5a5.487 5.487 0 00-4.131 1.869l1.204 1.204A.25.25 0 014.896 6H1.25A.25.25 0 011 5.75V2.104a.25.25 0 01.427-.177l1.38 1.38A7.001 7.001 0 0114.95 7.16a.75.75 0 11-1.49.178A5.501 5.501 0 008 2.5zM1.705 8.005a.75.75 0 01.834.656 5.501 5.501 0 009.592 2.97l-1.204-1.204a.25.25 0 01.177-.427h3.646a.25.25 0 01.25.25v3.646a.25.25 0 01-.427.177l-1.38-1.38A7.001 7.001 0 011.05 8.84a.75.75 0 01.656-.834z"></path></svg>
</div>
{{end}}
{{if and (not .PermalinkedPost) (not .UserFeed)}}
<div class="perma-link pointer ml3">
<a class="" href="/{{$evid}}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M10.604 1h4.146a.25.25 0 01.25.25v4.146a.25.25 0 01-.427.177L13.03 4.03 9.28 7.78a.75.75 0 01-1.06-1.06l3.75-3.75-1.543-1.543A.25.25 0 0110.604 1zM3.75 2A1.75 1.75 0 002 3.75v8.5c0 .966.784 1.75 1.75 1.75h8.5A1.75 1.75 0 0014 12.25v-3.5a.75.75 0 00-1.5 0v3.5a.25.25 0 01-.25.25h-8.5a.25.25 0 01-.25-.25v-8.5a.25.25 0 01.25-.25h3.5a.75.75 0 000-1.5h-3.5z"></path></svg>
</a>
</div>
{{end}}
{{if .UserFeed}}
<div class="perma-link pointer ml3">
<a class="" href="/{{index .Event.Content "room_path"}}/{{index .Event.ID}}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M10.604 1h4.146a.25.25 0 01.25.25v4.146a.25.25 0 01-.427.177L13.03 4.03 9.28 7.78a.75.75 0 01-1.06-1.06l3.75-3.75-1.543-1.543A.25.25 0 0110.604 1zM3.75 2A1.75 1.75 0 002 3.75v8.5c0 .966.784 1.75 1.75 1.75h8.5A1.75 1.75 0 0014 12.25v-3.5a.75.75 0 00-1.5 0v3.5a.25.25 0 01-.25.25h-8.5a.25.25 0 01-.25-.25v-8.5a.25.25 0 01.25-.25h3.5a.75.75 0 000-1.5h-3.5z"></path></svg>
</a>
</div>
{{end}}
{{if and (.LoggedInUser) (not .Nested)}}
<div class="post-menu ml3"
data-id="{{.Event.ID}}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M12.78 6.22a.75.75 0 010 1.06l-4.25 4.25a.75.75 0 01-1.06 0L3.22 7.28a.75.75 0 011.06-1.06L8 9.94l3.72-3.72a.75.75 0 011.06 0z"></path></svg>
</div>
{{end}}
</div>
</div>
<div class="post-container{{if or .Nested $reply .PermalinkedPost}}-s{{end}}">
{{if eq $msgtype "m.text"}}
{{if not .Event.IsArticle}}
{{$bod := (index .Event.Content "body")}}
{{$body := (index .Event.Content "bodyHTML")}}
{{$long := and ( gt (len $bod) 666) (not .PermalinkedPost)}}
{{$pid := (RandomString 8)}}
<div class="post-content relative pt3 mb2 {{if .Nested}}fs-09{{end}} {{if $long}}long pb3{{end}}"
id="tl-{{$pid}}">
{{$body}}
{{if $long}}
<div class="gradient"
id="gr-{{$pid}}">
</div>
<div class="read-more"
data-id="{{$pid}}">
<button class="small">Read More</button>
</div>
{{end}}
</div>
{{else}}
{{template "article-item" .}}
{{end}}
{{if not .Nested}}
{{if (index .Event.Content "shared_post")}}
{{$post := (index .Event.SharedPost)}}
{{$hms := .HomeServerURL}}
{{$rpth := .RoomPath}}
{{$liu := .LoggedInUser}}
{{template "timeline-event" Map "Event" $post "HomeServerURL" $hms "RoomPath" $rpth "LoggedInUser" $liu "Nested" true}}
{{end}}
{{end}}
{{$imgs := false}}
{{if index .Event.Content "images"}}
{{$imgs = true}}
{{$hms := .HomeServerURL}}
<div class="post-images">
{{range $idx, $image := index .Event.Content "images"}}
<div class="gr-center w-100">
<div class="pi-c"
style="padding-bottom:{{$image.aspect_ratio}}%;">
{{$mxc := index $image "mxc"}}
<a href="{{$hms}}/_matrix/media/r0/download/{{$mxc}}">
<img loading="lazy" src="{{$hms}}/_matrix/media/r0/download/{{$mxc}}"/>
</a>
</div>
</div>
{{end}}
</div>
{{end}}
{{if index .Event.Content "links"}}
<div class="link-items {{if $imgs}}mt3{{end}}">
{{range $idx, $link := index .Event.Content "links"}}
{{if (index $link "is_youtube")}}
{{template "youtube-item" .}}
{{else}}
<div class="link-item pa3 flex flex-column fs-09">
{{$title := index $link "title"}}
{{$desc := index $link "description"}}
<div class="">
<a href="{{index $link "href"}}">
<span class="primary">
{{if $title}}
{{if gt (len $title) 0}}
{{index $link "title"}}
{{end}}
{{else}}
{{index $link "href"}}
{{end}}
</span>
</a>
</div>
{{if $title}}
{{if gt (len $title) 0}}
<div class="small o-90 mt2">
{{index $link "href"}}
</div>
{{end}}
{{end}}
</div>
{{end}}
{{end}}
</div>
{{end}}
{{if index .Event.Content "attachments"}}
{{$ns := gt (len (index .Event.Content "attachments")) 0}}
<div class="attachment-items flex flex-column mb3 pa3">
{{$hms := .HomeServerURL}}
{{range $idx, $attachment := index .Event.Content "attachments"}}
{{$mxc := index $attachment "mxc"}}
<div class="attachment-item flex flex-column {{if $ns}}mb3{{end}}">
<div class="fs-09 flex-one">
<a href="{{$hms}}/_matrix/media/r0/download/{{$mxc}}">
<span class="primary">{{index $attachment "filename"}}</span>
</a>
</div>
<div class="mt1 small o-90">
{{$fs := (index $attachment "size")}}
{{index $attachment "mimetype"}} - {{FileSize $fs}}
</div>
</div>
{{end}}
</div>
{{end}}
{{else if eq $msgtype "m.image"}}
{{$hms := .HomeServerURL}}
<div class="post-images mv2">
<div class="gr-center w-100">
{{$mxc := StripMXCPrefix (index .Event.Content "url")}}
{{$info := index .Event.Content "info"}}
{{$height := index $info "h"}}
{{$width := index $info "w"}}
<div class="pi-c relative"
style="padding-bottom: calc(({{$height}}/{{$width}})*100%);">
<a href="{{$hms}}/_matrix/media/r0/download/{{$mxc}}">
<img loading="lazy" src="{{$hms}}/_matrix/media/r0/download/{{$mxc}}"/>
</a>
</div>
</div>
</div>
{{else if eq $msgtype "m.file"}}
<div class="attachment-items flex flex-column mv2 pa3">
{{$hms := .HomeServerURL}}
{{$mxc := StripMXCPrefix (index .Event.Content "url")}}
{{$info := index .Event.Content "info"}}
{{$mimetype := index $info "mimetype"}}
{{$size := index $info "size"}}
<div class="attachment-item flex flex-column ">
<div class="fs-09 flex-one">
<a href="{{$hms}}/_matrix/media/r0/download/{{$mxc}}">
<span class="primary">{{index .Event.Content "body"}}</span>
</a>
</div>
<div class="mt1 small o-90">
{{$mimetype}} - {{FileSize $size}}
</div>
</div>
</div>
{{else if eq $msgtype "m.video"}}
<div class="video-item flex flex-column mv2">
{{$hms := .HomeServerURL}}
{{$mxc := StripMXCPrefix (index .Event.Content "url")}}
{{$info := index .Event.Content "info"}}
{{$mimetype := index $info "mimetype"}}
{{$size := index $info "size"}}
{{$height := index $info "h"}}
{{$width := index $info "w"}}
<div class="vi-c" style="padding-bottom: calc(({{$height}}/{{$width}})*100%);">
<figure class="video-container" data-fullscreen="false">
<video controls>
<source src="{{$hms}}/_matrix/media/r0/download/{{$mxc}}"
type="">
</video>
</figure>
</div>
</div>
{{else if eq $msgtype "m.audio"}}
<div class="audio-item flex flex-column mv2">
{{$hms := .HomeServerURL}}
{{$mxc := StripMXCPrefix (index .Event.Content "url")}}
<audio controls>
<source src="{{$hms}}/_matrix/media/r0/download/{{$mxc}}"
type="{{.Attachment.FileType}}">
</audio>
</div>
{{end}}
{{$mrg := false}}
{{$lnk := index .Event.Content "links"}}
{{$img := index .Event.Content "images"}}
{{$att := index .Event.Content "attachments"}}
{{if or $lnk $img $att}}{{$mrg = true}}{{end}}
{{if gt .Event.TotalReplies 0}}
<div class="{{if $mrg}}mt3{{end}}">
{{if .UserFeed}}
<a class="" href="/{{index .Event.Content "room_path"}}/{{index .Event.ID}}">
{{else}}
<a class="" href="/{{$evid}}">
{{end}}
{{$rep := "Reply"}}
{{if gt .Event.TotalReplies 1}}{{$rep = "Replies"}}{{end}}
<span class="small primary hov-un">{{.Event.TotalReplies}} {{$rep}}</span>
</a>
</div>
{{end}}
</div>
{{$nsfw := index .Event.Content "nsfw"}}
{{if and $nsfw (not .PermalinkedPost)}}
<div class="nsfw-mask gr-default nsfw brd-btm"
id="nsfw-{{.Event.ShortID}}"
data-id="{{.Event.ShortID}}">
<div class="gr-center small">
NSFW. <span class="primary pointer hov-un">Click to View</span>
</div>
</div>
{{end}}
</div>
{{if or .Reply .NestedReply}}
<div class="flex reply-to-reply" data-id="{{.Event.ShortID}}">
<div class="flex-one"></div>
<div class="flex pointer">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M6.78 1.97a.75.75 0 010 1.06L3.81 6h6.44A4.75 4.75 0 0115 10.75v2.5a.75.75 0 01-1.5 0v-2.5a3.25 3.25 0 00-3.25-3.25H3.81l2.97 2.97a.75.75 0 11-1.06 1.06L1.47 7.28a.75.75 0 010-1.06l4.25-4.25a.75.75 0 011.06 0z"></path></svg>
</div>
</div>
{{end}}
{{if and (not .Nested) (not .NestedReply)}}
{{end}}
</div>
{{end}}

View File

@ -0,0 +1,73 @@
{{define "timeline-sidebar"}}
<div class="sidebar mt3 flex flex-column lh-copy">
<div class="fs-09">
<span class=""><strong>{{.Room.Name}}</strong></span>
</div>
<div class="mt3 fs-09">
{{.Room.Topic}}
</div>
{{if or .IsAdmin .IsOwner}}
<div class="mt3 tr">
<div class="load-room-settings flex">
<div class="">
<button>{{if .IsUserProfile}}Edit Profile{{else}}Settings{{end}}</button>
</div>
</div>
</div>
{{end}}
{{if .Room.Children}}
<div class="mt4 ">
<div class="mb2">
<span class="small bold">Sub-Spaces</span>
</div>
{{range .Room.Children}}
{{template "children" Map "Child" . "Nested" false}}
{{end}}
</div>
{{end}}
{{if .Room.Pages}}
<div class="mt4 ">
<div class="mb2">
<span class="small bold">Pages</span>
</div>
{{range .Room.Pages}}
{{template "children" Map "Child" . "Nested" false "Page" true}}
{{end}}
</div>
{{end}}
{{if gt .Room.Members 0}}
<div class="mt4 room-members" data-members={{.Room.Members}}>
<div class="fs-09 flex pointer">
<div class="flex-one">
<strong>{{.Room.Members}}</strong> {{if .IsUserProfile}}Follower{{else}}Member{{end}}{{if gt .Room.Members 1}}s{{end}}
</div>
{{if and .LoggedInUser .IsMember}}
<div class="gr-default o-60 hov-op">
<svg class="gr-center" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M12.78 6.22a.75.75 0 010 1.06l-4.25 4.25a.75.75 0 01-1.06 0L3.22 7.28a.75.75 0 011.06-1.06L8 9.94l3.72-3.72a.75.75 0 011.06 0z"></path></svg>
</div>
{{end}}
</div>
</div>
{{end}}
{{if and .LoggedInUser (.IsMember) (not .IsOwner) (not .IsUserProfile) (not .IsPermalink)}}
<div class="join-room mt3"
data-type="{{if .IsUserProfile}}user{{else}}room{{end}}"
data-alias={{.Room.Alias}}
data-name={{.Room.Path}}
data-id="{{.Room.ID}}">
<button class="light">{{if .IsMember}}Leave{{else}}Join{{end}} {{.Room.Path}}</button>
</div>
{{end}}
<div class="mt4 bor-btm"></div>
<div class="mt4 small flex flex-column">
<span class="">Created on {{.Room.CreatedAt}}</span>
<span class="mt2">By <a href="/{{.Room.Owner.UserID}}"><span class="primary">{{.Room.Owner.UserID}}</span></a></span>
</div>
<div class="mt3">
</div>
</div>
{{end}}

View File

@ -0,0 +1,413 @@
{{define "timeline"}}
<html>
<head>
<title>{{.Room.Name}} - Hummingbard</title>
<meta name="description" content="{{.Room.Topic}}">
{{template "common-head" .}}
{{if gt (len .Room.CSS) 0}}
<style nonce="{{.Nonce}}" type="text/css">
{{.Room.CSS}}
</style>
{{end}}
</head>
<body>
<div class="base {{if .LoggedInUser}}base-l{{end}}">
{{if .LoggedInUser}}
{{template "nav" Map "Page" .}}
{{end}}
<div class="root">
{{template "header" Map "Page" . "Name" "timeline"}}
<div class="content{{if .IsUserProfile}}-c{{else}} brd-r{{end}}">
<div class="con brd-lr">
{{if and .IsUserProfile (not .IsPermalink)}}
{{$hd := gt (len .Room.Header) 0}}
<div class="">
{{if $hd}}
<div class="room-header-alt bg-img" style="background-image:url({{.Room.Header}});">
</div>
{{end}}
<div class="room-meta{{if $hd}} room-meta-alt{{end}} brd-btm flex flex-column">
<div class="flex relative">
{{if gt (len .Room.Avatar) 0}}
<a href="{{.Room.Avatar}}">
<div class="room-avatar {{if $hd}}absolute{{else}}mb3{{end}} bg-img {{if $hd}}avbrd{{end}}"
style="background-image:url({{.Room.Avatar}});">
</div>
</a>
{{end}}
<div class="flex-one">
</div>
<div class="">
{{if .IsOwner}}
<div class="load-room-settings flex">
<div class="">
<button class="light">Edit Profile</button>
</div>
</div>
{{end}}
</div>
{{if and .LoggedInUser (.IsUserProfile) (not .IsOwner)}}
<div class="join-room"
data-type="{{if .IsUserProfile}}user{{else}}room{{end}}"
data-alias={{.Room.Alias}}
data-name={{.Room.Path}}
data-id="{{.Room.ID}}">
<button class="{{if .IsMember}}light{{end}}">{{if .IsMember}}Unfollow{{else}}Follow{{end}} {{.Room.Path}}</button>
</div>
{{end}}
</div>
{{$nm := .Room.Path}}
{{if gt (len .Room.Name) 0}}{{$nm = .Room.Name}}{{end}}
<div class="mt1">
<strong>{{$nm}}</strong>
</div>
{{if gt (len .Room.Name) 0}}
<div class="mt1 small primary">
{{.Room.Path}}
</div>
{{end}}
<div class="room-topic mt3 fs-09 lh-copy">
{{.Room.Topic}}
</div>
<div class="mt4 flex">
<div class="o-80 fs-09">
Joined on {{.Room.CreatedAt}}
</div>
</div>
</div>
</div>
{{end}}
{{if and (not .IsUserProfile) (gt (len .Room.Header) 0)}}
<div class="">
<div class="room-header bg-img" style="background-image:url({{.Room.Header}});">
</div>
</div>
{{end}}
<div class="pt3 pb3 ph3 brd-btm flex" style="min-height: 60px;">
{{if not .IsUserProfile}}
<div class="mr3">
{{if gt (len .Room.Avatar) 0}}
{{$hms := .HomeServerURL}}
{{$mxc := .Room.Avatar}}
<div class="thumbnail">
<img height="30" width="30" loading="lazy" src="{{.Room.Avatar}}"/>
</div>
{{else}}
<div class="gr-center">
<svg class="gr-center" height="30" width="30">
<circle cx="15" cy="15" r="15" stroke-width="0" fill="black" />
</svg>
</div>
{{end}}
</div>
<div class="gr-center small">
{{$path := .Room.Path}}
{{$ln := len .Room.PathItems}}
{{$sin := eq (len .Room.PathItems) 1 }}
{{$prm := .IsPermalink}}
{{range $id, $val := .Room.PathItems}}
{{$last := IsLastItem $id $ln}}
{{$mtc := or (eq $val.Path $path) ($sin)}}
{{if and $prm $last}}{{$mtc = true}}{{end}}
<span class=""><a href="/{{$val.Path}}"><span class="{{if $mtc}}bold{{end}}">{{$val.Item}}</span></a>
{{if not $last}} / {{end}}
</span>
{{end}}
</div>
{{else}}
{{if and .IsUserProfile .IsPermalink}}
<div class="gr-center flex">
<div class="">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M9.78 12.78a.75.75 0 01-1.06 0L4.47 8.53a.75.75 0 010-1.06l4.25-4.25a.75.75 0 011.06 1.06L6.06 8l3.72 3.72a.75.75 0 010 1.06z"></path></svg>
</div>
<div class="gr-center ml2 small">
<a href="/{{index .PermalinkedPost.Content "room_path"}}">
<span class="primary hov-un">Back To Profile<span>
</a>
</div>
</div>
{{end}}
{{end}}
{{if and .IsUserProfile (not .IsPermalink)}}
{{if gt .Room.Members 0}}
<div class="gr-center fs-09">
<strong>{{.Room.Members}}</strong> Follower{{if gt .Room.Members 1}}s{{end}}
</div>
{{end}}
{{end}}
<div class="flex-one"></div>
{{if and .LoggedInUser (not .IsPermalink) (not .IsPage)}}
{{if or (and .IsUserProfile .IsOwner) (and (not .IsUserProfile) (or .IsOwner .IsMember))}}
<div class="gr-center">
<div class="new-post">
<button>New Post</button>
</div>
</div>
{{end}}
<div class="share-post"></div>
{{end}}
{{if and (not .LoggedInUser) (not .IsPermalink) (not .IsReplyPermalink)}}
<div class="gr-center">
<div class="new-post">
<a href="/login">
<button>Log in to Post</button>
</a>
</div>
</div>
{{end}}
{{if and .LoggedInUser (not .IsMember) (not .IsOwner) (not .IsUserProfile) (not .IsPermalink)}}
<div class="join-room"
data-type="{{if .IsUserProfile}}user{{else}}room{{end}}"
data-alias={{.Room.Alias}}
data-name={{.Room.Path}}
data-id="{{.Room.ID}}">
<button class="light">{{if .IsMember}}Leave{{else}}Join{{end}} {{.Room.Path}}</button>
</div>
{{end}}
{{if and .LoggedInUser (not .IsPermalink) (.IsPage)}}
{{if or .IsOwner .IsMember}}
<div class="gr-center">
<div class="edit-page">
<button>Edit Page</button>
</div>
</div>
{{end}}
{{end}}
</div>
{{if or .IsAdmin .IsOwner}}
<div class="room-settings flex">
</div>
{{end}}
{{$hms := .HomeServerURL}}
{{if not .IsPage}}
{{if .IsPermalink}}
{{if .PermalinkedPost}}
{{$rpth := .Room.Path}}
{{$rid := .Room.ID}}
{{$liu := .LoggedInUser}}
<div class="permalinked-post">
{{template "timeline-event" Map "Event" .PermalinkedPost "HomeServerURL" $hms "PermalinkedPost" true "RoomPath" $rpth "LoggedInUser" $liu "RoomID" $rid "Focus" true}}
<div class="flex pa3 brd-btm" style="min-height: 60px;">
<div class="sort-replies">
<select class="small">
<option>Oldest</option>
<option {{if eq .Sort "recent"}}selected{{end}}>Recent</option>
<option {{if eq .Sort "replies"}}selected{{end}}>Most Replies</option>
</select>
</div>
<div class="flex-one"></div>
{{if .LoggedInUser}}
{{if or .IsMember .IsOwner}}
<div class="new-post">
<button>Reply</button>
</div>
{{end}}
{{else}}
<div class="new-post">
<a href="/">
<button>Log in to Reply</button>
</a>
</div>
{{end}}
<div class="share-post"></div>
</div>
{{end}}
</div>
{{end}}
{{if and .LoggedInUser }}
<div class="create-post"></div>
{{end}}
<div class="added-posts"></div>
<div class="posts">
{{if .IsReplyPermalink}}
<div class="brd-btm pa3 small flex">
<div class="">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M9.78 12.78a.75.75 0 01-1.06 0L4.47 8.53a.75.75 0 010-1.06l4.25-4.25a.75.75 0 011.06 1.06L6.06 8l3.72 3.72a.75.75 0 010 1.06z"></path></svg>
</div>
<div class="gr-center ml2">
<a href="/{{.RootEvent}}">
<span class="primary hov-un">View All Replies<span>
</a>
</div>
</div>
{{end}}
{{$rpth := .Room.Path}}
{{$rid := .Room.ID}}
{{$liu := .LoggedInUser}}
{{if .IsPermalink}}
<div class="replies gr-default mb5">
<div class="lds-ring gr-center pv4"><div></div><div></div><div></div><div></div></div>
</div>
{{else}}
{{$rpth := .Room.Path}}
{{$rid := .Room.ID}}
{{$liu := .LoggedInUser}}
{{range $idx, $post := .Posts}}
{{if and (not $post.Redacted)}}
{{template "timeline-event" Map "Event" $post "HomeServerURL" $hms "RoomPath" $rpth "LoggedInUser" $liu "RoomID" $rid}}
{{end}}
{{end}}
{{end}}
</div>
{{else}}
<div class="flex flex-column ma3">
<div class="">
<span class="f4 bold">{{Title .Room.Name}}</span>
</div>
<div class="mt4 lh-copy page">
{{$rid := .Room.ID}}
{{$rpth := .Room.Path}}
{{$liu := .LoggedInUser}}
{{range $idx, $post := .Posts}}
{{if and (eq $post.Type "com.hummingbard.post") (eq $idx 0) (not $post.Redacted)}}
<div class="flex flex-column">
<div class="">
{{$con := index $post.Content "bodyHTML"}}
{{$con}}
</div>
<div class="mt5">
<span class="small">Last Edited: {{$post.Date}}</span>
</div>
</div>
{{end}}
{{end}}
</div>
<div class="">
</div>
</div>
{{end}}
{{if and (not .IsPermalink) (not .IsPage)}}
<div class="more-posts flex flex-column ">
<div class="more-post-items">
</div>
<div class="load-more">
</div>
</div>
{{end}}
<div class="timeline">
</div>
<script nonce={{.Nonce}}>
window.timeline = {
room_id: {{.Room.ID}},
permalink: {{.IsPermalink}},
replyPermalink: {{.IsReplyPermalink}},
{{if .IsPermalink}}
event_id: {{.Room.EventID}},
thread_in_room_id: {{.Room.ThreadInRoomID}},
permalinkedPost: {{.PermalinkedPost}},
replies: {{.Posts}},
root_event: {{.RootEvent}},
{{end}}
alias: {{.Room.Alias}},
profile: {{.IsUserProfile}},
initialPosts: JSON.parse({{.InitialPosts}}),
end: {{.LastEvent}},
owner: {{.IsOwner}},
admin: {{.IsAdmin}},
member: {{.IsMember}},
state: {{.Room.State}},
room_type: {{.Room.Type}},
room_path: {{.Room.Path}},
children: {{.Room.Children}},
pages: {{.Room.Pages}},
}
let homeserverURL = {{.HomeServerURL}}
</script>
</div>
{{if not .IsUserProfile}}
{{template "timeline-sidebar" .}}
{{end}}
</div>
{{template "state" .}}
<script defer nonce={{.Nonce}} src="{{ InsertJS "timeline" }}"></script>
</div>
</div>
</body>
</html>
{{end}}

View File

@ -0,0 +1,51 @@
{{define "user-not-registered"}}
<html>
<head>
<title>Hummingbard</title>
{{template "common-head" .}}
</head>
<body>
<div class="root">
{{template "header" Map "Page" . "Name" "user-not-registered"}}
<div class="content-c mt4">
<div class="con gr-center">
<div class="flex flex-column mb4 flex brd pa3 lh-copy tc" style="max-width: 350px;">
{{if .User.AvatarURL}}
{{if gt (len .User.AvatarURL) 0}}
<div class="avatar mb3">
<img src={{.User.AvatarURL}} />
</div>
{{end}}
{{else}}
<div class="avatar mb3">
<svg class="gr-center" height="80" width="80">
<circle cx="40" cy="40" r="40" stroke-width="0" fill="black" />
</svg>
</div>
{{end}}
<div class="flex flex-column">
{{if .User.DisplayName}}
{{if gt (len .User.DisplayName) 0}}
<span class="f4 bold">{{.User.DisplayName}}</span>
<span class="fs-09 0-90">{{.User.UserID}}</span>
{{end}}
{{else}}
<span class="f5 bold">{{.User.UserID}}</span>
{{end}}
</div>
<div class="mt4">
This user exists on the <span class="primary">{{.User.ServerName}}</span> server, but does not have a hummingbard account.
</div>
</div>
</div>
</div>
{{template "state" .}}
{{template "footer" .}}
<script defer nonce={{.Nonce}} src="{{ InsertJS "timeline" }}"></script>
</div>
</body>
</html>
{{end}}

View File

@ -0,0 +1,28 @@
{{define "welcome"}}
<html>
<head>
<title>Welcome - Hummingbard</title>
{{template "common-head" .}}
</head>
<body>
<div class="root">
{{template "header" Map "Page" . "Name" "index-user"}}
<div class="center-content welcome">
<div class="lds-ring gr-center"><div></div><div></div><div></div><div></div></div>
</div>
<script nonce={{.Nonce}}>
let homeserverURL = {{.HomeServerURL}}
window.state = {
rooms: {{.Rooms}},
}
</script>
{{template "state" .}}
{{template "footer" .}}
<script defer nonce={{.Nonce}} src="{{ InsertJS "welcome" }}"></script>
</div>
</body>
</html>
{{end}}

42
ui/css/dark.css Normal file
View File

@ -0,0 +1,42 @@
@media (prefers-color-scheme: dark) {
:root {
--primary: #ff3030;
--secondary: #ff3030;
--light-border-color: #F3F3F3;
--dark-border-color: #e2e4e7;
--primary-darkestest: #0a0a0a;
--primary-darkest: white;
--primary-dark: #9e9e9e;
--primary-darkest-gray: #595959;
--primary-darker-gray: #777;
--primary-dark-gray: #999;
--primary-gray: #4b4b4b;
--primary-grayish: #4b4b4b;
--primary-kinda-gray: #cccccc94;
--primary-light-gray: #4b4b4b;
--primary-lightest-gray: #262626;
--primary-lightestest-gray: #262626;
--primary-text: #ebebeb;
--primary-link: #ebebeb;
--light-text: #777;
--yellow: #4b4b4b;
--green: #32bf32;
--yellow-alt: #FFCC00;
--background: #151515;
--pi-bg: #333;
--pi-alt-bg: #292929;
--text: white;
--alt-text: black;
--red: red;
--blue: blue;
--light-red: #f02c2c;
--m-bg: #424242;
--d-b: #9e9e9e;
--d-b-h: #696969;
--d-b-t: black;
--white: #292929;
--mask: #0d0d0d;
--img-filter: grayscale(40%);
}
}

1285
ui/css/index.css Normal file

File diff suppressed because it is too large Load Diff

266
ui/css/microtip.css Normal file
View File

@ -0,0 +1,266 @@
/* -------------------------------------------------------------------
Microtip
Modern, lightweight css-only tooltips
Just 1kb minified and gzipped
@author Ghosh
@package Microtip
----------------------------------------------------------------------
1. Base Styles
2. Direction Modifiers
3. Position Modifiers
--------------------------------------------------------------------*/
/* ------------------------------------------------
[1] Base Styles
-------------------------------------------------*/
[aria-label][role~="tooltip"] {
position: relative;
}
[aria-label][role~="tooltip"]::before,
[aria-label][role~="tooltip"]::after {
transform: translate3d(0, 0, 0);
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
will-change: transform;
opacity: 0;
pointer-events: none;
transition: all var(--microtip-transition-duration, .18s) var(--microtip-transition-easing, ease-in-out) var(--microtip-transition-delay, 0s);
position: absolute;
box-sizing: border-box;
z-index: 10;
transform-origin: top;
}
[aria-label][role~="tooltip"]::before {
background-size: 100% auto !important;
content: "";
}
[aria-label][role~="tooltip"]::after {
background: rgba(17, 17, 17, .9);
border-radius: 4px;
color: #ffffff;
content: attr(aria-label);
font-size: var(--microtip-font-size, 13px);
font-weight: var(--microtip-font-weight, normal);
text-transform: var(--microtip-text-transform, none);
padding: .5em 1em;
white-space: nowrap;
box-sizing: content-box;
}
[aria-label][role~="tooltip"]:hover::before,
[aria-label][role~="tooltip"]:hover::after,
[aria-label][role~="tooltip"]:focus::before,
[aria-label][role~="tooltip"]:focus::after {
opacity: 1;
pointer-events: auto;
}
/* ------------------------------------------------
[2] Position Modifiers
-------------------------------------------------*/
[role~="tooltip"][data-microtip-position|="top"]::before {
background: url("data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2236px%22%20height%3D%2212px%22%3E%3Cpath%20fill%3D%22rgba%2817,%2017,%2017,%200.9%29%22%20transform%3D%22rotate%280%29%22%20d%3D%22M2.658,0.000%20C-13.615,0.000%2050.938,0.000%2034.662,0.000%20C28.662,0.000%2023.035,12.002%2018.660,12.002%20C14.285,12.002%208.594,0.000%202.658,0.000%20Z%22/%3E%3C/svg%3E") no-repeat;
height: 6px;
width: 18px;
margin-bottom: 5px;
}
[role~="tooltip"][data-microtip-position|="top"]::after {
margin-bottom: 11px;
}
[role~="tooltip"][data-microtip-position|="top"]::before {
transform: translate3d(-50%, 0, 0);
bottom: 100%;
left: 50%;
}
[role~="tooltip"][data-microtip-position|="top"]:hover::before {
transform: translate3d(-50%, -5px, 0);
}
[role~="tooltip"][data-microtip-position|="top"]::after {
transform: translate3d(-50%, 0, 0);
bottom: 100%;
left: 50%;
}
[role~="tooltip"][data-microtip-position="top"]:hover::after {
transform: translate3d(-50%, -5px, 0);
}
/* ------------------------------------------------
[2.1] Top Left
-------------------------------------------------*/
[role~="tooltip"][data-microtip-position="top-left"]::after {
transform: translate3d(calc(-100% + 16px), 0, 0);
bottom: 100%;
}
[role~="tooltip"][data-microtip-position="top-left"]:hover::after {
transform: translate3d(calc(-100% + 16px), -5px, 0);
}
/* ------------------------------------------------
[2.2] Top Right
-------------------------------------------------*/
[role~="tooltip"][data-microtip-position="top-right"]::after {
transform: translate3d(calc(0% + -16px), 0, 0);
bottom: 100%;
}
[role~="tooltip"][data-microtip-position="top-right"]:hover::after {
transform: translate3d(calc(0% + -16px), -5px, 0);
}
/* ------------------------------------------------
[2.3] Bottom
-------------------------------------------------*/
[role~="tooltip"][data-microtip-position|="bottom"]::before {
background: url("data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2236px%22%20height%3D%2212px%22%3E%3Cpath%20fill%3D%22rgba%2817,%2017,%2017,%200.9%29%22%20transform%3D%22rotate%28180%2018%206%29%22%20d%3D%22M2.658,0.000%20C-13.615,0.000%2050.938,0.000%2034.662,0.000%20C28.662,0.000%2023.035,12.002%2018.660,12.002%20C14.285,12.002%208.594,0.000%202.658,0.000%20Z%22/%3E%3C/svg%3E") no-repeat;
height: 6px;
width: 18px;
margin-top: 5px;
margin-bottom: 0;
}
[role~="tooltip"][data-microtip-position|="bottom"]::after {
margin-top: 11px;
}
[role~="tooltip"][data-microtip-position|="bottom"]::before {
transform: translate3d(-50%, -10px, 0);
bottom: auto;
left: 50%;
top: 100%;
}
[role~="tooltip"][data-microtip-position|="bottom"]:hover::before {
transform: translate3d(-50%, 0, 0);
}
[role~="tooltip"][data-microtip-position|="bottom"]::after {
transform: translate3d(-50%, -10px, 0);
top: 100%;
left: 50%;
}
[role~="tooltip"][data-microtip-position="bottom"]:hover::after {
transform: translate3d(-50%, 0, 0);
}
/* ------------------------------------------------
[2.4] Bottom Left
-------------------------------------------------*/
[role~="tooltip"][data-microtip-position="bottom-left"]::after {
transform: translate3d(calc(-100% + 16px), -10px, 0);
top: 100%;
}
[role~="tooltip"][data-microtip-position="bottom-left"]:hover::after {
transform: translate3d(calc(-100% + 16px), 0, 0);
}
/* ------------------------------------------------
[2.5] Bottom Right
-------------------------------------------------*/
[role~="tooltip"][data-microtip-position="bottom-right"]::after {
transform: translate3d(calc(0% + -16px), -10px, 0);
top: 100%;
}
[role~="tooltip"][data-microtip-position="bottom-right"]:hover::after {
transform: translate3d(calc(0% + -16px), 0, 0);
}
/* ------------------------------------------------
[2.6] Left
-------------------------------------------------*/
[role~="tooltip"][data-microtip-position="left"]::before,
[role~="tooltip"][data-microtip-position="left"]::after {
bottom: auto;
left: auto;
right: 100%;
top: 50%;
transform: translate3d(10px, -50%, 0);
}
[role~="tooltip"][data-microtip-position="left"]::before {
background: url("data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2212px%22%20height%3D%2236px%22%3E%3Cpath%20fill%3D%22rgba%2817,%2017,%2017,%200.9%29%22%20transform%3D%22rotate%28-90%2018%2018%29%22%20d%3D%22M2.658,0.000%20C-13.615,0.000%2050.938,0.000%2034.662,0.000%20C28.662,0.000%2023.035,12.002%2018.660,12.002%20C14.285,12.002%208.594,0.000%202.658,0.000%20Z%22/%3E%3C/svg%3E") no-repeat;
height: 18px;
width: 6px;
margin-right: 5px;
margin-bottom: 0;
}
[role~="tooltip"][data-microtip-position="left"]::after {
margin-right: 11px;
}
[role~="tooltip"][data-microtip-position="left"]:hover::before,
[role~="tooltip"][data-microtip-position="left"]:hover::after {
transform: translate3d(0, -50%, 0);
}
/* ------------------------------------------------
[2.7] Right
-------------------------------------------------*/
[role~="tooltip"][data-microtip-position="right"]::before,
[role~="tooltip"][data-microtip-position="right"]::after {
bottom: auto;
left: 100%;
top: 50%;
transform: translate3d(-10px, -50%, 0);
}
[role~="tooltip"][data-microtip-position="right"]::before {
background: url("data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2212px%22%20height%3D%2236px%22%3E%3Cpath%20fill%3D%22rgba%2817,%2017,%2017,%200.9%29%22%20transform%3D%22rotate%2890%206%206%29%22%20d%3D%22M2.658,0.000%20C-13.615,0.000%2050.938,0.000%2034.662,0.000%20C28.662,0.000%2023.035,12.002%2018.660,12.002%20C14.285,12.002%208.594,0.000%202.658,0.000%20Z%22/%3E%3C/svg%3E") no-repeat;
height: 18px;
width: 6px;
margin-bottom: 0;
margin-left: 5px;
}
[role~="tooltip"][data-microtip-position="right"]::after {
margin-left: 11px;
}
[role~="tooltip"][data-microtip-position="right"]:hover::before,
[role~="tooltip"][data-microtip-position="right"]:hover::after {
transform: translate3d(0, -50%, 0);
}
/* ------------------------------------------------
[3] Size
-------------------------------------------------*/
[role~="tooltip"][data-microtip-size="small"]::after {
white-space: initial;
width: 80px;
}
[role~="tooltip"][data-microtip-size="medium"]::after {
white-space: initial;
width: 150px;
}
[role~="tooltip"][data-microtip-size="large"]::after {
white-space: initial;
width: 260px;
}

3
ui/css/tachyons.min.css vendored Normal file

File diff suppressed because one or more lines are too long

92
ui/css/tippy.css Normal file
View File

@ -0,0 +1,92 @@
.tippy-box[data-animation="fade"][data-state="hidden"] {
opacity: 0;
}
[data-tippy-root] {
max-width: calc(100vw - 10px);
}
.tippy-box {
position: relative;
background-color: var(--m-bg);
color: var(--primary-text);
font-size: 14px;
line-height: 1.4;
outline: 0;
transition-property: transform, visibility, opacity;
box-shadow: 0 20px 30px rgba(0,0,0,.05);
}
.tippy-box[data-theme~='menu'] {
box-shadow: 0 10px 70px rgba(0,0,0,.14);
}
.tippy-box[data-theme~='pop'] {
border-radius: 13px;
box-shadow: 0 0px 50px rgba(0,0,0,.05);
width: 250px;
}
.tippy-box[data-theme~='popup'] {
border-radius: 13px;
box-shadow: 0 0px 50px rgba(0,0,0,.08);
z-index: 10000;
}
.tippy-box[data-placement^="top"] > .tippy-arrow {
bottom: 0;
}
.tippy-box[data-placement^="top"] > .tippy-arrow:before {
bottom: -7px;
left: 0;
border-width: 8px 8px 0;
border-top-color: initial;
transform-origin: center top;
}
.tippy-box[data-placement^="bottom"] > .tippy-arrow {
top: 0;
}
.tippy-box[data-placement^="bottom"] > .tippy-arrow:before {
top: -7px;
left: 0;
border-width: 0 8px 8px;
border-bottom-color: initial;
transform-origin: center bottom;
}
.tippy-box[data-placement^="left"] > .tippy-arrow {
right: 0;
}
.tippy-box[data-placement^="left"] > .tippy-arrow:before {
border-width: 8px 0 8px 8px;
border-left-color: initial;
right: -7px;
transform-origin: center left;
}
.tippy-box[data-placement^="right"] > .tippy-arrow {
left: 0;
}
.tippy-box[data-placement^="right"] > .tippy-arrow:before {
left: -7px;
border-width: 8px 8px 8px 0;
border-right-color: initial;
transform-origin: center right;
}
.tippy-box[data-inertia][data-state="visible"] {
transition-timing-function: cubic-bezier(0.54, 1.5, 0.38, 1.11);
}
.tippy-arrow {
width: 16px;
height: 16px;
color: var(--m-bg);
}
.tippy-arrow:before {
content: "";
position: absolute;
border-color: transparent;
border-style: solid;
}
.tippy-content {
position: relative;
padding: 0;
z-index: 1;
}
.tippy-box[data-animation=shift-away][data-state=hidden]{opacity:0}.tippy-box[data-animation=shift-away][data-state=hidden][data-placement^=top]{transform:translateY(10px)}.tippy-box[data-animation=shift-away][data-state=hidden][data-placement^=bottom]{transform:translateY(-10px)}.tippy-box[data-animation=shift-away][data-state=hidden][data-placement^=left]{transform:translateX(10px)}.tippy-box[data-animation=shift-away][data-state=hidden][data-placement^=right]{transform:translateX(-10px)}

7
ui/js/babel.config.json Normal file
View File

@ -0,0 +1,7 @@
{
"presets": ["@babel/preset-env"],
"plugins": [
"@babel/plugin-proposal-nullish-coalescing-operator",
"@babel/plugin-proposal-optional-chaining"
]
}

6510
ui/js/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

Some files were not shown because too many files have changed in this diff Show More