]> git.plutz.net Git - rawnet/blobdiff - page_channel.sh
For progress frame use chunked encoding instead of long poll
[rawnet] / page_channel.sh
index 57de476c579b17dbc2db175d222bac7aa72694ea..111b6f95241b1a1151845bf8410b9b3c57836be5 100755 (executable)
 #!/bin/sh
 
-chan_db="$_DATA/channels.db"
+["$includepage_channel" ] && return 0
+includepage_channel="$0"
 
-channel='' video='' action=''
-path_info="$PATH_INFO"
-path_info="${path_info#/channel/}"
-if [ "$(checkid "${path_info%%/*}")" ]; then
-  channel="${path_info%%/*}"
-  path_info="${path_info#*/}"
-fi
-if [ "$(checkid "${path_info%%/*}")" ]; then
-  video="${path_info%%/*}"
-  path_info="${path_info#*/}"
-fi
-action="${path_info}"
-unset path_info
-
-# Channel
-# ID   NAME    DESCRIPTION     LOGO    THEME   AUTHORS DESCR_CACHE FUTUREUSE
-
-if [ "$channel" -a -f "$chan_db" -a -r "$chan_db" ]; then
-  read -r CHANNEL_ID CHANNEL_NAME CHANNEL_DESCRIPTION CHANNEL_LOGO \
-          CHANNEL_THEME CHANNEL_AUTHORS CHANNEL_DESCR_CACHE \
-          CHANNEL_FUTUREUSE <<-EOF
-       $(grep "^${channel}     " "${chan_db}")
-       EOF
-  if [ "$CHANNEL_ID" ]; then
-           CHANNEL_NAME="$(UNSTRING "${CHANNEL_NAME}")"
-    CHANNEL_DESCRIPTION="$(UNSTRING "$CHANNEL_DESCRIPTION")"
-        CHANNEL_AUTHORS="$(UNSTRING "$CHANNEL_AUTHORS")"
-    CHANNEL_DESCR_CACHE="$(UNSTRING "$CHANNEL_DESCR_CACHE")"
-    vid_db="${_DATA}/${CHANNEL_ID}/videos.db"
-  else
-    channel=''
-  fi
-fi
-
-update_channel(){
-  local id="${1}" name="${2}" description="${3}" logo="${4}" theme="${5}" \
-        authors="${6}" descr_cache="${7}" futureuse="${8}"
-  local ID INFO
-  if LOCK "$chan_db"; then
-    while read -r ID INFO; do
-      if [ "$id" = "$ID" ]; then
-       printf '%s      %s      %s      %s      %s      %s      %s      %s\n' \
-               "$id" "$(STRING "$name")" "$(STRING "$description")" \
-               "${logo:-\\}" "${theme:-\\}" "$(STRING "$authors")" \
-               "$(printf %s "$description" |markdown |STRING)" \
-               "${futureuse:-\\}"
-      else
-       printf '%s      %s\n' "$ID" "$INFO"
-      fi
-    done <"$chan_db" >"${chan_db}.$$"
-    mv -- "${chan_db}.$$" "${chan_db}"
-    RELEASE "$chan_db"
-  else
-    return 1
-  fi
-}
+. "$_EXEC/db_channel.sh"
+. "$_EXEC/widgets.sh"
 
-# Video
-# ID   NAME    DESCRIPTION     RESX    RESY    LENGTH  COVER   STATUS  UPLOADER        HITS
+read_channel "$channel"
 
 [ "$REQUEST_METHOD" = POST ] && case "$(POST action)" in
   newchannel)
     channel="$(POST channel |checkid)"
     if [ ! "$USER_ID" ]; then
       REDIRECT "${_BASE}/channel/#ERROR_NEWCHANNEL_NOTALLOWED"
-    elif LOCK "$chan_db"; then
-      if grep -q '^${channel}  ' "$chan_db"; then
-        RELEASE "$chan_db"
-        REDIRECT "${_BASE}/channel/#ERROR_NEWCHANNEL_EXISTS"
-      else
-       printf '%s      \\      \\      \\      \\      %s      \\      \\\n' \
-               "$channel" "$(STRING "$USER_ID")" \
-               >>"$chan_db"
-        RELEASE "$chan_db"
-        REDIRECT "${_BASE}/channel/${channel}/edit"
-      fi
+    elif new_channel "$channel"; then
+      REDIRECT "${_BASE}/channel/$channel/edit"
     else
       REDIRECT "${_BASE}/channel/#ERROR_NEWCHANNEL_NOLOCK"
     fi
     ;;
   update_channel)
-    if [ ! "$channel" ]; then
+    authors="${USER_ID}"
+    n="$(POST_COUNT author)"
+    while [ $n -gt 0 ]; do
+      newauthor="$(POST author $n |checkid)"
+      user_idmap "$newauthor" && authors="${authors}${BR}${newauthor}" >&-
+      n=$((n - 1))
+    done
+    n="$(POST_COUNT newauthor)"
+    while [ $n -gt 0 ]; do
+      newauthor="$(POST newauthor $n)"
+      newauthor="$(user_idof "$newauthor")" \
+      && authors="${authors}${BR}${newauthor}"
+      n=$((n - 1))
+    done
+
+    if [ ! "$CHANNEL_ID" ]; then
       REDIRECT "${_BASE}/channel/#ERROR_NOCHANNEL"
     elif [ ! "$USER_ID" ]; then
-      REDIRECT "${_BASE}/channel/${channel}/#ERROR_NOTLOGGEDIN"
-    elif [ "${CHANNEL_AUTHORS##*${USER_ID}*}" ]; then
-      REDIRECT "${_BASE}/channel/${channel}/#ERROR_UPDATE_NOTALLOWED"
-    elif update_channel "$channel" "$(POST name)" "$(POST description)" \
-                        "" "" "$USER_ID" "" ""; then
-      REDIRECT "${_BASE}/channel/${channel}/"
+      REDIRECT "${_BASE}/channel/$CHANNEL_ID/#ERROR_NOTLOGGEDIN"
+    elif ! AUTHOR; then
+      REDIRECT "${_BASE}/channel/$CHANNEL_ID/#ERROR_UPDATE_NOTALLOWED"
+    elif update_channel "$CHANNEL_ID" "name=$(POST name)" \
+                        "description=$(POST description)" \
+                        "authors=${authors}"; then
+      REDIRECT "${_BASE}/channel/$CHANNEL_ID/"
     else
-      REDIRECT "${_BASE}/channel/${channel}/#ERROR_UPDATE_NOLOCK"
+      REDIRECT "${_BASE}/channel/$CHANNEL_ID/#ERROR_UPDATE_NOLOCK"
     fi
     ;;
   update_channel_cancel)
-    REDIRECT "${_BASE}/channel/${channel}/"
+    REDIRECT "${_BASE}/channel/$CHANNEL_ID/"
     ;;
   newvideo)
+    . "$_EXEC/db_video.sh"
     video="$(POST video |checkid)"
 
-    [ "$channel" -a "$USER_ID" -a ! "${CHANNEL_AUTHORS##*${USER_ID}*}" ] \
-    && mkdir -p -- "${_DATA}/${channel}/"
+    AUTHOR \
+    && mkdir -p -- "${_DATA}/$CHANNEL_ID/"
 
     if [ ! "$video" ]; then
-      REDIRECT "${_BASE}/channel/${channel}/#ERROR_INVALID_ID"
-    elif [ ! "$channel" ]; then
+      REDIRECT "${_BASE}/channel/$CHANNEL_ID/#ERROR_INVALID_ID"
+    elif [ ! "$CHANNEL_ID" ]; then
       REDIRECT "${_BASE}/channel/#ERROR_NOCHANNEL"
     elif [ ! "$USER_ID" ]; then
-      REDIRECT "${_BASE}/channel/${channel}/#ERROR_NOTLOGGEDIN"
-    elif [ "${CHANNEL_AUTHORS##*${USER_ID}*}" ]; then
-      REDIRECT "${_BASE}/channel/${channel}/#ERROR_UPDATE_NOTALLOWED"
-    elif LOCK "$vid_db"; then
-      if grep -q '^${video}    ' "$vid_db"; then
-        RELEASE "$vid_db"
-        REDIRECT "${_BASE}/channel/${channel}/#ERROR_NEWVIDEO_EXISTS"
-      else
-               # ID    NAME    DESC    RESX    RESY    LENGTH  COVER   STATUS  UPLOADER HITS   FUTUREUSE
-       printf '%s      \\      \\      \\      \\      \\      \\      private %s      \\      \\\n' \
-               "$video" "$(STRING "$USER_ID")" \
-               >>"$vid_db"
-        RELEASE "$vid_db"
-        REDIRECT "${_BASE}/channel/${channel}/${video}/edit"
-      fi
+      REDIRECT "${_BASE}/channel/$CHANNEL_ID/#ERROR_NOTLOGGEDIN"
+    elif ! AUTHOR; then
+      REDIRECT "${_BASE}/channel/$CHANNEL_ID/#ERROR_UPDATE_NOTALLOWED"
+    elif new_video "$video"; then
+      REDIRECT "${_BASE}/channel/$CHANNEL_ID/$video/"
     else
-      REDIRECT "${_BASE}/channel/${channel}/#ERROR_NEWVIDEO_NOLOCK"
+      REDIRECT "${_BASE}/channel/$CHANNEL_ID/#ERROR_NEWVIDEO_NOLOCK"
     fi
     ;;
 esac
 
-w_video(){
-  local CID="$1" thumb
-  local ID NAME DESCRIPTION RESX RESY LENGTH COVER STATUS UPLOADER HITS DESCR_CACHE FUTUREUSE
-  if read -r ID NAME DESCRIPTION RESX RESY LENGTH COVER STATUS UPLOADER HITS FUTUREUSE; then
-    thumb="${_BASE}/${CID}/thumb_${ID}.jpg"
-    [ "$NAME" = \\ ] && NAME="(Unnamed Video)"
-    cat <<-EOF
-       [div .video
-         [h3 [a href="${ID}/" . $(UNSTRING "$NAME" |HTML)]]
-         [img href="${thumb}" alt="$(UNSTRING "$DESCR_CACHE")"]
-       ]
-       EOF
-  else
-    return 1
-  fi
-}
-
-w_channel(){
-  local vid_db
-  local ID NAME DESCRIPTION LOGO THEME AUTHORS DESCR_CACHE FUTUREUSE
-  if read -r ID NAME DESCRIPTION LOGO THEME AUTHORS DESCR_CACHE FUTUREUSE; then
-    vid_db="${_DATA}/${ID}/videos.db"
-    [ "$NAME" = \\ ] && NAME="(UNNAMED CHANNEL)"
-    cat <<-EOF
-       [div .channel
-         [h2 [a href="${_BASE}/channel/${ID}/" $(UNSTRING "${NAME}" |HTML)]]
-         [div .description . $(UNSTRING "$DESCR_CACHE")]
-         $( [ -f "$vid_db" -a -r "$vid_db" ] \
-            && while w_video "$ID"; do :; done <"$vid_db"
-         )
-       ]
-       EOF
-  else
-    return 1
-  fi
-}
-
-w_channel_list(){
-  if [ $USER_ID ]; then
-    printf '
-    [form .channel .newchannel method=POST
-      [hidden "channel" "%s"]
-      [submit "action" "newchannel" New Channel]
-    ]' "$(timeid)"
-  fi
-  [ -f "$chan_db" -a -r "$chan_db" ] \
-  && while w_channel; do :; done <"$chan_db"
-}
-
-if [ "$channel" -a "$video" ]; then
-  . ${_EXEC}/page_video.sh
-elif [ "$channel" -a "$action" = edit ]; then
-  [ "$USER_ID" -a ! "${CHANNEL_AUTHORS##*${USER_ID}*}" ] \
-  || REDIRECT "${_BASE}/${channel}/#ERROR_EDIT_NOTALLOWED"
+if [ "$CHANNEL_ID" -a "$action" = edit ]; then
+  AUTHOR || REDIRECT "${_BASE}/$CHANNEL_ID/#ERROR_EDIT_NOTALLOWED"
   yield_page "$CHANNEL_NAME - Edit" "channel edit" <<-EOF
+       [datalist #list_authors
+       $(user_idmap |cut -f2 |UNSTRING |while read name; do
+         printf '[option value="%s"]' "$(HTML "$name")"
+       done)
+       ]
        [form .channel .edit method=POST
-         [input name="name" value="$(HTML "$CHANNEL_NAME")" placeholder="Channel Name"]
+         [input name="name" value="$(HTML "$CHANNEL_NAME")" placeholder="Channel Name" autocomplete=off]
          [textarea name="description" placeholder="Description" . $(HTML "$CHANNEL_DESCRIPTION")]
+         [div .authors [h3 Authors:]
+           $(for each in $CHANNEL_AUTHORS; do
+             printf '[checkbox "author" "%s" id="author_%s" %s %s][label for="author_%s" . %s]\n' \
+                    "$each" "$each" "checked=checked" "$([ "$each" = "$USER_ID" ] && printf 'disabled=disabled')" \
+                    "$each" "$(user_idmap "$each" |UNSTRING |HTML)"
+           done
+           for n in 0 1 2 3 4 5 6 7 8 9; do
+             printf '[checkbox "" "" #newauthor%i][label for=newauthor%i . +]
+                     [input name=newauthor value="" placeholder="Author" list=list_authors]' \
+                     $n $n
+           done)
+         ]
          [submit "action" "update_channel" . Update]
          [submit "action" "update_channel_cancel" . Cancel]
        ]
        EOF
-elif [ "$channel" ]; then
+elif [ "$CHANNEL_ID" ]; then
   yield_page "$CHANNEL_NAME" "channel" <<-EOF
-       [nav [a href="../" Channels] - [span $(HTML "${CHANNEL_NAME:-(Unnamed Channel)}")]
-         $( [ "$USER_ID" -a ! "${CHANNEL_AUTHORS##*${USER_ID}*}" ] \
-             && printf ' - [a href="edit" edit]'
-          )
-       ]
+       [nav [a href="../" Channels] - [span $(HTML "${CHANNEL_NAME:-(Unnamed Channel)}")]]
        [h1 .name $(HTML "$CHANNEL_NAME")]
        [div .description . ${CHANNEL_DESCR_CACHE}]
-       [h1 .videos Videos]
-       [div .videos
-         $( [ "$USER_ID" -a ! "${CHANNEL_AUTHORS##*${USER_ID}*}" ] \
-            && printf '
-                [form .video .newvideo method=POST
-                  [hidden "video" "%s"]
-                  [submit "action" "newvideo" New Video]
-                ]' "$(timeid)"
-          )
-         $( [ -f "$vid_db" -a -r "$vid_db" ] \
-            && while w_video "$ID"; do :; done <"$vid_db"
-         )
+       [div .authors [h3 Authors:]
+         $(for each in $CHANNEL_AUTHORS; do
+           printf '[span .author . %s]\n' "$(user_idmap "$each" |UNSTRING |HTML)"
+         done |sort)
        ]
+       $(AUTHOR && printf '[a .button href="edit" edit]')
+       [h1 .videos Videos]
+       [div .videos . $(
+         AUTHOR && printf '
+            [form .video .newvideo method=POST
+              [hidden "video" "%s"]
+              [submit "action" "newvideo" New Video]
+            ]' "$(timeid)"
+         list_videos |while w_video; do :; done
+       )]
        EOF
 else
   yield_page "Channels" "channels" <<-EOF
-       $(w_channel_list)
+       $([ "$USER_ID" ] && printf '
+         [form .channel .newchannel method=POST
+           [hidden "channel" "%s"]
+           [submit "action" "newchannel" New Channel]
+         ]' "$(timeid)"
+       )
+       $([ -f "$chan_db" -a -r "$chan_db" ] \
+         && while w_channel; do :; done <"$chan_db"
+       )
        EOF
 fi