Skip to content

Fixes invalid authorname in explore #4054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions app/src/main/java/fr/free/nrw/commons/Media.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ class Media constructor(
val licenseUrl: String? = null,
/**
* Gets the name of the creator of the file.
* @return creator name as a String
* @return author name as a String
*/
/**
* Sets the creator name of the file.
* @param creator creator name as a string
* Sets the author name of the file.
* @param author creator name as a string
*/
var creator: String? = null,
var author: String? = null,

var user:String?=null,

/**
* Gets the categories the file falls under.
Expand All @@ -83,12 +85,13 @@ class Media constructor(
categories: List<String>?,
filename: String?,
fallbackDescription: String?,
creator: String?
author: String?, user:String?
) : this(
filename = filename,
fallbackDescription = fallbackDescription,
dateUploaded = Date(),
creator = creator,
author = author,
user=user,
categories = categories,
captions = captions
)
Expand Down
12 changes: 0 additions & 12 deletions app/src/main/java/fr/free/nrw/commons/auth/SessionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class SessionManager {
private final Context context;
private Account currentAccount; // Unlike a savings account... ;-)
private JsonKvStore defaultKvStore;
private static final String KEY_RAWUSERNAME = "rawusername";

@Inject
public SessionManager(Context context,
Expand Down Expand Up @@ -99,17 +98,6 @@ public String getUserName() {
return account == null ? null : account.name;
}

@Nullable
private String getRawUserName() {
Account account = getCurrentAccount();
return account == null ? null : accountManager().getUserData(account, KEY_RAWUSERNAME);
}

public String getAuthorName(){
return getRawUserName() == null ? getUserName() : getRawUserName();
}


@Nullable
public String getPassword() {
Account account = getCurrentAccount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

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

import fr.free.nrw.commons.Media;
import fr.free.nrw.commons.R;
Expand Down Expand Up @@ -87,9 +86,9 @@ public View getView(int position, View convertView, ViewGroup parent) {
Media item = data.get(position);
SimpleDraweeView imageView = convertView.findViewById(R.id.categoryImageView);
TextView fileName = convertView.findViewById(R.id.categoryImageTitle);
TextView author = convertView.findViewById(R.id.categoryImageAuthor);
TextView uploader = convertView.findViewById(R.id.categoryImageAuthor);
fileName.setText(item.getMostRelevantCaption());
setAuthorView(item, author);
setUploaderView(item, uploader);
imageView.setImageURI(item.getThumbUrl());
return convertView;
}
Expand All @@ -107,14 +106,14 @@ public Media getItem(int position) {
/**
* Shows author information if its present
* @param item
* @param author
* @param uploader
*/
private void setAuthorView(Media item, TextView author) {
if (!TextUtils.isEmpty(item.getCreator())) {
author.setVisibility(View.VISIBLE);
author.setText(getContext().getString(R.string.image_uploaded_by, item.getCreator()));
private void setUploaderView(Media item, TextView uploader) {
if (!TextUtils.isEmpty(item.getAuthor())) {
uploader.setVisibility(View.VISIBLE);
uploader.setText(getContext().getString(R.string.image_uploaded_by, item.getUser()));
} else {
author.setVisibility(View.GONE);
uploader.setVisibility(View.GONE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ data class Contribution constructor(
categories,
item.fileName,
formatDescriptions(item.uploadMediaDetails),
sessionManager.authorName
sessionManager.userName,
sessionManager.userName
),
localUri = item.mediaUri,
decimalCoords = item.gpsCoords.decimalCoords,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.contributions.ContributionsListAdapter.Callback;
import fr.free.nrw.commons.media.MediaClient;
import fr.free.nrw.commons.utils.ViewUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
Expand Down Expand Up @@ -68,7 +67,7 @@ public void init(final int position, final Contribution contribution) {
this.contribution = contribution;
this.position = position;
titleView.setText(contribution.getMedia().getMostRelevantCaption());
authorView.setText(contribution.getMedia().getCreator());
authorView.setText(contribution.getMedia().getAuthor());

imageView.getHierarchy().setPlaceholderImage(R.drawable.image_placeholder);
imageView.getHierarchy().setFailureImage(R.drawable.image_placeholder);
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/fr/free/nrw/commons/db/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import fr.free.nrw.commons.contributions.ContributionDao
* The database for accessing the respective DAOs
*
*/
@Database(entities = [Contribution::class], version = 6, exportSchema = false)
@Database(entities = [Contribution::class], version = 7, exportSchema = false)
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun contributionDao(): ContributionDao
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,10 @@ private Observable<Boolean> delete(Media media, String reason) {
String userPageString = "\n{{subst:idw|" + media.getFilename() +
"}} ~~~~";

String creator = media.getCreator();
String creator = media.getAuthor();
if (creator == null || creator.isEmpty()) {
throw new RuntimeException("Failed to nominate for deletion");
}
String creatorName = creator.replace(" (page does not exist)", "");

return pageEditClient.prependEdit(media.getFilename(), fileDeleteString + "\n", summary)
.flatMap(result -> {
Expand All @@ -113,7 +112,7 @@ private Observable<Boolean> delete(Media media, String reason) {
throw new RuntimeException("Failed to nominate for deletion");
}).flatMap(result -> {
if (result) {
return pageEditClient.appendEdit("User_Talk:" + creatorName, userPageString + "\n", summary);
return pageEditClient.appendEdit("User_Talk:" + creator, userPageString + "\n", summary);
}
throw new RuntimeException("Failed to nominate for deletion");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class MediaConverter @Inject constructor() {
safeParseDate(metadata.dateTime()),
metadata.licenseShortName(),
metadata.prefixedLicenseUrl,
getArtist(metadata),
getAuthor(metadata),
imageInfo.user,
MediaDataExtractorUtil.extractCategoriesFromList(metadata.categories),
metadata.latLng,
entity.labels().mapValues { it.value.value() },
Expand Down Expand Up @@ -61,11 +62,16 @@ class MediaConverter @Inject constructor() {
* @param metadata
* @return
*/
private fun getArtist(metadata: ExtMetadata): String? {
private fun getAuthor(metadata: ExtMetadata): String? {
return try {
val artistHtml = metadata.artist()
artistHtml.substring(artistHtml.indexOf("title=\""), artistHtml.indexOf("\">"))
.replace("title=\"User:", "")
val authorHtml = metadata.artist()
val anchorStartTagTerminalChars = "\">"
val anchorCloseTag = "</a>"

return authorHtml.substring(
authorHtml.indexOf(anchorStartTagTerminalChars) + anchorStartTagTerminalChars
.length, authorHtml.indexOf(anchorCloseTag)
)
} catch (ex: java.lang.Exception) {
""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ class SearchImagesViewHolder(containerView: View, val onImageClicked: (Int) -> U
categoryImageView.setOnClickListener { onImageClicked(item.second) }
categoryImageTitle.text = media.mostRelevantCaption
categoryImageView.setImageURI(media.thumbUrl)
if (media.creator?.isNotEmpty() == true) {
if (media.author?.isNotEmpty() == true) {
categoryImageAuthor.visibility = View.VISIBLE
categoryImageAuthor.text =
containerView.context.getString(R.string.image_uploaded_by, media.creator)
containerView.context.getString(R.string.image_uploaded_by, media.user)
} else {
categoryImageAuthor.visibility = View.GONE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -66,7 +65,6 @@
import fr.free.nrw.commons.nearby.Label;
import fr.free.nrw.commons.ui.widget.HtmlTextView;
import fr.free.nrw.commons.utils.ViewUtilWrapper;
import io.reactivex.Observable;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
Expand Down Expand Up @@ -542,10 +540,10 @@ private void setTextFields(Media media) {

updateCategoryList();

if (media.getCreator() == null || media.getCreator().equals("")) {
if (media.getAuthor() == null || media.getAuthor().equals("")) {
authorLayout.setVisibility(GONE);
} else {
author.setText(media.getCreator());
author.setText(media.getAuthor());
}
}

Expand Down Expand Up @@ -679,7 +677,7 @@ public void updateCategories(List<String> selectedCategories) {
@SuppressLint("StringFormatInvalid")
@OnClick(R.id.nominateDeletion)
public void onDeleteButtonClicked(){
if (AccountUtil.getUserName(getContext()) != null && AccountUtil.getUserName(getContext()).equals(media.getCreator())) {
if (AccountUtil.getUserName(getContext()) != null && AccountUtil.getUserName(getContext()).equals(media.getAuthor())) {
final ArrayAdapter<String> languageAdapter = new ArrayAdapter<>(getActivity(),
R.layout.simple_spinner_dropdown_list, reasonList);
final Spinner spinner = new Spinner(getActivity());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Initialize bookmark object
bookmark = new Bookmark(
m.getFilename(),
m.getCreator(),
m.getAuthor(),
BookmarkPicturesContentProvider.uriForName(m.getFilename())
);
updateBookmarkState(menu.findItem(R.id.menu_bookmark_current_image));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Interface for interacting with Commons media related APIs
*/
public interface MediaInterface {
String MEDIA_PARAMS="&prop=imageinfo&iiprop=url|extmetadata&iiurlwidth=640" +
String MEDIA_PARAMS="&prop=imageinfo&iiprop=url|extmetadata|user&&iiurlwidth=640" +
"&iiextmetadatafilter=DateTime|Categories|GPSLatitude|GPSLongitude|ImageDescription|DateTimeOriginal" +
"|Artist|LicenseShortName|LicenseUrl";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public String createFrom(Contribution contribution) {
.append("{{Information\n")
.append("|description=").append(media.getFallbackDescription()).append("\n")
.append("|source=").append("{{own}}\n")
.append("|author=[[User:").append(media.getCreator()).append("|")
.append(media.getCreator()).append("]]\n");
.append("|author=[[User:").append(media.getAuthor()).append("|")
.append(media.getAuthor()).append("]]\n");

String templatizedCreatedDate = getTemplatizedCreatedDate(
contribution.getDateCreated(), contribution.getDateCreatedSource());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,18 @@ public void startUpload(final Contribution contribution) {
final Media media = contribution.getMedia();
if (store.getBoolean("useAuthorName", false)) {
final String authorName = store.getString("authorName", "");
media.setCreator(authorName);
media.setAuthor(authorName);
}

if (TextUtils.isEmpty(media.getCreator())) {
if (TextUtils.isEmpty(media.getAuthor())) {
final Account currentAccount = sessionManager.getCurrentAccount();
if (currentAccount == null) {
Timber.d("Current account is null");
ViewUtil.showLongToast(context, context.getString(R.string.user_not_logged_in));
sessionManager.forceLogin(context);
return;
}
media.setCreator(sessionManager.getAuthorName());
media.setAuthor(sessionManager.getUserName());
}

if (media.getFallbackDescription() == null) {
Expand Down
6 changes: 4 additions & 2 deletions app/src/test/kotlin/ModelFunctions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ fun media(
dateUploaded: Date? = Date(),
license: String? = "license",
licenseUrl: String? = "licenseUrl",
creator: String? = "creator",
author: String? = "creator",
user:String?="user",
pageId: String = "pageId",
categories: List<String>? = listOf("categories"),
coordinates: LatLng? = LatLng(0.0, 0.0, 0.0f),
Expand All @@ -56,7 +57,8 @@ fun media(
dateUploaded,
license,
licenseUrl,
creator,
author,
user,
categories,
coordinates,
captions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class DeleteHelperTest {
whenever(media.displayTitle).thenReturn("Test file")

val creatorName = "Creator"
whenever(media.creator).thenReturn("$creatorName (page does not exist)")
whenever(media.author).thenReturn("$creatorName")
whenever(media.filename).thenReturn("Test file.jpg")

val makeDeletion = deleteHelper.makeDeletion(context, media, "Test reason")?.blockingGet()
Expand All @@ -78,7 +78,7 @@ class DeleteHelperTest {
.thenReturn(Observable.just(true))
whenever(media.displayTitle).thenReturn("Test file")
whenever(media.filename).thenReturn("Test file.jpg")
whenever(media.creator).thenReturn("Creator (page does not exist)")
whenever(media.author).thenReturn("Creator (page does not exist)")

deleteHelper.makeDeletion(context, media, "Test reason")?.blockingGet()
}
Expand All @@ -93,7 +93,7 @@ class DeleteHelperTest {
.thenReturn(Observable.just(false))
whenever(media.displayTitle).thenReturn("Test file")
whenever(media.filename).thenReturn("Test file.jpg")
whenever(media.creator).thenReturn("Creator (page does not exist)")
whenever(media.author).thenReturn("Creator (page does not exist)")

deleteHelper.makeDeletion(context, media, "Test reason")?.blockingGet()
}
Expand All @@ -108,7 +108,7 @@ class DeleteHelperTest {
.thenReturn(Observable.just(true))
whenever(media.displayTitle).thenReturn("Test file")
whenever(media.filename).thenReturn("Test file.jpg")
whenever(media.creator).thenReturn("Creator (page does not exist)")
whenever(media.author).thenReturn("Creator (page does not exist)")

deleteHelper.makeDeletion(context, media, "Test reason")?.blockingGet()
}
Expand All @@ -125,7 +125,7 @@ class DeleteHelperTest {
whenever(media.displayTitle).thenReturn("Test file")
whenever(media.filename).thenReturn("Test file.jpg")

whenever(media.creator).thenReturn(null)
whenever(media.author).thenReturn(null)

deleteHelper.makeDeletion(context, media, "Test reason")?.blockingGet()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class UploadControllerTest {
val contribution = mock(Contribution::class.java)
val media = mock<Media>()
whenever(contribution.media).thenReturn(media)
whenever(media.creator).thenReturn("Creator")
whenever(media.author).thenReturn("Creator")
uploadController!!.startUpload(contribution)
}
}