Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Commit

Permalink
Merge pull request #103 from matrix-org/t3chguy/search
Browse files Browse the repository at this point in the history
  • Loading branch information
t3chguy authored Jun 9, 2021
2 parents 32cddd1 + 4af79db commit c778780
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ WORKDIR /opt/matrix-static/
COPY --from=0 /src/matrix-static /bin/
COPY ./assets/ ./assets

EXPOSE 8000

ENTRYPOINT matrix-static
3 changes: 3 additions & 0 deletions assets/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,7 @@ span.redacted {
}
tr.evHighlight {
background-color: yellow;
}
form {
float: right;
}
40 changes: 34 additions & 6 deletions cmd/matrix-static/matrix-static.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ import (
"github.com/t3chguy/go-gin-prometheus"
"image/png"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"sync"
"time"
Expand All @@ -61,6 +63,8 @@ type configVars struct {
LogDir string
}

var roomAliasOrIdRegex = regexp.MustCompile(`(?:#\/)?([!#]\S+?:[a-z0-9.]+(?::\d+)?)\??`)

func main() {
// startup checks
if stat, err := os.Stat("./assets"); os.IsNotExist(err) || !stat.IsDir() {
Expand Down Expand Up @@ -166,12 +170,36 @@ func main() {
publicRouter.StaticFile("/robots.txt", "./assets/robots.txt")

publicRouter.GET("/", func(c *gin.Context) {
page := utils.StrToIntDefault(c.DefaultQuery("page", "1"), 1)
templates.WritePageTemplate(c.Writer, &templates.RoomsPage{
Rooms: worldReadableRooms.GetPage(page, PublicRoomsPageSize),
PageSize: PublicRoomsPageSize,
Page: page,
})
query := c.Query("query")

if query == "" {
page := utils.StrToIntDefault(c.DefaultQuery("page", "1"), 1)
templates.WritePageTemplate(c.Writer, &templates.RoomsPage{
Rooms: worldReadableRooms.GetPage(page, PublicRoomsPageSize),
PageSize: PublicRoomsPageSize,
Page: page,
})
} else {
matches := roomAliasOrIdRegex.FindStringSubmatch(query)
if len(matches) == 2 {
roomIdentifier := matches[1]
escaped := url.QueryEscape(roomIdentifier)

if roomIdentifier[0] == '!' {
c.Redirect(http.StatusTemporaryRedirect, "/room/"+escaped+"/")
} else if roomIdentifier[0] == '#' {
c.Redirect(http.StatusTemporaryRedirect, "/alias/"+escaped+"/")
}
} else {
// TODO pagination
templates.WritePageTemplate(c.Writer, &templates.RoomsPage{
Rooms: worldReadableRooms.GetFilteredPage(1, PublicRoomsPageSize, query),
PageSize: PublicRoomsPageSize,
Page: 1,
Query: query,
})
}
}
})

roomAliasCache := persistence.NewInMemoryStore(time.Hour)
Expand Down
30 changes: 24 additions & 6 deletions mxclient/public-rooms.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package mxclient
import (
"github.com/matrix-org/gomatrix"
"github.com/matrix-org/matrix-static/utils"
"strings"
"sync"
)

Expand Down Expand Up @@ -71,12 +72,29 @@ func (r *WorldReadableRooms) Update() error {
return nil
}

// For future when we support filtering the public room directory (LOCALLY)
//func (r *WorldReadableRooms) GetFilteredPage(page, pageSize int, query string) []gomatrix.PublicRoom {
// r.roomsMutex.RLock()
// defer r.roomsMutex.RUnlock()
// return nil
//}
// GetFilteredPage returns a filtered & paginated slice of the WorldReadableRooms Collection
func (r *WorldReadableRooms) GetFilteredPage(page, pageSize int, query string) []gomatrix.PublicRoom {
r.roomsMutex.RLock()
defer r.roomsMutex.RUnlock()

lowerQuery := strings.ToLower(query)

filteredRooms := make([]gomatrix.PublicRoom, 0, pageSize)
for _, room := range r.rooms {
if len(filteredRooms) >= pageSize {
break
}

if (strings.HasPrefix(lowerQuery, "#") && strings.Contains(strings.ToLower(room.CanonicalAlias), lowerQuery)) ||
strings.Contains(strings.ToLower(room.Name), lowerQuery) ||
strings.Contains(strings.ToLower(room.Topic), lowerQuery) {
filteredRooms = append(filteredRooms, room)
}
}

start, end := utils.CalcPaginationStartEnd(page, pageSize, len(filteredRooms))
return filteredRooms[start:end]
}

// GetPage returns a paginated slice of the WorldReadableRooms Collection
func (r *WorldReadableRooms) GetPage(page, pageSize int) []gomatrix.PublicRoom {
Expand Down
7 changes: 6 additions & 1 deletion templates/rooms.qtpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Rooms []gomatrix.PublicRoom
PageSize int
Page int
Query string
}
%}

Expand All @@ -23,6 +24,10 @@

{% func (p *RoomsPage) Header() %}
<h1>matrix-static</h1>
<form method="GET">
<input name="query" placeholder="Search rooms" type="text" value="{%s p.Query %}" />
<input type="submit" value="Go!" />
</form>
{% endfunc %}

{% func (p *RoomsPage) printRoomRow(Room gomatrix.PublicRoom) %}
Expand Down Expand Up @@ -62,7 +67,7 @@
<tr>
<th>Logo</th>
<th>Name & Alias</th>
<th>#Members</th>
<th>Members</th>
<th>Topic</th>
</tr>
</thead>
Expand Down

0 comments on commit c778780

Please sign in to comment.