2024-11-23 14:51:27 +01:00
|
|
|
#!/bin/sh
|
|
|
|
set -euo pipefail
|
|
|
|
|
2024-11-23 20:49:26 +01:00
|
|
|
if [ -z "$MM_SERVER" ]; then
|
|
|
|
echo "Set environment variable MM_SERVER to e.g. mattermost.example.com"
|
|
|
|
exit 1
|
|
|
|
fi
|
2024-11-23 17:36:10 +01:00
|
|
|
mattermost_name="Zeus Mattermost"
|
|
|
|
team_name="zeus"
|
|
|
|
|
|
|
|
dir="$(dirname "$0")"
|
|
|
|
|
2024-11-23 18:15:06 +01:00
|
|
|
out="$dir/dist/channels/index.html"
|
|
|
|
mkdir -p "$(dirname "$out")"
|
2024-11-23 14:51:27 +01:00
|
|
|
|
2024-11-23 20:49:26 +01:00
|
|
|
cp go_to_channel.png dist/
|
|
|
|
cp main.css dist/
|
|
|
|
|
2024-11-23 17:36:10 +01:00
|
|
|
custom_emoji_file="$(mktemp --tmpdir custom_emoji.XXXXXXXXXX.json)"
|
|
|
|
mmcli listcustomemoji | jq -s > "$custom_emoji_file"
|
2024-11-23 14:51:27 +01:00
|
|
|
|
|
|
|
jqo() {
|
|
|
|
json="$1"
|
|
|
|
shift 1
|
|
|
|
printf '%s' "$json" | jq -cr "$@" || { printf 'jq errored performing %s on this input: %s\n' "$*" "$json" >&2; }
|
|
|
|
}
|
|
|
|
|
|
|
|
htmlescape() {
|
|
|
|
sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\'/g'
|
|
|
|
}
|
|
|
|
|
2024-11-23 17:36:10 +01:00
|
|
|
emoji() {
|
|
|
|
env --chdir "$dir" \
|
|
|
|
python3 "$dir/emoji.py" "$custom_emoji_file"
|
|
|
|
}
|
|
|
|
|
|
|
|
markdown() {
|
|
|
|
htmlescape | \
|
2024-11-23 20:49:26 +01:00
|
|
|
{ pandoc --from=markdown+autolink_bare_uris+emoji --to=html || printf "Markdown conversion failed\n" >&2; } | \
|
|
|
|
sed -E '
|
|
|
|
s|<(h[0-6])[^>]*>|<div class="\1">|g
|
|
|
|
s|</(h[0-6])>|</div>|g
|
|
|
|
s/src="\(.*\)"/src=""/g
|
|
|
|
' | \
|
|
|
|
sed -f "$channels_sed_file" | \
|
2024-11-23 17:36:10 +01:00
|
|
|
emoji
|
|
|
|
}
|
|
|
|
|
2024-11-23 20:49:26 +01:00
|
|
|
format_number() {
|
|
|
|
awk '{printf "%'\''d\n", $1}'
|
|
|
|
}
|
|
|
|
|
2024-11-23 14:51:27 +01:00
|
|
|
format_timestamp() {
|
|
|
|
format_str="$1"
|
|
|
|
xargs -Ixxx date --date "@xxx" "+$format_str"
|
|
|
|
}
|
|
|
|
|
2024-11-23 17:36:10 +01:00
|
|
|
channels="$(mmcli ls "$team_name" | jq -cs 'sort_by(- .last_post_at) | .[]')"
|
|
|
|
channel_count="$(jqo "$channels" -s 'length')"
|
2024-11-23 20:49:26 +01:00
|
|
|
channels_sed_file="$(mktemp --tmpdir "mattermost_channels.XXXXXXXXX.sed")"
|
|
|
|
jqo "$channels" -s 'map(.name) | sort_by(length) | reverse | .[]' | sed 's|.*|s/~&/<a href=\"#channel_&\">CHANNEL[&]<\\/a>/g|' > "$channels_sed_file"
|
|
|
|
printf 's/CHANNEL\\[\\([^]]*\\)\\]/~\\1/g\n' >> "$channels_sed_file"
|
2024-11-23 17:36:10 +01:00
|
|
|
|
|
|
|
substitute_vars() {
|
|
|
|
sed '
|
|
|
|
s/$lastupdated/'"$(date '+%Y-%m-%d %H:%M')"'/g
|
|
|
|
s/$mattermost_name/'"$mattermost_name"'/g
|
|
|
|
s/$team_name/'"$team_name"'/g
|
|
|
|
s/$channel_count/'"$channel_count"'/g
|
|
|
|
'
|
|
|
|
}
|
|
|
|
|
|
|
|
substitute_vars < "$dir"/header.html > "$out"
|
2024-11-23 14:51:27 +01:00
|
|
|
|
2024-11-23 17:36:10 +01:00
|
|
|
printf '%s\n' "$channels" | while IFS="" read -r chan || [ -n "$chan" ]; do
|
2024-11-23 14:51:27 +01:00
|
|
|
if [ "$(jqo "$chan" '.type')" != O ]; then continue; fi
|
|
|
|
|
|
|
|
name="$(jqo "$chan" '.name' | htmlescape)"
|
|
|
|
display_name="$(jqo "$chan" '.display_name' | htmlescape)"
|
2024-11-23 17:36:10 +01:00
|
|
|
purpose="$(jqo "$chan" '.purpose' | markdown)"
|
|
|
|
header="$(jqo "$chan" '.header' | markdown)"
|
2024-11-23 14:51:27 +01:00
|
|
|
create_at="$(jqo "$chan" '.create_at / 1000' | format_timestamp '%Y-%m-%d')"
|
|
|
|
last_post_at="$(jqo "$chan" '.last_post_at / 1000' | format_timestamp '%Y-%m-%d %H:%M')"
|
2024-11-23 20:49:26 +01:00
|
|
|
total_msg_count="$(jqo "$chan" '.total_msg_count' | format_number)"
|
2024-11-23 14:51:27 +01:00
|
|
|
|
|
|
|
cat >> "$out" <<HERE
|
2024-11-23 20:49:26 +01:00
|
|
|
<div class="channel" id="channel_$name">
|
2024-11-23 21:13:51 +01:00
|
|
|
<h2>$display_name <a href="https://mattermost.zeus.gent/zeus/$name" target="mattermost-webapp"><img src="../go_to_channel.png" alt="Go to channel" class="go_to_channel" /></a></h2>
|
2024-11-23 20:49:26 +01:00
|
|
|
<dl>
|
|
|
|
HERE
|
|
|
|
if [ -n "$purpose" ]; then
|
|
|
|
cat >> "$out" <<HERE
|
|
|
|
<div class="purpose">
|
|
|
|
<dt>Purpose</dt>
|
|
|
|
<dd>$purpose</dd>
|
|
|
|
</div>
|
2024-11-23 14:51:27 +01:00
|
|
|
|
2024-11-23 20:49:26 +01:00
|
|
|
HERE
|
|
|
|
fi
|
|
|
|
if [ -n "$header" ]; then
|
|
|
|
cat >> "$out" <<HERE
|
|
|
|
<div class="header">
|
|
|
|
<dt>Header</dt>
|
|
|
|
<dd>$header</dd>
|
|
|
|
</div>
|
2024-11-23 14:51:27 +01:00
|
|
|
|
2024-11-23 20:49:26 +01:00
|
|
|
HERE
|
|
|
|
fi
|
|
|
|
cat >> "$out" <<HERE
|
|
|
|
<div class="stats">
|
|
|
|
<div class="create_at">
|
|
|
|
<dt>Created on</dt>
|
|
|
|
<dd>$create_at</dd>
|
|
|
|
</div>
|
|
|
|
<div class="last_post_at">
|
2024-11-23 14:51:27 +01:00
|
|
|
<dt>Last message</dt>
|
2024-11-23 20:49:26 +01:00
|
|
|
<dd>$last_post_at</dd>
|
|
|
|
</div>
|
|
|
|
<div class="total_msg_count">
|
|
|
|
<dt>Amount of messages</dt>
|
|
|
|
<dd>$total_msg_count</dd>
|
|
|
|
</div>
|
2024-11-23 14:51:27 +01:00
|
|
|
</div>
|
2024-11-23 20:49:26 +01:00
|
|
|
</dl>
|
|
|
|
</div>
|
2024-11-23 14:51:27 +01:00
|
|
|
HERE
|
|
|
|
done
|
|
|
|
|
2024-11-23 17:36:10 +01:00
|
|
|
substitute_vars < "$dir"/footer.html >> "$out"
|