| 1 | package tinyclient |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | "strconv" |
| 9 | ) |
| 10 | |
| 11 | func (a *App) typedBackendRead(w http.ResponseWriter, r *http.Request, actor string) { |
| 12 | if a.backend.Policy().Features.Grasp08 { |
| 13 | if err := a.privateReadAllowed(r, actor); err != nil { |
| 14 | http.Error(w, err.Error(), 403) |
| 15 | return |
| 16 | } |
| 17 | } |
| 18 | q := r.URL.Query() |
| 19 | var result any |
| 20 | var err error |
| 21 | switch q.Get("op" ) { |
| 22 | case "rooms" , "room" , "thread" : |
| 23 | reader, ok := a.backend.(RoomsReader) |
| 24 | if !ok { |
| 25 | http.Error(w, "typed room reads unavailable" , 501) |
| 26 | return |
| 27 | } |
| 28 | limit, _ := strconv.Atoi(q.Get("limit" )) |
| 29 | switch q.Get("op" ) { |
| 30 | case "rooms" : |
| 31 | result, err = reader.ListRooms(r.Context(), actor, q.Get("cursor" ), limit) |
| 32 | case "room" : |
| 33 | result, err = reader.ReadRoom(r.Context(), actor, q.Get("room" ), q.Get("cursor" ), limit) |
| 34 | case "thread" : |
| 35 | result, err = reader.ReadThread(r.Context(), actor, q.Get("room" ), q.Get("root" ), q.Get("cursor" ), limit) |
| 36 | } |
| 37 | case "activity" : |
| 38 | reader, ok := a.backend.(ChatActivityReader) |
| 39 | if !ok { |
| 40 | http.Error(w, "chat activity unavailable" , 501) |
| 41 | return |
| 42 | } |
| 43 | result, err = reader.ReadChatActivity(r.Context(), actor, q.Get("room" ), q.Get("root" )) |
| 44 | default: |
| 45 | err = errors.New("unsupported client read" ) |
| 46 | } |
| 47 | if err != nil { |
| 48 | http.Error(w, err.Error(), 403) |
| 49 | return |
| 50 | } |
| 51 | writeJSON(w, 200, result) |
| 52 | } |
| 53 | |
| 54 | type remoteRooms struct{ *requestBackend } |
| 55 | |
| 56 | func (b *remoteRooms) ListRooms(ctx context.Context, actor, cursor string, limit int) (RoomList, error) { |
| 57 | var result RoomList |
| 58 | err := b.readAsActor(ctx, actor, url.Values{"op" : {"rooms" }, "cursor" : {cursor}, "limit" : {strconv.Itoa(limit)}}, &result) |
| 59 | return result, err |
| 60 | } |
| 61 | func (b *remoteRooms) ReadRoom(ctx context.Context, actor, room, cursor string, limit int) (RoomPage, error) { |
| 62 | var result RoomPage |
| 63 | err := b.readAsActor(ctx, actor, url.Values{"op" : {"room" }, "room" : {room}, "cursor" : {cursor}, "limit" : {strconv.Itoa(limit)}}, &result) |
| 64 | return result, err |
| 65 | } |
| 66 | func (b *remoteRooms) ReadThread(ctx context.Context, actor, room, root, cursor string, limit int) (RoomPage, error) { |
| 67 | var result RoomPage |
| 68 | err := b.readAsActor(ctx, actor, url.Values{"op" : {"thread" }, "room" : {room}, "root" : {root}, "cursor" : {cursor}, "limit" : {strconv.Itoa(limit)}}, &result) |
| 69 | return result, err |
| 70 | } |
| 71 | func (b *requestBackend) ReadChatActivity(ctx context.Context, actor, room, root string) (any, error) { |
| 72 | if !b.snapshot.Activity { |
| 73 | return nil, nil |
| 74 | } |
| 75 | var result any |
| 76 | err := b.readAsActor(ctx, actor, url.Values{"op" : {"activity" }, "room" : {room}, "root" : {root}}, &result) |
| 77 | return result, err |
| 78 | } |