| 1 | package tinyclient |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | // socialReferencePage tries the Social renderer for NIP-19 event references. |
| 10 | // The generic event page remains the fallback for event kinds that Social does |
| 11 | // not publish or for backends that predate the Social browse methods. |
| 12 | func (a *App) socialReferencePage(writer http.ResponseWriter, request *http.Request, identifier string, address bool, actor string) bool { |
| 13 | if !a.backend.Policy().Features.Pages { |
| 14 | return false |
| 15 | } |
| 16 | if address && !socialArticleAddress(identifier) { |
| 17 | return false |
| 18 | } |
| 19 | params := map[string]any{"limit" : 30} |
| 20 | if cursor := request.URL.Query().Get("cursor" ); cursor != "" { |
| 21 | params["cursor" ] = cursor |
| 22 | } |
| 23 | if address { |
| 24 | params["address" ] = identifier |
| 25 | } else { |
| 26 | params["id" ] = identifier |
| 27 | } |
| 28 | raw, err := json.Marshal(params) |
| 29 | if err != nil { |
| 30 | return false |
| 31 | } |
| 32 | result, err := a.backend.Query(request.Context(), "browsesocialthread" , []json.RawMessage{raw}, actor) |
| 33 | if err != nil || !socialThreadPost(result) { |
| 34 | return false |
| 35 | } |
| 36 | data := PageData{ |
| 37 | Event: result, |
| 38 | Query: request.URL.Query(), |
| 39 | Tab: "social-thread" , |
| 40 | Title: "Social | " + a.backend.Slug(), |
| 41 | } |
| 42 | a.render(writer, request, data) |
| 43 | return true |
| 44 | } |
| 45 | |
| 46 | func socialArticleAddress(value string) bool { |
| 47 | parts := strings.SplitN(value, ":" , 3) |
| 48 | return len(parts) == 3 && parts[0] == "30023" && hexID.MatchString(parts[1]) |
| 49 | } |
| 50 | |
| 51 | func socialThreadPost(value any) bool { |
| 52 | post, ok := valueMap(value)["post" ] |
| 53 | if !ok { |
| 54 | return false |
| 55 | } |
| 56 | row := valueMap(post) |
| 57 | kind := int(unixSeconds(row["kind" ])) |
| 58 | return kind == 1 || kind == 1111 || kind == 30023 |
| 59 | } |