|
| 1 | +BEGIN; |
| 2 | + |
| 3 | +-- accept friendship request |
| 4 | + |
| 5 | +CREATE OR REPLACE FUNCTION vibetype.friendship_accept( |
| 6 | + requestor_account_id UUID |
| 7 | +) RETURNS VOID AS $$ |
| 8 | +DECLARE |
| 9 | + _friend_account_id UUID; |
| 10 | + _count INTEGER; |
| 11 | +BEGIN |
| 12 | + |
| 13 | + _friend_account_id := vibetype.invoker_account_id(); |
| 14 | + |
| 15 | + UPDATE vibetype.friendship SET |
| 16 | + status = 'accepted'::vibetype.friendship_status |
| 17 | + -- updated_by filled by trigger |
| 18 | + WHERE account_id = requestor_account_id AND friend_account_id = _friend_account_id |
| 19 | + AND status = 'requested'::vibetype.friendship_status; |
| 20 | + |
| 21 | + GET DIAGNOSTICS _count = ROW_COUNT; |
| 22 | + IF _count = 0 THEN |
| 23 | + RAISE EXCEPTION 'Friendship request does not exist' USING ERRCODE = 'VTFAC'; |
| 24 | + END IF; |
| 25 | + |
| 26 | + INSERT INTO vibetype.friendship(account_id, friend_account_id, status, created_by) |
| 27 | + VALUES (_friend_account_id, requestor_account_id, 'accepted'::vibetype.friendship_status, _friend_account_id); |
| 28 | + |
| 29 | +END; $$ LANGUAGE plpgsql SECURITY INVOKER; |
| 30 | + |
| 31 | +COMMENT ON FUNCTION vibetype.friendship_accept(UUID) IS 'Accepts a friendship request.'; |
| 32 | + |
| 33 | +GRANT EXECUTE ON FUNCTION vibetype.friendship_accept(UUID) TO vibetype_account; |
| 34 | + |
| 35 | +-- reject or cancel friendship |
| 36 | + |
| 37 | +CREATE OR REPLACE FUNCTION vibetype.friendship_cancel( |
| 38 | + friend_account_id UUID |
| 39 | +) RETURNS VOID AS $$ |
| 40 | +DECLARE |
| 41 | + _account_id UUID; |
| 42 | +BEGIN |
| 43 | + |
| 44 | + _account_id := vibetype.invoker_account_id(); |
| 45 | + |
| 46 | + DELETE FROM vibetype.friendship f |
| 47 | + WHERE (account_id = _account_id AND f.friend_account_id = friendship_cancel.friend_account_id) |
| 48 | + OR (account_id = friendship_cancel.friend_account_id AND f.friend_account_id = _account_id); |
| 49 | + |
| 50 | +END; $$ LANGUAGE plpgsql SECURITY INVOKER; |
| 51 | + |
| 52 | +COMMENT ON FUNCTION vibetype.friendship_cancel(UUID) IS 'Rejects or cancels a friendship (in both directions).'; |
| 53 | + |
| 54 | +GRANT EXECUTE ON FUNCTION vibetype.friendship_cancel(UUID) TO vibetype_account; |
| 55 | + |
| 56 | +-- create notification for a request |
| 57 | + |
| 58 | +CREATE OR REPLACE FUNCTION vibetype.friendship_notify_request( |
| 59 | + friend_account_id UUID, |
| 60 | + language TEXT |
| 61 | +) RETURNS VOID AS $$ |
| 62 | +BEGIN |
| 63 | + |
| 64 | + INSERT INTO vibetype_private.notification (channel, payload) |
| 65 | + VALUES ( |
| 66 | + 'friendship_request', |
| 67 | + jsonb_pretty(jsonb_build_object( |
| 68 | + 'data', jsonb_build_object( |
| 69 | + 'requestor_account_id', vibetype.invoker_account_id(), |
| 70 | + 'requestee_account_id', friendship_notify_request.friend_account_id |
| 71 | + ), |
| 72 | + 'template', jsonb_build_object('language', friendship_notify_request.language) |
| 73 | + )) |
| 74 | + ); |
| 75 | + |
| 76 | +END; $$ LANGUAGE plpgsql SECURITY DEFINER; |
| 77 | + |
| 78 | +COMMENT ON FUNCTION vibetype.friendship_notify_request(UUID, TEXT) IS 'Creates a notification for a friendship_request'; |
| 79 | + |
| 80 | +GRANT EXECUTE ON FUNCTION vibetype.friendship_notify_request(UUID, TEXT) TO vibetype_account; |
| 81 | + |
| 82 | +-- request friendship |
| 83 | + |
| 84 | +CREATE OR REPLACE FUNCTION vibetype.friendship_request( |
| 85 | + friend_account_id UUID, |
| 86 | + language TEXT |
| 87 | +) RETURNS VOID AS $$ |
| 88 | +DECLARE |
| 89 | + _account_id UUID; |
| 90 | +BEGIN |
| 91 | + |
| 92 | + _account_id := vibetype.invoker_account_id(); |
| 93 | + |
| 94 | + IF EXISTS( |
| 95 | + SELECT 1 |
| 96 | + FROM vibetype.friendship f |
| 97 | + WHERE (f.account_id = _account_id AND f.friend_account_id = friendship_request.friend_account_id) |
| 98 | + OR (f.account_id = friendship_request.friend_account_id AND f.friend_account_id = _account_id) |
| 99 | + ) |
| 100 | + THEN |
| 101 | + RAISE EXCEPTION 'Friendship already exists or has already been requested.' USING ERRCODE = 'VTREQ'; |
| 102 | + END IF; |
| 103 | + |
| 104 | + INSERT INTO vibetype.friendship(account_id, friend_account_id, status, created_by) |
| 105 | + VALUES (_account_id, friendship_request.friend_account_id, 'requested'::vibetype.friendship_status, _account_id); |
| 106 | + |
| 107 | + PERFORM vibetype.friendship_notify_request(friendship_request.friend_account_id, friendship_request.language); |
| 108 | + |
| 109 | +END; $$ LANGUAGE plpgsql SECURITY INVOKER; |
| 110 | + |
| 111 | +COMMENT ON FUNCTION vibetype.friendship_request(UUID, TEXT) IS 'Starts a new friendship request.'; |
| 112 | + |
| 113 | +GRANT EXECUTE ON FUNCTION vibetype.friendship_request(UUID, TEXT) TO vibetype_account; |
| 114 | + |
| 115 | + |
| 116 | +-- toggle closeness of friendship |
| 117 | + |
| 118 | +CREATE OR REPLACE FUNCTION vibetype.friendship_toggle_closeness( |
| 119 | + friend_account_id UUID |
| 120 | +) RETURNS BOOLEAN AS $$ |
| 121 | +DECLARE |
| 122 | + _account_id UUID; |
| 123 | + _is_close_friend BOOLEAN; |
| 124 | + current_status vibetype.friendship_status; |
| 125 | +BEGIN |
| 126 | + |
| 127 | + _account_id := vibetype.invoker_account_id(); |
| 128 | + |
| 129 | + SELECT status INTO current_status |
| 130 | + FROM vibetype.friendship f |
| 131 | + WHERE f.account_id = _account_id AND f.friend_account_id = friendship_toggle_closeness.friend_account_id; |
| 132 | + |
| 133 | + IF current_status IS NULL OR current_status != 'accepted'::vibetype.friendship_status THEN |
| 134 | + RAISE EXCEPTION 'Friendship request does not exist' USING ERRCODE = 'VTFTC'; |
| 135 | + END IF; |
| 136 | + |
| 137 | + UPDATE vibetype.friendship f |
| 138 | + SET is_close_friend = NOT is_close_friend |
| 139 | + WHERE account_id = vibetype.invoker_account_id() |
| 140 | + AND f.friend_account_id = friendship_toggle_closeness.friend_account_id |
| 141 | + RETURNING is_close_friend INTO _is_close_friend; |
| 142 | + |
| 143 | + RETURN _is_close_friend; |
| 144 | + |
| 145 | +END; $$ LANGUAGE plpgsql SECURITY INVOKER; |
| 146 | + |
| 147 | +COMMENT ON FUNCTION vibetype.friendship_toggle_closeness(UUID) IS 'Toggles a frien1dship relation between ''not a close friend'' and ''close friend''.'; |
| 148 | + |
| 149 | +GRANT EXECUTE ON FUNCTION vibetype.friendship_toggle_closeness(UUID) TO vibetype_account; |
| 150 | + |
| 151 | +COMMIT; |
0 commit comments