Skip to content

Commit

Permalink
check resharing
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Feb 17, 2025
1 parent 097a1c2 commit bd3ef72
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
if (content == null) {
throw new IllegalArgumentException(PARAM_NOTE_ID + " is not given, argument " + PARAM_NEWNOTE + " is missing and " + PARAM_CONTENT + " is missing.");
} else {
note = new Note(-1, null, Calendar.getInstance(), NoteUtil.generateNoteTitle(content), content, getString(R.string.category_readonly), false, null, DBStatus.VOID, -1, "", 0, false);
note = new Note(-1, null, Calendar.getInstance(), NoteUtil.generateNoteTitle(content), content, getString(R.string.category_readonly), false, null, DBStatus.VOID, -1, "", 0, false, false);
requireActivity().runOnUiThread(() -> onNoteLoaded(note));
requireActivity().invalidateOptionsMenu();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ private void launchNewNote() {
if (content == null) {
content = "";
}
final var newNote = new Note(null, Calendar.getInstance(), NoteUtil.generateNonEmptyNoteTitle(content, this), content, categoryTitle, favorite, null, false);
final var newNote = new Note(null, Calendar.getInstance(), NoteUtil.generateNonEmptyNoteTitle(content, this), content, categoryTitle, favorite, null, false, false);
fragment = getNewNoteFragment(newNote);
replaceFragment();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ public NotesListWidgetData getNoteListWidgetData(int appWidgetId) {
@NonNull
@MainThread
public LiveData<Note> addNoteAndSync(Account account, Note note) {
final var entity = new Note(0, null, note.getModified(), note.getTitle(), note.getContent(), note.getCategory(), note.getFavorite(), note.getETag(), DBStatus.LOCAL_EDITED, account.getId(), generateNoteExcerpt(note.getContent(), note.getTitle()), 0, note.isShared());
final var entity = new Note(0, null, note.getModified(), note.getTitle(), note.getContent(), note.getCategory(), note.getFavorite(), note.getETag(), DBStatus.LOCAL_EDITED, account.getId(), generateNoteExcerpt(note.getContent(), note.getTitle()), 0, note.isShared(), note.getReadonly());
final var ret = new MutableLiveData<Note>();
executor.submit(() -> ret.postValue(addNote(account.getId(), entity)));
return map(ret, newNote -> {
Expand Down Expand Up @@ -508,7 +508,7 @@ public Note addNote(long accountId, @NonNull Note note) {

@MainThread
public LiveData<Note> moveNoteToAnotherAccount(Account account, @NonNull Note note) {
final var fullNote = new Note(null, note.getModified(), note.getTitle(), note.getContent(), note.getCategory(), note.getFavorite(), null, note.isShared());
final var fullNote = new Note(null, note.getModified(), note.getTitle(), note.getContent(), note.getCategory(), note.getFavorite(), null, note.isShared(), note.getReadonly());
fullNote.setStatus(DBStatus.LOCAL_EDITED);
deleteNoteAndSync(account, note.getId());
return addNoteAndSync(account, fullNote);
Expand Down Expand Up @@ -570,7 +570,7 @@ public Note updateNoteAndSync(@NonNull Account localAccount, @NonNull Note oldNo
// https://github.com/nextcloud/notes-android/issues/1198
@Nullable final Long remoteId = db.getNoteDao().getRemoteId(oldNote.getId());
if (newContent == null) {
newNote = new Note(oldNote.getId(), remoteId, oldNote.getModified(), oldNote.getTitle(), oldNote.getContent(), oldNote.getCategory(), oldNote.getFavorite(), oldNote.getETag(), DBStatus.LOCAL_EDITED, localAccount.getId(), oldNote.getExcerpt(), oldNote.getScrollY(), oldNote.isShared());
newNote = new Note(oldNote.getId(), remoteId, oldNote.getModified(), oldNote.getTitle(), oldNote.getContent(), oldNote.getCategory(), oldNote.getFavorite(), oldNote.getETag(), DBStatus.LOCAL_EDITED, localAccount.getId(), oldNote.getExcerpt(), oldNote.getScrollY(), oldNote.isShared(), oldNote.getReadonly());
} else {
final String title;
if (newTitle != null) {
Expand All @@ -584,7 +584,7 @@ public Note updateNoteAndSync(@NonNull Account localAccount, @NonNull Note oldNo
title = oldNote.getTitle();
}
}
newNote = new Note(oldNote.getId(), remoteId, Calendar.getInstance(), title, newContent, oldNote.getCategory(), oldNote.getFavorite(), oldNote.getETag(), DBStatus.LOCAL_EDITED, localAccount.getId(), generateNoteExcerpt(newContent, title), oldNote.getScrollY(), oldNote.isShared());
newNote = new Note(oldNote.getId(), remoteId, Calendar.getInstance(), title, newContent, oldNote.getCategory(), oldNote.getFavorite(), oldNote.getETag(), DBStatus.LOCAL_EDITED, localAccount.getId(), generateNoteExcerpt(newContent, title), oldNote.getScrollY(), oldNote.isShared(), oldNote.getReadonly());
}
int rows = db.getNoteDao().updateNote(newNote);
// if data was changed, set new status and schedule sync (with callback); otherwise invoke callback directly.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ public interface NoteDao {
String getNoteById = "SELECT * FROM NOTE WHERE id = :id";
String count = "SELECT COUNT(*) FROM NOTE WHERE status != 'LOCAL_DELETED' AND accountId = :accountId";
String countFavorites = "SELECT COUNT(*) FROM NOTE WHERE status != 'LOCAL_DELETED' AND accountId = :accountId AND favorite = 1";
String searchRecentByModified = "SELECT id, remoteId, accountId, title, favorite, isShared, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) ORDER BY favorite DESC, modified DESC";
String searchRecentLexicographically = "SELECT id, remoteId, accountId, title, favorite, isShared, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) ORDER BY favorite DESC, title COLLATE LOCALIZED ASC";
String searchFavoritesByModified = "SELECT id, remoteId, accountId, title, favorite, isShared, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND favorite = 1 ORDER BY modified DESC";
String searchFavoritesLexicographically = "SELECT id, remoteId, accountId, title, favorite, isShared, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND favorite = 1 ORDER BY title COLLATE LOCALIZED ASC";
String searchUncategorizedByModified = "SELECT id, remoteId, accountId, title, favorite, isShared, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND category = '' ORDER BY favorite DESC, modified DESC";
String searchUncategorizedLexicographically = "SELECT id, remoteId, accountId, title, favorite, isShared, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND category = '' ORDER BY favorite DESC, title COLLATE LOCALIZED ASC";
String searchCategoryByModified = "SELECT id, remoteId, accountId, title, favorite, isShared, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND (category = :category OR category LIKE :category || '/%') ORDER BY category, favorite DESC, modified DESC";
String searchCategoryLexicographically = "SELECT id, remoteId, accountId, title, favorite, isShared, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND (category = :category OR category LIKE :category || '/%') ORDER BY category, favorite DESC, title COLLATE LOCALIZED ASC";
String searchRecentByModified = "SELECT id, remoteId, accountId, title, favorite, isShared, readonly, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) ORDER BY favorite DESC, modified DESC";
String searchRecentLexicographically = "SELECT id, remoteId, accountId, title, favorite, isShared, readonly, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) ORDER BY favorite DESC, title COLLATE LOCALIZED ASC";
String searchFavoritesByModified = "SELECT id, remoteId, accountId, title, favorite, isShared, readonly, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND favorite = 1 ORDER BY modified DESC";
String searchFavoritesLexicographically = "SELECT id, remoteId, accountId, title, favorite, isShared, readonly, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND favorite = 1 ORDER BY title COLLATE LOCALIZED ASC";
String searchUncategorizedByModified = "SELECT id, remoteId, accountId, title, favorite, isShared, readonly, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND category = '' ORDER BY favorite DESC, modified DESC";
String searchUncategorizedLexicographically = "SELECT id, remoteId, accountId, title, favorite, isShared, readonly, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND category = '' ORDER BY favorite DESC, title COLLATE LOCALIZED ASC";
String searchCategoryByModified = "SELECT id, remoteId, accountId, title, favorite, isShared, readonly, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND (category = :category OR category LIKE :category || '/%') ORDER BY category, favorite DESC, modified DESC";
String searchCategoryLexicographically = "SELECT id, remoteId, accountId, title, favorite, isShared, readonly, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND (category = :category OR category LIKE :category || '/%') ORDER BY category, favorite DESC, title COLLATE LOCALIZED ASC";

@Query(getNoteById)
LiveData<Note> getNoteById$(long id);
Expand Down Expand Up @@ -141,7 +141,7 @@ public interface NoteDao {
* Gets a list of {@link Note} objects with filled {@link Note#id} and {@link Note#remoteId},
* where {@link Note#remoteId} is not <code>null</code>
*/
@Query("SELECT id, remoteId, 0 as accountId, '' as title, 0 as favorite, 0 as isShared, '' as excerpt, 0 as modified, '' as eTag, 0 as status, '' as category, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND remoteId IS NOT NULL")
@Query("SELECT id, remoteId, 0 as accountId, '' as title, 0 as favorite, 0 as isShared, 0 as readonly, '' as excerpt, 0 as modified, '' as eTag, 0 as status, '' as category, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND remoteId IS NOT NULL")
List<Note> getRemoteIdAndId(long accountId);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
@Index(name = "IDX_NOTE_CATEGORY", value = "category"),
@Index(name = "IDX_NOTE_FAVORITE", value = "favorite"),
@Index(name = "IDX_NOTE_IS_SHARED", value = "isShared"),
@Index(name = "IDX_READONLY", value = "readonly"),
@Index(name = "IDX_NOTE_MODIFIED", value = "modified"),
@Index(name = "IDX_NOTE_REMOTEID", value = "remoteId"),
@Index(name = "IDX_NOTE_STATUS", value = "status")
Expand Down Expand Up @@ -86,6 +87,10 @@ public class Note implements Serializable, Item {
@ColumnInfo(defaultValue = "0")
private boolean isShared = false;

@Expose
@ColumnInfo(defaultValue = "0")
private boolean readonly = false;

@Expose
@Nullable
@SerializedName("etag")
Expand All @@ -103,7 +108,7 @@ public Note() {
}

@Ignore
public Note(@Nullable Long remoteId, @Nullable Calendar modified, @NonNull String title, @NonNull String content, @NonNull String category, boolean favorite, @Nullable String eTag, boolean isShared) {
public Note(@Nullable Long remoteId, @Nullable Calendar modified, @NonNull String title, @NonNull String content, @NonNull String category, boolean favorite, @Nullable String eTag, boolean isShared, boolean readonly) {
this.remoteId = remoteId;
this.title = title;
this.modified = modified;
Expand All @@ -115,8 +120,8 @@ public Note(@Nullable Long remoteId, @Nullable Calendar modified, @NonNull Strin
}

@Ignore
public Note(long id, @Nullable Long remoteId, @Nullable Calendar modified, @NonNull String title, @NonNull String content, @NonNull String category, boolean favorite, @Nullable String etag, @NonNull DBStatus status, long accountId, @NonNull String excerpt, int scrollY, boolean isShared) {
this(remoteId, modified, title, content, category, favorite, etag, isShared);
public Note(long id, @Nullable Long remoteId, @Nullable Calendar modified, @NonNull String title, @NonNull String content, @NonNull String category, boolean favorite, @Nullable String etag, @NonNull DBStatus status, long accountId, @NonNull String excerpt, int scrollY, boolean isShared, boolean readonly) {
this(remoteId, modified, title, content, category, favorite, etag, isShared, readonly);
this.id = id;
this.status = status;
this.accountId = accountId;
Expand Down Expand Up @@ -202,6 +207,14 @@ public void setContent(@NonNull String content) {
this.content = content;
}

public boolean getReadonly() {
return readonly;
}

public void setReadonly(boolean value) {
readonly = value;
}

public boolean getFavorite() {
return favorite;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ public Capabilities deserialize(JsonElement json, Type typeOfT, JsonDeserializat
final var password = publicObject.getAsJsonObject("password");
final var enforced = password.getAsJsonPrimitive("enforced");
final var askForOptionalPassword = password.getAsJsonPrimitive("askForOptionalPassword");
final var isReSharingAllowed = filesSharing.getAsJsonPrimitive("resharing");

response.setPublicPasswordEnforced(enforced.getAsBoolean());
response.setAskForOptionalPassword(askForOptionalPassword.getAsBoolean());
response.setReSharingAllowed(isReSharingAllowed.getAsBoolean());
}

if (capabilities.has(CAPABILITIES_NOTES)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;

import androidx.activity.result.ActivityResultLauncher;
Expand Down Expand Up @@ -114,7 +115,12 @@ private void initializeArguments() {
binding.pickContactEmailBtn.setOnClickListener(v -> checkContactPermission());
binding.btnShareButton.setOnClickListener(v -> ShareUtil.openShareDialog(this, note.getTitle(), note.getContent()));

setupSearchView((SearchManager) getSystemService(Context.SEARCH_SERVICE), getComponentName());
if (note.getReadonly()) {
setupReadOnlySearchView();
} else {
setupSearchView((SearchManager) getSystemService(Context.SEARCH_SERVICE), getComponentName());
}

refreshCapabilitiesFromDB();
refreshSharesFromDB();
});
Expand All @@ -136,6 +142,24 @@ public void onStop() {
UsersAndGroupsSearchConfig.INSTANCE.reset();
}

private void disableSearchView(View view) {
view.setEnabled(false);

if (view instanceof ViewGroup viewGroup) {
for (int i = 0; i < viewGroup.getChildCount(); i++) {
disableSearchView(viewGroup.getChildAt(i));
}
}
}

private void setupReadOnlySearchView() {
binding.searchView.setIconifiedByDefault(false);
binding.searchView.setQueryHint(getResources().getString(R.string.note_share_activity_resharing_not_allowed));
binding.searchView.setInputType(InputType.TYPE_NULL);
binding.pickContactEmailBtn.setVisibility(View.GONE);
disableSearchView(binding.searchView);
}

private void setupSearchView(@Nullable SearchManager searchManager, ComponentName componentName) {
if (searchManager == null) {
binding.searchView.setVisibility(View.GONE);
Expand Down Expand Up @@ -182,24 +206,26 @@ public boolean onQueryTextChange(String newText) {
}

final var isFederationShareAllowed = capabilities.getFederationShare();
try(var cursor = provider.searchForUsersOrGroups(newText, isFederationShareAllowed)) {
runOnUiThread(() -> {
{
if (cursor == null || cursor.getCount() == 0) {
return;
}
try {
var cursor = provider.searchForUsersOrGroups(newText, isFederationShareAllowed);

if (binding.searchView.getVisibility() == View.VISIBLE) {
suggestionAdapter.swapCursor(cursor);
}
if (cursor == null || cursor.getCount() == 0) {
return;
}

binding.progressBar.setVisibility(View.GONE);
runOnUiThread(() -> {
if (binding.searchView.getVisibility() == View.VISIBLE) {
suggestionAdapter.swapCursor(cursor);
}

binding.progressBar.setVisibility(View.GONE);
});

} catch (Exception e) {
Log_OC.d(TAG, "Exception setupSearchView.onQueryTextChange: " + e);
runOnUiThread(() -> binding.progressBar.setVisibility(View.GONE));
}

}, SEARCH_DELAY_MS, TimeUnit.MILLISECONDS);

return false;
Expand Down Expand Up @@ -500,7 +526,8 @@ public void onSaveInstanceState(@NonNull Bundle outState) {
}

private boolean isReshareForbidden(OCShare share) {
return false;
return ShareType.FEDERATED == share.getShareType() ||
capabilities != null && !capabilities.isReSharingAllowed();
}

@VisibleForTesting
Expand Down
Loading

0 comments on commit bd3ef72

Please sign in to comment.