160 lines
4.0 KiB
Go
160 lines
4.0 KiB
Go
/*
|
|
Pont Mattermost Portail des assos
|
|
Copyright (C) 2021 Romain de Laage
|
|
|
|
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/>.
|
|
*/
|
|
|
|
package main
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"bufio"
|
|
"bytes"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"encoding/json"
|
|
)
|
|
|
|
type Owner struct {
|
|
Id string `json:"id"`
|
|
Login string `json:"login"`
|
|
Name string `json:"name"`
|
|
Image string `json:"image"`
|
|
Deleted_at *string `json:"deleted_at,omitempty"`
|
|
In_cemetery_at *string `json:"in_cemetry_at,omitempty"`
|
|
Model string `json:"model"`
|
|
}
|
|
|
|
type VisibilityT struct {
|
|
Id string `json:"id"`
|
|
Type string `json:"type"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
type Article struct {
|
|
Id string `json:"id"`
|
|
Title string `json:"title"`
|
|
Description string `json:"description"`
|
|
Content string `json:"content"`
|
|
Image string `json:"image,omitempty"`
|
|
Created_at string `json:"created_at"`
|
|
Owned_by Owner `json:"owned_by"`
|
|
Visibility VisibilityT `json:"visibility"`
|
|
Event *string `json:"event,omitempty"`
|
|
}
|
|
|
|
type Message struct {
|
|
Channel_id string `json:"channel_id"`
|
|
Message_content string `json:"message"`
|
|
}
|
|
|
|
func main() {
|
|
channel_id := os.Getenv("MM_ASSOS_CHANNEL")
|
|
api_token := os.Getenv("MM_ASSOS_TOKEN")
|
|
api_endpointmm := os.Getenv("MM_ASSOS_ENDPOINTMM")
|
|
api_endpointassos := os.Getenv("MM_ASSOS_ENDPOINTASSOS")
|
|
db_path := os.Getenv("MM_ASSOS_DBPATH")
|
|
|
|
if channel_id == "" || api_token == "" || api_endpointmm == "" || api_endpointassos == "" || db_path == "" {
|
|
log.Fatalln("MM_ASSOS_CHANNEL, MM_ASSOS_ENDPOINTMM, MM_ASSOS_ENDPOINTASSOS, MM_ASSOS_DBPATH or MM_ASSOS_TOKEN not defined")
|
|
os.Exit(1)
|
|
}
|
|
|
|
resp, err := http.Get(api_endpointassos)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
os.Exit(1)
|
|
}
|
|
if resp.StatusCode != 200 {
|
|
log.Fatalln("Error when fetching articles, server returned " + resp.Status)
|
|
os.Exit(1)
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
log.Printf("Successfully got articles")
|
|
var articles []Article
|
|
json.Unmarshal(body, &articles)
|
|
|
|
for i := 0; i < len(articles); i++ {
|
|
db_file, err := os.OpenFile(db_path, os.O_CREATE|os.O_RDWR, 0777)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
scanner := bufio.NewScanner(db_file)
|
|
scanner.Split(bufio.ScanLines)
|
|
var entries []string
|
|
|
|
for scanner.Scan() {
|
|
entries = append(entries, scanner.Text())
|
|
}
|
|
|
|
in_db := 0
|
|
|
|
for _, entry := range entries {
|
|
if articles[i].Id == entry {
|
|
in_db = 1
|
|
}
|
|
}
|
|
|
|
if in_db == 0 {
|
|
var message Message
|
|
message.Channel_id = channel_id
|
|
message.Message_content = "# " + articles[i].Title + "\n" + "par *" + articles[i].Owned_by.Name + "*\n\n" + articles[i].Content
|
|
|
|
json_content, err := json.Marshal(message)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", api_endpointmm, bytes.NewBuffer(json_content))
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Authorization", "Bearer " + api_token)
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
os.Exit(1)
|
|
}
|
|
if resp.StatusCode != 201 {
|
|
log.Fatalln("Error while posting message, server returned " + resp.Status)
|
|
os.Exit(1)
|
|
}
|
|
|
|
_, err = db_file.WriteString(articles[i].Id + "\n")
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
db_file.Close()
|
|
}
|
|
}
|