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