aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/mygram/droid/Post.java
blob: b4211cff4903c528d9e23d4f44d2076da432428f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * mygram-droid: Android TV client for Foldergram.
 * Copyright (C) 2026 Danilo M. <danix@danix.xyz>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 */


package mygram.droid;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/** One Foldergram post: a photo, a video, or a carousel of slides. */
final class Post {
	/** One slide. A single post has exactly one. */
	static final class Media {
		final boolean video;
		final int width, height;
		final long durationMs;
		final String thumbnailUrl, previewUrl;

		Media(JSONObject o, String base) {
			video = "video".equals(o.optString("mediaType"));
			width = o.optInt("width");
			height = o.optInt("height");
			durationMs = o.optLong("durationMs");
			thumbnailUrl = Api.resolve(base, str(o, "thumbnailUrl"));
			previewUrl = Api.resolve(base, str(o, "previewUrl"));
		}
	}

	final int id;
	final String folderSlug, folderName;
	final List<Media> media = new ArrayList<>();

	Post(JSONObject o, String base) {
		id = o.optInt("id");
		folderSlug = o.optString("folderSlug");
		folderName = o.optString("folderName");
		JSONArray items = o.optJSONArray("mediaItems");
		if (items != null)
			for (int i = 0; i < items.length(); i++)
				media.add(new Media(items.optJSONObject(i), base));
		if (media.isEmpty())
			media.add(new Media(o, base));
	}

	/** The posts in a paged response's "items". */
	static List<Post> list(JSONObject page, String base) {
		List<Post> out = new ArrayList<>();
		JSONArray items = page.optJSONArray("items");
		if (items != null)
			for (int i = 0; i < items.length(); i++)
				out.add(new Post(items.optJSONObject(i), base));
		return out;
	}

	/** optString turns JSON null into "null"; this returns null. */
	static String str(JSONObject o, String key) {
		return o.isNull(key) ? null : o.optString(key);
	}
}