kopia lustrzana https://github.com/reiver/greatape
feat(app): 🚧 implement direct messages
rodzic
088e7d68d7
commit
ebe2a28467
|
@ -5,7 +5,7 @@ import "time"
|
|||
const Public = "https://www.w3.org/ns/activitystreams#Public"
|
||||
|
||||
type Activity struct {
|
||||
Context string `json:"@context"`
|
||||
Context interface{} `json:"@context"`
|
||||
ID string `json:"id,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
Actor string `json:"actor,omitempty"`
|
||||
|
|
|
@ -5,8 +5,8 @@ import "encoding/json"
|
|||
const ActivityStreams = "https://www.w3.org/ns/activitystreams"
|
||||
|
||||
type Object struct {
|
||||
Context string `json:"@context" validate:"activitystream"`
|
||||
Type string `json:"type"`
|
||||
Context interface{} `json:"@context"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
func UnmarshalObject(data []byte) (Object, error) {
|
||||
|
|
|
@ -79,7 +79,7 @@ var InboxPost = route.New(HttpPost, "/u/:username/inbox", func(x IContext) error
|
|||
{
|
||||
activity := &activitypub.Activity{}
|
||||
if err := x.ParseBodyAndValidate(activity); err != nil {
|
||||
return x.BadRequest("bar_request")
|
||||
return x.BadRequest("bad_request")
|
||||
}
|
||||
|
||||
switch activity.Object.(map[string]interface{})["type"] {
|
||||
|
@ -114,14 +114,41 @@ var InboxPost = route.New(HttpPost, "/u/:username/inbox", func(x IContext) error
|
|||
})
|
||||
|
||||
var InboxGet = route.New(HttpGet, "/u/:username/inbox", func(x IContext) error {
|
||||
user := x.Request().Params("username")
|
||||
actor := x.StringUtil().Format("%s://%s/u/%s", config.PROTOCOL, config.DOMAIN, user)
|
||||
username := x.Request().Params("username")
|
||||
actor := x.StringUtil().Format("%s://%s/u/%s", config.PROTOCOL, config.DOMAIN, username)
|
||||
id := x.StringUtil().Format("%s://%s/u/%s/inbox", config.PROTOCOL, config.DOMAIN, username)
|
||||
|
||||
messages := &[]types.MessageResponse{}
|
||||
err := repos.FindIncomingActivitiesForUser(messages, actor).Error
|
||||
if err != nil {
|
||||
x.InternalServerError("internal server error")
|
||||
x.InternalServerError("internal_server_error")
|
||||
}
|
||||
|
||||
return x.JSON(messages)
|
||||
items := []*activitypub.Activity{}
|
||||
for _, message := range *messages {
|
||||
note := &activitypub.Note{
|
||||
Context: "https://www.w3.org/ns/activitystreams",
|
||||
To: []string{
|
||||
"https://www.w3.org/ns/activitystreams#Public",
|
||||
},
|
||||
Content: message.Content,
|
||||
Type: "Note",
|
||||
AttributedTo: message.From,
|
||||
}
|
||||
|
||||
activity := note.Wrap(username)
|
||||
items = append(items, activity)
|
||||
}
|
||||
|
||||
outbox := &activitypub.Outbox{
|
||||
Context: "https://www.w3.org/ns/activitystreams",
|
||||
ID: id,
|
||||
Type: "OrderedCollection",
|
||||
TotalItems: len(items),
|
||||
OrderedItems: items,
|
||||
}
|
||||
|
||||
json, _ := outbox.Marshal()
|
||||
x.Response().Header("Content-Type", "application/activity+json; charset=utf-8")
|
||||
return x.WriteString(string(json))
|
||||
})
|
||||
|
|
|
@ -84,7 +84,7 @@ var OutboxGet = route.New(HttpGet, "/u/:username/outbox", func(x IContext) error
|
|||
messages := &[]types.MessageResponse{}
|
||||
err := repos.FindOutgoingActivitiesByUser(messages, actor).Error
|
||||
if err != nil {
|
||||
x.InternalServerError("internal server error")
|
||||
x.InternalServerError("internal_server_error")
|
||||
}
|
||||
|
||||
items := []*activitypub.Activity{}
|
||||
|
|
|
@ -136,6 +136,9 @@
|
|||
<hr />
|
||||
<h2>Feed</h2>
|
||||
<ul class="list-group" id="feed"></ul>
|
||||
<hr />
|
||||
<h2>Direct Messages</h2>
|
||||
<ul class="list-group" id="inbox"></ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -175,7 +178,6 @@
|
|||
Authorization: "Bearer " + localStorage.getItem("token"),
|
||||
},
|
||||
}).then(function (response) {
|
||||
console.log(response);
|
||||
const feed = document.getElementById("feed");
|
||||
feed.innerHTML = "";
|
||||
for (let i = 0; i < response.data.orderedItems.length; i++) {
|
||||
|
@ -192,6 +194,30 @@
|
|||
});
|
||||
}
|
||||
|
||||
function refreshInbox() {
|
||||
axios({
|
||||
method: "get",
|
||||
url: "/u/" + username + "/inbox",
|
||||
headers: {
|
||||
Authorization: "Bearer " + localStorage.getItem("token"),
|
||||
},
|
||||
}).then(function (response) {
|
||||
const feed = document.getElementById("inbox");
|
||||
feed.innerHTML = "";
|
||||
for (let i = 0; i < response.data.orderedItems.length; i++) {
|
||||
let item = document.createElement("li");
|
||||
item.innerHTML =
|
||||
"<article><em>From: " +
|
||||
response.data.orderedItems[i].object.attributedTo +
|
||||
"</em><hr><p>" +
|
||||
response.data.orderedItems[i].object.content +
|
||||
"</p></article>";
|
||||
item.className = "list-group-item";
|
||||
feed.appendChild(item);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
document.getElementById("frmPost").onsubmit = function () {
|
||||
axios({
|
||||
method: "post",
|
||||
|
@ -235,7 +261,9 @@
|
|||
"/u/" + username + "/follow";
|
||||
document.getElementById("frmPost").action =
|
||||
"/u/" + username + "/outbox";
|
||||
refreshFeed();
|
||||
|
||||
refreshFeed();
|
||||
refreshInbox();
|
||||
});
|
||||
};
|
||||
</script>
|
||||
|
|
Ładowanie…
Reference in New Issue