Skip to content

Commit

Permalink
Remote data: Add activities section for matrix, mail and github
Browse files Browse the repository at this point in the history
  • Loading branch information
Lino committed Feb 13, 2024
1 parent 0566d37 commit 8bde79a
Show file tree
Hide file tree
Showing 12 changed files with 220 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/deploy-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
- name: Build
env:
HUGO_ENV: production
HUGO_MATRIX_ACCESS_TOKEN: ${{ secrets.HUGO_MATRIX_ACCESS_TOKEN }}
HUGO_MATRIX_HOME_SERVER: ${{ secrets.HUGO_MATRIX_HOME_SERVER }}
URL_DEVELOPMENT: ${{ vars.URL_DEVELOPMENT }}
run: hugo --minify -b "$URL_DEVELOPMENT/$GITHUB_REF_NAME" --destination "branch/$GITHUB_REF_NAME"

Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/deploy-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
- name: Build
env:
HUGO_ENV: production
HUGO_MATRIX_ACCESS_TOKEN: ${{ secrets.HUGO_MATRIX_ACCESS_TOKEN }}
HUGO_MATRIX_HOME_SERVER: ${{ secrets.HUGO_MATRIX_HOME_SERVER }}
URL_PRODUCTION: ${{ vars.URL_PRODUCTION }}
run: hugo --minify -b "$URL_PRODUCTION"

Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ The website is built using the static website generator Hugo.
hugo server -w
```

Note: You can change the base address `-b` and the bind address `--bind` if your run it on a remote machine.

You can change the base address `-b` and the bind address `--bind` if your run it on a remote machine.
```console
hugo server -w -b http://localhost:1313/ --bind 0.0.0.0
```
You can set the environment variables `HUGO_MATRIX_ACCESS_TOKEN` and `HUGO_MATRIX_HOME_SERVER` to get real matrix-activity informations. Else some dummy data is used.
```console
HUGO_MATRIX_ACCESS_TOKEN="secret" HUGO_MATRIX_HOME_SERVER="htps://matrix.org" hugo server -w
```

## Build for production

Expand Down Expand Up @@ -50,5 +53,8 @@ The actions are defined in `.github/workflows` and could be configured with [Git

To display up to date information, we fetch and generate some data during the build process. These informations are available as shortcodes for the content and as partials for the templates. They are used at various places of the website.

- Nodecount (ff-community api)
- Lastchange (ff-community api)
- map-nodecount (ff-community api)
- community-lastchange (ff-community api)
- mail-subjects (activities section)
- matrix-activity (activities section)
- github-activity (activities section)
2 changes: 2 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ defaultContentLanguageInSubdir = true
SectionPagesMenu = "main"
enableRobotsTXT = true

timeout = "100s"

disableKinds = ['taxonomy', 'term']

[languages]
Expand Down
12 changes: 12 additions & 0 deletions content/_index.de.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,15 @@ Die Grundlage von Freifunk bildet ein sogenanntes Mesh-Netzwerk. Alle WLAN-Route
## Wie kann ich mitmachen?

Mach mit und werde FreifunkerIn! Komm zu den Treffen oder geh direkt zum HowTo und mach deinen Router fit für Freifunk! Gibt es noch keine weiteren Freifunker_innen in deiner Gegend? Dann bist du die oder der Erste und andere werden dir bald folgen!

## Aktivitäten

{{< matrix-activity >}}

---

{{< github-activity >}}

---

{{< mail-subjects >}}
12 changes: 12 additions & 0 deletions content/_index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,15 @@ Freifunk's foundation is a so called mesh network. All routers within the Freifu
## How can I contribute?

Join us and become a Freifunk activist! Come to one of the meetings or go directly to the HowTo and get your router ready for Freifunk! There are still no other network activists in your area? Well, then you're the 1st one and further will soon follow!

## Activities

{{< matrix-activity >}}

---

{{< github-activity >}}

---

{{< mail-subjects >}}
34 changes: 34 additions & 0 deletions layouts/partials/github-activity.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{{/* Return map with the github activity data:
- repo_count
- top_repos
- commit_count */}}

{{/* Number of repos in the organization.
Private and archived repos are included. */}}
{{ $repo_list_json := getJSON "https://api.github.com/orgs/freifunk-berlin/repos?per_page=100&sort=pushed"}}
{{ $repo_count := len $repo_list_json}}

{{/* Top three active repos.
Based on latest push. */}}
{{ $top_repos := slice }}
{{ range $index, $element := $repo_list_json }}
{{ if eq $index 3 }}
{{ break }}
{{ end }}
{{ $top_repos = $top_repos | append $element.name }}
{{ end }}

{{/* Number of commits of the last seven days in the most active repos.
Checking all repos would take to long. This only represents a a part of the total commits. A plus gets always appended to the counter. */}}
{{ $past_seven_days_date := now.AddDate 0 0 -7 }}
{{ $past_seven_days := $past_seven_days_date.Format "2006-01-02T15:04:05Z" }}
{{ $commit_count := 0 }}
{{ range $element := $top_repos }}
{{ $commit_url := printf "https://api.github.com/repos/freifunk-berlin/%s/commits?per_page=100&since=%s" $element $past_seven_days }}
{{ $commit_json := getJSON $commit_url }}
{{ $commit_count = add $commit_count (len $commit_json) }}
{{ end }}
{{ $commit_count = printf "%d+" $commit_count }}

{{ $github_activity := dict "repo_count" $repo_count "top_repos" $top_repos "commit_count" $commit_count }}
{{ return $github_activity}}
46 changes: 46 additions & 0 deletions layouts/partials/mail-subjects.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{{/* Return the five latest subjects from the mailing list.
Only checks the current and previous month. Duplicates are removed, when they follow each other directly. */}}

{{ $current_month := now.Format "2006-January" }}
{{ $current_month_url := printf "https://lists.berlin.freifunk.net/pipermail/berlin/%s/subject.html" $current_month }}
{{ $current_month_response := resources.GetRemote $current_month_url }}
{{ $current_month_html := $current_month_response.Content }}
{{ $current_month_subjects := findRE "\\[Berlin-wireless\\] (.*?)\n" $current_month_html }}
{{ $current_month_reversed_subjects := slice }}
{{ $array_length := len $current_month_subjects }}
{{- range $index, $element := $current_month_subjects -}}
{{ $current_month_reversed_subjects = $current_month_reversed_subjects | append (index $current_month_subjects (sub $array_length (add $index 1))) }}
{{- end }}

{{ $previous_month_date := now.AddDate 0 -1 0 }}
{{ $previous_month := $previous_month_date.Format "2006-January" }}
{{ $previous_month_url := printf "https://lists.berlin.freifunk.net/pipermail/berlin/%s/subject.html" $previous_month }}
{{ $previous_month_response := resources.GetRemote $previous_month_url }}
{{ $previous_month_html := $previous_month_response.Content }}
{{ $previous_month_subjects := findRE "\\[Berlin-wireless\\] (.*?)\n" $previous_month_html }}
{{ $previous_month_reversed_subjects := slice }}
{{ $array_length := len $previous_month_subjects }}
{{- range $index, $element := $previous_month_subjects -}}
{{ $previous_month_reversed_subjects = $previous_month_reversed_subjects | append (index $previous_month_subjects (sub $array_length (add $index 1))) }}
{{- end }}

{{ $subjects := $current_month_reversed_subjects | append $previous_month_reversed_subjects }}

{{ $previous_subject := "" }}
{{ $current_subject := "" }}
{{ $subject_count := 0 }}
{{ $latest_subjects := slice }}

{{ range $element := $subjects }}
{{ if eq $subject_count 5 }}
{{ break }}
{{ end }}
{{ $current_subject = trim (strings.TrimPrefix "[Berlin-wireless]" $element) " " }}
{{ if not (eq $previous_subject $current_subject) }}
{{ $subject_count = add $subject_count 1 }}
{{ $latest_subjects = $latest_subjects | append $current_subject }}
{{ end }}
{{ $previous_subject = $current_subject }}
{{ end }}

{{ return $latest_subjects }}
57 changes: 57 additions & 0 deletions layouts/partials/matrix-activity.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{{/* Return map with the matrix activity data:
- message_count
- room_count
- member_count */}}

{{ $ACCESS_TOKEN := getenv "HUGO_MATRIX_ACCESS_TOKEN" }}
{{ $HOME_SERVER := getenv "HUGO_MATRIX_HOME_SERVER" }}

{{/* Defaults for development without matrix access token. */}}
{{ $matrix_activity := dict "message_count" 161 "room_count" 42 "member_count" 999 }}

{{ if $ACCESS_TOKEN }}

{{/* Number of members in main chat. */}}
{{ $room_url := printf "%s/_matrix/client/v1/rooms/!zTqqomsFqlyQjqrZpm:matrix.org/hierarchy?access_token=%s" $HOME_SERVER $ACCESS_TOKEN }}
{{ $room_json := getJSON $room_url }}
{{ $room := index $room_json.rooms 0 }}


{{/* Number of rooms in the space.
Only checks the first page. If more rooms exist, a plus gets appended to the counter.
Max rooms per page depends on server configuration. */}}
{{ $space_url := printf "%s/_matrix/client/v1/rooms/!EzZtBKEuKZqxrYSugs:digitale-gesellschaft.ch/hierarchy?access_token=%s" $HOME_SERVER $ACCESS_TOKEN }}
{{ $space_json := getJSON $space_url }}
{{ $room_count := len $space_json.rooms }}
{{ if isset $space_json "next_batch" }}
{{ $room_count = printf "%d+" $room_count}}
{{ end }}


{{/* Number of messages in the past seven days in main chat.
Only checks the first page. If the oldest message on that page is within the seven days, a plus gets appended to the counter.
Max messages per page depends on server configuration. */}}
{{ $messages_url := printf "%s/_matrix/client/v3/rooms/!zTqqomsFqlyQjqrZpm:matrix.org/messages?access_token=%s&limit=100&dir=b" $HOME_SERVER $ACCESS_TOKEN }}
{{ $messages_json := getJSON $messages_url }}
{{ $oneWeekAgoUnixSeconds := now.Unix | sub 604800 }}
{{ $oneWeekAgoUnixMilliseconds := $oneWeekAgoUnixSeconds | mul -1000 }}
{{ $message_array_len := len $messages_json.chunk }}
{{ $message_last_index := sub $message_array_len 1 }}
{{ $message_count := 0 }}
{{ $oneWeekAgoUnixMilliseconds}}
{{ range $index, $element := $messages_json.chunk }}
{{ if gt $element.origin_server_ts $oneWeekAgoUnixMilliseconds }}
{{ $message_count = add $message_count 1 }}
{{ if eq $index $message_last_index }}
{{ $message_count = printf "%d+" $message_count}}
{{ end }}
{{ else }}
{{ break }}
{{ end }}
{{ end }}

{{ $matrix_activity = dict "message_count" $message_count "room_count" $room_count "member_count" $room.num_joined_members }}

{{ end }}

{{ return $matrix_activity}}
19 changes: 19 additions & 0 deletions layouts/shortcodes/github-activity.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{{ $github_activity := partial "github-activity.html" . }}

{{ if eq .Site.Language.Lang "de" }}
Zur Versionsverwaltunng der Software nutzen wir <a href="https://github.com/freifunk-berlin/">Github</a>.
Dort gab es in den letzten sieben Tagen <b>{{ $github_activity.commit_count }} (commits)</b> Änderungen am Code.
Auf der Platform werden insgesamt <b>{{ $github_activity.repo_count }} (repositories)</b> Projekte verwaltet.
Die aktivsten sind derzeit:
{{ else if eq .Site.Language.Lang "en" }}
We use <a href="https://github.com/freifunk-berlin/">Github</a> for version control of the software.
There have been <b>{{ $github_activity.commit_count }} (commits)</b> changes to the code in the last seven days.
A total of <b>{{ $github_activity.repo_count }} (repositories)</b> projects are managed on the platform.
The most active are currently:
{{ end }}

<ul style="text-align:left;margin-left: auto;margin-right: auto;width: fit-content;">
{{ range $element := $github_activity.top_repos }}
<li><a href="https://github.com/freifunk-berlin/{{ $element }}">{{ $element }}</a></li>
{{ end }}
</ul>
13 changes: 13 additions & 0 deletions layouts/shortcodes/mail-subjects.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{ $mail_subjects := partial "mail-subjects.html" . }}

{{ if eq .Site.Language.Lang "de" }}
Auf der <a href="https://lists.berlin.freifunk.net/cgi-bin/mailman/listinfo/berlin">Mailingliste</a> werden gerade folgende Themen diskutiert:
{{ else if eq .Site.Language.Lang "en" }}
The following topics are currently being discussed on the <a href="https://lists.berlin.freifunk.net/cgi-bin/mailman/listinfo/berlin">mailing list</a>:
{{ end }}

<ul style="text-align:left;margin-left: auto;margin-right: auto;width: fit-content;">
{{ range $element := $mail_subjects }}
<li>{{ $element }}</li>
{{ end }}
</ul>
11 changes: 11 additions & 0 deletions layouts/shortcodes/matrix-activity.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{ $matrix_activity := partial "matrix-activity.html" . }}

{{ if eq .Site.Language.Lang "de" }}
Im <a href="https://matrix.to/#/#berlin.freifunk.net:matrix.org">Freifunk Berlin Matrix Space</a> gibt es aktuell
<b>{{ $matrix_activity.room_count }} Chat-Räume mit {{ $matrix_activity.member_count }} Usern</b>.
Im Hauptchat wurden in den letzten sieben Tagen <b>{{ $matrix_activity.message_count }} Nachrichten</b> verschickt.
{{ else if eq .Site.Language.Lang "en" }}
In the <a href="https://matrix.to/#/#berlin.freifunk.net:matrix.org">Freifunk Berlin Matrix Space</a> there are currently
<b>{{ $matrix_activity.room_count }} chat rooms with {{ $matrix_activity.member_count }} users</b>.
In the main chat <b>{{ $matrix_activity.message_count }} messages</b> have been sent in the last seven days.
{{ end }}

0 comments on commit 8bde79a

Please sign in to comment.