Commit 70d31152 authored by Linus's avatar Linus 🤔
Browse files

Now works. To do: email confimation.

parent 9eaffe8f
Pipeline #79436 passed with stage
in 22 seconds
......@@ -11,7 +11,7 @@ import (
// partnerListing represents a tutorium partner listing.
type partnerListing struct {
email string `toml:"Email"`
Email string
NumPeople int
Message string
PostTime string
......@@ -19,13 +19,18 @@ type partnerListing struct {
// tutSwapListing represents a tutorium swap listing.
type tutSwapListing struct {
email string `toml:"Email"`
Email string
From string
To string
Message string
PostTime string
}
type toSend struct {
EmailId string
Listing interface{}
}
type partnerMap map[string][]partnerListing
type swapMap map[string][]tutSwapListing
......
......@@ -50,7 +50,7 @@ func doDel(w http.ResponseWriter, r *http.Request) error {
}
i := 0
for _, listing := range value {
if strings.ToLower(listing.email) != strings.ToLower(email) {
if strings.ToLower(listing.Email) != strings.ToLower(email) {
value[i] = listing
i++
}
......@@ -71,7 +71,7 @@ func doDel(w http.ResponseWriter, r *http.Request) error {
}
i := 0
for _, swapListing := range value {
if strings.ToLower(swapListing.email) != strings.ToLower(email) {
if strings.ToLower(swapListing.Email) != strings.ToLower(email) {
value[i] = swapListing
i++
}
......
package main
import (
"git.rwth-aachen.de/h/apb/mail"
"html/template"
"net/http"
)
......@@ -17,6 +18,7 @@ var regFunctions = map[string]func(http.ResponseWriter, *http.Request) error{
"/addlisting": addListing,
"/addswap": addSwap,
"/info": info,
"/respond": respond,
}
func errorClosure(f func(http.ResponseWriter, *http.Request) error) func(http.ResponseWriter, *http.Request) {
......@@ -58,6 +60,33 @@ func renderMessage(msg string, title string, w http.ResponseWriter, r *http.Requ
}, w, r)
}
func respond(w http.ResponseWriter, r *http.Request) error {
r.ParseForm()
message := r.FormValue("message")
email := r.FormValue("email")
id := r.FormValue("email_id")
if message == "" || email == "" {
return renderError("Nachricht oder Email leer", w, r)
}
if id == "" {
return renderError("Email-ID leer (hast du den DOM verändert?)", w, r)
}
to, err := mail.GetEmail(id)
if err != nil {
return err
}
body := "Nachricht von: " + email + "\n\n" + message + "\n\n" +
"Gesendet durch die Abgabepartnerbörse"
err = mail.Send(to, "Abgabepartnerbörse - Antwort!", body)
if err != nil {
return nil
}
return renderMessage("Hab die Mail gesendet. :)", "Okay", w, r)
}
func welcome(w http.ResponseWriter, r *http.Request) error {
if r.URL.Path != "/" {
http.NotFound(w, r)
......
package mail
import (
"errors"
"io/ioutil"
"log"
"math/rand"
"net/smtp"
"strconv"
"github.com/BurntSushi/toml"
)
// This section is to store emails while also keeping them a secret
// random id -> mail
var mailLookup = map[string]string{}
// mail -> random id
var idLookup = map[string]string{}
// GetId returns the id to a given email. If the id
// does not exist yet, it will create the id.
func GetId(email string) string {
if id, ok := idLookup[email]; ok {
return id
}
id := strconv.Itoa(rand.Int())
log.Println("Setting " + id + " to " + email)
idLookup[email] = id
mailLookup[id] = email
return id
}
// GetEmail returns the email to a given id. If the id is invalid,
// it will return an error.
func GetEmail(id string) (string, error) {
if id, ok := mailLookup[id]; ok {
log.Println(id)
return id, nil
}
return "", errors.New("This id does not exist")
}
type emailConfig struct {
Email string
Password string
......@@ -15,14 +49,17 @@ type emailConfig struct {
var config emailConfig
func send(to string, subject string, body string) error {
// Send sends an email to to, with subject, with body
func Send(to string, subject string, body string) error {
from := config.Email
pass := config.Password
msg := "From: " + from + "\n" +
"To: " + to + "\n" +
msg := "From: " + "Abgabepartnerboerse <" + from + ">" + "\n" +
"To: " + to + " <" + to + ">" + "\n" +
"Subject: " + subject + "\n\n" +
body
log.Println(msg)
err := smtp.SendMail("smtp.gmail.com:587",
smtp.PlainAuth("", from, pass, "smtp.gmail.com"),
......
package main
import (
"log"
"git.rwth-aachen.de/h/apb/mail"
"html/template"
"net/http"
"sort"
......@@ -100,12 +102,23 @@ func doSearch(w http.ResponseWriter, r *http.Request) error {
otherTutsHidden = ""
}
tmpl, err := template.ParseFiles("templates/template.html", "templates/found.html", "templates/disclaimer.html", resultTemplate)
resultsToIds := []toSend{}
for _, r := range results {
log.Println(r)
resultsToIds = append(resultsToIds, toSend{
EmailId: mail.GetId(r.Email),
Listing: r,
})
}
tmpl, err := template.ParseFiles("templates/template.html", "templates/found.html",
"templates/disclaimer.html", "templates/respond_box.html", resultTemplate)
if err != nil {
return err
}
return tmpl.ExecuteTemplate(w, "template", map[string]interface{}{
"FoundListings": results,
"FoundListings": resultsToIds,
"Subject": tutString,
"Title": "Resultate",
"SubjectText": subjectText,
......
......@@ -3,73 +3,96 @@
grid-template-columns: auto auto;
grid-template-rows: auto auto auto;
grid-template-areas:
"time time"
"email_text email"
"num_text num"
"from_text from"
"to_text to"
"message message";
"time time"
"email_text email"
"num_text num"
"from_text from"
"to_text to"
"message message"
"reply_btn reply_btn"
"reply_box reply_box";
font-family: sans-serif;
border: 1px solid black;
box-sizing:border-box;
padding: 10px;
}
.from_text {
.reply_box {
grid-area: reply_box;
display: grid;
grid-template-columns: auto;
grid-template-rows: auto auto;
grid-template-areas:
"reply_area"
"respond_email"
"respond_email_label"
"respond_btn";
}
.foundcard.content {
padding-left: 20px;
padding-right: 20px;
}
.respond_email {
grid-area: respond_email;
}
.respond_email_label {
grid-area: respond_email_label;
}
.respond_btn {
grid-area: respond_btn;
}
.reply_area {
grid-area: reply_area;
}
.from_text {
grid-area: from_text;
}
.from {
padding-left: 20px;
padding-right: 20px;
grid-area: from;
}
.to_text {
padding-left: 20px;
padding-right: 20px;
grid-area: to_text;
}
.to {
padding-left: 20px;
padding-right: 20px;
grid-area: to;
}
.email_text {
padding-left: 20px;
padding-right: 20px;
grid-area: email_text;
}
.email {
padding-left: 20px;
padding-right: 20px;
grid-area: email;
}
.reply_btn {
grid-area: reply_btn;
text-align: center;
margin: auto;
}
.num_text {
padding-left: 20px;
padding-right: 20px;
grid-area: num_text;
}
.num {
padding-left: 20px;
padding-right: 20px;
grid-area: num;
}
.time {
padding-left: 20px;
padding-right: 20px;
grid-area: time;
}
.message {
padding-left: 20px;
padding-right: 20px;
grid-area: message;
}
......@@ -83,3 +106,5 @@
}
......@@ -23,6 +23,7 @@
<p class="num"><input type="number" name="num_students"></p>
<textarea class="message biginput" placeholder="Persönliche Nachricht" name="message" rows=4></textarea>
</div>
<br>
{{ template "disclaimer" . }}
<br>
<input type="submit" value="Absenden">
......
{{ define "respond_box" }}
<button class="reply_btn" style="display: none;" onclick="showOrHideBox(this)">Antworten!</button>
<form class="reply_box" method="POST" action="/respond">
<textarea class="reply_area biginput" placeholder="Deine Antwort" name="message" rows=4></textarea>
<input class="respond_email" type="email" placeholder="Deine Email (zum Antworten)" name="email"></input>
<label class="respond_email_label" for="email">Diese Email speichern wir nicht. Sie wird lediglich zum Aufgeber der Nachricht weitergeleitet.</label>
<input value="{{ .EmailId }}" name="email_id" hidden>
<input class="respond_btn" type="submit" value="Senden">
</form>
{{ end }}
{{ define "respond_box_script" }}
<script>
var replyBtns = document.getElementsByClassName("reply_btn");
for (i = 0; i < replyBtns.length; i++) {
replyBtns[i].style.display = "";
}
var elements = document.getElementsByClassName("reply_box");
for (i = 0; i < elements.length; i++) {
elements[i].style.display = "none";
}
function showOrHideBox(box) {
box = box.parentElement.getElementsByClassName("reply_box")[0];
if (box.style.display == "none") {
box.style.display = "";
} else {
box.style.display = "none";
}
}
</script>
{{ end }}
......@@ -2,12 +2,15 @@
{{ range .FoundListings }}
<div class="foundcard">
<small class="time">Aufgegeben am {{ .PostTime }}</small>
<small class="time">Aufgegeben am {{ .Listing.PostTime }}</small>
<p class="num_text">Anzahl Studierende</p>
<p class="num">{{ .NumPeople }}</p>
<pre class="message">{{ .Message }}</pre>
<p class="num">{{ .Listing.NumPeople }}</p>
<pre class="message">{{ .Listing.Message }}</pre>
{{ template "respond_box" . }}
</div>
<br><br>
{{ end }}
{{ template "respond_box_script" . }}
{{ end }}
......@@ -2,14 +2,17 @@
{{ range .FoundSwaps }}
<div class="foundcard">
<small class="time">Aufgegeben am {{ .PostTime }}</small>
<small class="time">Aufgegeben am {{ .Listing.PostTime }}</small>
<p class="from_text">Von Tutorium</p>
<p class="from">{{ .From }}</p>
<p class="from">{{ .Listing.From }}</p>
<p class="to_text">Zu Tutorien</p>
<p class="to">{{ .To }}</p>
<pre class="message">{{ .Message }}</pre>
<p class="to">{{ .Listing.To }}</p>
<pre class="message">{{ .Listing.Message }}</pre>
{{ template "respond_box" . }}
</div>
<br><br>
{{ end }}
{{ template "respond_box_script" . }}
{{ end }}
package main
import (
"git.rwth-aachen.de/h/apb/mail"
"html/template"
"net/http"
)
......@@ -36,13 +37,21 @@ func tutSwapSearch(w http.ResponseWriter, r *http.Request) error {
resultTemplate = "templates/swap/result_some.html"
}
resultsToIds := []toSend{}
for _, r := range results {
resultsToIds = append(resultsToIds, toSend{
EmailId: mail.GetId(r.Email),
Listing: r,
})
}
tmpl, err := template.ParseFiles("templates/template.html", "templates/tutswap_found.html",
"templates/disclaimer.html", resultTemplate)
"templates/disclaimer.html", "templates/respond_box.html", resultTemplate)
if err != nil {
return err
}
return tmpl.ExecuteTemplate(w, "template", map[string]interface{}{
"FoundSwaps": results,
"FoundSwaps": resultsToIds,
"Subject": subj,
"Title": "Resultate",
})
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment