|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +// In-memory followers map for demo (replace with persistent storage in production) |
| 10 | +var followers = make(map[string]map[string]bool) // username -> set of follower actor IDs |
| 11 | + |
| 12 | +// ActivityPubActor represents a minimal ActivityPub Person actor |
| 13 | +func ActivityPubActor(w http.ResponseWriter, r *http.Request) { |
| 14 | + username := strings.TrimPrefix(r.URL.Path, "/users/") |
| 15 | + username = strings.TrimSuffix(username, "/actor") |
| 16 | + actor := map[string]interface{}{ |
| 17 | + "@context": "https://www.w3.org/ns/activitystreams", |
| 18 | + "id": r.Host + "/users/" + username + "/actor", |
| 19 | + "type": "Person", |
| 20 | + "preferredUsername": username, |
| 21 | + "inbox": r.Host + "/users/" + username + "/inbox", |
| 22 | + "outbox": r.Host + "/users/" + username + "/outbox", |
| 23 | + "publicKey": map[string]interface{}{ |
| 24 | + "id": r.Host + "/users/" + username + "#main-key", |
| 25 | + "owner": r.Host + "/users/" + username + "/actor", |
| 26 | + "publicKeyPem": "TODO: Add public key", |
| 27 | + }, |
| 28 | + } |
| 29 | + w.Header().Set("Content-Type", "application/activity+json") |
| 30 | + json.NewEncoder(w).Encode(actor) |
| 31 | +} |
| 32 | + |
| 33 | +// ActivityPubInbox handles incoming ActivityPub activities |
| 34 | +// Update ActivityPubInbox to use persistent followers storage |
| 35 | +func ActivityPubInbox(w http.ResponseWriter, r *http.Request) { |
| 36 | + username := strings.TrimPrefix(r.URL.Path, "/users/") |
| 37 | + username = strings.TrimSuffix(username, "/inbox") |
| 38 | + var activity map[string]interface{} |
| 39 | + if err := json.NewDecoder(r.Body).Decode(&activity); err != nil { |
| 40 | + w.WriteHeader(http.StatusBadRequest) |
| 41 | + return |
| 42 | + } |
| 43 | + typeVal, _ := activity["type"].(string) |
| 44 | + switch typeVal { |
| 45 | + case "Follow": |
| 46 | + actor, _ := activity["actor"].(string) |
| 47 | + fmap := loadFollowers(username) |
| 48 | + fmap[actor] = true |
| 49 | + saveFollowers(username, fmap) |
| 50 | + // Respond with Accept activity |
| 51 | + accept := map[string]interface{}{ |
| 52 | + "@context": "https://www.w3.org/ns/activitystreams", |
| 53 | + "id": r.Host + "/users/" + username + "/accepts/" + actor, |
| 54 | + "type": "Accept", |
| 55 | + "actor": r.Host + "/users/" + username + "/actor", |
| 56 | + "object": activity, |
| 57 | + } |
| 58 | + w.Header().Set("Content-Type", "application/activity+json") |
| 59 | + json.NewEncoder(w).Encode(accept) |
| 60 | + return |
| 61 | + case "Like", "Announce": |
| 62 | + // Optionally handle likes and boosts |
| 63 | + w.WriteHeader(http.StatusAccepted) |
| 64 | + return |
| 65 | + default: |
| 66 | + w.WriteHeader(http.StatusAccepted) |
| 67 | + return |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// ActivityPubOutbox returns the user's posts as Create activities |
| 72 | +func ActivityPubOutbox(w http.ResponseWriter, r *http.Request) { |
| 73 | + username := strings.TrimPrefix(r.URL.Path, "/users/") |
| 74 | + username = strings.TrimSuffix(username, "/outbox") |
| 75 | + posts, err := FetchPostsByUsername(username) |
| 76 | + if err != nil { |
| 77 | + w.WriteHeader(http.StatusInternalServerError) |
| 78 | + return |
| 79 | + } |
| 80 | + orderedItems := []interface{}{} |
| 81 | + for _, post := range posts { |
| 82 | + create := map[string]interface{}{ |
| 83 | + "@context": "https://www.w3.org/ns/activitystreams", |
| 84 | + "id": w.Header().Get("Host") + "/users/" + username + "/outbox/" + post.Id, |
| 85 | + "type": "Create", |
| 86 | + "actor": w.Header().Get("Host") + "/users/" + username + "/actor", |
| 87 | + "object": map[string]interface{}{ |
| 88 | + "id": w.Header().Get("Host") + "/posts/" + post.Id, |
| 89 | + "type": "Note", |
| 90 | + "attributedTo": w.Header().Get("Host") + "/users/" + username + "/actor", |
| 91 | + "content": post.Content, |
| 92 | + "published": post.CreatedAt, |
| 93 | + "to": []string{"https://www.w3.org/ns/activitystreams#Public"}, |
| 94 | + }, |
| 95 | + } |
| 96 | + orderedItems = append(orderedItems, create) |
| 97 | + } |
| 98 | + outbox := map[string]interface{}{ |
| 99 | + "@context": "https://www.w3.org/ns/activitystreams", |
| 100 | + "id": w.Header().Get("Host") + "/users/" + username + "/outbox", |
| 101 | + "type": "OrderedCollection", |
| 102 | + "totalItems": len(orderedItems), |
| 103 | + "orderedItems": orderedItems, |
| 104 | + } |
| 105 | + w.Header().Set("Content-Type", "application/activity+json") |
| 106 | + json.NewEncoder(w).Encode(outbox) |
| 107 | +} |
| 108 | + |
| 109 | +// Followers endpoint: returns a list of follower actor IDs |
| 110 | +func ActivityPubFollowers(w http.ResponseWriter, r *http.Request) { |
| 111 | + username := strings.TrimPrefix(r.URL.Path, "/users/") |
| 112 | + username = strings.TrimSuffix(username, "/followers") |
| 113 | + fmap := loadFollowers(username) |
| 114 | + ids := []string{} |
| 115 | + for id := range fmap { |
| 116 | + ids = append(ids, id) |
| 117 | + } |
| 118 | + resp := map[string]interface{}{ |
| 119 | + "@context": "https://www.w3.org/ns/activitystreams", |
| 120 | + "id": r.Host + "/users/" + username + "/followers", |
| 121 | + "type": "Collection", |
| 122 | + "totalItems": len(ids), |
| 123 | + "items": ids, |
| 124 | + } |
| 125 | + w.Header().Set("Content-Type", "application/activity+json") |
| 126 | + json.NewEncoder(w).Encode(resp) |
| 127 | +} |
| 128 | + |
| 129 | +// Following endpoint: returns an empty list for now (implement if you track following) |
| 130 | +func ActivityPubFollowing(w http.ResponseWriter, r *http.Request) { |
| 131 | + username := strings.TrimPrefix(r.URL.Path, "/users/") |
| 132 | + username = strings.TrimSuffix(username, "/following") |
| 133 | + resp := map[string]interface{}{ |
| 134 | + "@context": "https://www.w3.org/ns/activitystreams", |
| 135 | + "id": r.Host + "/users/" + username + "/following", |
| 136 | + "type": "Collection", |
| 137 | + "totalItems": 0, |
| 138 | + "items": []string{}, |
| 139 | + } |
| 140 | + w.Header().Set("Content-Type", "application/activity+json") |
| 141 | + json.NewEncoder(w).Encode(resp) |
| 142 | +} |
0 commit comments