writeChildUnarchiveGuard returns a 400 early when a child unarchive request obviously races an archived parent. The durable invariant is enforced atomically in chatd.Server.UnarchiveChat; this guard just surfaces the error before we take any locks. Returns true when a response has been written.
( ctx context.Context, rw http.ResponseWriter, chat database.Chat, )
| 2927 | // |
| 2928 | // Returns true when a response has been written. |
| 2929 | func (api *API) writeChildUnarchiveGuard( |
| 2930 | ctx context.Context, |
| 2931 | rw http.ResponseWriter, |
| 2932 | chat database.Chat, |
| 2933 | ) bool { |
| 2934 | parent, err := api.Database.GetChatByID(ctx, chat.ParentChatID.UUID) |
| 2935 | if err != nil { |
| 2936 | if errors.Is(err, sql.ErrNoRows) { |
| 2937 | httpapi.ResourceNotFound(rw) |
| 2938 | return true |
| 2939 | } |
| 2940 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 2941 | Message: "Failed to load parent chat.", |
| 2942 | Detail: err.Error(), |
| 2943 | }) |
| 2944 | return true |
| 2945 | } |
| 2946 | if parent.Archived { |
| 2947 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 2948 | Message: "Cannot unarchive a child chat while its parent is archived. Unarchive the parent chat to cascade.", |
| 2949 | }) |
| 2950 | return true |
| 2951 | } |
| 2952 | return false |
| 2953 | } |
| 2954 | |
| 2955 | // EXPERIMENTAL: this endpoint is experimental and is subject to change. |
| 2956 | // |
no test coverage detected