xref: /haiku/src/apps/haikudepot/server/WebAppInterface.h (revision 68d37cfb3a755a7270d772b505ee15c8b18aa5e0)
1 /*
2  * Copyright 2014, Stephan Aßmus <superstippi@gmx.de>.
3  * Copyright 2016-2020, Andrew Lindesay <apl@lindesay.co.nz>.
4  * All rights reserved. Distributed under the terms of the MIT License.
5  */
6 #ifndef WEB_APP_INTERFACE_H
7 #define WEB_APP_INTERFACE_H
8 
9 
10 #include <Application.h>
11 #include <JsonWriter.h>
12 #include <String.h>
13 #include <package/PackageVersion.h>
14 
15 #include "UserCredentials.h"
16 #include "UserDetail.h"
17 #include "UserUsageConditions.h"
18 
19 
20 class BDataIO;
21 class BMessage;
22 using BPackageKit::BPackageVersion;
23 
24 
25 /*! These are error codes that are sent back to the client from the server */
26 
27 #define ERROR_CODE_NONE							0
28 #define ERROR_CODE_VALIDATION					-32800
29 #define ERROR_CODE_OBJECTNOTFOUND				-32801
30 #define ERROR_CODE_CAPTCHABADRESPONSE			-32802
31 #define ERROR_CODE_AUTHORIZATIONFAILURE			-32803
32 #define ERROR_CODE_BADPKGICON					-32804
33 #define ERROR_CODE_LIMITEXCEEDED				-32805
34 #define ERROR_CODE_AUTHORIZATIONRULECONFLICT	-32806
35 
36 /*! This constant can be used to indicate the lack of a rating. */
37 
38 #define RATING_NONE -1
39 
40 
41 class WebAppInterface {
42 public:
43 								WebAppInterface();
44 								WebAppInterface(const WebAppInterface& other);
45 	virtual						~WebAppInterface();
46 
47 			WebAppInterface&	operator=(const WebAppInterface& other);
48 
49 			void				SetAuthorization(const UserCredentials& value);
50 			const BString&		Nickname() const;
51 
52 			status_t			GetChangelog(
53 									const BString& packageName,
54 									BMessage& message);
55 
56 			status_t			RetreiveUserRatingsForPackageForDisplay(
57 									const BString& packageName,
58 									const BString& webAppRepositoryCode,
59 									int resultOffset, int maxResults,
60 									BMessage& message);
61 
62 			status_t			RetreiveUserRatingForPackageAndVersionByUser(
63 									const BString& packageName,
64 									const BPackageVersion& version,
65 									const BString& architecture,
66 									const BString& repositoryCode,
67 									const BString& userNickname,
68 									BMessage& message);
69 
70 			status_t			CreateUserRating(
71 									const BString& packageName,
72 									const BPackageVersion& version,
73 									const BString& architecture,
74 									const BString& repositoryCode,
75 									const BString& languageCode,
76 									const BString& comment,
77 									const BString& stability,
78 									int rating,
79 									BMessage& message);
80 
81 			status_t			UpdateUserRating(
82 									const BString& ratingID,
83 									const BString& languageCode,
84 									const BString& comment,
85 									const BString& stability,
86 									int rating, bool active,
87 									BMessage& message);
88 
89 			status_t			RetrieveUserDetailForCredentials(
90 									const UserCredentials& credentials,
91 									BMessage& message);
92 
93 			status_t			RetrieveCurrentUserDetail(
94 									BMessage& message);
95 
96 			status_t			RetrieveUserUsageConditions(
97 									const BString& code,
98 									UserUsageConditions& conditions);
99 
100 			status_t			AgreeUserUsageConditions(const BString& code,
101 									BMessage& responsePayload);
102 
103 			status_t			RetrieveScreenshot(
104 									const BString& code,
105 									int32 width, int32 height,
106 									BDataIO* stream);
107 
108 			status_t			RequestCaptcha(BMessage& message);
109 
110 			status_t			CreateUser(const BString& nickName,
111 									const BString& passwordClear,
112 									const BString& email,
113 									const BString& captchaToken,
114 									const BString& captchaResponse,
115 									const BString& languageCode,
116 									const BString& userUsageConditionsCode,
117 									BMessage& message);
118 
119 			status_t			AuthenticateUser(const BString& nickName,
120 									const BString& passwordClear,
121 									BMessage& message);
122 
123 	static	int32				ErrorCodeFromResponse(
124 									BMessage& responseEnvelopeMessage);
125 
126 	static	status_t			UnpackUserDetail(
127 									BMessage& responseEnvelopeMessage,
128 									UserDetail& userDetail);
129 private:
130 
131 
132 			status_t			_RetrieveUserUsageConditionsMeta(
133 									const BString& code, BMessage& message);
134 			status_t			_RetrieveUserUsageConditionsCopy(
135 									const BString& code, BDataIO* stream);
136 
137 			void				_WriteStandardJsonRpcEnvelopeValues(
138 									BJsonWriter& writer,
139 									const char* methodName);
140 			status_t			_SendJsonRequest(const char* domain,
141 									const BString& jsonString, uint32 flags,
142 									BMessage& reply) const;
143 			status_t			_SendJsonRequest(const char* domain,
144 									UserCredentials credentials,
145 									BPositionIO* requestData,
146 									size_t requestDataSize, uint32 flags,
147 									BMessage& reply) const;
148 			status_t			_SendJsonRequest(const char* domain,
149 									BPositionIO* requestData,
150 									size_t requestDataSize, uint32 flags,
151 									BMessage& reply) const;
152 
153 			status_t			_SendRawGetRequest(
154 									const BString urlPathComponents,
155 									BDataIO* stream);
156 	static	void				_LogPayload(BPositionIO* requestData,
157 									size_t size);
158 	static	off_t				_LengthAndSeekToZero(BPositionIO* data);
159 
160 private:
161 			UserCredentials		fCredentials;
162 	static	int					fRequestIndex;
163 };
164 
165 
166 #endif // WEB_APP_INTERFACE_H
167