Skip to content

Commit

Permalink
Starting to add meta and read
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdeka committed Apr 10, 2015
1 parent b1b9f55 commit d5c0f6b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ out:
[
{
id: "...",
meta: {
"some": "meta",
"other": {
"beautiful": [ "meta" ]
}
},
owner: {
id: "..."
},
Expand All @@ -47,6 +53,12 @@ out:
```
{
id: "...",
meta: {
"some": "meta",
"other": {
"beautiful": [ "meta" ]
}
},
owner: {
id: "..."
},
Expand All @@ -62,15 +74,21 @@ out:

`POST /threads`

create a new thread, upon creation postman sends a mail containing the message *msg* to the *to* email address setting the sender as the *from* mail address and the *reply-to* field to the email address of the mail node (inbound.yourdomain.com).
create a new thread, upon creation postman sends a mail containing the message *msg* to the *to* email address setting the sender as the *from* mail address and the *reply-to* field to the email address of the mail node (inbound.yourdomain.com). You can include additional info in the *meta* field, they will be always returned when you ask for the thread.

in:
```
{
from: "pinco@random.com",
to: "pinco@random.com",
msg: "hello!"
msg: "hello!",
meta: {
"some": "meta",
"other": {
"beautiful": [ "meta" ]
}
}
}
```
Expand Down
12 changes: 10 additions & 2 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"gopkg.in/mgo.v2/bson"
"time"
)

type Owner struct {
Expand All @@ -11,11 +12,18 @@ type Owner struct {
type Message struct {
From string `json:"from" bson:"from"`
To string `json:"to" bson:"to"`
Msg string `json:"msg" bson:"msg"`
Msg string `json:"msg" bson:"msg"`
Read *time.Time `json:"read,omitempty" bson:"read,omitempty"`
}

type Thread struct {
Id bson.ObjectId `json:"id" bson:"_id"`
Owner Owner `json:"owner" bson:"owner"`
Owner Owner `json:"owner" bson:"owner"`
Meta map[string]interface {} `json:"meta,omitempty" bson:"meta,omitempty"`
Messages []Message `json:"messages" bson:"messages"`
}

type ThreadCreationReq struct {
*Message
Meta map[string]interface {} `json:"meta"`
}
11 changes: 7 additions & 4 deletions postman.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ func StirNegroni() *negroni.Negroni{
return n
}

//the mail provider wants to validate the endpoint before you can start using it
func HeadInbound(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
}

//process a new mail message coming from the mail provider
func ProcessInbound(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(884808408)
mPostValue := r.FormValue("mandrill_events")
Expand Down Expand Up @@ -111,23 +113,24 @@ func ProcessInbound(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
}

//creates a new thread upon user request, he'll become the owner of the thread
func CreateThread(w http.ResponseWriter, r *http.Request) {
var nMsg Message
err := UnmarshalObject(r.Body, &nMsg)
var nThrReq ThreadCreationReq
err := UnmarshalObject(r.Body, &nThrReq)
if err != nil{
http.Error(w, "Your JSON is not GOOD", http.StatusBadRequest)
return
}
thedb := context.Get(r, db).(*mgo.Database)
tColl := thedb.C("message_threads")
var owner = Owner{bson.ObjectIdHex(context.Get(r, userId).(string))}
nThread := Thread{bson.NewObjectId(), owner, []Message{nMsg}}
nThread := Thread{ bson.NewObjectId(), owner, nThrReq.Meta, []Message{ Message{From: nThrReq.From, To: nThrReq.To, Msg: nThrReq.Msg} } }
err = tColl.Insert(nThread)
if err != nil {
panic("Can't create thread:" + err.Error())
}
//actually send out the mail
go NewMailProvider(config).SendMail(nThread.Id.Hex(), nMsg.From, []string{nMsg.To}, nMsg.Msg)
go NewMailProvider(config).SendMail(nThread.Id.Hex(), nThrReq.From, []string{nThrReq.To}, nThrReq.Msg)
//config thread creation
JSONResponse(w, nThread)
}
Expand Down
3 changes: 3 additions & 0 deletions postman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/codegangsta/negroni"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"fmt"
)

const TestAuthHeader string = "Basic NTE4Y2JiMTM4OWRhNzlkM2EyNTQ1M2Y5Om5vcGFzc3c="
Expand Down Expand Up @@ -105,6 +106,8 @@ func TestCreateThread(t *testing.T) {
//save created thread so we can test later
createdThreadId = nt.Id
nt.Id = ""
fmt.Printf("%+v\n", nt)
fmt.Printf("%+v\n", tt)
require.Equal(reflect.DeepEqual(tt, nt), true)
}

Expand Down

0 comments on commit d5c0f6b

Please sign in to comment.