1 /* 2 * Copyright 2014, Stephan Aßmus <superstippi@gmx.de>. 3 * Copyright 2016-2022, 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 const BString& webAppRepositorySourceCode, 61 int resultOffset, int maxResults, 62 BMessage& message); 63 64 status_t RetreiveUserRatingForPackageAndVersionByUser( 65 const BString& packageName, 66 const BPackageVersion& version, 67 const BString& architecture, 68 const BString& webAppRepositoryCode, 69 const BString& webAppRepositorySourceCode, 70 const BString& userNickname, 71 BMessage& message); 72 73 status_t CreateUserRating( 74 const BString& packageName, 75 const BPackageVersion& version, 76 const BString& architecture, 77 const BString& webAppRepositoryCode, 78 const BString& webAppRepositorySourceCode, 79 const BString& languageCode, 80 const BString& comment, 81 const BString& stability, 82 int rating, 83 BMessage& message); 84 85 status_t UpdateUserRating( 86 const BString& ratingID, 87 const BString& languageCode, 88 const BString& comment, 89 const BString& stability, 90 int rating, bool active, 91 BMessage& message); 92 93 status_t RetrieveUserDetailForCredentials( 94 const UserCredentials& credentials, 95 BMessage& message); 96 97 status_t RetrieveCurrentUserDetail( 98 BMessage& message); 99 100 status_t RetrieveUserUsageConditions( 101 const BString& code, 102 UserUsageConditions& conditions); 103 104 status_t AgreeUserUsageConditions(const BString& code, 105 BMessage& responsePayload); 106 107 status_t RetrieveScreenshot( 108 const BString& code, 109 int32 width, int32 height, 110 BDataIO* stream); 111 112 status_t RequestCaptcha(BMessage& message); 113 114 status_t CreateUser(const BString& nickName, 115 const BString& passwordClear, 116 const BString& email, 117 const BString& captchaToken, 118 const BString& captchaResponse, 119 const BString& languageCode, 120 const BString& userUsageConditionsCode, 121 BMessage& message); 122 123 status_t AuthenticateUser(const BString& nickName, 124 const BString& passwordClear, 125 BMessage& message); 126 127 status_t IncrementViewCounter( 128 const PackageInfoRef package, 129 const DepotInfoRef depot, 130 BMessage& message); 131 132 static int32 ErrorCodeFromResponse( 133 BMessage& responseEnvelopeMessage); 134 135 static status_t UnpackUserDetail( 136 BMessage& responseEnvelopeMessage, 137 UserDetail& userDetail); 138 private: 139 140 141 status_t _RetrieveUserUsageConditionsMeta( 142 const BString& code, BMessage& message); 143 status_t _RetrieveUserUsageConditionsCopy( 144 const BString& code, BDataIO* stream); 145 146 status_t _SendJsonRequest(const char* urlPathComponents, 147 const BString& jsonString, uint32 flags, 148 BMessage& reply) const; 149 status_t _SendJsonRequest(const char* urlPathComponents, 150 UserCredentials credentials, 151 BPositionIO* requestData, 152 size_t requestDataSize, uint32 flags, 153 BMessage& reply) const; 154 status_t _SendJsonRequest(const char* urlPathComponents, 155 BPositionIO* requestData, 156 size_t requestDataSize, uint32 flags, 157 BMessage& reply) const; 158 159 status_t _SendRawGetRequest( 160 const BString urlPathComponents, 161 BDataIO* stream); 162 static void _LogPayload(BPositionIO* requestData, 163 size_t size); 164 static off_t _LengthAndSeekToZero(BPositionIO* data); 165 166 private: 167 UserCredentials fCredentials; 168 static int fRequestIndex; 169 }; 170 171 172 #endif // WEB_APP_INTERFACE_H 173