xref: /haiku/src/apps/haikudepot/server/PopulatePkgChangelogFromServerProcess.cpp (revision 909af08f4328301fbdef1ffb41f566c3b5bec0c7)
1 /*
2  * Copyright 2016-2024, Andrew Lindesay <apl@lindesay.co.nz>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  *
5  * Note that this file included code earlier from `Model.cpp` and
6  * copyrights have been latterly been carried across in 2024.
7  */
8 
9 
10 #include "PopulatePkgChangelogFromServerProcess.h"
11 
12 #include <Autolock.h>
13 #include <Catalog.h>
14 
15 #include "Logger.h"
16 #include "ServerHelper.h"
17 
18 
19 #undef B_TRANSLATION_CONTEXT
20 #define B_TRANSLATION_CONTEXT "PopulatePkgChangelogFromServerProcess"
21 
22 
23 PopulatePkgChangelogFromServerProcess::PopulatePkgChangelogFromServerProcess(
24 	PackageInfoRef packageInfo, Model* model)
25 	:
26 	fModel(model),
27 	fPackageInfo(packageInfo)
28 {
29 }
30 
31 
32 PopulatePkgChangelogFromServerProcess::~PopulatePkgChangelogFromServerProcess()
33 {
34 }
35 
36 
37 const char*
38 PopulatePkgChangelogFromServerProcess::Name() const
39 {
40 	return "PopulatePkgChangelogFromServerProcess";
41 }
42 
43 
44 const char*
45 PopulatePkgChangelogFromServerProcess::Description() const
46 {
47 	return B_TRANSLATE("Fetching changelog for package");
48 }
49 
50 
51 status_t
52 PopulatePkgChangelogFromServerProcess::RunInternal()
53 {
54 	// TODO; use API spec to code generation techniques instead of this manually written client.
55 
56 	BMessage responsePayload;
57 	BString packageName = fPackageInfo->Name();
58 	status_t result;
59 
60 	result = fModel->GetWebAppInterface()->GetChangelog(packageName, responsePayload);
61 
62 	if (result == B_OK) {
63 		BMessage resultMessage;
64 		BString content;
65 
66 		result = responsePayload.FindMessage("result", &resultMessage);
67 
68 		if (result == B_OK) {
69 			if (resultMessage.FindString("content", &content) == B_OK)
70 				content.Trim();
71 
72 			if (!content.IsEmpty()) {
73 				// TODO; later work to make the `PackageInfo` immutable to avoid this locking
74 				BAutolock locker(fModel->Lock());
75 				fPackageInfo->SetChangelog(content);
76 				HDDEBUG("changelog populated for [%s]", packageName.String());
77 			} else {
78 				HDDEBUG("no changelog present for [%s]", packageName.String());
79 			}
80 		} else {
81 			int32 errorCode = WebAppInterface::ErrorCodeFromResponse(responsePayload);
82 
83 			if (errorCode != ERROR_CODE_NONE)
84 				ServerHelper::NotifyServerJsonRpcError(responsePayload);
85 		}
86 	} else {
87 		ServerHelper::NotifyTransportError(result);
88 	}
89 
90 	return result;
91 }
92